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