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