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