]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
6d3d756a | 2 | // Name: src/common/filefn.cpp |
c801d85f KB |
3 | // Purpose: File- and directory-related functions |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
e90c1d2a VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
c801d85f KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
c801d85f KB |
22 | |
23 | #ifdef __BORLANDC__ | |
7af89395 | 24 | #pragma hdrstop |
c801d85f KB |
25 | #endif |
26 | ||
94268cae WS |
27 | #include "wx/filefn.h" |
28 | ||
8898456d WS |
29 | #ifndef WX_PRECOMP |
30 | #include "wx/intl.h" | |
e4db172a | 31 | #include "wx/log.h" |
de6185e2 | 32 | #include "wx/utils.h" |
0bf751e7 | 33 | #include "wx/crt.h" |
8898456d WS |
34 | #endif |
35 | ||
7bcc9eb9 | 36 | #include "wx/dynarray.h" |
94268cae | 37 | #include "wx/file.h" |
9e8d8607 | 38 | #include "wx/filename.h" |
8ff12342 | 39 | #include "wx/dir.h" |
c801d85f | 40 | |
8daf3c36 VZ |
41 | #include "wx/tokenzr.h" |
42 | ||
fd3f686c | 43 | // there are just too many of those... |
3f4a0c5b | 44 | #ifdef __VISUALC__ |
fd3f686c VZ |
45 | #pragma warning(disable:4706) // assignment within conditional expression |
46 | #endif // VC++ | |
47 | ||
c801d85f KB |
48 | #include <ctype.h> |
49 | #include <stdio.h> | |
50 | #include <stdlib.h> | |
51 | #include <string.h> | |
13ff2485 | 52 | #if !wxONLY_WATCOM_EARLIER_THAN(1,4) |
3f4a0c5b VZ |
53 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
54 | #include <errno.h> | |
55 | #endif | |
c801d85f | 56 | #endif |
3f4a0c5b | 57 | |
76a5e5d2 | 58 | #if defined(__WXMAC__) |
28301089 | 59 | #include "wx/mac/private.h" // includes mac headers |
76a5e5d2 SC |
60 | #endif |
61 | ||
34138703 | 62 | #ifdef __WINDOWS__ |
18a1516c | 63 | #include "wx/msw/private.h" |
3d5231db | 64 | #include "wx/msw/mslu.h" |
b0091f58 | 65 | |
3ffbc733 VZ |
66 | // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path() |
67 | // | |
68 | // note that it must be included after <windows.h> | |
69 | #ifdef __GNUWIN32__ | |
e02e8816 MB |
70 | #ifdef __CYGWIN__ |
71 | #include <sys/cygwin.h> | |
72 | #endif | |
3ffbc733 | 73 | #endif // __GNUWIN32__ |
18a1516c MW |
74 | |
75 | // io.h is needed for _get_osfhandle() | |
76 | // Already included by filefn.h for many Windows compilers | |
77 | #if defined __MWERKS__ || defined __CYGWIN__ | |
78 | #include <io.h> | |
79 | #endif | |
3ffbc733 | 80 | #endif // __WINDOWS__ |
c801d85f | 81 | |
68351053 | 82 | #if defined(__VMS__) |
3c70014d MW |
83 | #include <fab.h> |
84 | #endif | |
85 | ||
13b1f8a7 VZ |
86 | // TODO: Borland probably has _wgetcwd as well? |
87 | #ifdef _MSC_VER | |
88 | #define HAVE_WGETCWD | |
89 | #endif | |
90 | ||
e90c1d2a VZ |
91 | // ---------------------------------------------------------------------------- |
92 | // constants | |
93 | // ---------------------------------------------------------------------------- | |
94 | ||
13b1f8a7 VZ |
95 | #ifndef _MAXPATHLEN |
96 | #define _MAXPATHLEN 1024 | |
97 | #endif | |
c801d85f | 98 | |
da2b4b7a | 99 | #ifdef __WXMAC__ |
0ad76eea | 100 | // # include "MoreFilesX.h" |
17dff81c | 101 | #endif |
c801d85f | 102 | |
e90c1d2a VZ |
103 | // ---------------------------------------------------------------------------- |
104 | // private globals | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
1f1070e2 | 107 | // MT-FIXME: get rid of this horror and all code using it |
e90c1d2a VZ |
108 | static wxChar wxFileFunctionsBuffer[4*_MAXPATHLEN]; |
109 | ||
5d33ed2c DW |
110 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
111 | // | |
32e768ae | 112 | // VisualAge C++ V4.0 cannot have any external linkage const decs |
5d33ed2c DW |
113 | // in headers included by more than one primary source |
114 | // | |
30984dea | 115 | const int wxInvalidOffset = -1; |
5d33ed2c DW |
116 | #endif |
117 | ||
a339970a VZ |
118 | // ---------------------------------------------------------------------------- |
119 | // macros | |
120 | // ---------------------------------------------------------------------------- | |
121 | ||
56614e51 | 122 | // translate the filenames before passing them to OS functions |
334d2448 | 123 | #define OS_FILENAME(s) (s.fn_str()) |
a339970a | 124 | |
e90c1d2a VZ |
125 | // ============================================================================ |
126 | // implementation | |
127 | // ============================================================================ | |
128 | ||
56614e51 VZ |
129 | // ---------------------------------------------------------------------------- |
130 | // wrappers around standard POSIX functions | |
131 | // ---------------------------------------------------------------------------- | |
132 | ||
311b0403 MW |
133 | #if wxUSE_UNICODE && defined __BORLANDC__ \ |
134 | && __BORLANDC__ >= 0x550 && __BORLANDC__ <= 0x551 | |
135 | ||
136 | // BCC 5.5 and 5.5.1 have a bug in _wopen where files are created read only | |
137 | // regardless of the mode parameter. This hack works around the problem by | |
138 | // setting the mode with _wchmod. | |
0597e7f9 | 139 | // |
52de37c7 | 140 | int wxCRT_Open(const wchar_t *pathname, int flags, mode_t mode) |
311b0403 MW |
141 | { |
142 | int moreflags = 0; | |
143 | ||
144 | // we only want to fix the mode when the file is actually created, so | |
145 | // when creating first try doing it O_EXCL so we can tell if the file | |
146 | // was already there. | |
147 | if ((flags & O_CREAT) && !(flags & O_EXCL) && (mode & wxS_IWUSR) != 0) | |
148 | moreflags = O_EXCL; | |
149 | ||
150 | int fd = _wopen(pathname, flags | moreflags, mode); | |
151 | ||
152 | // the file was actually created and needs fixing | |
153 | if (fd != -1 && (flags & O_CREAT) != 0 && (mode & wxS_IWUSR) != 0) | |
154 | { | |
155 | close(fd); | |
156 | _wchmod(pathname, mode); | |
157 | fd = _wopen(pathname, flags & ~(O_EXCL | O_CREAT)); | |
158 | } | |
159 | // the open failed, but it may have been because the added O_EXCL stopped | |
160 | // the opening of an existing file, so try again without. | |
161 | else if (fd == -1 && moreflags != 0) | |
162 | { | |
163 | fd = _wopen(pathname, flags & ~O_CREAT); | |
164 | } | |
165 | ||
166 | return fd; | |
167 | } | |
168 | ||
169 | #endif | |
170 | ||
92980e90 RR |
171 | // ---------------------------------------------------------------------------- |
172 | // wxPathList | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
34e2d943 | 175 | bool wxPathList::Add(const wxString& path) |
f526f752 | 176 | { |
31a8bf3f RR |
177 | // add a path separator to force wxFileName to interpret it always as a directory |
178 | // (i.e. if we are called with '/home/user' we want to consider it a folder and | |
179 | // not, as wxFileName would consider, a filename). | |
8daf3c36 | 180 | wxFileName fn(path + wxFileName::GetPathSeparator()); |
31a8bf3f RR |
181 | |
182 | // add only normalized relative/absolute paths | |
34e2d943 RR |
183 | // NB: we won't do wxPATH_NORM_DOTS in order to avoid problems when trying to |
184 | // normalize paths which starts with ".." (which can be normalized only if | |
185 | // we use also wxPATH_NORM_ABSOLUTE - which we don't want to use). | |
186 | if (!fn.Normalize(wxPATH_NORM_TILDE|wxPATH_NORM_LONG|wxPATH_NORM_ENV_VARS)) | |
187 | return false; | |
f526f752 | 188 | |
8daf3c36 VZ |
189 | wxString toadd = fn.GetPath(); |
190 | if (Index(toadd) == wxNOT_FOUND) | |
191 | wxArrayString::Add(toadd); // do not add duplicates | |
34e2d943 RR |
192 | |
193 | return true; | |
f526f752 MB |
194 | } |
195 | ||
8daf3c36 | 196 | void wxPathList::Add(const wxArrayString &arr) |
c801d85f | 197 | { |
8daf3c36 VZ |
198 | for (size_t j=0; j < arr.GetCount(); j++) |
199 | Add(arr[j]); | |
c801d85f KB |
200 | } |
201 | ||
202 | // Add paths e.g. from the PATH environment variable | |
7bea7b91 | 203 | void wxPathList::AddEnvList (const wxString& WXUNUSED_IN_WINCE(envVariable)) |
c801d85f | 204 | { |
1c193821 JS |
205 | // No environment variables on WinCE |
206 | #ifndef __WXWINCE__ | |
8daf3c36 VZ |
207 | |
208 | // The space has been removed from the tokenizers, otherwise a | |
209 | // path such as "C:\Program Files" would be split into 2 paths: | |
210 | // "C:\Program" and "Files"; this is true for both Windows and Unix. | |
211 | ||
db4444f0 | 212 | static const wxChar PATH_TOKS[] = |
5a3912f2 | 213 | #if defined(__WINDOWS__) || defined(__OS2__) |
3103e8a9 | 214 | wxT(";"); // Don't separate with colon in DOS (used for drive) |
c801d85f | 215 | #else |
8daf3c36 | 216 | wxT(":;"); |
c801d85f KB |
217 | #endif |
218 | ||
8daf3c36 VZ |
219 | wxString val; |
220 | if ( wxGetEnv(envVariable, &val) ) | |
c801d85f | 221 | { |
8daf3c36 VZ |
222 | // split into an array of string the value of the env var |
223 | wxArrayString arr = wxStringTokenize(val, PATH_TOKS); | |
224 | WX_APPEND_ARRAY(*this, arr); | |
c801d85f | 225 | } |
7bea7b91 | 226 | #endif // !__WXWINCE__ |
c801d85f KB |
227 | } |
228 | ||
229 | // Given a full filename (with path), ensure that that file can | |
230 | // be accessed again USING FILENAME ONLY by adding the path | |
231 | // to the list if not already there. | |
34e2d943 | 232 | bool wxPathList::EnsureFileAccessible (const wxString& path) |
c801d85f | 233 | { |
34e2d943 | 234 | return Add(wxPathOnly(path)); |
c801d85f KB |
235 | } |
236 | ||
34e2d943 | 237 | #if WXWIN_COMPATIBILITY_2_6 |
8daf3c36 | 238 | bool wxPathList::Member (const wxString& path) const |
c801d85f | 239 | { |
8daf3c36 | 240 | return Index(path) != wxNOT_FOUND; |
c801d85f | 241 | } |
34e2d943 | 242 | #endif |
c801d85f | 243 | |
8daf3c36 | 244 | wxString wxPathList::FindValidPath (const wxString& file) const |
c801d85f | 245 | { |
31a8bf3f RR |
246 | // normalize the given string as it could be a path + a filename |
247 | // and not only a filename | |
8daf3c36 VZ |
248 | wxFileName fn(file); |
249 | wxString strend; | |
c801d85f | 250 | |
34e2d943 RR |
251 | // NB: normalize without making absolute otherwise calling this function with |
252 | // e.g. "b/c.txt" would result in removing the directory 'b' and the for loop | |
253 | // below would only add to the paths of this list the 'c.txt' part when doing | |
254 | // the existence checks... | |
255 | // NB: we don't use wxPATH_NORM_DOTS here, too (see wxPathList::Add for more info) | |
256 | if (!fn.Normalize(wxPATH_NORM_TILDE|wxPATH_NORM_LONG|wxPATH_NORM_ENV_VARS)) | |
257 | return wxEmptyString; | |
31a8bf3f RR |
258 | |
259 | wxASSERT_MSG(!fn.IsDir(), wxT("Cannot search for directories; only for files")); | |
8daf3c36 | 260 | if (fn.IsAbsolute()) |
31a8bf3f | 261 | strend = fn.GetFullName(); // search for the file name and ignore the path part |
8daf3c36 VZ |
262 | else |
263 | strend = fn.GetFullPath(); | |
c801d85f | 264 | |
8daf3c36 | 265 | for (size_t i=0; i<GetCount(); i++) |
c801d85f | 266 | { |
8daf3c36 VZ |
267 | wxString strstart = Item(i); |
268 | if (!strstart.IsEmpty() && strstart.Last() != wxFileName::GetPathSeparator()) | |
269 | strstart += wxFileName::GetPathSeparator(); | |
c801d85f | 270 | |
8daf3c36 VZ |
271 | if (wxFileExists(strstart + strend)) |
272 | return strstart + strend; // Found! | |
273 | } | |
274 | ||
275 | return wxEmptyString; // Not found | |
c801d85f KB |
276 | } |
277 | ||
8daf3c36 | 278 | wxString wxPathList::FindAbsoluteValidPath (const wxString& file) const |
c801d85f | 279 | { |
e90c1d2a | 280 | wxString f = FindValidPath(file); |
fc447c7f | 281 | if ( f.empty() || wxIsAbsolutePath(f) ) |
e90c1d2a VZ |
282 | return f; |
283 | ||
ce045aed | 284 | wxString buf = ::wxGetCwd(); |
13b1f8a7 | 285 | |
e90c1d2a | 286 | if ( !wxEndsWithPathSeparator(buf) ) |
c801d85f | 287 | { |
e90c1d2a | 288 | buf += wxFILE_SEP_PATH; |
c801d85f | 289 | } |
e90c1d2a VZ |
290 | buf += f; |
291 | ||
292 | return buf; | |
c801d85f KB |
293 | } |
294 | ||
8daf3c36 VZ |
295 | // ---------------------------------------------------------------------------- |
296 | // miscellaneous global functions (TOFIX!) | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
299 | static inline wxChar* MYcopystring(const wxString& s) | |
300 | { | |
301 | wxChar* copy = new wxChar[s.length() + 1]; | |
302 | return wxStrcpy(copy, s.c_str()); | |
303 | } | |
304 | ||
52de37c7 VS |
305 | template<typename CharType> |
306 | static inline CharType* MYcopystring(const CharType* s) | |
8daf3c36 | 307 | { |
52de37c7 | 308 | CharType* copy = new CharType[wxStrlen(s) + 1]; |
8daf3c36 VZ |
309 | return wxStrcpy(copy, s); |
310 | } | |
311 | ||
312 | ||
3f4a0c5b | 313 | bool |
c801d85f KB |
314 | wxFileExists (const wxString& filename) |
315 | { | |
68351053 WS |
316 | #if defined(__WXPALMOS__) |
317 | return false; | |
318 | #elif defined(__WIN32__) && !defined(__WXMICROWIN__) | |
4ea2c29f VZ |
319 | // we must use GetFileAttributes() instead of the ANSI C functions because |
320 | // it can cope with network (UNC) paths unlike them | |
e0a050e3 | 321 | DWORD ret = ::GetFileAttributes(filename.fn_str()); |
2db300c6 | 322 | |
a28ae409 | 323 | return (ret != (DWORD)-1) && !(ret & FILE_ATTRIBUTE_DIRECTORY); |
4ea2c29f | 324 | #else // !__WIN32__ |
27d98837 MW |
325 | #ifndef S_ISREG |
326 | #define S_ISREG(mode) ((mode) & S_IFREG) | |
327 | #endif | |
4ea2c29f | 328 | wxStructStat st; |
f3660dcb | 329 | #ifndef wxNEED_WX_UNISTD_H |
27d98837 | 330 | return (wxStat( filename.fn_str() , &st) == 0 && S_ISREG(st.st_mode)) |
bf58daba SN |
331 | #ifdef __OS2__ |
332 | || (errno == EACCES) // if access is denied something with that name | |
333 | // exists and is opened in exclusive mode. | |
334 | #endif | |
335 | ; | |
f3660dcb | 336 | #else |
27d98837 | 337 | return wxStat( filename , &st) == 0 && S_ISREG(st.st_mode); |
f3660dcb | 338 | #endif |
4ea2c29f | 339 | #endif // __WIN32__/!__WIN32__ |
c801d85f KB |
340 | } |
341 | ||
3f4a0c5b | 342 | bool |
c801d85f KB |
343 | wxIsAbsolutePath (const wxString& filename) |
344 | { | |
b494c48b | 345 | if (!filename.empty()) |
2985ad5d | 346 | { |
e8e1d09e GD |
347 | // Unix like or Windows |
348 | if (filename[0] == wxT('/')) | |
79e162f5 | 349 | return true; |
c801d85f | 350 | #ifdef __VMS__ |
e8e1d09e | 351 | if ((filename[0] == wxT('[') && filename[1] != wxT('.'))) |
79e162f5 | 352 | return true; |
c801d85f | 353 | #endif |
5a3912f2 | 354 | #if defined(__WINDOWS__) || defined(__OS2__) |
e8e1d09e GD |
355 | // MSDOS like |
356 | if (filename[0] == wxT('\\') || (wxIsalpha (filename[0]) && filename[1] == wxT(':'))) | |
79e162f5 | 357 | return true; |
c801d85f | 358 | #endif |
c801d85f | 359 | } |
79e162f5 | 360 | return false ; |
c801d85f KB |
361 | } |
362 | ||
363 | /* | |
364 | * Strip off any extension (dot something) from end of file, | |
365 | * IF one exists. Inserts zero into buffer. | |
366 | * | |
367 | */ | |
3f4a0c5b | 368 | |
52de37c7 VS |
369 | template<typename T> |
370 | static void wxDoStripExtension(T *buffer) | |
c801d85f | 371 | { |
b0c5cd0f WS |
372 | int len = wxStrlen(buffer); |
373 | int i = len-1; | |
374 | while (i > 0) | |
c801d85f | 375 | { |
b0c5cd0f WS |
376 | if (buffer[i] == wxT('.')) |
377 | { | |
378 | buffer[i] = 0; | |
379 | break; | |
380 | } | |
381 | i --; | |
c801d85f | 382 | } |
c801d85f KB |
383 | } |
384 | ||
52de37c7 VS |
385 | void wxStripExtension(char *buffer) { wxDoStripExtension(buffer); } |
386 | void wxStripExtension(wchar_t *buffer) { wxDoStripExtension(buffer); } | |
387 | ||
47fa7969 JS |
388 | void wxStripExtension(wxString& buffer) |
389 | { | |
a6f4dbbd | 390 | //RN: Be careful about the handling the case where |
ce045aed WS |
391 | //buffer.length() == 0 |
392 | for(size_t i = buffer.length() - 1; i != wxString::npos; --i) | |
47fa7969 | 393 | { |
2cbfa061 RN |
394 | if (buffer.GetChar(i) == wxT('.')) |
395 | { | |
396 | buffer = buffer.Left(i); | |
397 | break; | |
398 | } | |
47fa7969 | 399 | } |
47fa7969 JS |
400 | } |
401 | ||
c801d85f | 402 | // Destructive removal of /./ and /../ stuff |
52de37c7 VS |
403 | template<typename CharType> |
404 | static CharType *wxDoRealPath (CharType *path) | |
c801d85f | 405 | { |
e2fc40b4 | 406 | static const CharType SEP = wxFILE_SEP_PATH; |
2049ba38 | 407 | #ifdef __WXMSW__ |
2b5f62a0 | 408 | wxUnix2DosFilename(path); |
c801d85f KB |
409 | #endif |
410 | if (path[0] && path[1]) { | |
411 | /* MATTHEW: special case "/./x" */ | |
52de37c7 | 412 | CharType *p; |
223d09f6 | 413 | if (path[2] == SEP && path[1] == wxT('.')) |
c801d85f KB |
414 | p = &path[0]; |
415 | else | |
416 | p = &path[2]; | |
417 | for (; *p; p++) | |
418 | { | |
3f4a0c5b VZ |
419 | if (*p == SEP) |
420 | { | |
223d09f6 | 421 | if (p[1] == wxT('.') && p[2] == wxT('.') && (p[3] == SEP || p[3] == wxT('\0'))) |
3f4a0c5b | 422 | { |
52de37c7 | 423 | CharType *q; |
f0e1c343 JS |
424 | for (q = p - 1; q >= path && *q != SEP; q--) |
425 | { | |
426 | // Empty | |
427 | } | |
428 | ||
223d09f6 | 429 | if (q[0] == SEP && (q[1] != wxT('.') || q[2] != wxT('.') || q[3] != SEP) |
3f4a0c5b VZ |
430 | && (q - 1 <= path || q[-1] != SEP)) |
431 | { | |
50920146 | 432 | wxStrcpy (q, p + 3); |
223d09f6 | 433 | if (path[0] == wxT('\0')) |
3f4a0c5b VZ |
434 | { |
435 | path[0] = SEP; | |
223d09f6 | 436 | path[1] = wxT('\0'); |
3f4a0c5b | 437 | } |
5a3912f2 | 438 | #if defined(__WXMSW__) || defined(__OS2__) |
3f4a0c5b | 439 | /* Check that path[2] is NULL! */ |
223d09f6 | 440 | else if (path[1] == wxT(':') && !path[2]) |
3f4a0c5b VZ |
441 | { |
442 | path[2] = SEP; | |
223d09f6 | 443 | path[3] = wxT('\0'); |
3f4a0c5b VZ |
444 | } |
445 | #endif | |
446 | p = q - 1; | |
447 | } | |
448 | } | |
223d09f6 | 449 | else if (p[1] == wxT('.') && (p[2] == SEP || p[2] == wxT('\0'))) |
50920146 | 450 | wxStrcpy (p, p + 2); |
3f4a0c5b | 451 | } |
c801d85f KB |
452 | } |
453 | } | |
454 | return path; | |
455 | } | |
456 | ||
52de37c7 VS |
457 | char *wxRealPath(char *path) |
458 | { | |
459 | return wxDoRealPath(path); | |
460 | } | |
461 | ||
462 | wchar_t *wxRealPath(wchar_t *path) | |
463 | { | |
464 | return wxDoRealPath(path); | |
465 | } | |
466 | ||
ce045aed WS |
467 | wxString wxRealPath(const wxString& path) |
468 | { | |
469 | wxChar *buf1=MYcopystring(path); | |
470 | wxChar *buf2=wxRealPath(buf1); | |
471 | wxString buf(buf2); | |
472 | delete [] buf1; | |
473 | return buf; | |
474 | } | |
475 | ||
476 | ||
c801d85f | 477 | // Must be destroyed |
50920146 | 478 | wxChar *wxCopyAbsolutePath(const wxString& filename) |
c801d85f | 479 | { |
ce045aed WS |
480 | if (filename.empty()) |
481 | return (wxChar *) NULL; | |
c801d85f | 482 | |
ce045aed WS |
483 | if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer, filename))) |
484 | { | |
485 | wxString buf = ::wxGetCwd(); | |
486 | wxChar ch = buf.Last(); | |
2049ba38 | 487 | #ifdef __WXMSW__ |
ce045aed WS |
488 | if (ch != wxT('\\') && ch != wxT('/')) |
489 | buf << wxT("\\"); | |
c801d85f | 490 | #else |
ce045aed WS |
491 | if (ch != wxT('/')) |
492 | buf << wxT("/"); | |
c801d85f | 493 | #endif |
ce045aed WS |
494 | buf << wxFileFunctionsBuffer; |
495 | buf = wxRealPath( buf ); | |
496 | return MYcopystring( buf ); | |
497 | } | |
498 | return MYcopystring( wxFileFunctionsBuffer ); | |
c801d85f KB |
499 | } |
500 | ||
501 | /*- | |
502 | Handles: | |
503 | ~/ => home dir | |
504 | ~user/ => user's home dir | |
505 | If the environment variable a = "foo" and b = "bar" then: | |
506 | Unix: | |
3f4a0c5b VZ |
507 | $a => foo |
508 | $a$b => foobar | |
509 | $a.c => foo.c | |
510 | xxx$a => xxxfoo | |
511 | ${a}! => foo! | |
512 | $(b)! => bar! | |
513 | \$a => \$a | |
c801d85f | 514 | MSDOS: |
3f4a0c5b VZ |
515 | $a ==> $a |
516 | $(a) ==> foo | |
517 | $(a)$b ==> foo$b | |
518 | $(a)$(b)==> foobar | |
519 | test.$$ ==> test.$$ | |
c801d85f KB |
520 | */ |
521 | ||
522 | /* input name in name, pathname output to buf. */ | |
523 | ||
52de37c7 VS |
524 | template<typename CharType> |
525 | static CharType *wxDoExpandPath(CharType *buf, const wxString& name) | |
c801d85f | 526 | { |
52de37c7 VS |
527 | register CharType *d, *s, *nm; |
528 | CharType lnm[_MAXPATHLEN]; | |
33ac7e6f | 529 | int q; |
c801d85f KB |
530 | |
531 | // Some compilers don't like this line. | |
52de37c7 | 532 | // const CharType trimchars[] = wxT("\n \t"); |
c801d85f | 533 | |
52de37c7 | 534 | CharType trimchars[4]; |
223d09f6 KB |
535 | trimchars[0] = wxT('\n'); |
536 | trimchars[1] = wxT(' '); | |
537 | trimchars[2] = wxT('\t'); | |
c801d85f KB |
538 | trimchars[3] = 0; |
539 | ||
e2fc40b4 | 540 | static const CharType SEP = wxFILE_SEP_PATH; |
2049ba38 | 541 | #ifdef __WXMSW__ |
e2fc40b4 | 542 | //wxUnix2DosFilename(path); |
c801d85f | 543 | #endif |
e2fc40b4 | 544 | |
223d09f6 | 545 | buf[0] = wxT('\0'); |
52de37c7 | 546 | if (name.empty()) |
3f4a0c5b | 547 | return buf; |
8fd7108e | 548 | nm = ::MYcopystring(static_cast<const CharType*>(name.c_str())); // Make a scratch copy |
52de37c7 | 549 | CharType *nm_tmp = nm; |
c801d85f KB |
550 | |
551 | /* Skip leading whitespace and cr */ | |
52de37c7 | 552 | while (wxStrchr(trimchars, *nm) != NULL) |
3f4a0c5b | 553 | nm++; |
c801d85f | 554 | /* And strip off trailing whitespace and cr */ |
50920146 | 555 | s = nm + (q = wxStrlen(nm)) - 1; |
52de37c7 | 556 | while (q-- && wxStrchr(trimchars, *s) != NULL) |
223d09f6 | 557 | *s = wxT('\0'); |
c801d85f KB |
558 | |
559 | s = nm; | |
560 | d = lnm; | |
2049ba38 | 561 | #ifdef __WXMSW__ |
dc259b79 | 562 | q = FALSE; |
c801d85f | 563 | #else |
223d09f6 | 564 | q = nm[0] == wxT('\\') && nm[1] == wxT('~'); |
c801d85f KB |
565 | #endif |
566 | ||
567 | /* Expand inline environment variables */ | |
c2ff79b1 DW |
568 | #ifdef __VISAGECPP__ |
569 | while (*d) | |
570 | { | |
571 | *d++ = *s; | |
223d09f6 | 572 | if(*s == wxT('\\')) |
c2ff79b1 DW |
573 | { |
574 | *(d - 1) = *++s; | |
575 | if (*d) | |
576 | { | |
577 | s++; | |
578 | continue; | |
579 | } | |
580 | else | |
581 | break; | |
582 | } | |
583 | else | |
584 | #else | |
22394d24 | 585 | while ((*d++ = *s) != 0) { |
c2ff79b1 | 586 | # ifndef __WXMSW__ |
223d09f6 | 587 | if (*s == wxT('\\')) { |
c40158e4 | 588 | if ((*(d - 1) = *++s)!=0) { |
3f4a0c5b VZ |
589 | s++; |
590 | continue; | |
591 | } else | |
592 | break; | |
593 | } else | |
c2ff79b1 | 594 | # endif |
c801d85f | 595 | #endif |
1c193821 JS |
596 | // No env variables on WinCE |
597 | #ifndef __WXWINCE__ | |
2049ba38 | 598 | #ifdef __WXMSW__ |
223d09f6 | 599 | if (*s++ == wxT('$') && (*s == wxT('{') || *s == wxT(')'))) |
c801d85f | 600 | #else |
223d09f6 | 601 | if (*s++ == wxT('$')) |
3f4a0c5b VZ |
602 | #endif |
603 | { | |
52de37c7 | 604 | register CharType *start = d; |
223d09f6 | 605 | register int braces = (*s == wxT('{') || *s == wxT('(')); |
52de37c7 | 606 | register CharType *value; |
22394d24 | 607 | while ((*d++ = *s) != 0) |
223d09f6 | 608 | if (braces ? (*s == wxT('}') || *s == wxT(')')) : !(wxIsalnum(*s) || *s == wxT('_')) ) |
3f4a0c5b VZ |
609 | break; |
610 | else | |
611 | s++; | |
612 | *--d = 0; | |
50920146 | 613 | value = wxGetenv(braces ? start + 1 : start); |
3f4a0c5b | 614 | if (value) { |
f0e1c343 JS |
615 | for ((d = start - 1); (*d++ = *value++) != 0;) |
616 | { | |
617 | // Empty | |
618 | } | |
619 | ||
3f4a0c5b VZ |
620 | d--; |
621 | if (braces && *s) | |
622 | s++; | |
623 | } | |
624 | } | |
1c193821 JS |
625 | #endif |
626 | // __WXWINCE__ | |
c801d85f KB |
627 | } |
628 | ||
629 | /* Expand ~ and ~user */ | |
52de37c7 | 630 | wxString homepath; |
c801d85f | 631 | nm = lnm; |
223d09f6 | 632 | if (nm[0] == wxT('~') && !q) |
c801d85f | 633 | { |
3f4a0c5b VZ |
634 | /* prefix ~ */ |
635 | if (nm[1] == SEP || nm[1] == 0) | |
636 | { /* ~/filename */ | |
52de37c7 VS |
637 | homepath = wxGetUserHome(wxEmptyString); |
638 | if (!homepath.empty()) { | |
639 | s = (CharType*)(const CharType*)homepath.c_str(); | |
3f4a0c5b VZ |
640 | if (*++nm) |
641 | nm++; | |
642 | } | |
c801d85f | 643 | } else |
3f4a0c5b | 644 | { /* ~user/filename */ |
52de37c7 | 645 | register CharType *nnm; |
f0e1c343 JS |
646 | for (s = nm; *s && *s != SEP; s++) |
647 | { | |
648 | // Empty | |
649 | } | |
3f4a0c5b | 650 | int was_sep; /* MATTHEW: Was there a separator, or NULL? */ |
c801d85f | 651 | was_sep = (*s == SEP); |
3f4a0c5b VZ |
652 | nnm = *s ? s + 1 : s; |
653 | *s = 0; | |
52de37c7 VS |
654 | homepath = wxGetUserHome(wxString(nm + 1)); |
655 | if (homepath.empty()) | |
7448de8d WS |
656 | { |
657 | if (was_sep) /* replace only if it was there: */ | |
658 | *s = SEP; | |
295272bd | 659 | s = NULL; |
7448de8d WS |
660 | } |
661 | else | |
662 | { | |
3f4a0c5b | 663 | nm = nnm; |
52de37c7 | 664 | s = (CharType*)(const CharType*)homepath.c_str(); |
3f4a0c5b VZ |
665 | } |
666 | } | |
c801d85f KB |
667 | } |
668 | ||
669 | d = buf; | |
670 | if (s && *s) { /* MATTHEW: s could be NULL if user '~' didn't exist */ | |
3f4a0c5b | 671 | /* Copy home dir */ |
223d09f6 | 672 | while (wxT('\0') != (*d++ = *s++)) |
3f4a0c5b VZ |
673 | /* loop */; |
674 | // Handle root home | |
675 | if (d - 1 > buf && *(d - 2) != SEP) | |
676 | *(d - 1) = SEP; | |
c801d85f KB |
677 | } |
678 | s = nm; | |
f0e1c343 JS |
679 | while ((*d++ = *s++) != 0) |
680 | { | |
681 | // Empty | |
682 | } | |
c801d85f KB |
683 | delete[] nm_tmp; // clean up alloc |
684 | /* Now clean up the buffer */ | |
685 | return wxRealPath(buf); | |
686 | } | |
687 | ||
52de37c7 VS |
688 | char *wxExpandPath(char *buf, const wxString& name) |
689 | { | |
690 | return wxDoExpandPath(buf, name); | |
691 | } | |
692 | ||
693 | wchar_t *wxExpandPath(wchar_t *buf, const wxString& name) | |
694 | { | |
695 | return wxDoExpandPath(buf, name); | |
696 | } | |
697 | ||
698 | ||
c801d85f KB |
699 | /* Contract Paths to be build upon an environment variable |
700 | component: | |
701 | ||
702 | example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib | |
703 | ||
704 | The call wxExpandPath can convert these back! | |
705 | */ | |
50920146 | 706 | wxChar * |
7bea7b91 WS |
707 | wxContractPath (const wxString& filename, |
708 | const wxString& WXUNUSED_IN_WINCE(envname), | |
709 | const wxString& user) | |
c801d85f | 710 | { |
50920146 | 711 | static wxChar dest[_MAXPATHLEN]; |
c801d85f | 712 | |
b494c48b | 713 | if (filename.empty()) |
50920146 | 714 | return (wxChar *) NULL; |
c801d85f | 715 | |
52de37c7 | 716 | wxStrcpy (dest, filename); |
2049ba38 | 717 | #ifdef __WXMSW__ |
2b5f62a0 | 718 | wxUnix2DosFilename(dest); |
c801d85f KB |
719 | #endif |
720 | ||
721 | // Handle environment | |
52de37c7 | 722 | wxString val; |
1c193821 | 723 | #ifndef __WXWINCE__ |
999836aa | 724 | wxChar *tcp; |
52de37c7 | 725 | if (!envname.empty() && !(val = wxGetenv (envname)).empty() && |
50920146 | 726 | (tcp = wxStrstr (dest, val)) != NULL) |
c801d85f | 727 | { |
52de37c7 | 728 | wxStrcpy (wxFileFunctionsBuffer, tcp + val.length()); |
223d09f6 KB |
729 | *tcp++ = wxT('$'); |
730 | *tcp++ = wxT('{'); | |
52de37c7 | 731 | wxStrcpy (tcp, envname); |
223d09f6 | 732 | wxStrcat (tcp, wxT("}")); |
e90c1d2a | 733 | wxStrcat (tcp, wxFileFunctionsBuffer); |
c801d85f | 734 | } |
1c193821 | 735 | #endif |
c801d85f KB |
736 | |
737 | // Handle User's home (ignore root homes!) | |
844ca096 | 738 | val = wxGetUserHome (user); |
52de37c7 | 739 | if (val.empty()) |
844ca096 VZ |
740 | return dest; |
741 | ||
52de37c7 | 742 | const size_t len = val.length(); |
844ca096 VZ |
743 | if (len <= 2) |
744 | return dest; | |
745 | ||
746 | if (wxStrncmp(dest, val, len) == 0) | |
747 | { | |
748 | wxStrcpy(wxFileFunctionsBuffer, wxT("~")); | |
b494c48b | 749 | if (!user.empty()) |
52de37c7 | 750 | wxStrcat(wxFileFunctionsBuffer, user); |
844ca096 VZ |
751 | wxStrcat(wxFileFunctionsBuffer, dest + len); |
752 | wxStrcpy (dest, wxFileFunctionsBuffer); | |
753 | } | |
c801d85f KB |
754 | |
755 | return dest; | |
756 | } | |
757 | ||
1f1070e2 | 758 | // Return just the filename, not the path (basename) |
50920146 | 759 | wxChar *wxFileNameFromPath (wxChar *path) |
c801d85f | 760 | { |
1f1070e2 VZ |
761 | wxString p = path; |
762 | wxString n = wxFileNameFromPath(p); | |
2db300c6 | 763 | |
1f1070e2 | 764 | return path + p.length() - n.length(); |
c801d85f KB |
765 | } |
766 | ||
1f1070e2 | 767 | wxString wxFileNameFromPath (const wxString& path) |
c801d85f | 768 | { |
1f1070e2 VZ |
769 | wxString name, ext; |
770 | wxFileName::SplitPath(path, NULL, &name, &ext); | |
2db300c6 | 771 | |
1f1070e2 VZ |
772 | wxString fullname = name; |
773 | if ( !ext.empty() ) | |
774 | { | |
775 | fullname << wxFILE_SEP_EXT << ext; | |
c801d85f | 776 | } |
1f1070e2 VZ |
777 | |
778 | return fullname; | |
c801d85f KB |
779 | } |
780 | ||
781 | // Return just the directory, or NULL if no directory | |
50920146 OK |
782 | wxChar * |
783 | wxPathOnly (wxChar *path) | |
c801d85f | 784 | { |
e8e1d09e | 785 | if (path && *path) |
c801d85f | 786 | { |
e8e1d09e | 787 | static wxChar buf[_MAXPATHLEN]; |
2db300c6 | 788 | |
e8e1d09e GD |
789 | // Local copy |
790 | wxStrcpy (buf, path); | |
2db300c6 | 791 | |
e8e1d09e GD |
792 | int l = wxStrlen(path); |
793 | int i = l - 1; | |
2db300c6 | 794 | |
e8e1d09e GD |
795 | // Search backward for a backward or forward slash |
796 | while (i > -1) | |
797 | { | |
e8e1d09e GD |
798 | // Unix like or Windows |
799 | if (path[i] == wxT('/') || path[i] == wxT('\\')) | |
800 | { | |
801 | buf[i] = 0; | |
802 | return buf; | |
803 | } | |
c801d85f | 804 | #ifdef __VMS__ |
e8e1d09e GD |
805 | if (path[i] == wxT(']')) |
806 | { | |
807 | buf[i+1] = 0; | |
808 | return buf; | |
809 | } | |
a339970a | 810 | #endif |
e8e1d09e | 811 | i --; |
c801d85f | 812 | } |
2db300c6 | 813 | |
5a3912f2 | 814 | #if defined(__WXMSW__) || defined(__OS2__) |
e8e1d09e GD |
815 | // Try Drive specifier |
816 | if (wxIsalpha (buf[0]) && buf[1] == wxT(':')) | |
3f4a0c5b | 817 | { |
e8e1d09e GD |
818 | // A:junk --> A:. (since A:.\junk Not A:\junk) |
819 | buf[2] = wxT('.'); | |
820 | buf[3] = wxT('\0'); | |
821 | return buf; | |
3f4a0c5b | 822 | } |
c801d85f KB |
823 | #endif |
824 | } | |
e8e1d09e | 825 | return (wxChar *) NULL; |
c801d85f KB |
826 | } |
827 | ||
828 | // Return just the directory, or NULL if no directory | |
829 | wxString wxPathOnly (const wxString& path) | |
830 | { | |
b494c48b | 831 | if (!path.empty()) |
c801d85f | 832 | { |
e8e1d09e | 833 | wxChar buf[_MAXPATHLEN]; |
2db300c6 | 834 | |
e8e1d09e | 835 | // Local copy |
52de37c7 | 836 | wxStrcpy(buf, path); |
2db300c6 | 837 | |
ce045aed | 838 | int l = path.length(); |
e8e1d09e GD |
839 | int i = l - 1; |
840 | ||
841 | // Search backward for a backward or forward slash | |
842 | while (i > -1) | |
843 | { | |
e8e1d09e GD |
844 | // Unix like or Windows |
845 | if (path[i] == wxT('/') || path[i] == wxT('\\')) | |
846 | { | |
5c760b84 JS |
847 | // Don't return an empty string |
848 | if (i == 0) | |
849 | i ++; | |
e8e1d09e GD |
850 | buf[i] = 0; |
851 | return wxString(buf); | |
852 | } | |
c801d85f | 853 | #ifdef __VMS__ |
e8e1d09e GD |
854 | if (path[i] == wxT(']')) |
855 | { | |
856 | buf[i+1] = 0; | |
857 | return wxString(buf); | |
858 | } | |
a339970a | 859 | #endif |
e8e1d09e | 860 | i --; |
c801d85f | 861 | } |
2db300c6 | 862 | |
5a3912f2 | 863 | #if defined(__WXMSW__) || defined(__OS2__) |
e8e1d09e GD |
864 | // Try Drive specifier |
865 | if (wxIsalpha (buf[0]) && buf[1] == wxT(':')) | |
3f4a0c5b | 866 | { |
e8e1d09e GD |
867 | // A:junk --> A:. (since A:.\junk Not A:\junk) |
868 | buf[2] = wxT('.'); | |
869 | buf[3] = wxT('\0'); | |
870 | return wxString(buf); | |
3f4a0c5b | 871 | } |
c801d85f KB |
872 | #endif |
873 | } | |
b494c48b | 874 | return wxEmptyString; |
c801d85f KB |
875 | } |
876 | ||
877 | // Utility for converting delimiters in DOS filenames to UNIX style | |
878 | // and back again - or we get nasty problems with delimiters. | |
879 | // Also, convert to lower case, since case is significant in UNIX. | |
880 | ||
da2b4b7a | 881 | #if defined(__WXMAC__) |
b0091f58 | 882 | |
a2b77260 | 883 | #define kDefaultPathStyle kCFURLPOSIXPathStyle |
a1c34a78 | 884 | |
a62848fd | 885 | wxString wxMacFSRefToPath( const FSRef *fsRef , CFStringRef additionalPathComponent ) |
a2b77260 | 886 | { |
a62848fd | 887 | CFURLRef fullURLRef; |
a2b77260 SC |
888 | fullURLRef = CFURLCreateFromFSRef(NULL, fsRef); |
889 | if ( additionalPathComponent ) | |
890 | { | |
891 | CFURLRef parentURLRef = fullURLRef ; | |
892 | fullURLRef = CFURLCreateCopyAppendingPathComponent(NULL, parentURLRef, | |
893 | additionalPathComponent,false); | |
894 | CFRelease( parentURLRef ) ; | |
895 | } | |
a62848fd WS |
896 | CFStringRef cfString = CFURLCopyFileSystemPath(fullURLRef, kDefaultPathStyle); |
897 | CFRelease( fullURLRef ) ; | |
739cb14a SC |
898 | CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, cfString); |
899 | CFRelease( cfString ); | |
a8d69700 | 900 | CFStringNormalize(cfMutableString,kCFStringNormalizationFormC); |
80539f04 | 901 | return wxCFStringRef(cfMutableString).AsString(); |
bedaf53e | 902 | } |
e7549107 | 903 | |
a62848fd | 904 | OSStatus wxMacPathToFSRef( const wxString&path , FSRef *fsRef ) |
bedaf53e | 905 | { |
b1d4dd7a | 906 | OSStatus err = noErr ; |
80539f04 | 907 | CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(path)); |
a8d69700 | 908 | CFStringNormalize(cfMutableString,kCFStringNormalizationFormD); |
739cb14a SC |
909 | CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kDefaultPathStyle, false); |
910 | CFRelease( cfMutableString ); | |
a62848fd WS |
911 | if ( NULL != url ) |
912 | { | |
913 | if ( CFURLGetFSRef(url, fsRef) == false ) | |
914 | err = fnfErr ; | |
a2b77260 | 915 | CFRelease( url ) ; |
bfc2bf62 SC |
916 | } |
917 | else | |
918 | { | |
a2b77260 | 919 | err = fnfErr ; |
bfc2bf62 | 920 | } |
a2b77260 | 921 | return err ; |
bedaf53e SC |
922 | } |
923 | ||
a62848fd | 924 | wxString wxMacHFSUniStrToString( ConstHFSUniStr255Param uniname ) |
c4e41ce3 | 925 | { |
a2b77260 SC |
926 | CFStringRef cfname = CFStringCreateWithCharacters( kCFAllocatorDefault, |
927 | uniname->unicode, | |
928 | uniname->length ); | |
739cb14a SC |
929 | CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, cfname); |
930 | CFRelease( cfname ); | |
a8d69700 | 931 | CFStringNormalize(cfMutableString,kCFStringNormalizationFormC); |
80539f04 | 932 | return wxCFStringRef(cfMutableString).AsString() ; |
c4e41ce3 | 933 | } |
e7549107 | 934 | |
fb743cab SC |
935 | #ifndef __LP64__ |
936 | ||
a2b77260 | 937 | wxString wxMacFSSpec2MacFilename( const FSSpec *spec ) |
17dff81c | 938 | { |
a2b77260 SC |
939 | FSRef fsRef ; |
940 | if ( FSpMakeFSRef( spec , &fsRef) == noErr ) | |
e7549107 | 941 | { |
a2b77260 | 942 | return wxMacFSRefToPath( &fsRef ) ; |
e7549107 | 943 | } |
a2b77260 | 944 | return wxEmptyString ; |
17dff81c | 945 | } |
e7549107 | 946 | |
a2b77260 | 947 | void wxMacFilename2FSSpec( const wxString& path , FSSpec *spec ) |
e7549107 | 948 | { |
0ad76eea SC |
949 | OSStatus err = noErr; |
950 | FSRef fsRef; | |
951 | wxMacPathToFSRef( path , &fsRef ); | |
952 | err = FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, spec, NULL); | |
953 | verify_noerr( err ); | |
e7549107 | 954 | } |
fb743cab | 955 | #endif |
12d67f8a | 956 | |
e8e1d09e GD |
957 | #endif // __WXMAC__ |
958 | ||
52de37c7 VS |
959 | template<typename T> |
960 | static void wxDoDos2UnixFilename(T *s) | |
c801d85f KB |
961 | { |
962 | if (s) | |
963 | while (*s) | |
964 | { | |
4603b4f9 JS |
965 | if (*s == _T('\\')) |
966 | *s = _T('/'); | |
e7549107 | 967 | #ifdef __WXMSW__ |
3f4a0c5b | 968 | else |
52de37c7 | 969 | *s = wxTolower(*s); // Case INDEPENDENT |
c801d85f | 970 | #endif |
3f4a0c5b | 971 | s++; |
c801d85f KB |
972 | } |
973 | } | |
974 | ||
52de37c7 VS |
975 | void wxDos2UnixFilename(char *s) { wxDoDos2UnixFilename(s); } |
976 | void wxDos2UnixFilename(wchar_t *s) { wxDoDos2UnixFilename(s); } | |
977 | ||
978 | template<typename T> | |
979 | static void | |
5a3912f2 | 980 | #if defined(__WXMSW__) || defined(__OS2__) |
52de37c7 | 981 | wxDoUnix2DosFilename(T *s) |
46dc76ba | 982 | #else |
52de37c7 | 983 | wxDoUnix2DosFilename(T *WXUNUSED(s) ) |
46dc76ba | 984 | #endif |
c801d85f KB |
985 | { |
986 | // Yes, I really mean this to happen under DOS only! JACS | |
5a3912f2 | 987 | #if defined(__WXMSW__) || defined(__OS2__) |
c801d85f KB |
988 | if (s) |
989 | while (*s) | |
990 | { | |
223d09f6 KB |
991 | if (*s == wxT('/')) |
992 | *s = wxT('\\'); | |
3f4a0c5b | 993 | s++; |
c801d85f KB |
994 | } |
995 | #endif | |
996 | } | |
997 | ||
52de37c7 VS |
998 | void wxUnix2DosFilename(char *s) { wxDoUnix2DosFilename(s); } |
999 | void wxUnix2DosFilename(wchar_t *s) { wxDoUnix2DosFilename(s); } | |
1000 | ||
c801d85f | 1001 | // Concatenate two files to form third |
3f4a0c5b | 1002 | bool |
c801d85f KB |
1003 | wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& file3) |
1004 | { | |
b0c5cd0f WS |
1005 | #if wxUSE_FILE |
1006 | ||
1007 | wxFile in1(file1), in2(file2); | |
1008 | wxTempFile out(file3); | |
1009 | ||
1010 | if ( !in1.IsOpened() || !in2.IsOpened() || !out.IsOpened() ) | |
1011 | return false; | |
1012 | ||
1013 | ssize_t ofs; | |
1014 | unsigned char buf[1024]; | |
1015 | ||
1016 | for( int i=0; i<2; i++) | |
c801d85f | 1017 | { |
b0c5cd0f WS |
1018 | wxFile *in = i==0 ? &in1 : &in2; |
1019 | do{ | |
1020 | if ( (ofs = in->Read(buf,WXSIZEOF(buf))) == wxInvalidOffset ) return false; | |
1021 | if ( ofs > 0 ) | |
1022 | if ( !out.Write(buf,ofs) ) | |
1023 | return false; | |
1024 | } while ( ofs == (ssize_t)WXSIZEOF(buf) ); | |
c801d85f KB |
1025 | } |
1026 | ||
b0c5cd0f WS |
1027 | return out.Commit(); |
1028 | ||
1029 | #else | |
c801d85f | 1030 | |
b0c5cd0f WS |
1031 | wxUnusedVar(file1); |
1032 | wxUnusedVar(file2); | |
1033 | wxUnusedVar(file3); | |
1034 | return false; | |
c801d85f | 1035 | |
b0c5cd0f | 1036 | #endif |
c801d85f KB |
1037 | } |
1038 | ||
0597e7f9 VZ |
1039 | // helper of generic implementation of wxCopyFile() |
1040 | #if !(defined(__WIN32__) || defined(__OS2__) || defined(__PALMOS__)) && \ | |
1041 | wxUSE_FILE | |
1042 | ||
1043 | static bool | |
1044 | wxDoCopyFile(wxFile& fileIn, | |
1045 | const wxStructStat& fbuf, | |
1046 | const wxString& filenameDst, | |
1047 | bool overwrite) | |
1048 | { | |
1049 | // reset the umask as we want to create the file with exactly the same | |
1050 | // permissions as the original one | |
1051 | wxCHANGE_UMASK(0); | |
1052 | ||
1053 | // create file2 with the same permissions than file1 and open it for | |
1054 | // writing | |
1055 | ||
1056 | wxFile fileOut; | |
1057 | if ( !fileOut.Create(filenameDst, overwrite, fbuf.st_mode & 0777) ) | |
1058 | return false; | |
1059 | ||
1060 | // copy contents of file1 to file2 | |
1061 | char buf[4096]; | |
1062 | for ( ;; ) | |
1063 | { | |
1064 | ssize_t count = fileIn.Read(buf, WXSIZEOF(buf)); | |
1065 | if ( count == wxInvalidOffset ) | |
1066 | return false; | |
1067 | ||
1068 | // end of file? | |
1069 | if ( !count ) | |
1070 | break; | |
1071 | ||
1072 | if ( fileOut.Write(buf, count) < (size_t)count ) | |
1073 | return false; | |
1074 | } | |
1075 | ||
1076 | // we can expect fileIn to be closed successfully, but we should ensure | |
1077 | // that fileOut was closed as some write errors (disk full) might not be | |
1078 | // detected before doing this | |
1079 | return fileIn.Close() && fileOut.Close(); | |
1080 | } | |
1081 | ||
1082 | #endif // generic implementation of wxCopyFile | |
1083 | ||
c801d85f | 1084 | // Copy files |
3f4a0c5b | 1085 | bool |
4658c44e | 1086 | wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) |
c801d85f | 1087 | { |
04ef50df | 1088 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
4658c44e VZ |
1089 | // CopyFile() copies file attributes and modification time too, so use it |
1090 | // instead of our code if available | |
1091 | // | |
1092 | // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite | |
e0a050e3 | 1093 | if ( !::CopyFile(file1.fn_str(), file2.fn_str(), !overwrite) ) |
564225a1 VZ |
1094 | { |
1095 | wxLogSysError(_("Failed to copy the file '%s' to '%s'"), | |
1096 | file1.c_str(), file2.c_str()); | |
1097 | ||
79e162f5 | 1098 | return false; |
564225a1 | 1099 | } |
5a3912f2 | 1100 | #elif defined(__OS2__) |
1f3d9911 | 1101 | if ( ::DosCopy(file1.c_str(), file2.c_str(), overwrite ? DCPY_EXISTING : 0) != 0 ) |
79e162f5 | 1102 | return false; |
68351053 WS |
1103 | #elif defined(__PALMOS__) |
1104 | // TODO with http://www.palmos.com/dev/support/docs/protein_books/Memory_Databases_Files/ | |
1105 | return false; | |
98438730 | 1106 | #elif wxUSE_FILE // !Win32 |
32f31043 | 1107 | |
b1ac3b56 | 1108 | wxStructStat fbuf; |
32f31043 | 1109 | // get permissions of file1 |
b1ac3b56 | 1110 | if ( wxStat( file1.c_str(), &fbuf) != 0 ) |
32f31043 VZ |
1111 | { |
1112 | // the file probably doesn't exist or we haven't the rights to read | |
1113 | // from it anyhow | |
1114 | wxLogSysError(_("Impossible to get permissions for file '%s'"), | |
1115 | file1.c_str()); | |
79e162f5 | 1116 | return false; |
32f31043 VZ |
1117 | } |
1118 | ||
1119 | // open file1 for reading | |
1120 | wxFile fileIn(file1, wxFile::read); | |
a339970a | 1121 | if ( !fileIn.IsOpened() ) |
79e162f5 | 1122 | return false; |
c801d85f | 1123 | |
32f31043 VZ |
1124 | // remove file2, if it exists. This is needed for creating |
1125 | // file2 with the correct permissions in the next step | |
4658c44e | 1126 | if ( wxFileExists(file2) && (!overwrite || !wxRemoveFile(file2))) |
32f31043 VZ |
1127 | { |
1128 | wxLogSysError(_("Impossible to overwrite the file '%s'"), | |
1129 | file2.c_str()); | |
79e162f5 | 1130 | return false; |
32f31043 VZ |
1131 | } |
1132 | ||
0597e7f9 | 1133 | wxDoCopyFile(fileIn, fbuf, file2, overwrite); |
32f31043 | 1134 | |
0597e7f9 VZ |
1135 | #if defined(__WXMAC__) || defined(__WXCOCOA__) |
1136 | // copy the resource fork of the file too if it's present | |
1137 | wxString pathRsrcOut; | |
1138 | wxFile fileRsrcIn; | |
c801d85f | 1139 | |
c7386783 | 1140 | { |
0597e7f9 VZ |
1141 | // suppress error messages from this block as resource forks don't have |
1142 | // to exist | |
1143 | wxLogNull noLog; | |
1144 | ||
1145 | // it's not enough to check for file existence: it always does on HFS | |
1146 | // but is empty for files without resources | |
1147 | if ( fileRsrcIn.Open(file1 + wxT("/..namedfork/rsrc")) && | |
1148 | fileRsrcIn.Length() > 0 ) | |
1149 | { | |
1150 | // we must be using HFS or another filesystem with resource fork | |
1151 | // support, suppose that destination file system also is HFS[-like] | |
1152 | pathRsrcOut = file2 + wxT("/..namedfork/rsrc"); | |
1153 | } | |
1154 | else // check if we have resource fork in separate file (non-HFS case) | |
1155 | { | |
1156 | wxFileName fnRsrc(file1); | |
1157 | fnRsrc.SetName(wxT("._") + fnRsrc.GetName()); | |
a339970a | 1158 | |
0597e7f9 VZ |
1159 | fileRsrcIn.Close(); |
1160 | if ( fileRsrcIn.Open( fnRsrc.GetFullPath() ) ) | |
1161 | { | |
1162 | fnRsrc = file2; | |
1163 | fnRsrc.SetName(wxT("._") + fnRsrc.GetName()); | |
a339970a | 1164 | |
0597e7f9 VZ |
1165 | pathRsrcOut = fnRsrc.GetFullPath(); |
1166 | } | |
1167 | } | |
c7386783 | 1168 | } |
c801d85f | 1169 | |
0597e7f9 VZ |
1170 | if ( !pathRsrcOut.empty() ) |
1171 | { | |
1172 | if ( !wxDoCopyFile(fileRsrcIn, fbuf, pathRsrcOut, overwrite) ) | |
1173 | return false; | |
1174 | } | |
1175 | #endif // wxMac || wxCocoa | |
abb74e97 | 1176 | |
5fde6fcc | 1177 | #if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__) |
abb74e97 VZ |
1178 | // no chmod in VA. Should be some permission API for HPFS386 partitions |
1179 | // however | |
c3396917 | 1180 | if ( chmod(OS_FILENAME(file2), fbuf.st_mode) != 0 ) |
32f31043 VZ |
1181 | { |
1182 | wxLogSysError(_("Impossible to set permissions for the file '%s'"), | |
1183 | file2.c_str()); | |
79e162f5 | 1184 | return false; |
32f31043 | 1185 | } |
4658c44e | 1186 | #endif // OS/2 || Mac |
98438730 WS |
1187 | |
1188 | #else // !Win32 && ! wxUSE_FILE | |
1189 | ||
1190 | // impossible to simulate with wxWidgets API | |
1191 | wxUnusedVar(file1); | |
1192 | wxUnusedVar(file2); | |
1193 | wxUnusedVar(overwrite); | |
1194 | return false; | |
1195 | ||
564225a1 | 1196 | #endif // __WXMSW__ && __WIN32__ |
4658c44e | 1197 | |
79e162f5 | 1198 | return true; |
c801d85f KB |
1199 | } |
1200 | ||
3f4a0c5b | 1201 | bool |
57e988b8 | 1202 | wxRenameFile(const wxString& file1, const wxString& file2, bool overwrite) |
c801d85f | 1203 | { |
57e988b8 VZ |
1204 | if ( !overwrite && wxFileExists(file2) ) |
1205 | { | |
1206 | wxLogSysError | |
1207 | ( | |
1208 | _("Failed to rename the file '%s' to '%s' because the destination file already exists."), | |
1209 | file1.c_str(), file2.c_str() | |
1210 | ); | |
1211 | ||
1212 | return false; | |
1213 | } | |
1214 | ||
68351053 | 1215 | #if !defined(__WXWINCE__) && !defined(__WXPALMOS__) |
1c193821 | 1216 | // Normal system call |
c3396917 | 1217 | if ( wxRename (file1, file2) == 0 ) |
79e162f5 | 1218 | return true; |
1c193821 | 1219 | #endif |
a339970a | 1220 | |
c801d85f | 1221 | // Try to copy |
57e988b8 | 1222 | if (wxCopyFile(file1, file2, overwrite)) { |
c801d85f | 1223 | wxRemoveFile(file1); |
79e162f5 | 1224 | return true; |
c801d85f KB |
1225 | } |
1226 | // Give up | |
79e162f5 | 1227 | return false; |
c801d85f KB |
1228 | } |
1229 | ||
1230 | bool wxRemoveFile(const wxString& file) | |
1231 | { | |
161f4f73 VZ |
1232 | #if defined(__VISUALC__) \ |
1233 | || defined(__BORLANDC__) \ | |
1234 | || defined(__WATCOMC__) \ | |
ba1e9d6c | 1235 | || defined(__DMC__) \ |
e874f867 SC |
1236 | || defined(__GNUWIN32__) \ |
1237 | || (defined(__MWERKS__) && defined(__MSL__)) | |
68351053 | 1238 | int res = wxRemove(file); |
c4e41ce3 | 1239 | #elif defined(__WXMAC__) |
e0a050e3 | 1240 | int res = unlink(file.fn_str()); |
68351053 WS |
1241 | #elif defined(__WXPALMOS__) |
1242 | int res = 1; | |
1243 | // TODO with VFSFileDelete() | |
c801d85f | 1244 | #else |
68351053 | 1245 | int res = unlink(OS_FILENAME(file)); |
c801d85f | 1246 | #endif |
a339970a | 1247 | |
68351053 | 1248 | return res == 0; |
c801d85f KB |
1249 | } |
1250 | ||
1a33c3ba | 1251 | bool wxMkdir(const wxString& dir, int perm) |
c801d85f | 1252 | { |
68351053 WS |
1253 | #if defined(__WXPALMOS__) |
1254 | return false; | |
1255 | #elif defined(__WXMAC__) && !defined(__UNIX__) | |
e0a050e3 | 1256 | return (mkdir(dir.fn_str() , 0 ) == 0); |
7708abe9 | 1257 | #else // !Mac |
50920146 | 1258 | const wxChar *dirname = dir.c_str(); |
1a33c3ba | 1259 | |
c2ff79b1 | 1260 | // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too |
7708abe9 | 1261 | // for the GNU compiler |
5a3912f2 | 1262 | #if (!(defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__) |
9c8cd106 | 1263 | #if defined(MSVCRT) |
79e162f5 | 1264 | wxUnusedVar(perm); |
2e38557f | 1265 | if ( mkdir(wxFNCONV(dirname)) != 0 ) |
9c8cd106 WS |
1266 | #else |
1267 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) | |
2e38557f | 1268 | #endif |
5a3912f2 | 1269 | #elif defined(__OS2__) |
7a893a31 | 1270 | wxUnusedVar(perm); |
f6bcfd97 | 1271 | if (::DosCreateDir((PSZ)dirname, NULL) != 0) // enhance for EAB's?? |
b916f809 VS |
1272 | #elif defined(__DOS__) |
1273 | #if defined(__WATCOMC__) | |
1274 | (void)perm; | |
1275 | if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) | |
1276 | #elif defined(__DJGPP__) | |
1277 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) | |
1278 | #else | |
1279 | #error "Unsupported DOS compiler!" | |
1280 | #endif | |
1281 | #else // !MSW, !DOS and !OS/2 VAC++ | |
a6f4dbbd | 1282 | wxUnusedVar(perm); |
1c193821 JS |
1283 | #ifdef __WXWINCE__ |
1284 | if ( !CreateDirectory(dirname, NULL) ) | |
1285 | #else | |
a6f4dbbd | 1286 | if ( wxMkDir(dir.fn_str()) != 0 ) |
1c193821 | 1287 | #endif |
7708abe9 | 1288 | #endif // !MSW/MSW |
1a33c3ba VZ |
1289 | { |
1290 | wxLogSysError(_("Directory '%s' couldn't be created"), dirname); | |
1291 | ||
79e162f5 | 1292 | return false; |
1a33c3ba VZ |
1293 | } |
1294 | ||
79e162f5 | 1295 | return true; |
e7549107 | 1296 | #endif // Mac/!Mac |
c801d85f KB |
1297 | } |
1298 | ||
1299 | bool wxRmdir(const wxString& dir, int WXUNUSED(flags)) | |
1300 | { | |
68351053 | 1301 | #if defined(__VMS__) |
79e162f5 | 1302 | return false; //to be changed since rmdir exists in VMS7.x |
5a3912f2 | 1303 | #elif defined(__OS2__) |
1f3d9911 | 1304 | return (::DosDeleteDir(dir.c_str()) == 0); |
68351053 | 1305 | #elif defined(__WXWINCE__) |
4d21409e | 1306 | return (RemoveDirectory(dir) != 0); |
68351053 WS |
1307 | #elif defined(__WXPALMOS__) |
1308 | // TODO with VFSFileRename() | |
1309 | return false; | |
a3ef5bf5 | 1310 | #else |
1c193821 | 1311 | return (wxRmDir(OS_FILENAME(dir)) == 0); |
c801d85f | 1312 | #endif |
c801d85f KB |
1313 | } |
1314 | ||
c801d85f | 1315 | // does the path exists? (may have or not '/' or '\\' at the end) |
e960c20e | 1316 | bool wxDirExists(const wxString& pathName) |
c801d85f | 1317 | { |
e960c20e | 1318 | wxString strPath(pathName); |
e32d659d | 1319 | |
5a3912f2 | 1320 | #if defined(__WINDOWS__) || defined(__OS2__) |
f6bcfd97 BP |
1321 | // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists, |
1322 | // so remove all trailing backslashes from the path - but don't do this for | |
56614e51 | 1323 | // the paths "d:\" (which are different from "d:") nor for just "\" |
f6bcfd97 BP |
1324 | while ( wxEndsWithPathSeparator(strPath) ) |
1325 | { | |
1326 | size_t len = strPath.length(); | |
cc4a1ce1 | 1327 | if ( len == 1 || (len == 3 && strPath[len - 2] == _T(':')) ) |
f6bcfd97 | 1328 | break; |
c801d85f | 1329 | |
f6bcfd97 BP |
1330 | strPath.Truncate(len - 1); |
1331 | } | |
1332 | #endif // __WINDOWS__ | |
1333 | ||
5a3912f2 SN |
1334 | #ifdef __OS2__ |
1335 | // OS/2 can't handle "d:", it wants either "d:\" or "d:." | |
1d4f9cb7 | 1336 | if (strPath.length() == 2 && strPath[1u] == _T(':')) |
5a3912f2 SN |
1337 | strPath << _T('.'); |
1338 | #endif | |
1339 | ||
68351053 WS |
1340 | #if defined(__WXPALMOS__) |
1341 | return false; | |
1342 | #elif defined(__WIN32__) && !defined(__WXMICROWIN__) | |
e32d659d | 1343 | // stat() can't cope with network paths |
e0a050e3 | 1344 | DWORD ret = ::GetFileAttributes(strPath.fn_str()); |
2db300c6 | 1345 | |
a28ae409 | 1346 | return (ret != (DWORD)-1) && (ret & FILE_ATTRIBUTE_DIRECTORY); |
27b2dd53 | 1347 | #elif defined(__OS2__) |
fe1f34f8 SN |
1348 | FILESTATUS3 Info = {{0}}; |
1349 | APIRET rc = ::DosQueryPathInfo((PSZ)(WXSTRINGCAST strPath), FIL_STANDARD, | |
1350 | (void*) &Info, sizeof(FILESTATUS3)); | |
1351 | ||
1352 | return ((rc == NO_ERROR) && (Info.attrFile & FILE_DIRECTORY)) || | |
1353 | (rc == ERROR_SHARING_VIOLATION); | |
1354 | // If we got a sharing violation, there must be something with this name. | |
e32d659d | 1355 | #else // !__WIN32__ |
4c97e024 | 1356 | |
f6bcfd97 | 1357 | wxStructStat st; |
71c97a89 | 1358 | #ifndef __VISAGECPP__ |
5a3912f2 | 1359 | return wxStat(strPath.c_str(), &st) == 0 && ((st.st_mode & S_IFMT) == S_IFDIR); |
71c97a89 DW |
1360 | #else |
1361 | // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only | |
e960c20e | 1362 | return wxStat(strPath.c_str(), &st) == 0 && (st.st_mode == S_IFDIR); |
71c97a89 DW |
1363 | #endif |
1364 | ||
e32d659d | 1365 | #endif // __WIN32__/!__WIN32__ |
c801d85f KB |
1366 | } |
1367 | ||
1368 | // Get a temporary filename, opening and closing the file. | |
50920146 | 1369 | wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf) |
c801d85f | 1370 | { |
e44d5a58 VZ |
1371 | wxString filename; |
1372 | if ( !wxGetTempFileName(prefix, filename) ) | |
ade35f11 | 1373 | return NULL; |
c801d85f | 1374 | |
ade35f11 VZ |
1375 | if ( buf ) |
1376 | wxStrcpy(buf, filename); | |
1377 | else | |
f526f752 | 1378 | buf = MYcopystring(filename); |
c801d85f | 1379 | |
ade35f11 | 1380 | return buf; |
c801d85f KB |
1381 | } |
1382 | ||
c0ab6adf JS |
1383 | bool wxGetTempFileName(const wxString& prefix, wxString& buf) |
1384 | { | |
e44d5a58 | 1385 | #if wxUSE_FILE |
522d32e5 | 1386 | buf = wxFileName::CreateTempFileName(prefix); |
ade35f11 VZ |
1387 | |
1388 | return !buf.empty(); | |
e44d5a58 VZ |
1389 | #else // !wxUSE_FILE |
1390 | wxUnusedVar(prefix); | |
1391 | wxUnusedVar(buf); | |
1392 | ||
1393 | return false; | |
1394 | #endif // wxUSE_FILE/!wxUSE_FILE | |
c0ab6adf JS |
1395 | } |
1396 | ||
c801d85f KB |
1397 | // Get first file name matching given wild card. |
1398 | ||
8ff12342 VS |
1399 | static wxDir *gs_dir = NULL; |
1400 | static wxString gs_dirPath; | |
1401 | ||
52de37c7 | 1402 | wxString wxFindFirstFile(const wxString& spec, int flags) |
8ff12342 | 1403 | { |
9a5ead1e | 1404 | wxSplitPath(spec, &gs_dirPath, NULL, NULL); |
a6f4dbbd | 1405 | if ( gs_dirPath.empty() ) |
8ff12342 | 1406 | gs_dirPath = wxT("."); |
083f7497 | 1407 | if ( !wxEndsWithPathSeparator(gs_dirPath ) ) |
8ff12342 VS |
1408 | gs_dirPath << wxFILE_SEP_PATH; |
1409 | ||
1410 | if (gs_dir) | |
1411 | delete gs_dir; | |
1412 | gs_dir = new wxDir(gs_dirPath); | |
2db300c6 | 1413 | |
8ff12342 VS |
1414 | if ( !gs_dir->IsOpened() ) |
1415 | { | |
1416 | wxLogSysError(_("Can not enumerate files '%s'"), spec); | |
1417 | return wxEmptyString; | |
1418 | } | |
2db300c6 | 1419 | |
999836aa | 1420 | int dirFlags; |
8ff12342 VS |
1421 | switch (flags) |
1422 | { | |
1423 | case wxDIR: dirFlags = wxDIR_DIRS; break; | |
1424 | case wxFILE: dirFlags = wxDIR_FILES; break; | |
1425 | default: dirFlags = wxDIR_DIRS | wxDIR_FILES; break; | |
1426 | } | |
2db300c6 | 1427 | |
8ff12342 | 1428 | wxString result; |
52de37c7 | 1429 | gs_dir->GetFirst(&result, wxFileNameFromPath(spec), dirFlags); |
a6f4dbbd | 1430 | if ( result.empty() ) |
e168b6ac | 1431 | { |
8ff12342 | 1432 | wxDELETE(gs_dir); |
e168b6ac VS |
1433 | return result; |
1434 | } | |
8ff12342 VS |
1435 | |
1436 | return gs_dirPath + result; | |
1437 | } | |
1438 | ||
1439 | wxString wxFindNextFile() | |
1440 | { | |
1441 | wxASSERT_MSG( gs_dir, wxT("You must call wxFindFirstFile before!") ); | |
1442 | ||
1443 | wxString result; | |
1444 | gs_dir->GetNext(&result); | |
2db300c6 | 1445 | |
a6f4dbbd | 1446 | if ( result.empty() ) |
e168b6ac | 1447 | { |
8ff12342 | 1448 | wxDELETE(gs_dir); |
e168b6ac VS |
1449 | return result; |
1450 | } | |
2db300c6 | 1451 | |
8ff12342 VS |
1452 | return gs_dirPath + result; |
1453 | } | |
1454 | ||
c801d85f KB |
1455 | |
1456 | // Get current working directory. | |
ce045aed WS |
1457 | // If buf is NULL, allocates space using new, else copies into buf. |
1458 | // wxGetWorkingDirectory() is obsolete, use wxGetCwd() | |
1459 | // wxDoGetCwd() is their common core to be moved | |
1460 | // to wxGetCwd() once wxGetWorkingDirectory() will be removed. | |
1461 | // Do not expose wxDoGetCwd in headers! | |
1462 | ||
1463 | wxChar *wxDoGetCwd(wxChar *buf, int sz) | |
c801d85f | 1464 | { |
68351053 | 1465 | #if defined(__WXPALMOS__) |
ce045aed WS |
1466 | // TODO |
1467 | if(buf && sz>0) buf[0] = _T('\0'); | |
cdc05919 | 1468 | return buf; |
68351053 | 1469 | #elif defined(__WXWINCE__) |
abf912c5 | 1470 | // TODO |
ce045aed | 1471 | if(buf && sz>0) buf[0] = _T('\0'); |
cdc05919 | 1472 | return buf; |
1c193821 | 1473 | #else |
13b1f8a7 | 1474 | if ( !buf ) |
f6bcfd97 | 1475 | { |
13b1f8a7 VZ |
1476 | buf = new wxChar[sz + 1]; |
1477 | } | |
1478 | ||
79e162f5 | 1479 | bool ok wxDUMMY_INITIALIZE(false); |
13b1f8a7 VZ |
1480 | |
1481 | // for the compilers which have Unicode version of _getcwd(), call it | |
1482 | // directly, for the others call the ANSI version and do the translation | |
dbc38199 | 1483 | #if !wxUSE_UNICODE |
247f8a77 | 1484 | #define cbuf buf |
dbc38199 | 1485 | #else // wxUSE_UNICODE |
79e162f5 | 1486 | bool needsANSI = true; |
dbc38199 | 1487 | |
247f8a77 | 1488 | #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU |
c35c94f6 | 1489 | char cbuf[_MAXPATHLEN]; |
247f8a77 | 1490 | #endif |
dbc38199 | 1491 | |
13b1f8a7 | 1492 | #ifdef HAVE_WGETCWD |
247f8a77 | 1493 | #if wxUSE_UNICODE_MSLU |
406d283a | 1494 | if ( wxGetOsVersion() != wxOS_WINDOWS_9X ) |
f5c0e657 | 1495 | #else |
79e162f5 | 1496 | char *cbuf = NULL; // never really used because needsANSI will always be false |
247f8a77 VS |
1497 | #endif |
1498 | { | |
1499 | ok = _wgetcwd(buf, sz) != NULL; | |
79e162f5 | 1500 | needsANSI = false; |
247f8a77 | 1501 | } |
13b1f8a7 | 1502 | #endif |
13b1f8a7 | 1503 | |
247f8a77 | 1504 | if ( needsANSI ) |
dbc38199 | 1505 | #endif // wxUSE_UNICODE |
247f8a77 | 1506 | { |
29d0a26e | 1507 | #if defined(_MSC_VER) || defined(__MINGW32__) |
dbc38199 | 1508 | ok = _getcwd(cbuf, sz) != NULL; |
5a3912f2 | 1509 | #elif defined(__OS2__) |
13b1f8a7 | 1510 | APIRET rc; |
5a3912f2 SN |
1511 | ULONG ulDriveNum = 0; |
1512 | ULONG ulDriveMap = 0; | |
a62848fd | 1513 | rc = ::DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap); |
5a3912f2 | 1514 | ok = rc == 0; |
a62848fd WS |
1515 | if (ok) |
1516 | { | |
1517 | sz -= 3; | |
1518 | rc = ::DosQueryCurrentDir( 0 // current drive | |
5a3912f2 SN |
1519 | ,cbuf + 3 |
1520 | ,(PULONG)&sz | |
1521 | ); | |
7a893a31 | 1522 | cbuf[0] = char('A' + (ulDriveNum - 1)); |
5a3912f2 SN |
1523 | cbuf[1] = ':'; |
1524 | cbuf[2] = '\\'; | |
1525 | ok = rc == 0; | |
1526 | } | |
13b1f8a7 | 1527 | #else // !Win32/VC++ !Mac !OS2 |
dbc38199 | 1528 | ok = getcwd(cbuf, sz) != NULL; |
13b1f8a7 | 1529 | #endif // platform |
247f8a77 | 1530 | |
0ad76eea | 1531 | #if wxUSE_UNICODE |
247f8a77 VS |
1532 | // finally convert the result to Unicode if needed |
1533 | wxConvFile.MB2WC(buf, cbuf, sz); | |
1534 | #endif // wxUSE_UNICODE | |
1535 | } | |
13b1f8a7 VZ |
1536 | |
1537 | if ( !ok ) | |
19193a2c | 1538 | { |
13b1f8a7 | 1539 | wxLogSysError(_("Failed to get the working directory")); |
19193a2c | 1540 | |
13b1f8a7 VZ |
1541 | // VZ: the old code used to return "." on error which didn't make any |
1542 | // sense at all to me - empty string is a better error indicator | |
1543 | // (NULL might be even better but I'm afraid this could lead to | |
1544 | // problems with the old code assuming the return is never NULL) | |
1545 | buf[0] = _T('\0'); | |
19193a2c | 1546 | } |
13b1f8a7 | 1547 | else // ok, but we might need to massage the path into the right format |
19193a2c | 1548 | { |
4b00a538 | 1549 | #ifdef __DJGPP__ |
13b1f8a7 VZ |
1550 | // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths |
1551 | // with / deliminers. We don't like that. | |
1552 | for (wxChar *ch = buf; *ch; ch++) | |
1553 | { | |
1554 | if (*ch == wxT('/')) | |
1555 | *ch = wxT('\\'); | |
1556 | } | |
1557 | #endif // __DJGPP__ | |
1558 | ||
17234b26 MB |
1559 | // MBN: we hope that in the case the user is compiling a GTK+/Motif app, |
1560 | // he needs Unix as opposed to Win32 pathnames | |
1561 | #if defined( __CYGWIN__ ) && defined( __WINDOWS__ ) | |
3ffbc733 | 1562 | // another example of DOS/Unix mix (Cygwin) |
13b1f8a7 | 1563 | wxString pathUnix = buf; |
7682e22e VZ |
1564 | #if wxUSE_UNICODE |
1565 | char bufA[_MAXPATHLEN]; | |
1566 | cygwin_conv_to_full_win32_path(pathUnix.mb_str(wxConvFile), bufA); | |
1567 | wxConvFile.MB2WC(buf, bufA, sz); | |
1568 | #else | |
13b1f8a7 | 1569 | cygwin_conv_to_full_win32_path(pathUnix, buf); |
7682e22e | 1570 | #endif // wxUSE_UNICODE |
e02e8816 | 1571 | #endif // __CYGWIN__ |
13b1f8a7 | 1572 | } |
4b00a538 | 1573 | |
13b1f8a7 | 1574 | return buf; |
dbc38199 VS |
1575 | |
1576 | #if !wxUSE_UNICODE | |
247f8a77 | 1577 | #undef cbuf |
dbc38199 | 1578 | #endif |
1c193821 JS |
1579 | |
1580 | #endif | |
1581 | // __WXWINCE__ | |
c801d85f KB |
1582 | } |
1583 | ||
ce045aed WS |
1584 | #if WXWIN_COMPATIBILITY_2_6 |
1585 | wxChar *wxGetWorkingDirectory(wxChar *buf, int sz) | |
7af89395 | 1586 | { |
ce045aed WS |
1587 | return wxDoGetCwd(buf,sz); |
1588 | } | |
1589 | #endif // WXWIN_COMPATIBILITY_2_6 | |
dc259b79 | 1590 | |
ce045aed WS |
1591 | wxString wxGetCwd() |
1592 | { | |
1593 | wxString str; | |
1594 | wxDoGetCwd(wxStringBuffer(str, _MAXPATHLEN), _MAXPATHLEN); | |
eac2aeb0 | 1595 | return str; |
7af89395 VZ |
1596 | } |
1597 | ||
c801d85f KB |
1598 | bool wxSetWorkingDirectory(const wxString& d) |
1599 | { | |
5a3912f2 | 1600 | #if defined(__OS2__) |
43c97407 SN |
1601 | if (d[1] == ':') |
1602 | { | |
5a1e0e91 | 1603 | ::DosSetDefaultDisk(wxToupper(d[0]) - _T('A') + 1); |
2b2883a5 JS |
1604 | // do not call DosSetCurrentDir when just changing drive, |
1605 | // since it requires e.g. "d:." instead of "d:"! | |
1606 | if (d.length() == 2) | |
1607 | return true; | |
43c97407 | 1608 | } |
1f3d9911 | 1609 | return (::DosSetCurrentDir(d.c_str()) == 0); |
5a3912f2 SN |
1610 | #elif defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__) |
1611 | return (chdir(wxFNSTRINGCAST d.fn_str()) == 0); | |
34138703 | 1612 | #elif defined(__WINDOWS__) |
a62848fd | 1613 | |
c801d85f | 1614 | #ifdef __WIN32__ |
1c193821 JS |
1615 | #ifdef __WXWINCE__ |
1616 | // No equivalent in WinCE | |
abf912c5 | 1617 | wxUnusedVar(d); |
79e162f5 | 1618 | return false; |
c801d85f | 1619 | #else |
e0a050e3 | 1620 | return (bool)(SetCurrentDirectory(d.fn_str()) != 0); |
1c193821 JS |
1621 | #endif |
1622 | #else | |
1623 | // Must change drive, too. | |
1624 | bool isDriveSpec = ((strlen(d) > 1) && (d[1] == ':')); | |
1625 | if (isDriveSpec) | |
c801d85f | 1626 | { |
1c193821 | 1627 | wxChar firstChar = d[0]; |
a62848fd | 1628 | |
1c193821 JS |
1629 | // To upper case |
1630 | if (firstChar > 90) | |
1631 | firstChar = firstChar - 32; | |
a62848fd | 1632 | |
1c193821 JS |
1633 | // To a drive number |
1634 | unsigned int driveNo = firstChar - 64; | |
1635 | if (driveNo > 0) | |
1636 | { | |
1637 | unsigned int noDrives; | |
1638 | _dos_setdrive(driveNo, &noDrives); | |
1639 | } | |
c801d85f | 1640 | } |
1c193821 | 1641 | bool success = (chdir(WXSTRINGCAST d) == 0); |
a62848fd | 1642 | |
1c193821 | 1643 | return success; |
c801d85f | 1644 | #endif |
a62848fd | 1645 | |
c801d85f KB |
1646 | #endif |
1647 | } | |
1648 | ||
631f1bfe JS |
1649 | // Get the OS directory if appropriate (such as the Windows directory). |
1650 | // On non-Windows platform, probably just return the empty string. | |
1651 | wxString wxGetOSDirectory() | |
1652 | { | |
1c193821 JS |
1653 | #ifdef __WXWINCE__ |
1654 | return wxString(wxT("\\Windows")); | |
1655 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
50920146 | 1656 | wxChar buf[256]; |
631f1bfe JS |
1657 | GetWindowsDirectory(buf, 256); |
1658 | return wxString(buf); | |
2d3441d7 GD |
1659 | #elif defined(__WXMAC__) |
1660 | return wxMacFindFolder(kOnSystemDisk, 'macs', false); | |
631f1bfe JS |
1661 | #else |
1662 | return wxEmptyString; | |
1663 | #endif | |
1664 | } | |
1665 | ||
52de37c7 | 1666 | bool wxEndsWithPathSeparator(const wxString& filename) |
c801d85f | 1667 | { |
52de37c7 | 1668 | return !filename.empty() && wxIsPathSeparator(filename.Last()); |
c801d85f KB |
1669 | } |
1670 | ||
79e162f5 | 1671 | // find a file in a list of directories, returns false if not found |
52de37c7 | 1672 | bool wxFindFileInPath(wxString *pStr, const wxString& szPath, const wxString& szFile) |
c801d85f | 1673 | { |
a17e237f | 1674 | // we assume that it's not empty |
52de37c7 | 1675 | wxCHECK_MSG( !szFile.empty(), false, |
f6bcfd97 | 1676 | _T("empty file name in wxFindFileInPath")); |
a17e237f VZ |
1677 | |
1678 | // skip path separator in the beginning of the file name if present | |
52de37c7 VS |
1679 | wxString szFile2; |
1680 | if ( wxIsPathSeparator(szFile[0u]) ) | |
1681 | szFile2 = szFile.Mid(1); | |
1682 | else | |
1683 | szFile2 = szFile; | |
1684 | ||
1685 | wxStringTokenizer tkn(szPath, wxPATH_SEP); | |
1686 | ||
1687 | while ( tkn.HasMoreTokens() ) | |
a17e237f | 1688 | { |
52de37c7 VS |
1689 | wxString strFile = tkn.GetNextToken(); |
1690 | if ( !wxEndsWithPathSeparator(strFile) ) | |
a17e237f | 1691 | strFile += wxFILE_SEP_PATH; |
52de37c7 | 1692 | strFile += szFile2; |
a17e237f | 1693 | |
52de37c7 VS |
1694 | if ( wxFileExists(strFile) ) |
1695 | { | |
a17e237f | 1696 | *pStr = strFile; |
52de37c7 | 1697 | return true; |
a17e237f | 1698 | } |
c801d85f | 1699 | } |
c801d85f | 1700 | |
52de37c7 | 1701 | return false; |
c801d85f KB |
1702 | } |
1703 | ||
163b3ad7 | 1704 | void WXDLLIMPEXP_BASE wxSplitPath(const wxString& fileName, |
3826db3e VZ |
1705 | wxString *pstrPath, |
1706 | wxString *pstrName, | |
1707 | wxString *pstrExt) | |
1708 | { | |
52de37c7 | 1709 | wxFileName::SplitPath(fileName, pstrPath, pstrName, pstrExt); |
3826db3e | 1710 | } |
7f555861 | 1711 | |
2c5e5cbd VS |
1712 | #if wxUSE_DATETIME |
1713 | ||
163b3ad7 | 1714 | time_t WXDLLIMPEXP_BASE wxFileModificationTime(const wxString& filename) |
a47ce4a7 | 1715 | { |
2936a6b1 VZ |
1716 | wxDateTime mtime; |
1717 | if ( !wxFileName(filename).GetTimes(NULL, &mtime, NULL) ) | |
1718 | return (time_t)-1; | |
a62848fd | 1719 | |
2936a6b1 | 1720 | return mtime.GetTicks(); |
a47ce4a7 VS |
1721 | } |
1722 | ||
2c5e5cbd VS |
1723 | #endif // wxUSE_DATETIME |
1724 | ||
a47ce4a7 | 1725 | |
9e152a55 WS |
1726 | // Parses the filterStr, returning the number of filters. |
1727 | // Returns 0 if none or if there's a problem. | |
daf32463 | 1728 | // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpeg" |
9e152a55 | 1729 | |
163b3ad7 | 1730 | int WXDLLIMPEXP_BASE wxParseCommonDialogsFilter(const wxString& filterStr, |
7bea7b91 WS |
1731 | wxArrayString& descriptions, |
1732 | wxArrayString& filters) | |
9e152a55 WS |
1733 | { |
1734 | descriptions.Clear(); | |
1735 | filters.Clear(); | |
1736 | ||
1737 | wxString str(filterStr); | |
1738 | ||
1739 | wxString description, filter; | |
1740 | int pos = 0; | |
1741 | while( pos != wxNOT_FOUND ) | |
1742 | { | |
1743 | pos = str.Find(wxT('|')); | |
1744 | if ( pos == wxNOT_FOUND ) | |
1745 | { | |
1746 | // if there are no '|'s at all in the string just take the entire | |
daf32463 | 1747 | // string as filter and make description empty for later autocompletion |
9e152a55 WS |
1748 | if ( filters.IsEmpty() ) |
1749 | { | |
daf32463 | 1750 | descriptions.Add(wxEmptyString); |
9e152a55 WS |
1751 | filters.Add(filterStr); |
1752 | } | |
1753 | else | |
1754 | { | |
1755 | wxFAIL_MSG( _T("missing '|' in the wildcard string!") ); | |
1756 | } | |
1757 | ||
1758 | break; | |
1759 | } | |
1760 | ||
1761 | description = str.Left(pos); | |
1762 | str = str.Mid(pos + 1); | |
1763 | pos = str.Find(wxT('|')); | |
1764 | if ( pos == wxNOT_FOUND ) | |
1765 | { | |
1766 | filter = str; | |
1767 | } | |
1768 | else | |
1769 | { | |
1770 | filter = str.Left(pos); | |
1771 | str = str.Mid(pos + 1); | |
1772 | } | |
1773 | ||
1774 | descriptions.Add(description); | |
1775 | filters.Add(filter); | |
1776 | } | |
1777 | ||
daf32463 WS |
1778 | #if defined(__WXMOTIF__) |
1779 | // split it so there is one wildcard per entry | |
1780 | for( size_t i = 0 ; i < descriptions.GetCount() ; i++ ) | |
1781 | { | |
1782 | pos = filters[i].Find(wxT(';')); | |
1783 | if (pos != wxNOT_FOUND) | |
1784 | { | |
1785 | // first split only filters | |
1786 | descriptions.Insert(descriptions[i],i+1); | |
1787 | filters.Insert(filters[i].Mid(pos+1),i+1); | |
1788 | filters[i]=filters[i].Left(pos); | |
1789 | ||
1790 | // autoreplace new filter in description with pattern: | |
1791 | // C/C++ Files(*.cpp;*.c;*.h)|*.cpp;*.c;*.h | |
1792 | // cause split into: | |
1793 | // C/C++ Files(*.cpp)|*.cpp | |
1794 | // C/C++ Files(*.c;*.h)|*.c;*.h | |
1795 | // and next iteration cause another split into: | |
1796 | // C/C++ Files(*.cpp)|*.cpp | |
1797 | // C/C++ Files(*.c)|*.c | |
1798 | // C/C++ Files(*.h)|*.h | |
1799 | for ( size_t k=i;k<i+2;k++ ) | |
1800 | { | |
1801 | pos = descriptions[k].Find(filters[k]); | |
1802 | if (pos != wxNOT_FOUND) | |
1803 | { | |
1804 | wxString before = descriptions[k].Left(pos); | |
1805 | wxString after = descriptions[k].Mid(pos+filters[k].Len()); | |
1806 | pos = before.Find(_T('('),true); | |
1807 | if (pos>before.Find(_T(')'),true)) | |
1808 | { | |
1809 | before = before.Left(pos+1); | |
1810 | before << filters[k]; | |
1811 | pos = after.Find(_T(')')); | |
1812 | int pos1 = after.Find(_T('(')); | |
1813 | if (pos != wxNOT_FOUND && (pos<pos1 || pos1==wxNOT_FOUND)) | |
1814 | { | |
1815 | before << after.Mid(pos); | |
1816 | descriptions[k] = before; | |
1817 | } | |
1818 | } | |
1819 | } | |
1820 | } | |
1821 | } | |
1822 | } | |
1823 | #endif | |
1824 | ||
1825 | // autocompletion | |
1826 | for( size_t j = 0 ; j < descriptions.GetCount() ; j++ ) | |
1827 | { | |
489f6cf7 | 1828 | if ( descriptions[j].empty() && !filters[j].empty() ) |
daf32463 WS |
1829 | { |
1830 | descriptions[j].Printf(_("Files (%s)"), filters[j].c_str()); | |
1831 | } | |
1832 | } | |
1833 | ||
9e152a55 WS |
1834 | return filters.GetCount(); |
1835 | } | |
1836 | ||
968956aa | 1837 | #if defined(__WINDOWS__) && !(defined(__UNIX__) || defined(__OS2__)) |
f2346d3f | 1838 | static bool wxCheckWin32Permission(const wxString& path, DWORD access) |
3ff07edb RR |
1839 | { |
1840 | // quoting the MSDN: "To obtain a handle to a directory, call the | |
f2346d3f VZ |
1841 | // CreateFile function with the FILE_FLAG_BACKUP_SEMANTICS flag", but this |
1842 | // doesn't work under Win9x/ME but then it's not needed there anyhow | |
3ff07edb | 1843 | bool isdir = wxDirExists(path); |
f2346d3f | 1844 | if ( isdir && wxGetOsVersion() == wxOS_WINDOWS_9X ) |
3ff07edb | 1845 | { |
f2346d3f VZ |
1846 | // FAT directories always allow all access, even if they have the |
1847 | // readonly flag set | |
3ff07edb RR |
1848 | return true; |
1849 | } | |
3ff07edb | 1850 | |
f2346d3f VZ |
1851 | HANDLE h = ::CreateFile |
1852 | ( | |
52de37c7 | 1853 | path.wx_str(), |
f2346d3f VZ |
1854 | access, |
1855 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | |
1856 | NULL, | |
1857 | OPEN_EXISTING, | |
1858 | isdir ? FILE_FLAG_BACKUP_SEMANTICS : 0, | |
1859 | NULL | |
1860 | ); | |
1861 | if ( h != INVALID_HANDLE_VALUE ) | |
1862 | CloseHandle(h); | |
1863 | ||
1864 | return h != INVALID_HANDLE_VALUE; | |
3ff07edb | 1865 | } |
f2346d3f | 1866 | #endif // __WINDOWS__ |
3ff07edb RR |
1867 | |
1868 | bool wxIsWritable(const wxString &path) | |
1869 | { | |
2203a185 | 1870 | #if defined( __UNIX__ ) || defined(__OS2__) |
3ff07edb | 1871 | // access() will take in count also symbolic links |
0d4ba4ef | 1872 | return wxAccess(path.c_str(), W_OK) == 0; |
3ff07edb | 1873 | #elif defined( __WINDOWS__ ) |
f2346d3f | 1874 | return wxCheckWin32Permission(path, GENERIC_WRITE); |
3ff07edb | 1875 | #else |
a8b6a1b1 | 1876 | wxUnusedVar(path); |
3ff07edb RR |
1877 | // TODO |
1878 | return false; | |
1879 | #endif | |
1880 | } | |
1881 | ||
1882 | bool wxIsReadable(const wxString &path) | |
1883 | { | |
2203a185 | 1884 | #if defined( __UNIX__ ) || defined(__OS2__) |
3ff07edb | 1885 | // access() will take in count also symbolic links |
0d4ba4ef | 1886 | return wxAccess(path.c_str(), R_OK) == 0; |
3ff07edb | 1887 | #elif defined( __WINDOWS__ ) |
f2346d3f | 1888 | return wxCheckWin32Permission(path, GENERIC_READ); |
3ff07edb | 1889 | #else |
a8b6a1b1 | 1890 | wxUnusedVar(path); |
3ff07edb RR |
1891 | // TODO |
1892 | return false; | |
1893 | #endif | |
1894 | } | |
1895 | ||
1896 | bool wxIsExecutable(const wxString &path) | |
1897 | { | |
2203a185 | 1898 | #if defined( __UNIX__ ) || defined(__OS2__) |
3ff07edb | 1899 | // access() will take in count also symbolic links |
0d4ba4ef | 1900 | return wxAccess(path.c_str(), X_OK) == 0; |
3ff07edb | 1901 | #elif defined( __WINDOWS__ ) |
f2346d3f | 1902 | return wxCheckWin32Permission(path, GENERIC_EXECUTE); |
3ff07edb | 1903 | #else |
a8b6a1b1 | 1904 | wxUnusedVar(path); |
3ff07edb RR |
1905 | // TODO |
1906 | return false; | |
1907 | #endif | |
1908 | } | |
1909 | ||
693b31be MW |
1910 | // Return the type of an open file |
1911 | // | |
1912 | // Some file types on some platforms seem seekable but in fact are not. | |
1913 | // The main use of this function is to allow such cases to be detected | |
1914 | // (IsSeekable() is implemented as wxGetFileKind() == wxFILE_KIND_DISK). | |
1915 | // | |
1916 | // This is important for the archive streams, which benefit greatly from | |
1917 | // being able to seek on a stream, but which will produce corrupt archives | |
1918 | // if they unknowingly seek on a non-seekable stream. | |
1919 | // | |
1920 | // wxFILE_KIND_DISK is a good catch all return value, since other values | |
1921 | // disable features of the archive streams. Some other value must be returned | |
1922 | // for a file type that appears seekable but isn't. | |
1923 | // | |
1924 | // Known examples: | |
1925 | // * Pipes on Windows | |
1926 | // * Files on VMS with a record format other than StreamLF | |
1927 | // | |
1928 | wxFileKind wxGetFileKind(int fd) | |
1929 | { | |
1930 | #if defined __WXMSW__ && !defined __WXWINCE__ && defined wxGetOSFHandle | |
1931 | switch (::GetFileType(wxGetOSFHandle(fd)) & ~FILE_TYPE_REMOTE) | |
1932 | { | |
1933 | case FILE_TYPE_CHAR: | |
1934 | return wxFILE_KIND_TERMINAL; | |
1935 | case FILE_TYPE_DISK: | |
1936 | return wxFILE_KIND_DISK; | |
1937 | case FILE_TYPE_PIPE: | |
1938 | return wxFILE_KIND_PIPE; | |
1939 | } | |
1940 | ||
1941 | return wxFILE_KIND_UNKNOWN; | |
1942 | ||
1943 | #elif defined(__UNIX__) | |
1944 | if (isatty(fd)) | |
1945 | return wxFILE_KIND_TERMINAL; | |
1946 | ||
1947 | struct stat st; | |
1948 | fstat(fd, &st); | |
1949 | ||
1950 | if (S_ISFIFO(st.st_mode)) | |
1951 | return wxFILE_KIND_PIPE; | |
1952 | if (!S_ISREG(st.st_mode)) | |
1953 | return wxFILE_KIND_UNKNOWN; | |
1954 | ||
1955 | #if defined(__VMS__) | |
1956 | if (st.st_fab_rfm != FAB$C_STMLF) | |
1957 | return wxFILE_KIND_UNKNOWN; | |
1958 | #endif | |
1959 | ||
1960 | return wxFILE_KIND_DISK; | |
1961 | ||
1962 | #else | |
1963 | #define wxFILEKIND_STUB | |
1964 | (void)fd; | |
1965 | return wxFILE_KIND_DISK; | |
1966 | #endif | |
1967 | } | |
1968 | ||
1969 | wxFileKind wxGetFileKind(FILE *fp) | |
1970 | { | |
1971 | // Note: The watcom rtl dll doesn't have fileno (the static lib does). | |
1972 | // Should be fixed in version 1.4. | |
1973 | #if defined(wxFILEKIND_STUB) || wxONLY_WATCOM_EARLIER_THAN(1,4) | |
1974 | (void)fp; | |
1975 | return wxFILE_KIND_DISK; | |
bade0251 | 1976 | #elif defined(__WINDOWS__) && !defined(__CYGWIN__) && !defined(__WATCOMC__) && !defined(__WINE__) |
693b31be MW |
1977 | return fp ? wxGetFileKind(_fileno(fp)) : wxFILE_KIND_UNKNOWN; |
1978 | #else | |
1979 | return fp ? wxGetFileKind(fileno(fp)) : wxFILE_KIND_UNKNOWN; | |
1980 | #endif | |
1981 | } | |
1982 | ||
9e152a55 | 1983 | |
7f555861 JS |
1984 | //------------------------------------------------------------------------ |
1985 | // wild character routines | |
1986 | //------------------------------------------------------------------------ | |
1987 | ||
1988 | bool wxIsWild( const wxString& pattern ) | |
1989 | { | |
52de37c7 | 1990 | for ( wxString::const_iterator p = pattern.begin(); p != pattern.end(); ++p ) |
2b5f62a0 | 1991 | { |
52de37c7 | 1992 | switch ( (*p).GetValue() ) |
2b5f62a0 | 1993 | { |
52de37c7 VS |
1994 | case wxT('?'): |
1995 | case wxT('*'): | |
1996 | case wxT('['): | |
1997 | case wxT('{'): | |
1998 | return true; | |
1999 | ||
2000 | case wxT('\\'): | |
2001 | if ( ++p == pattern.end() ) | |
2002 | return false; | |
3f4a0c5b | 2003 | } |
7f555861 | 2004 | } |
79e162f5 | 2005 | return false; |
dfcb1ae0 | 2006 | } |
dfcb1ae0 | 2007 | |
2b5f62a0 VZ |
2008 | /* |
2009 | * Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU> | |
2010 | * | |
2011 | * The match procedure is public domain code (from ircII's reg.c) | |
b931e275 | 2012 | * but modified to suit our tastes (RN: No "%" syntax I guess) |
2b5f62a0 | 2013 | */ |
7be1f0d9 | 2014 | |
2b5f62a0 | 2015 | bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) |
7f555861 | 2016 | { |
7448de8d WS |
2017 | if (text.empty()) |
2018 | { | |
2019 | /* Match if both are empty. */ | |
2020 | return pat.empty(); | |
2021 | } | |
2022 | ||
2023 | const wxChar *m = pat.c_str(), | |
2024 | *n = text.c_str(), | |
2025 | *ma = NULL, | |
b931e275 | 2026 | *na = NULL; |
7448de8d | 2027 | int just = 0, |
7448de8d WS |
2028 | acount = 0, |
2029 | count = 0; | |
2030 | ||
2031 | if (dot_special && (*n == wxT('.'))) | |
2032 | { | |
2033 | /* Never match so that hidden Unix files | |
2034 | * are never found. */ | |
2035 | return false; | |
2036 | } | |
2037 | ||
2038 | for (;;) | |
2039 | { | |
2040 | if (*m == wxT('*')) | |
2b5f62a0 | 2041 | { |
7448de8d WS |
2042 | ma = ++m; |
2043 | na = n; | |
2044 | just = 1; | |
7448de8d | 2045 | acount = count; |
2b5f62a0 | 2046 | } |
7448de8d | 2047 | else if (*m == wxT('?')) |
2b5f62a0 | 2048 | { |
7448de8d WS |
2049 | m++; |
2050 | if (!*n++) | |
79e162f5 | 2051 | return false; |
2b5f62a0 | 2052 | } |
7448de8d | 2053 | else |
2b5f62a0 | 2054 | { |
7448de8d WS |
2055 | if (*m == wxT('\\')) |
2056 | { | |
2057 | m++; | |
2058 | /* Quoting "nothing" is a bad thing */ | |
2059 | if (!*m) | |
2060 | return false; | |
2061 | } | |
2062 | if (!*m) | |
2063 | { | |
2064 | /* | |
2065 | * If we are out of both strings or we just | |
2066 | * saw a wildcard, then we can say we have a | |
2067 | * match | |
2068 | */ | |
2069 | if (!*n) | |
2070 | return true; | |
2071 | if (just) | |
2072 | return true; | |
2073 | just = 0; | |
2074 | goto not_matched; | |
2075 | } | |
2076 | /* | |
2077 | * We could check for *n == NULL at this point, but | |
2078 | * since it's more common to have a character there, | |
2079 | * check to see if they match first (m and n) and | |
2080 | * then if they don't match, THEN we can check for | |
2081 | * the NULL of n | |
2082 | */ | |
2083 | just = 0; | |
2084 | if (*m == *n) | |
2085 | { | |
2086 | m++; | |
7448de8d WS |
2087 | count++; |
2088 | n++; | |
2089 | } | |
2090 | else | |
2091 | { | |
2092 | ||
2093 | not_matched: | |
2094 | ||
2095 | /* | |
2096 | * If there are no more characters in the | |
2097 | * string, but we still need to find another | |
2098 | * character (*m != NULL), then it will be | |
2099 | * impossible to match it | |
2100 | */ | |
2101 | if (!*n) | |
2102 | return false; | |
2b5f62a0 | 2103 | |
7448de8d WS |
2104 | if (ma) |
2105 | { | |
2106 | m = ma; | |
2107 | n = ++na; | |
2108 | count = acount; | |
3f4a0c5b | 2109 | } |
7448de8d WS |
2110 | else |
2111 | return false; | |
2112 | } | |
3f4a0c5b | 2113 | } |
7448de8d | 2114 | } |
2b5f62a0 | 2115 | } |
fd3f686c | 2116 | |
3f4a0c5b | 2117 | #ifdef __VISUALC__ |
fd3f686c | 2118 | #pragma warning(default:4706) // assignment within conditional expression |
53c6e7cc | 2119 | #endif // VC++ |