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