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