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