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