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