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