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