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