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