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