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