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