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