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