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