]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
21 | #pragma implementation "filefn.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | #include "wx/defs.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
32 | #include "wx/utils.h" | |
33 | #include "wx/intl.h" | |
34 | #include "wx/file.h" | |
35 | #include "wx/filename.h" | |
36 | #include "wx/dir.h" | |
37 | ||
38 | // there are just too many of those... | |
39 | #ifdef __VISUALC__ | |
40 | #pragma warning(disable:4706) // assignment within conditional expression | |
41 | #endif // VC++ | |
42 | ||
43 | #include <ctype.h> | |
44 | #include <stdio.h> | |
45 | #include <stdlib.h> | |
46 | #include <string.h> | |
47 | #if !defined(__WATCOMC__) | |
48 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) | |
49 | #include <errno.h> | |
50 | #endif | |
51 | #endif | |
52 | ||
53 | #if defined(__WXMAC__) | |
54 | #include "wx/mac/private.h" // includes mac headers | |
55 | #endif | |
56 | ||
57 | #ifdef __WXWINCE__ | |
58 | #include "wx/msw/wince/time.h" | |
59 | #include "wx/msw/private.h" | |
60 | #else | |
61 | #include <time.h> | |
62 | #endif | |
63 | ||
64 | #ifdef __WXWINCE__ | |
65 | // Nothing | |
66 | #elif !defined(__MWERKS__) | |
67 | #include <sys/types.h> | |
68 | #include <sys/stat.h> | |
69 | #else | |
70 | #ifdef __MACH__ | |
71 | #include <sys/types.h> | |
72 | #include <utime.h> | |
73 | #include <sys/stat.h> | |
74 | #include <unistd.h> | |
75 | #else | |
76 | #include <stat.h> | |
77 | #include <unistd.h> | |
78 | #include <unix.h> | |
79 | #include <fcntl.h> | |
80 | #endif | |
81 | #endif | |
82 | ||
83 | #ifdef __OS2__ | |
84 | // need to check for __OS2__ first since currently both | |
85 | // __OS2__ and __UNIX__ are defined. | |
86 | #include <process.h> | |
87 | #include "wx/os2/private.h" | |
88 | #ifdef __EMX__ | |
89 | #include <unistd.h> | |
90 | #endif | |
91 | #elif defined(__UNIX__) | |
92 | #include <unistd.h> | |
93 | #include <dirent.h> | |
94 | #include <fcntl.h> | |
95 | #endif | |
96 | ||
97 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
98 | #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__) && !defined(__WXWINCE__) | |
99 | #include <direct.h> | |
100 | #include <dos.h> | |
101 | #include <io.h> | |
102 | #endif // __WINDOWS__ | |
103 | #endif // native Win compiler | |
104 | ||
105 | #if defined(__DOS__) | |
106 | #ifdef __WATCOMC__ | |
107 | #include <direct.h> | |
108 | #include <dos.h> | |
109 | #include <io.h> | |
110 | #endif | |
111 | #ifdef __DJGPP__ | |
112 | #include <unistd.h> | |
113 | #endif | |
114 | #endif | |
115 | ||
116 | #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs | |
117 | // this (3.1 I believe) and how to test for it. | |
118 | // If this works for Borland 4.0 as well, then no worries. | |
119 | #include <dir.h> | |
120 | #endif | |
121 | ||
122 | #ifdef __SALFORDC__ | |
123 | #include <dir.h> | |
124 | #include <unix.h> | |
125 | #endif | |
126 | ||
127 | #include "wx/log.h" | |
128 | ||
129 | // No, Cygwin doesn't appear to have fnmatch.h after all. | |
130 | #if defined(HAVE_FNMATCH_H) | |
131 | #include "fnmatch.h" | |
132 | #endif | |
133 | ||
134 | #ifdef __WINDOWS__ | |
135 | #include "wx/msw/wrapwin.h" | |
136 | #include "wx/msw/mslu.h" | |
137 | ||
138 | // for _getcwd | |
139 | #ifdef __MINGW32__ | |
140 | #include <io.h> | |
141 | #endif | |
142 | ||
143 | // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path() | |
144 | // | |
145 | // note that it must be included after <windows.h> | |
146 | #ifdef __GNUWIN32__ | |
147 | #ifdef __CYGWIN__ | |
148 | #include <sys/cygwin.h> | |
149 | #endif | |
150 | #endif // __GNUWIN32__ | |
151 | #endif // __WINDOWS__ | |
152 | ||
153 | // TODO: Borland probably has _wgetcwd as well? | |
154 | #ifdef _MSC_VER | |
155 | #define HAVE_WGETCWD | |
156 | #endif | |
157 | ||
158 | // ---------------------------------------------------------------------------- | |
159 | // constants | |
160 | // ---------------------------------------------------------------------------- | |
161 | ||
162 | #ifndef _MAXPATHLEN | |
163 | #define _MAXPATHLEN 1024 | |
164 | #endif | |
165 | ||
166 | #ifdef __WXMAC__ | |
167 | # ifdef __DARWIN__ | |
168 | # include "MoreFilesX.h" | |
169 | # else | |
170 | # include "MoreFiles.h" | |
171 | # include "MoreFilesExtras.h" | |
172 | # include "FullPath.h" | |
173 | # include "FSpCompat.h" | |
174 | # endif | |
175 | #endif | |
176 | ||
177 | // ---------------------------------------------------------------------------- | |
178 | // private globals | |
179 | // ---------------------------------------------------------------------------- | |
180 | ||
181 | // MT-FIXME: get rid of this horror and all code using it | |
182 | static wxChar wxFileFunctionsBuffer[4*_MAXPATHLEN]; | |
183 | ||
184 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 | |
185 | // | |
186 | // VisualAge C++ V4.0 cannot have any external linkage const decs | |
187 | // in headers included by more than one primary source | |
188 | // | |
189 | const off_t wxInvalidOffset = (off_t)-1; | |
190 | #endif | |
191 | ||
192 | // ---------------------------------------------------------------------------- | |
193 | // macros | |
194 | // ---------------------------------------------------------------------------- | |
195 | ||
196 | // we need to translate Mac filenames before passing them to OS functions | |
197 | #define OS_FILENAME(s) (s.fn_str()) | |
198 | ||
199 | // ============================================================================ | |
200 | // implementation | |
201 | // ============================================================================ | |
202 | ||
203 | #ifdef wxNEED_WX_UNISTD_H | |
204 | ||
205 | WXDLLEXPORT int wxStat( const wxChar *file_name, wxStructStat *buf ) | |
206 | { | |
207 | return stat( wxConvFile.cWX2MB( file_name ), buf ); | |
208 | } | |
209 | ||
210 | WXDLLEXPORT int wxAccess( const wxChar *pathname, int mode ) | |
211 | { | |
212 | return access( wxConvFile.cWX2MB( pathname ), mode ); | |
213 | } | |
214 | ||
215 | WXDLLEXPORT int wxOpen( const wxChar *pathname, int flags, mode_t mode ) | |
216 | { | |
217 | return open( wxConvFile.cWX2MB( pathname ), flags, mode ); | |
218 | } | |
219 | ||
220 | #endif | |
221 | // wxNEED_WX_UNISTD_H | |
222 | ||
223 | // ---------------------------------------------------------------------------- | |
224 | // wxPathList | |
225 | // ---------------------------------------------------------------------------- | |
226 | ||
227 | // IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList) | |
228 | ||
229 | static inline wxChar* MYcopystring(const wxString& s) | |
230 | { | |
231 | wxChar* copy = new wxChar[s.length() + 1]; | |
232 | return wxStrcpy(copy, s.c_str()); | |
233 | } | |
234 | ||
235 | static inline wxChar* MYcopystring(const wxChar* s) | |
236 | { | |
237 | wxChar* copy = new wxChar[wxStrlen(s) + 1]; | |
238 | return wxStrcpy(copy, s); | |
239 | } | |
240 | ||
241 | void wxPathList::Add (const wxString& path) | |
242 | { | |
243 | wxStringList::Add (WXSTRINGCAST path); | |
244 | } | |
245 | ||
246 | // Add paths e.g. from the PATH environment variable | |
247 | void wxPathList::AddEnvList (const wxString& envVariable) | |
248 | { | |
249 | // No environment variables on WinCE | |
250 | #ifndef __WXWINCE__ | |
251 | static const wxChar PATH_TOKS[] = | |
252 | #if defined(__WINDOWS__) || defined(__OS2__) | |
253 | /* | |
254 | The space has been removed from the tokenizers, otherwise a | |
255 | path such as "C:\Program Files" would be split into 2 paths: | |
256 | "C:\Program" and "Files" | |
257 | */ | |
258 | // wxT(" ;"); // Don't seperate with colon in DOS (used for drive) | |
259 | wxT(";"); // Don't seperate with colon in DOS (used for drive) | |
260 | #else | |
261 | wxT(" :;"); | |
262 | #endif | |
263 | ||
264 | wxChar *val = wxGetenv (WXSTRINGCAST envVariable); | |
265 | if (val && *val) | |
266 | { | |
267 | wxChar *s = MYcopystring (val); | |
268 | wxChar *save_ptr, *token = wxStrtok (s, PATH_TOKS, &save_ptr); | |
269 | ||
270 | if (token) | |
271 | { | |
272 | Add(token); | |
273 | while (token) | |
274 | { | |
275 | if ( (token = wxStrtok ((wxChar *) NULL, PATH_TOKS, &save_ptr)) | |
276 | != NULL ) | |
277 | { | |
278 | Add(token); | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | // suppress warning about unused variable save_ptr when wxStrtok() is a | |
284 | // macro which throws away its third argument | |
285 | save_ptr = token; | |
286 | ||
287 | delete [] s; | |
288 | } | |
289 | #endif | |
290 | } | |
291 | ||
292 | // Given a full filename (with path), ensure that that file can | |
293 | // be accessed again USING FILENAME ONLY by adding the path | |
294 | // to the list if not already there. | |
295 | void wxPathList::EnsureFileAccessible (const wxString& path) | |
296 | { | |
297 | wxString path_only(wxPathOnly(path)); | |
298 | if ( !path_only.IsEmpty() ) | |
299 | { | |
300 | if ( !Member(path_only) ) | |
301 | Add(path_only); | |
302 | } | |
303 | } | |
304 | ||
305 | bool wxPathList::Member (const wxString& path) | |
306 | { | |
307 | for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext()) | |
308 | { | |
309 | wxString path2( node->GetData() ); | |
310 | if ( | |
311 | #if defined(__WINDOWS__) || defined(__OS2__) || defined(__VMS__) || defined (__WXMAC__) | |
312 | // Case INDEPENDENT | |
313 | path.CompareTo (path2, wxString::ignoreCase) == 0 | |
314 | #else | |
315 | // Case sensitive File System | |
316 | path.CompareTo (path2) == 0 | |
317 | #endif | |
318 | ) | |
319 | return TRUE; | |
320 | } | |
321 | return FALSE; | |
322 | } | |
323 | ||
324 | wxString wxPathList::FindValidPath (const wxString& file) | |
325 | { | |
326 | if (wxFileExists (wxExpandPath(wxFileFunctionsBuffer, file))) | |
327 | return wxString(wxFileFunctionsBuffer); | |
328 | ||
329 | wxChar buf[_MAXPATHLEN]; | |
330 | wxStrcpy(buf, wxFileFunctionsBuffer); | |
331 | ||
332 | wxChar *filename = wxIsAbsolutePath (buf) ? wxFileNameFromPath (buf) : (wxChar *)buf; | |
333 | ||
334 | for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext()) | |
335 | { | |
336 | const wxChar *path = node->GetData(); | |
337 | wxStrcpy (wxFileFunctionsBuffer, path); | |
338 | wxChar ch = wxFileFunctionsBuffer[wxStrlen(wxFileFunctionsBuffer)-1]; | |
339 | if (ch != wxT('\\') && ch != wxT('/')) | |
340 | wxStrcat (wxFileFunctionsBuffer, wxT("/")); | |
341 | wxStrcat (wxFileFunctionsBuffer, filename); | |
342 | #ifdef __WINDOWS__ | |
343 | wxUnix2DosFilename (wxFileFunctionsBuffer); | |
344 | #endif | |
345 | if (wxFileExists (wxFileFunctionsBuffer)) | |
346 | { | |
347 | return wxString(wxFileFunctionsBuffer); // Found! | |
348 | } | |
349 | } // for() | |
350 | ||
351 | return wxEmptyString; // Not found | |
352 | } | |
353 | ||
354 | wxString wxPathList::FindAbsoluteValidPath (const wxString& file) | |
355 | { | |
356 | wxString f = FindValidPath(file); | |
357 | if ( wxIsAbsolutePath(f) ) | |
358 | return f; | |
359 | ||
360 | wxString buf; | |
361 | wxGetWorkingDirectory(wxStringBuffer(buf, _MAXPATHLEN), _MAXPATHLEN); | |
362 | ||
363 | if ( !wxEndsWithPathSeparator(buf) ) | |
364 | { | |
365 | buf += wxFILE_SEP_PATH; | |
366 | } | |
367 | buf += f; | |
368 | ||
369 | return buf; | |
370 | } | |
371 | ||
372 | bool | |
373 | wxFileExists (const wxString& filename) | |
374 | { | |
375 | // we must use GetFileAttributes() instead of the ANSI C functions because | |
376 | // it can cope with network (UNC) paths unlike them | |
377 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
378 | DWORD ret = ::GetFileAttributes(filename); | |
379 | ||
380 | return (ret != (DWORD)-1) && !(ret & FILE_ATTRIBUTE_DIRECTORY); | |
381 | #else // !__WIN32__ | |
382 | wxStructStat st; | |
383 | return wxStat(filename, &st) == 0 && (st.st_mode & S_IFREG); | |
384 | #endif // __WIN32__/!__WIN32__ | |
385 | } | |
386 | ||
387 | bool | |
388 | wxIsAbsolutePath (const wxString& filename) | |
389 | { | |
390 | if (filename != wxT("")) | |
391 | { | |
392 | #if defined(__WXMAC__) && !defined(__DARWIN__) | |
393 | // Classic or Carbon CodeWarrior like | |
394 | // Carbon with Apple DevTools is Unix like | |
395 | ||
396 | // This seems wrong to me, but there is no fix. since | |
397 | // "MacOS:MyText.txt" is absolute whereas "MyDir:MyText.txt" | |
398 | // is not. Or maybe ":MyDir:MyText.txt" has to be used? RR. | |
399 | if (filename.Find(':') != wxNOT_FOUND && filename[0] != ':') | |
400 | return TRUE ; | |
401 | #else | |
402 | // Unix like or Windows | |
403 | if (filename[0] == wxT('/')) | |
404 | return TRUE; | |
405 | #endif | |
406 | #ifdef __VMS__ | |
407 | if ((filename[0] == wxT('[') && filename[1] != wxT('.'))) | |
408 | return TRUE; | |
409 | #endif | |
410 | #if defined(__WINDOWS__) || defined(__OS2__) | |
411 | // MSDOS like | |
412 | if (filename[0] == wxT('\\') || (wxIsalpha (filename[0]) && filename[1] == wxT(':'))) | |
413 | return TRUE; | |
414 | #endif | |
415 | } | |
416 | return FALSE ; | |
417 | } | |
418 | ||
419 | /* | |
420 | * Strip off any extension (dot something) from end of file, | |
421 | * IF one exists. Inserts zero into buffer. | |
422 | * | |
423 | */ | |
424 | ||
425 | void wxStripExtension(wxChar *buffer) | |
426 | { | |
427 | int len = wxStrlen(buffer); | |
428 | int i = len-1; | |
429 | while (i > 0) | |
430 | { | |
431 | if (buffer[i] == wxT('.')) | |
432 | { | |
433 | buffer[i] = 0; | |
434 | break; | |
435 | } | |
436 | i --; | |
437 | } | |
438 | } | |
439 | ||
440 | void wxStripExtension(wxString& buffer) | |
441 | { | |
442 | size_t len = buffer.Length(); | |
443 | size_t i = len-1; | |
444 | while (i > 0) | |
445 | { | |
446 | if (buffer.GetChar(i) == wxT('.')) | |
447 | { | |
448 | buffer = buffer.Left(i); | |
449 | break; | |
450 | } | |
451 | i --; | |
452 | } | |
453 | } | |
454 | ||
455 | // Destructive removal of /./ and /../ stuff | |
456 | wxChar *wxRealPath (wxChar *path) | |
457 | { | |
458 | #ifdef __WXMSW__ | |
459 | static const wxChar SEP = wxT('\\'); | |
460 | wxUnix2DosFilename(path); | |
461 | #else | |
462 | static const wxChar SEP = wxT('/'); | |
463 | #endif | |
464 | if (path[0] && path[1]) { | |
465 | /* MATTHEW: special case "/./x" */ | |
466 | wxChar *p; | |
467 | if (path[2] == SEP && path[1] == wxT('.')) | |
468 | p = &path[0]; | |
469 | else | |
470 | p = &path[2]; | |
471 | for (; *p; p++) | |
472 | { | |
473 | if (*p == SEP) | |
474 | { | |
475 | if (p[1] == wxT('.') && p[2] == wxT('.') && (p[3] == SEP || p[3] == wxT('\0'))) | |
476 | { | |
477 | wxChar *q; | |
478 | for (q = p - 1; q >= path && *q != SEP; q--) | |
479 | { | |
480 | // Empty | |
481 | } | |
482 | ||
483 | if (q[0] == SEP && (q[1] != wxT('.') || q[2] != wxT('.') || q[3] != SEP) | |
484 | && (q - 1 <= path || q[-1] != SEP)) | |
485 | { | |
486 | wxStrcpy (q, p + 3); | |
487 | if (path[0] == wxT('\0')) | |
488 | { | |
489 | path[0] = SEP; | |
490 | path[1] = wxT('\0'); | |
491 | } | |
492 | #if defined(__WXMSW__) || defined(__OS2__) | |
493 | /* Check that path[2] is NULL! */ | |
494 | else if (path[1] == wxT(':') && !path[2]) | |
495 | { | |
496 | path[2] = SEP; | |
497 | path[3] = wxT('\0'); | |
498 | } | |
499 | #endif | |
500 | p = q - 1; | |
501 | } | |
502 | } | |
503 | else if (p[1] == wxT('.') && (p[2] == SEP || p[2] == wxT('\0'))) | |
504 | wxStrcpy (p, p + 2); | |
505 | } | |
506 | } | |
507 | } | |
508 | return path; | |
509 | } | |
510 | ||
511 | // Must be destroyed | |
512 | wxChar *wxCopyAbsolutePath(const wxString& filename) | |
513 | { | |
514 | if (filename == wxT("")) | |
515 | return (wxChar *) NULL; | |
516 | ||
517 | if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer, filename))) { | |
518 | wxChar buf[_MAXPATHLEN]; | |
519 | buf[0] = wxT('\0'); | |
520 | wxGetWorkingDirectory(buf, WXSIZEOF(buf)); | |
521 | wxChar ch = buf[wxStrlen(buf) - 1]; | |
522 | #ifdef __WXMSW__ | |
523 | if (ch != wxT('\\') && ch != wxT('/')) | |
524 | wxStrcat(buf, wxT("\\")); | |
525 | #else | |
526 | if (ch != wxT('/')) | |
527 | wxStrcat(buf, wxT("/")); | |
528 | #endif | |
529 | wxStrcat(buf, wxFileFunctionsBuffer); | |
530 | return MYcopystring( wxRealPath(buf) ); | |
531 | } | |
532 | return MYcopystring( wxFileFunctionsBuffer ); | |
533 | } | |
534 | ||
535 | /*- | |
536 | Handles: | |
537 | ~/ => home dir | |
538 | ~user/ => user's home dir | |
539 | If the environment variable a = "foo" and b = "bar" then: | |
540 | Unix: | |
541 | $a => foo | |
542 | $a$b => foobar | |
543 | $a.c => foo.c | |
544 | xxx$a => xxxfoo | |
545 | ${a}! => foo! | |
546 | $(b)! => bar! | |
547 | \$a => \$a | |
548 | MSDOS: | |
549 | $a ==> $a | |
550 | $(a) ==> foo | |
551 | $(a)$b ==> foo$b | |
552 | $(a)$(b)==> foobar | |
553 | test.$$ ==> test.$$ | |
554 | */ | |
555 | ||
556 | /* input name in name, pathname output to buf. */ | |
557 | ||
558 | wxChar *wxExpandPath(wxChar *buf, const wxChar *name) | |
559 | { | |
560 | register wxChar *d, *s, *nm; | |
561 | wxChar lnm[_MAXPATHLEN]; | |
562 | int q; | |
563 | ||
564 | // Some compilers don't like this line. | |
565 | // const wxChar trimchars[] = wxT("\n \t"); | |
566 | ||
567 | wxChar trimchars[4]; | |
568 | trimchars[0] = wxT('\n'); | |
569 | trimchars[1] = wxT(' '); | |
570 | trimchars[2] = wxT('\t'); | |
571 | trimchars[3] = 0; | |
572 | ||
573 | #ifdef __WXMSW__ | |
574 | const wxChar SEP = wxT('\\'); | |
575 | #else | |
576 | const wxChar SEP = wxT('/'); | |
577 | #endif | |
578 | buf[0] = wxT('\0'); | |
579 | if (name == NULL || *name == wxT('\0')) | |
580 | return buf; | |
581 | nm = MYcopystring(name); // Make a scratch copy | |
582 | wxChar *nm_tmp = nm; | |
583 | ||
584 | /* Skip leading whitespace and cr */ | |
585 | while (wxStrchr((wxChar *)trimchars, *nm) != NULL) | |
586 | nm++; | |
587 | /* And strip off trailing whitespace and cr */ | |
588 | s = nm + (q = wxStrlen(nm)) - 1; | |
589 | while (q-- && wxStrchr((wxChar *)trimchars, *s) != NULL) | |
590 | *s = wxT('\0'); | |
591 | ||
592 | s = nm; | |
593 | d = lnm; | |
594 | #ifdef __WXMSW__ | |
595 | q = FALSE; | |
596 | #else | |
597 | q = nm[0] == wxT('\\') && nm[1] == wxT('~'); | |
598 | #endif | |
599 | ||
600 | /* Expand inline environment variables */ | |
601 | #ifdef __VISAGECPP__ | |
602 | while (*d) | |
603 | { | |
604 | *d++ = *s; | |
605 | if(*s == wxT('\\')) | |
606 | { | |
607 | *(d - 1) = *++s; | |
608 | if (*d) | |
609 | { | |
610 | s++; | |
611 | continue; | |
612 | } | |
613 | else | |
614 | break; | |
615 | } | |
616 | else | |
617 | #else | |
618 | while ((*d++ = *s) != 0) { | |
619 | # ifndef __WXMSW__ | |
620 | if (*s == wxT('\\')) { | |
621 | if ((*(d - 1) = *++s)) { | |
622 | s++; | |
623 | continue; | |
624 | } else | |
625 | break; | |
626 | } else | |
627 | # endif | |
628 | #endif | |
629 | // No env variables on WinCE | |
630 | #ifndef __WXWINCE__ | |
631 | #ifdef __WXMSW__ | |
632 | if (*s++ == wxT('$') && (*s == wxT('{') || *s == wxT(')'))) | |
633 | #else | |
634 | if (*s++ == wxT('$')) | |
635 | #endif | |
636 | { | |
637 | register wxChar *start = d; | |
638 | register int braces = (*s == wxT('{') || *s == wxT('(')); | |
639 | register wxChar *value; | |
640 | while ((*d++ = *s) != 0) | |
641 | if (braces ? (*s == wxT('}') || *s == wxT(')')) : !(wxIsalnum(*s) || *s == wxT('_')) ) | |
642 | break; | |
643 | else | |
644 | s++; | |
645 | *--d = 0; | |
646 | value = wxGetenv(braces ? start + 1 : start); | |
647 | if (value) { | |
648 | for ((d = start - 1); (*d++ = *value++) != 0;) | |
649 | { | |
650 | // Empty | |
651 | } | |
652 | ||
653 | d--; | |
654 | if (braces && *s) | |
655 | s++; | |
656 | } | |
657 | } | |
658 | #endif | |
659 | // __WXWINCE__ | |
660 | } | |
661 | ||
662 | /* Expand ~ and ~user */ | |
663 | nm = lnm; | |
664 | if (nm[0] == wxT('~') && !q) | |
665 | { | |
666 | /* prefix ~ */ | |
667 | if (nm[1] == SEP || nm[1] == 0) | |
668 | { /* ~/filename */ | |
669 | // FIXME: wxGetUserHome could return temporary storage in Unicode mode | |
670 | if ((s = WXSTRINGCAST wxGetUserHome(wxT(""))) != NULL) { | |
671 | if (*++nm) | |
672 | nm++; | |
673 | } | |
674 | } else | |
675 | { /* ~user/filename */ | |
676 | register wxChar *nnm; | |
677 | register wxChar *home; | |
678 | for (s = nm; *s && *s != SEP; s++) | |
679 | { | |
680 | // Empty | |
681 | } | |
682 | int was_sep; /* MATTHEW: Was there a separator, or NULL? */ | |
683 | was_sep = (*s == SEP); | |
684 | nnm = *s ? s + 1 : s; | |
685 | *s = 0; | |
686 | // FIXME: wxGetUserHome could return temporary storage in Unicode mode | |
687 | if ((home = WXSTRINGCAST wxGetUserHome(wxString(nm + 1))) == NULL) { | |
688 | if (was_sep) /* replace only if it was there: */ | |
689 | *s = SEP; | |
690 | s = NULL; | |
691 | } else { | |
692 | nm = nnm; | |
693 | s = home; | |
694 | } | |
695 | } | |
696 | } | |
697 | ||
698 | d = buf; | |
699 | if (s && *s) { /* MATTHEW: s could be NULL if user '~' didn't exist */ | |
700 | /* Copy home dir */ | |
701 | while (wxT('\0') != (*d++ = *s++)) | |
702 | /* loop */; | |
703 | // Handle root home | |
704 | if (d - 1 > buf && *(d - 2) != SEP) | |
705 | *(d - 1) = SEP; | |
706 | } | |
707 | s = nm; | |
708 | while ((*d++ = *s++) != 0) | |
709 | { | |
710 | // Empty | |
711 | } | |
712 | delete[] nm_tmp; // clean up alloc | |
713 | /* Now clean up the buffer */ | |
714 | return wxRealPath(buf); | |
715 | } | |
716 | ||
717 | /* Contract Paths to be build upon an environment variable | |
718 | component: | |
719 | ||
720 | example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib | |
721 | ||
722 | The call wxExpandPath can convert these back! | |
723 | */ | |
724 | wxChar * | |
725 | wxContractPath (const wxString& filename, const wxString& envname, const wxString& user) | |
726 | { | |
727 | static wxChar dest[_MAXPATHLEN]; | |
728 | ||
729 | if (filename == wxT("")) | |
730 | return (wxChar *) NULL; | |
731 | ||
732 | wxStrcpy (dest, WXSTRINGCAST filename); | |
733 | #ifdef __WXMSW__ | |
734 | wxUnix2DosFilename(dest); | |
735 | #endif | |
736 | ||
737 | // Handle environment | |
738 | const wxChar *val; | |
739 | #ifndef __WXWINCE__ | |
740 | wxChar *tcp; | |
741 | if (envname != WXSTRINGCAST NULL && (val = wxGetenv (WXSTRINGCAST envname)) != NULL && | |
742 | (tcp = wxStrstr (dest, val)) != NULL) | |
743 | { | |
744 | wxStrcpy (wxFileFunctionsBuffer, tcp + wxStrlen (val)); | |
745 | *tcp++ = wxT('$'); | |
746 | *tcp++ = wxT('{'); | |
747 | wxStrcpy (tcp, WXSTRINGCAST envname); | |
748 | wxStrcat (tcp, wxT("}")); | |
749 | wxStrcat (tcp, wxFileFunctionsBuffer); | |
750 | } | |
751 | #endif | |
752 | ||
753 | // Handle User's home (ignore root homes!) | |
754 | size_t len; | |
755 | if ((val = wxGetUserHome (user)) != NULL && | |
756 | (len = wxStrlen(val)) > 2 && | |
757 | wxStrncmp(dest, val, len) == 0) | |
758 | { | |
759 | wxStrcpy(wxFileFunctionsBuffer, wxT("~")); | |
760 | if (user != wxT("")) | |
761 | wxStrcat(wxFileFunctionsBuffer, (const wxChar*) user); | |
762 | wxStrcat(wxFileFunctionsBuffer, dest + len); | |
763 | wxStrcpy (dest, wxFileFunctionsBuffer); | |
764 | } | |
765 | ||
766 | return dest; | |
767 | } | |
768 | ||
769 | // Return just the filename, not the path (basename) | |
770 | wxChar *wxFileNameFromPath (wxChar *path) | |
771 | { | |
772 | wxString p = path; | |
773 | wxString n = wxFileNameFromPath(p); | |
774 | ||
775 | return path + p.length() - n.length(); | |
776 | } | |
777 | ||
778 | wxString wxFileNameFromPath (const wxString& path) | |
779 | { | |
780 | wxString name, ext; | |
781 | wxFileName::SplitPath(path, NULL, &name, &ext); | |
782 | ||
783 | wxString fullname = name; | |
784 | if ( !ext.empty() ) | |
785 | { | |
786 | fullname << wxFILE_SEP_EXT << ext; | |
787 | } | |
788 | ||
789 | return fullname; | |
790 | } | |
791 | ||
792 | // Return just the directory, or NULL if no directory | |
793 | wxChar * | |
794 | wxPathOnly (wxChar *path) | |
795 | { | |
796 | if (path && *path) | |
797 | { | |
798 | static wxChar buf[_MAXPATHLEN]; | |
799 | ||
800 | // Local copy | |
801 | wxStrcpy (buf, path); | |
802 | ||
803 | int l = wxStrlen(path); | |
804 | int i = l - 1; | |
805 | ||
806 | // Search backward for a backward or forward slash | |
807 | while (i > -1) | |
808 | { | |
809 | #if defined(__WXMAC__) && !defined(__DARWIN__) | |
810 | // Classic or Carbon CodeWarrior like | |
811 | // Carbon with Apple DevTools is Unix like | |
812 | if (path[i] == wxT(':') ) | |
813 | { | |
814 | buf[i] = 0; | |
815 | return buf; | |
816 | } | |
817 | #else | |
818 | // Unix like or Windows | |
819 | if (path[i] == wxT('/') || path[i] == wxT('\\')) | |
820 | { | |
821 | buf[i] = 0; | |
822 | return buf; | |
823 | } | |
824 | #endif | |
825 | #ifdef __VMS__ | |
826 | if (path[i] == wxT(']')) | |
827 | { | |
828 | buf[i+1] = 0; | |
829 | return buf; | |
830 | } | |
831 | #endif | |
832 | i --; | |
833 | } | |
834 | ||
835 | #if defined(__WXMSW__) || defined(__OS2__) | |
836 | // Try Drive specifier | |
837 | if (wxIsalpha (buf[0]) && buf[1] == wxT(':')) | |
838 | { | |
839 | // A:junk --> A:. (since A:.\junk Not A:\junk) | |
840 | buf[2] = wxT('.'); | |
841 | buf[3] = wxT('\0'); | |
842 | return buf; | |
843 | } | |
844 | #endif | |
845 | } | |
846 | return (wxChar *) NULL; | |
847 | } | |
848 | ||
849 | // Return just the directory, or NULL if no directory | |
850 | wxString wxPathOnly (const wxString& path) | |
851 | { | |
852 | if (path != wxT("")) | |
853 | { | |
854 | wxChar buf[_MAXPATHLEN]; | |
855 | ||
856 | // Local copy | |
857 | wxStrcpy (buf, WXSTRINGCAST path); | |
858 | ||
859 | int l = path.Length(); | |
860 | int i = l - 1; | |
861 | ||
862 | // Search backward for a backward or forward slash | |
863 | while (i > -1) | |
864 | { | |
865 | #if defined(__WXMAC__) && !defined(__DARWIN__) | |
866 | // Classic or Carbon CodeWarrior like | |
867 | // Carbon with Apple DevTools is Unix like | |
868 | if (path[i] == wxT(':') ) | |
869 | { | |
870 | buf[i] = 0; | |
871 | return wxString(buf); | |
872 | } | |
873 | #else | |
874 | // Unix like or Windows | |
875 | if (path[i] == wxT('/') || path[i] == wxT('\\')) | |
876 | { | |
877 | buf[i] = 0; | |
878 | return wxString(buf); | |
879 | } | |
880 | #endif | |
881 | #ifdef __VMS__ | |
882 | if (path[i] == wxT(']')) | |
883 | { | |
884 | buf[i+1] = 0; | |
885 | return wxString(buf); | |
886 | } | |
887 | #endif | |
888 | i --; | |
889 | } | |
890 | ||
891 | #if defined(__WXMSW__) || defined(__OS2__) | |
892 | // Try Drive specifier | |
893 | if (wxIsalpha (buf[0]) && buf[1] == wxT(':')) | |
894 | { | |
895 | // A:junk --> A:. (since A:.\junk Not A:\junk) | |
896 | buf[2] = wxT('.'); | |
897 | buf[3] = wxT('\0'); | |
898 | return wxString(buf); | |
899 | } | |
900 | #endif | |
901 | } | |
902 | return wxString(wxT("")); | |
903 | } | |
904 | ||
905 | // Utility for converting delimiters in DOS filenames to UNIX style | |
906 | // and back again - or we get nasty problems with delimiters. | |
907 | // Also, convert to lower case, since case is significant in UNIX. | |
908 | ||
909 | #if defined(__WXMAC__) | |
910 | wxString wxMacFSSpec2MacFilename( const FSSpec *spec ) | |
911 | { | |
912 | #ifdef __DARWIN__ | |
913 | int i; | |
914 | int j; | |
915 | OSErr theErr; | |
916 | OSStatus theStatus = noErr; | |
917 | Boolean isDirectory = FALSE; | |
918 | Str255 theParentPath = "\p"; | |
919 | FSSpec theParentSpec; | |
920 | FSRef theParentRef; | |
921 | char theFileName[FILENAME_MAX]; | |
922 | char thePath[FILENAME_MAX]; | |
923 | ||
924 | strcpy(thePath, ""); | |
925 | ||
926 | // GD: Separate file name from path and make a FSRef to the parent | |
927 | // directory. This is necessary since FSRefs cannot reference files | |
928 | // that have not yet been created. | |
929 | // Based on example code from Apple Technical Note TN2022 | |
930 | // http://developer.apple.com/technotes/tn/tn2022.html | |
931 | ||
932 | // check whether we are converting a directory | |
933 | isDirectory = ((spec->name)[spec->name[0]] == ':'); | |
934 | // count length of file name | |
935 | for (i = spec->name[0] - (isDirectory ? 1 : 0); ((spec->name[i] != ':') && (i > 0)); i--); | |
936 | // copy file name | |
937 | // prepend path separator since it will later be appended to the path | |
938 | theFileName[0] = wxFILE_SEP_PATH; | |
939 | for (j = i + 1; j <= spec->name[0] - (isDirectory ? 1 : 0); j++) { | |
940 | theFileName[j - i] = spec->name[j]; | |
941 | } | |
942 | theFileName[j - i] = '\0'; | |
943 | // copy path if any | |
944 | for (j = 1; j <= i; j++) { | |
945 | theParentPath[++theParentPath[0]] = spec->name[j]; | |
946 | } | |
947 | theErr = FSMakeFSSpec(spec->vRefNum, spec->parID, theParentPath, &theParentSpec); | |
948 | if (theErr == noErr) { | |
949 | // convert the FSSpec to an FSRef | |
950 | theErr = FSpMakeFSRef(&theParentSpec, &theParentRef); | |
951 | } | |
952 | if (theErr == noErr) { | |
953 | // get the POSIX path associated with the FSRef | |
954 | theStatus = FSRefMakePath(&theParentRef, | |
955 | (UInt8 *)thePath, sizeof(thePath)); | |
956 | } | |
957 | if (theStatus == noErr) { | |
958 | // append file name to path | |
959 | // includes previously prepended path separator | |
960 | strcat(thePath, theFileName); | |
961 | } | |
962 | ||
963 | // create path string for return value | |
964 | wxString result( thePath , wxConvLocal) ; | |
965 | #else | |
966 | Handle myPath ; | |
967 | short length ; | |
968 | ||
969 | // get length of path and allocate handle | |
970 | FSpGetFullPath( spec , &length , &myPath ) ; | |
971 | ::SetHandleSize( myPath , length + 1 ) ; | |
972 | ::HLock( myPath ) ; | |
973 | (*myPath)[length] = 0 ; | |
974 | if ((length > 0) && ((*myPath)[length-1] == ':')) | |
975 | (*myPath)[length-1] = 0 ; | |
976 | ||
977 | // create path string for return value | |
978 | wxString result( *myPath , wxConvLocal) ; | |
979 | ||
980 | // free allocated handle | |
981 | ::HUnlock( myPath ) ; | |
982 | ::DisposeHandle( myPath ) ; | |
983 | #endif | |
984 | ||
985 | return result ; | |
986 | } | |
987 | #ifndef __DARWIN__ | |
988 | // Mac file names are POSIX (Unix style) under Darwin | |
989 | // therefore the conversion functions below are not needed | |
990 | ||
991 | static wxChar sMacFileNameConversion[ 1000 ] ; | |
992 | static char scMacFileNameConversion[ 1000 ] ; | |
993 | ||
994 | #endif | |
995 | void wxMacFilename2FSSpec( const char *path , FSSpec *spec ) | |
996 | { | |
997 | OSStatus err = noErr ; | |
998 | #ifdef __DARWIN__ | |
999 | FSRef theRef; | |
1000 | ||
1001 | // get the FSRef associated with the POSIX path | |
1002 | err = FSPathMakeRef((const UInt8 *) path, &theRef, NULL); | |
1003 | // convert the FSRef to an FSSpec | |
1004 | err = FSGetCatalogInfo(&theRef, kFSCatInfoNone, NULL, NULL, spec, NULL); | |
1005 | #else | |
1006 | if ( strchr( path , ':' ) == NULL ) | |
1007 | { | |
1008 | // try whether it is a volume / or a mounted volume | |
1009 | strncpy( scMacFileNameConversion , path , 1000 ) ; | |
1010 | scMacFileNameConversion[998] = 0 ; | |
1011 | strcat( scMacFileNameConversion , ":" ) ; | |
1012 | err = FSpLocationFromFullPath( strlen(scMacFileNameConversion) , scMacFileNameConversion , spec ) ; | |
1013 | } | |
1014 | else | |
1015 | { | |
1016 | err = FSpLocationFromFullPath( strlen(path) , path , spec ) ; | |
1017 | } | |
1018 | #endif | |
1019 | } | |
1020 | ||
1021 | #if wxUSE_UNICODE | |
1022 | WXDLLEXPORT void wxMacFilename2FSSpec( const wxChar *path , FSSpec *spec ) | |
1023 | { | |
1024 | return wxMacFilename2FSSpec( wxConvFile.cWC2MB(path) , spec ) ; | |
1025 | } | |
1026 | #endif | |
1027 | ||
1028 | #ifndef __DARWIN__ | |
1029 | ||
1030 | wxString wxMac2UnixFilename (const wxChar *str) | |
1031 | { | |
1032 | wxChar *s = sMacFileNameConversion ; | |
1033 | wxStrcpy( s , str ) ; | |
1034 | if (s) | |
1035 | { | |
1036 | memmove( s+1 , s ,wxStrlen( s ) + 1 * sizeof(wxChar)) ; | |
1037 | if ( *s == ':' ) | |
1038 | *s = '.' ; | |
1039 | else | |
1040 | *s = '/' ; | |
1041 | ||
1042 | while (*s) | |
1043 | { | |
1044 | if (*s == ':') | |
1045 | *s = '/'; | |
1046 | else | |
1047 | *s = wxTolower(*s); // Case INDEPENDENT | |
1048 | s++; | |
1049 | } | |
1050 | } | |
1051 | return wxString(sMacFileNameConversion) ; | |
1052 | } | |
1053 | ||
1054 | wxString wxUnix2MacFilename (const wxChar *str) | |
1055 | { | |
1056 | wxChar *s = sMacFileNameConversion ; | |
1057 | wxStrcpy( s , str ) ; | |
1058 | if (s) | |
1059 | { | |
1060 | if ( *s == '.' ) | |
1061 | { | |
1062 | // relative path , since it goes on with slash which is translated to a : | |
1063 | memmove( s , s+1 ,wxStrlen( s ) * sizeof(wxChar)) ; | |
1064 | } | |
1065 | else if ( *s == '/' ) | |
1066 | { | |
1067 | // absolute path -> on mac just start with the drive name | |
1068 | memmove( s , s+1 ,wxStrlen( s ) * sizeof(wxChar) ) ; | |
1069 | } | |
1070 | else | |
1071 | { | |
1072 | wxASSERT_MSG( 1 , wxT("unkown path beginning") ) ; | |
1073 | } | |
1074 | while (*s) | |
1075 | { | |
1076 | if (*s == '/' || *s == '\\') | |
1077 | { | |
1078 | // convert any back-directory situations | |
1079 | if ( *(s+1) == '.' && *(s+2) == '.' && ( (*(s+3) == '/' || *(s+3) == '\\') ) ) | |
1080 | { | |
1081 | *s = ':'; | |
1082 | memmove( s+1 , s+3 ,(wxStrlen( s+3 ) + 1)*sizeof(wxChar) ) ; | |
1083 | } | |
1084 | else | |
1085 | *s = ':'; | |
1086 | } | |
1087 | s++ ; | |
1088 | } | |
1089 | } | |
1090 | return wxString(sMacFileNameConversion) ; | |
1091 | } | |
1092 | ||
1093 | wxString wxMacFSSpec2UnixFilename( const FSSpec *spec ) | |
1094 | { | |
1095 | return wxMac2UnixFilename( wxMacFSSpec2MacFilename( spec) ) ; | |
1096 | } | |
1097 | ||
1098 | void wxUnixFilename2FSSpec( const char *path , FSSpec *spec ) | |
1099 | { | |
1100 | wxString var = wxUnix2MacFilename( path ) ; | |
1101 | wxMacFilename2FSSpec( var , spec ) ; | |
1102 | } | |
1103 | #endif // ! __DARWIN__ | |
1104 | ||
1105 | #endif // __WXMAC__ | |
1106 | ||
1107 | void | |
1108 | wxDos2UnixFilename (wxChar *s) | |
1109 | { | |
1110 | if (s) | |
1111 | while (*s) | |
1112 | { | |
1113 | if (*s == _T('\\')) | |
1114 | *s = _T('/'); | |
1115 | #ifdef __WXMSW__ | |
1116 | else | |
1117 | *s = wxTolower (*s); // Case INDEPENDENT | |
1118 | #endif | |
1119 | s++; | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | void | |
1124 | #if defined(__WXMSW__) || defined(__OS2__) | |
1125 | wxUnix2DosFilename (wxChar *s) | |
1126 | #else | |
1127 | wxUnix2DosFilename (wxChar *WXUNUSED(s) ) | |
1128 | #endif | |
1129 | { | |
1130 | // Yes, I really mean this to happen under DOS only! JACS | |
1131 | #if defined(__WXMSW__) || defined(__OS2__) | |
1132 | if (s) | |
1133 | while (*s) | |
1134 | { | |
1135 | if (*s == wxT('/')) | |
1136 | *s = wxT('\\'); | |
1137 | s++; | |
1138 | } | |
1139 | #endif | |
1140 | } | |
1141 | ||
1142 | // Concatenate two files to form third | |
1143 | bool | |
1144 | wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& file3) | |
1145 | { | |
1146 | wxString outfile; | |
1147 | if ( !wxGetTempFileName( wxT("cat"), outfile) ) | |
1148 | return FALSE; | |
1149 | ||
1150 | FILE *fp1 wxDUMMY_INITIALIZE(NULL); | |
1151 | FILE *fp2 = NULL; | |
1152 | FILE *fp3 = NULL; | |
1153 | // Open the inputs and outputs | |
1154 | if ((fp1 = wxFopen ( file1, wxT("rb"))) == NULL || | |
1155 | (fp2 = wxFopen ( file2, wxT("rb"))) == NULL || | |
1156 | (fp3 = wxFopen ( outfile, wxT("wb"))) == NULL) | |
1157 | { | |
1158 | if (fp1) | |
1159 | fclose (fp1); | |
1160 | if (fp2) | |
1161 | fclose (fp2); | |
1162 | if (fp3) | |
1163 | fclose (fp3); | |
1164 | return FALSE; | |
1165 | } | |
1166 | ||
1167 | int ch; | |
1168 | while ((ch = getc (fp1)) != EOF) | |
1169 | (void) putc (ch, fp3); | |
1170 | fclose (fp1); | |
1171 | ||
1172 | while ((ch = getc (fp2)) != EOF) | |
1173 | (void) putc (ch, fp3); | |
1174 | fclose (fp2); | |
1175 | ||
1176 | fclose (fp3); | |
1177 | bool result = wxRenameFile(outfile, file3); | |
1178 | return result; | |
1179 | } | |
1180 | ||
1181 | // Copy files | |
1182 | bool | |
1183 | wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) | |
1184 | { | |
1185 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
1186 | // CopyFile() copies file attributes and modification time too, so use it | |
1187 | // instead of our code if available | |
1188 | // | |
1189 | // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite | |
1190 | if ( !::CopyFile(file1, file2, !overwrite) ) | |
1191 | { | |
1192 | wxLogSysError(_("Failed to copy the file '%s' to '%s'"), | |
1193 | file1.c_str(), file2.c_str()); | |
1194 | ||
1195 | return FALSE; | |
1196 | } | |
1197 | #elif defined(__OS2__) | |
1198 | if ( ::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) != 0 ) | |
1199 | return FALSE; | |
1200 | #else // !Win32 | |
1201 | ||
1202 | wxStructStat fbuf; | |
1203 | // get permissions of file1 | |
1204 | if ( wxStat( file1.c_str(), &fbuf) != 0 ) | |
1205 | { | |
1206 | // the file probably doesn't exist or we haven't the rights to read | |
1207 | // from it anyhow | |
1208 | wxLogSysError(_("Impossible to get permissions for file '%s'"), | |
1209 | file1.c_str()); | |
1210 | return FALSE; | |
1211 | } | |
1212 | ||
1213 | // open file1 for reading | |
1214 | wxFile fileIn(file1, wxFile::read); | |
1215 | if ( !fileIn.IsOpened() ) | |
1216 | return FALSE; | |
1217 | ||
1218 | // remove file2, if it exists. This is needed for creating | |
1219 | // file2 with the correct permissions in the next step | |
1220 | if ( wxFileExists(file2) && (!overwrite || !wxRemoveFile(file2))) | |
1221 | { | |
1222 | wxLogSysError(_("Impossible to overwrite the file '%s'"), | |
1223 | file2.c_str()); | |
1224 | return FALSE; | |
1225 | } | |
1226 | ||
1227 | #ifdef __UNIX__ | |
1228 | // reset the umask as we want to create the file with exactly the same | |
1229 | // permissions as the original one | |
1230 | mode_t oldUmask = umask( 0 ); | |
1231 | #endif // __UNIX__ | |
1232 | ||
1233 | // create file2 with the same permissions than file1 and open it for | |
1234 | // writing | |
1235 | ||
1236 | wxFile fileOut; | |
1237 | if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) ) | |
1238 | return FALSE; | |
1239 | ||
1240 | #ifdef __UNIX__ | |
1241 | /// restore the old umask | |
1242 | umask(oldUmask); | |
1243 | #endif // __UNIX__ | |
1244 | ||
1245 | // copy contents of file1 to file2 | |
1246 | char buf[4096]; | |
1247 | size_t count; | |
1248 | for ( ;; ) | |
1249 | { | |
1250 | count = fileIn.Read(buf, WXSIZEOF(buf)); | |
1251 | if ( fileIn.Error() ) | |
1252 | return FALSE; | |
1253 | ||
1254 | // end of file? | |
1255 | if ( !count ) | |
1256 | break; | |
1257 | ||
1258 | if ( fileOut.Write(buf, count) < count ) | |
1259 | return FALSE; | |
1260 | } | |
1261 | ||
1262 | // we can expect fileIn to be closed successfully, but we should ensure | |
1263 | // that fileOut was closed as some write errors (disk full) might not be | |
1264 | // detected before doing this | |
1265 | if ( !fileIn.Close() || !fileOut.Close() ) | |
1266 | return FALSE; | |
1267 | ||
1268 | #if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__) | |
1269 | // no chmod in VA. Should be some permission API for HPFS386 partitions | |
1270 | // however | |
1271 | if ( chmod(OS_FILENAME(file2), fbuf.st_mode) != 0 ) | |
1272 | { | |
1273 | wxLogSysError(_("Impossible to set permissions for the file '%s'"), | |
1274 | file2.c_str()); | |
1275 | return FALSE; | |
1276 | } | |
1277 | #endif // OS/2 || Mac | |
1278 | #endif // __WXMSW__ && __WIN32__ | |
1279 | ||
1280 | return TRUE; | |
1281 | } | |
1282 | ||
1283 | bool | |
1284 | wxRenameFile (const wxString& file1, const wxString& file2) | |
1285 | { | |
1286 | #ifndef __WXWINCE__ | |
1287 | // Normal system call | |
1288 | if ( wxRename (file1, file2) == 0 ) | |
1289 | return TRUE; | |
1290 | #endif | |
1291 | ||
1292 | // Try to copy | |
1293 | if (wxCopyFile(file1, file2)) { | |
1294 | wxRemoveFile(file1); | |
1295 | return TRUE; | |
1296 | } | |
1297 | // Give up | |
1298 | return FALSE; | |
1299 | } | |
1300 | ||
1301 | bool wxRemoveFile(const wxString& file) | |
1302 | { | |
1303 | #if defined(__VISUALC__) \ | |
1304 | || defined(__BORLANDC__) \ | |
1305 | || defined(__WATCOMC__) \ | |
1306 | || defined(__GNUWIN32__) | |
1307 | int res = wxRemove(file); | |
1308 | #elif defined(__WXMAC__) | |
1309 | int res = unlink(wxFNCONV(file)); | |
1310 | #else | |
1311 | int res = unlink(OS_FILENAME(file)); | |
1312 | #endif | |
1313 | ||
1314 | return res == 0; | |
1315 | } | |
1316 | ||
1317 | bool wxMkdir(const wxString& dir, int perm) | |
1318 | { | |
1319 | #if defined(__WXMAC__) && !defined(__UNIX__) | |
1320 | return (mkdir( wxFNCONV(dir) , 0 ) == 0); | |
1321 | #else // !Mac | |
1322 | const wxChar *dirname = dir.c_str(); | |
1323 | ||
1324 | // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too | |
1325 | // for the GNU compiler | |
1326 | #if (!(defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__) | |
1327 | #ifndef MSVCRT | |
1328 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) | |
1329 | #else | |
1330 | if ( mkdir(wxFNCONV(dirname)) != 0 ) | |
1331 | #endif | |
1332 | #elif defined(__OS2__) | |
1333 | if (::DosCreateDir((PSZ)dirname, NULL) != 0) // enhance for EAB's?? | |
1334 | #elif defined(__DOS__) | |
1335 | #if defined(__WATCOMC__) | |
1336 | (void)perm; | |
1337 | if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) | |
1338 | #elif defined(__DJGPP__) | |
1339 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) | |
1340 | #else | |
1341 | #error "Unsupported DOS compiler!" | |
1342 | #endif | |
1343 | #else // !MSW, !DOS and !OS/2 VAC++ | |
1344 | (void)perm; | |
1345 | #ifdef __WXWINCE__ | |
1346 | if ( !CreateDirectory(dirname, NULL) ) | |
1347 | #else | |
1348 | if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) | |
1349 | #endif | |
1350 | #endif // !MSW/MSW | |
1351 | { | |
1352 | wxLogSysError(_("Directory '%s' couldn't be created"), dirname); | |
1353 | ||
1354 | return FALSE; | |
1355 | } | |
1356 | ||
1357 | return TRUE; | |
1358 | #endif // Mac/!Mac | |
1359 | } | |
1360 | ||
1361 | bool wxRmdir(const wxString& dir, int WXUNUSED(flags)) | |
1362 | { | |
1363 | #ifdef __VMS__ | |
1364 | return FALSE; //to be changed since rmdir exists in VMS7.x | |
1365 | #elif defined(__OS2__) | |
1366 | return (::DosDeleteDir((PSZ)dir.c_str()) == 0); | |
1367 | #else | |
1368 | ||
1369 | #ifdef __WXWINCE__ | |
1370 | return (CreateDirectory(dir, NULL) != 0); | |
1371 | #else | |
1372 | return (wxRmDir(OS_FILENAME(dir)) == 0); | |
1373 | #endif | |
1374 | ||
1375 | #endif | |
1376 | } | |
1377 | ||
1378 | // does the path exists? (may have or not '/' or '\\' at the end) | |
1379 | bool wxPathExists(const wxChar *pszPathName) | |
1380 | { | |
1381 | wxString strPath(pszPathName); | |
1382 | ||
1383 | #if defined(__WINDOWS__) || defined(__OS2__) | |
1384 | // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists, | |
1385 | // so remove all trailing backslashes from the path - but don't do this for | |
1386 | // the pathes "d:\" (which are different from "d:") nor for just "\" | |
1387 | while ( wxEndsWithPathSeparator(strPath) ) | |
1388 | { | |
1389 | size_t len = strPath.length(); | |
1390 | if ( len == 1 || (len == 3 && strPath[len - 2] == _T(':')) ) | |
1391 | break; | |
1392 | ||
1393 | strPath.Truncate(len - 1); | |
1394 | } | |
1395 | #endif // __WINDOWS__ | |
1396 | ||
1397 | #ifdef __OS2__ | |
1398 | // OS/2 can't handle "d:", it wants either "d:\" or "d:." | |
1399 | if (strPath.length() == 2 && strPath[1] == _T(':')) | |
1400 | strPath << _T('.'); | |
1401 | #endif | |
1402 | ||
1403 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
1404 | // stat() can't cope with network paths | |
1405 | DWORD ret = ::GetFileAttributes(strPath); | |
1406 | ||
1407 | return (ret != (DWORD)-1) && (ret & FILE_ATTRIBUTE_DIRECTORY); | |
1408 | #else // !__WIN32__ | |
1409 | ||
1410 | wxStructStat st; | |
1411 | #ifndef __VISAGECPP__ | |
1412 | return wxStat(strPath.c_str(), &st) == 0 && ((st.st_mode & S_IFMT) == S_IFDIR); | |
1413 | #else | |
1414 | // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only | |
1415 | return wxStat(pszPathName, &st) == 0 && (st.st_mode == S_IFDIR); | |
1416 | #endif | |
1417 | ||
1418 | #endif // __WIN32__/!__WIN32__ | |
1419 | } | |
1420 | ||
1421 | // Get a temporary filename, opening and closing the file. | |
1422 | wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf) | |
1423 | { | |
1424 | wxString filename = wxFileName::CreateTempFileName(prefix); | |
1425 | if ( filename.empty() ) | |
1426 | return NULL; | |
1427 | ||
1428 | if ( buf ) | |
1429 | wxStrcpy(buf, filename); | |
1430 | else | |
1431 | buf = MYcopystring(filename); | |
1432 | ||
1433 | return buf; | |
1434 | } | |
1435 | ||
1436 | bool wxGetTempFileName(const wxString& prefix, wxString& buf) | |
1437 | { | |
1438 | buf = wxFileName::CreateTempFileName(prefix); | |
1439 | ||
1440 | return !buf.empty(); | |
1441 | } | |
1442 | ||
1443 | // Get first file name matching given wild card. | |
1444 | ||
1445 | static wxDir *gs_dir = NULL; | |
1446 | static wxString gs_dirPath; | |
1447 | ||
1448 | wxString wxFindFirstFile(const wxChar *spec, int flags) | |
1449 | { | |
1450 | wxSplitPath(spec, &gs_dirPath, NULL, NULL); | |
1451 | if ( gs_dirPath.IsEmpty() ) | |
1452 | gs_dirPath = wxT("."); | |
1453 | if ( !wxEndsWithPathSeparator(gs_dirPath ) ) | |
1454 | gs_dirPath << wxFILE_SEP_PATH; | |
1455 | ||
1456 | if (gs_dir) | |
1457 | delete gs_dir; | |
1458 | gs_dir = new wxDir(gs_dirPath); | |
1459 | ||
1460 | if ( !gs_dir->IsOpened() ) | |
1461 | { | |
1462 | wxLogSysError(_("Can not enumerate files '%s'"), spec); | |
1463 | return wxEmptyString; | |
1464 | } | |
1465 | ||
1466 | int dirFlags; | |
1467 | switch (flags) | |
1468 | { | |
1469 | case wxDIR: dirFlags = wxDIR_DIRS; break; | |
1470 | case wxFILE: dirFlags = wxDIR_FILES; break; | |
1471 | default: dirFlags = wxDIR_DIRS | wxDIR_FILES; break; | |
1472 | } | |
1473 | ||
1474 | wxString result; | |
1475 | gs_dir->GetFirst(&result, wxFileNameFromPath(wxString(spec)), dirFlags); | |
1476 | if ( result.IsEmpty() ) | |
1477 | { | |
1478 | wxDELETE(gs_dir); | |
1479 | return result; | |
1480 | } | |
1481 | ||
1482 | return gs_dirPath + result; | |
1483 | } | |
1484 | ||
1485 | wxString wxFindNextFile() | |
1486 | { | |
1487 | wxASSERT_MSG( gs_dir, wxT("You must call wxFindFirstFile before!") ); | |
1488 | ||
1489 | wxString result; | |
1490 | gs_dir->GetNext(&result); | |
1491 | ||
1492 | if ( result.IsEmpty() ) | |
1493 | { | |
1494 | wxDELETE(gs_dir); | |
1495 | return result; | |
1496 | } | |
1497 | ||
1498 | return gs_dirPath + result; | |
1499 | } | |
1500 | ||
1501 | ||
1502 | // Get current working directory. | |
1503 | // If buf is NULL, allocates space using new, else | |
1504 | // copies into buf. | |
1505 | wxChar *wxGetWorkingDirectory(wxChar *buf, int sz) | |
1506 | { | |
1507 | #ifdef __WXWINCE__ | |
1508 | return NULL; | |
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 | // This is not legal code as the compiler | |
1526 | // is allowed destroy the wxCharBuffer. | |
1527 | // wxCharBuffer c_buffer(sz); | |
1528 | // char *cbuf = (char*)(const char*)c_buffer; | |
1529 | char cbuf[_MAXPATHLEN]; | |
1530 | #endif | |
1531 | ||
1532 | #ifdef HAVE_WGETCWD | |
1533 | #if wxUSE_UNICODE_MSLU | |
1534 | if ( wxGetOsVersion() != wxWIN95 ) | |
1535 | #else | |
1536 | char *cbuf = NULL; // never really used because needsANSI will always be FALSE | |
1537 | #endif | |
1538 | { | |
1539 | ok = _wgetcwd(buf, sz) != NULL; | |
1540 | needsANSI = FALSE; | |
1541 | } | |
1542 | #endif | |
1543 | ||
1544 | if ( needsANSI ) | |
1545 | #endif // wxUSE_UNICODE | |
1546 | { | |
1547 | #if defined(_MSC_VER) || defined(__MINGW32__) | |
1548 | ok = _getcwd(cbuf, sz) != NULL; | |
1549 | #elif defined(__WXMAC__) && !defined(__DARWIN__) | |
1550 | FSSpec cwdSpec ; | |
1551 | FCBPBRec pb; | |
1552 | OSErr error; | |
1553 | Str255 fileName ; | |
1554 | pb.ioNamePtr = (StringPtr) &fileName; | |
1555 | pb.ioVRefNum = 0; | |
1556 | pb.ioRefNum = LMGetCurApRefNum(); | |
1557 | pb.ioFCBIndx = 0; | |
1558 | error = PBGetFCBInfoSync(&pb); | |
1559 | if ( error == noErr ) | |
1560 | { | |
1561 | cwdSpec.vRefNum = pb.ioFCBVRefNum; | |
1562 | cwdSpec.parID = pb.ioFCBParID; | |
1563 | cwdSpec.name[0] = 0 ; | |
1564 | wxString res = wxMacFSSpec2MacFilename( &cwdSpec ) ; | |
1565 | wxStrcpy( buf , res ) ; | |
1566 | ok = TRUE; | |
1567 | } | |
1568 | else | |
1569 | { | |
1570 | ok = FALSE; | |
1571 | } | |
1572 | #elif defined(__OS2__) | |
1573 | APIRET rc; | |
1574 | ULONG ulDriveNum = 0; | |
1575 | ULONG ulDriveMap = 0; | |
1576 | rc = ::DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap); | |
1577 | ok = rc == 0; | |
1578 | if (ok) | |
1579 | { | |
1580 | sz -= 3; | |
1581 | rc = ::DosQueryCurrentDir( 0 // current drive | |
1582 | ,cbuf + 3 | |
1583 | ,(PULONG)&sz | |
1584 | ); | |
1585 | cbuf[0] = 'A' + (ulDriveNum - 1); | |
1586 | cbuf[1] = ':'; | |
1587 | cbuf[2] = '\\'; | |
1588 | ok = rc == 0; | |
1589 | } | |
1590 | #else // !Win32/VC++ !Mac !OS2 | |
1591 | ok = getcwd(cbuf, sz) != NULL; | |
1592 | #endif // platform | |
1593 | ||
1594 | #if wxUSE_UNICODE && !defined(__WXMAC__) | |
1595 | // finally convert the result to Unicode if needed | |
1596 | wxConvFile.MB2WC(buf, cbuf, sz); | |
1597 | #endif // wxUSE_UNICODE | |
1598 | } | |
1599 | ||
1600 | if ( !ok ) | |
1601 | { | |
1602 | wxLogSysError(_("Failed to get the working directory")); | |
1603 | ||
1604 | // VZ: the old code used to return "." on error which didn't make any | |
1605 | // sense at all to me - empty string is a better error indicator | |
1606 | // (NULL might be even better but I'm afraid this could lead to | |
1607 | // problems with the old code assuming the return is never NULL) | |
1608 | buf[0] = _T('\0'); | |
1609 | } | |
1610 | else // ok, but we might need to massage the path into the right format | |
1611 | { | |
1612 | #ifdef __DJGPP__ | |
1613 | // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths | |
1614 | // with / deliminers. We don't like that. | |
1615 | for (wxChar *ch = buf; *ch; ch++) | |
1616 | { | |
1617 | if (*ch == wxT('/')) | |
1618 | *ch = wxT('\\'); | |
1619 | } | |
1620 | #endif // __DJGPP__ | |
1621 | ||
1622 | // MBN: we hope that in the case the user is compiling a GTK+/Motif app, | |
1623 | // he needs Unix as opposed to Win32 pathnames | |
1624 | #if defined( __CYGWIN__ ) && defined( __WINDOWS__ ) | |
1625 | // another example of DOS/Unix mix (Cygwin) | |
1626 | wxString pathUnix = buf; | |
1627 | cygwin_conv_to_full_win32_path(pathUnix, buf); | |
1628 | #endif // __CYGWIN__ | |
1629 | } | |
1630 | ||
1631 | return buf; | |
1632 | ||
1633 | #if !wxUSE_UNICODE | |
1634 | #undef cbuf | |
1635 | #endif | |
1636 | ||
1637 | #endif | |
1638 | // __WXWINCE__ | |
1639 | } | |
1640 | ||
1641 | wxString wxGetCwd() | |
1642 | { | |
1643 | wxChar *buffer = new wxChar[_MAXPATHLEN]; | |
1644 | wxGetWorkingDirectory(buffer, _MAXPATHLEN); | |
1645 | wxString str( buffer ); | |
1646 | delete [] buffer; | |
1647 | ||
1648 | return str; | |
1649 | } | |
1650 | ||
1651 | bool wxSetWorkingDirectory(const wxString& d) | |
1652 | { | |
1653 | #if defined(__OS2__) | |
1654 | return (::DosSetCurrentDir((PSZ)d.c_str()) == 0); | |
1655 | #elif defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__) | |
1656 | return (chdir(wxFNSTRINGCAST d.fn_str()) == 0); | |
1657 | #elif defined(__WINDOWS__) | |
1658 | ||
1659 | #ifdef __WIN32__ | |
1660 | #ifdef __WXWINCE__ | |
1661 | // No equivalent in WinCE | |
1662 | return FALSE; | |
1663 | #else | |
1664 | return (bool)(SetCurrentDirectory(d) != 0); | |
1665 | #endif | |
1666 | #else | |
1667 | // Must change drive, too. | |
1668 | bool isDriveSpec = ((strlen(d) > 1) && (d[1] == ':')); | |
1669 | if (isDriveSpec) | |
1670 | { | |
1671 | wxChar firstChar = d[0]; | |
1672 | ||
1673 | // To upper case | |
1674 | if (firstChar > 90) | |
1675 | firstChar = firstChar - 32; | |
1676 | ||
1677 | // To a drive number | |
1678 | unsigned int driveNo = firstChar - 64; | |
1679 | if (driveNo > 0) | |
1680 | { | |
1681 | unsigned int noDrives; | |
1682 | _dos_setdrive(driveNo, &noDrives); | |
1683 | } | |
1684 | } | |
1685 | bool success = (chdir(WXSTRINGCAST d) == 0); | |
1686 | ||
1687 | return success; | |
1688 | #endif | |
1689 | ||
1690 | #endif | |
1691 | } | |
1692 | ||
1693 | // Get the OS directory if appropriate (such as the Windows directory). | |
1694 | // On non-Windows platform, probably just return the empty string. | |
1695 | wxString wxGetOSDirectory() | |
1696 | { | |
1697 | #ifdef __WXWINCE__ | |
1698 | return wxString(wxT("\\Windows")); | |
1699 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
1700 | wxChar buf[256]; | |
1701 | GetWindowsDirectory(buf, 256); | |
1702 | return wxString(buf); | |
1703 | #elif defined(__WXMAC__) | |
1704 | return wxMacFindFolder(kOnSystemDisk, 'macs', false); | |
1705 | #else | |
1706 | return wxEmptyString; | |
1707 | #endif | |
1708 | } | |
1709 | ||
1710 | bool wxEndsWithPathSeparator(const wxChar *pszFileName) | |
1711 | { | |
1712 | size_t len = wxStrlen(pszFileName); | |
1713 | ||
1714 | return len && wxIsPathSeparator(pszFileName[len - 1]); | |
1715 | } | |
1716 | ||
1717 | // find a file in a list of directories, returns FALSE if not found | |
1718 | bool wxFindFileInPath(wxString *pStr, const wxChar *pszPath, const wxChar *pszFile) | |
1719 | { | |
1720 | // we assume that it's not empty | |
1721 | wxCHECK_MSG( !wxIsEmpty(pszFile), FALSE, | |
1722 | _T("empty file name in wxFindFileInPath")); | |
1723 | ||
1724 | // skip path separator in the beginning of the file name if present | |
1725 | if ( wxIsPathSeparator(*pszFile) ) | |
1726 | pszFile++; | |
1727 | ||
1728 | // copy the path (strtok will modify it) | |
1729 | wxChar *szPath = new wxChar[wxStrlen(pszPath) + 1]; | |
1730 | wxStrcpy(szPath, pszPath); | |
1731 | ||
1732 | wxString strFile; | |
1733 | wxChar *pc, *save_ptr; | |
1734 | for ( pc = wxStrtok(szPath, wxPATH_SEP, &save_ptr); | |
1735 | pc != NULL; | |
1736 | pc = wxStrtok((wxChar *) NULL, wxPATH_SEP, &save_ptr) ) | |
1737 | { | |
1738 | // search for the file in this directory | |
1739 | strFile = pc; | |
1740 | if ( !wxEndsWithPathSeparator(pc) ) | |
1741 | strFile += wxFILE_SEP_PATH; | |
1742 | strFile += pszFile; | |
1743 | ||
1744 | if ( wxFileExists(strFile) ) { | |
1745 | *pStr = strFile; | |
1746 | break; | |
1747 | } | |
1748 | } | |
1749 | ||
1750 | // suppress warning about unused variable save_ptr when wxStrtok() is a | |
1751 | // macro which throws away its third argument | |
1752 | save_ptr = pc; | |
1753 | ||
1754 | delete [] szPath; | |
1755 | ||
1756 | return pc != NULL; // if TRUE => we breaked from the loop | |
1757 | } | |
1758 | ||
1759 | void WXDLLEXPORT wxSplitPath(const wxChar *pszFileName, | |
1760 | wxString *pstrPath, | |
1761 | wxString *pstrName, | |
1762 | wxString *pstrExt) | |
1763 | { | |
1764 | // it can be empty, but it shouldn't be NULL | |
1765 | wxCHECK_RET( pszFileName, wxT("NULL file name in wxSplitPath") ); | |
1766 | ||
1767 | wxFileName::SplitPath(pszFileName, pstrPath, pstrName, pstrExt); | |
1768 | } | |
1769 | ||
1770 | time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename) | |
1771 | { | |
1772 | #ifdef __WXWINCE__ | |
1773 | FILETIME creationTime, lastAccessTime, lastWriteTime; | |
1774 | HANDLE fileHandle = ::CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
1775 | 0, FILE_ATTRIBUTE_NORMAL, 0); | |
1776 | if (fileHandle == INVALID_HANDLE_VALUE) | |
1777 | return 0; | |
1778 | else | |
1779 | { | |
1780 | if (GetFileTime(fileHandle, & creationTime, & lastAccessTime, & lastWriteTime)) | |
1781 | { | |
1782 | CloseHandle(fileHandle); | |
1783 | ||
1784 | wxDateTime dateTime; | |
1785 | FILETIME ftLocal; | |
1786 | if ( !::FileTimeToLocalFileTime(&lastWriteTime, &ftLocal) ) | |
1787 | { | |
1788 | wxLogLastError(_T("FileTimeToLocalFileTime")); | |
1789 | } | |
1790 | ||
1791 | SYSTEMTIME st; | |
1792 | if ( !::FileTimeToSystemTime(&ftLocal, &st) ) | |
1793 | { | |
1794 | wxLogLastError(_T("FileTimeToSystemTime")); | |
1795 | } | |
1796 | ||
1797 | dateTime.Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear, | |
1798 | st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); | |
1799 | return dateTime.GetTicks(); | |
1800 | } | |
1801 | else | |
1802 | return 0; | |
1803 | } | |
1804 | #else | |
1805 | wxStructStat buf; | |
1806 | wxStat( filename, &buf); | |
1807 | ||
1808 | return buf.st_mtime; | |
1809 | #endif | |
1810 | } | |
1811 | ||
1812 | ||
1813 | //------------------------------------------------------------------------ | |
1814 | // wild character routines | |
1815 | //------------------------------------------------------------------------ | |
1816 | ||
1817 | bool wxIsWild( const wxString& pattern ) | |
1818 | { | |
1819 | wxString tmp = pattern; | |
1820 | wxChar *pat = WXSTRINGCAST(tmp); | |
1821 | while (*pat) | |
1822 | { | |
1823 | switch (*pat++) | |
1824 | { | |
1825 | case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'): | |
1826 | return TRUE; | |
1827 | case wxT('\\'): | |
1828 | if (!*pat++) | |
1829 | return FALSE; | |
1830 | } | |
1831 | } | |
1832 | return FALSE; | |
1833 | } | |
1834 | ||
1835 | /* | |
1836 | * Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU> | |
1837 | * | |
1838 | * The match procedure is public domain code (from ircII's reg.c) | |
1839 | */ | |
1840 | ||
1841 | bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) | |
1842 | { | |
1843 | if (text.empty()) | |
1844 | { | |
1845 | /* Match if both are empty. */ | |
1846 | return pat.empty(); | |
1847 | } | |
1848 | ||
1849 | const wxChar *m = pat.c_str(), | |
1850 | *n = text.c_str(), | |
1851 | *ma = NULL, | |
1852 | *na = NULL, | |
1853 | *mp = NULL, | |
1854 | *np = NULL; | |
1855 | int just = 0, | |
1856 | pcount = 0, | |
1857 | acount = 0, | |
1858 | count = 0; | |
1859 | ||
1860 | if (dot_special && (*n == wxT('.'))) | |
1861 | { | |
1862 | /* Never match so that hidden Unix files | |
1863 | * are never found. */ | |
1864 | return FALSE; | |
1865 | } | |
1866 | ||
1867 | for (;;) | |
1868 | { | |
1869 | if (*m == wxT('*')) | |
1870 | { | |
1871 | ma = ++m; | |
1872 | na = n; | |
1873 | just = 1; | |
1874 | mp = NULL; | |
1875 | acount = count; | |
1876 | } | |
1877 | else if (*m == wxT('?')) | |
1878 | { | |
1879 | m++; | |
1880 | if (!*n++) | |
1881 | return FALSE; | |
1882 | } | |
1883 | else | |
1884 | { | |
1885 | if (*m == wxT('\\')) | |
1886 | { | |
1887 | m++; | |
1888 | /* Quoting "nothing" is a bad thing */ | |
1889 | if (!*m) | |
1890 | return FALSE; | |
1891 | } | |
1892 | if (!*m) | |
1893 | { | |
1894 | /* | |
1895 | * If we are out of both strings or we just | |
1896 | * saw a wildcard, then we can say we have a | |
1897 | * match | |
1898 | */ | |
1899 | if (!*n) | |
1900 | return TRUE; | |
1901 | if (just) | |
1902 | return TRUE; | |
1903 | just = 0; | |
1904 | goto not_matched; | |
1905 | } | |
1906 | /* | |
1907 | * We could check for *n == NULL at this point, but | |
1908 | * since it's more common to have a character there, | |
1909 | * check to see if they match first (m and n) and | |
1910 | * then if they don't match, THEN we can check for | |
1911 | * the NULL of n | |
1912 | */ | |
1913 | just = 0; | |
1914 | if (*m == *n) | |
1915 | { | |
1916 | m++; | |
1917 | if (*n == wxT(' ')) | |
1918 | mp = NULL; | |
1919 | count++; | |
1920 | n++; | |
1921 | } | |
1922 | else | |
1923 | { | |
1924 | ||
1925 | not_matched: | |
1926 | ||
1927 | /* | |
1928 | * If there are no more characters in the | |
1929 | * string, but we still need to find another | |
1930 | * character (*m != NULL), then it will be | |
1931 | * impossible to match it | |
1932 | */ | |
1933 | if (!*n) | |
1934 | return FALSE; | |
1935 | if (mp) | |
1936 | { | |
1937 | m = mp; | |
1938 | if (*np == wxT(' ')) | |
1939 | { | |
1940 | mp = NULL; | |
1941 | goto check_percent; | |
1942 | } | |
1943 | n = ++np; | |
1944 | count = pcount; | |
1945 | } | |
1946 | else | |
1947 | check_percent: | |
1948 | ||
1949 | if (ma) | |
1950 | { | |
1951 | m = ma; | |
1952 | n = ++na; | |
1953 | count = acount; | |
1954 | } | |
1955 | else | |
1956 | return FALSE; | |
1957 | } | |
1958 | } | |
1959 | } | |
1960 | } | |
1961 | ||
1962 | ||
1963 | #ifdef __VISUALC__ | |
1964 | #pragma warning(default:4706) // assignment within conditional expression | |
1965 | #endif // VC++ |