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