]> git.saurik.com Git - wxWidgets.git/blame - src/common/filefn.cpp
added wxDir::Traverse
[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
e90c1d2a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
7af89395 21 #pragma implementation "filefn.h"
c801d85f
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26#include "wx/defs.h"
27
28#ifdef __BORLANDC__
7af89395 29 #pragma hdrstop
c801d85f
KB
30#endif
31
c801d85f 32#include "wx/utils.h"
3096bd2f 33#include "wx/intl.h"
32f31043 34#include "wx/file.h"
9e8d8607 35#include "wx/filename.h"
c801d85f 36
fd3f686c 37// there are just too many of those...
3f4a0c5b 38#ifdef __VISUALC__
fd3f686c
VZ
39 #pragma warning(disable:4706) // assignment within conditional expression
40#endif // VC++
41
c801d85f
KB
42#include <ctype.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#if !defined(__WATCOMC__)
3f4a0c5b
VZ
47 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
48 #include <errno.h>
49 #endif
c801d85f 50#endif
3f4a0c5b 51
c801d85f 52#include <time.h>
3f4a0c5b 53
469e1e5c 54#ifndef __MWERKS__
7af89395
VZ
55 #include <sys/types.h>
56 #include <sys/stat.h>
469e1e5c 57#else
7af89395
VZ
58 #include <stat.h>
59 #include <unistd.h>
5b781a67 60 #include <unix.h>
469e1e5c 61#endif
c801d85f 62
aad5220b 63#ifdef __UNIX__
7af89395
VZ
64 #include <unistd.h>
65 #include <dirent.h>
aad5220b 66#endif
c801d85f 67
f6bcfd97 68#ifdef __WXPM__
c2ff79b1 69 #include <process.h>
f6bcfd97 70 #include "wx/os2/private.h"
c2ff79b1 71#endif
34138703 72#ifdef __WINDOWS__
a3ef5bf5 73#if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__)
7af89395
VZ
74 #include <direct.h>
75 #include <dos.h>
32f31043 76 #include <io.h>
7af89395
VZ
77#endif // __WINDOWS__
78#endif // native Win compiler
c801d85f
KB
79
80#ifdef __GNUWIN32__
7af89395
VZ
81 #ifndef __TWIN32__
82 #include <sys/unistd.h>
83 #endif
c801d85f
KB
84#endif
85
86#ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
87 // this (3.1 I believe) and how to test for it.
88 // If this works for Borland 4.0 as well, then no worries.
7af89395 89 #include <dir.h>
c801d85f
KB
90#endif
91
a3ef5bf5 92#ifdef __SALFORDC__
7af89395
VZ
93 #include <dir.h>
94 #include <unix.h>
a3ef5bf5
JS
95#endif
96
dfcb1ae0 97#include "wx/setup.h"
f5abe911 98#include "wx/log.h"
99cc00ed 99
7be1f0d9
JS
100// No, Cygwin doesn't appear to have fnmatch.h after all.
101#if defined(HAVE_FNMATCH_H)
7af89395 102 #include "fnmatch.h"
dfcb1ae0
KB
103#endif
104
34138703 105#ifdef __WINDOWS__
659d096c 106 #include <windows.h>
c801d85f
KB
107#endif
108
e90c1d2a
VZ
109// ----------------------------------------------------------------------------
110// constants
111// ----------------------------------------------------------------------------
112
c801d85f
KB
113#define _MAXPATHLEN 500
114
08942a0c 115extern wxChar *wxBuffer;
e7549107 116
03e11df5
GD
117#if defined(__WXMAC__) && !defined(__UNIX__)
118 #include "morefile.h"
119 #include "moreextr.h"
120 #include "fullpath.h"
121 #include "fspcompa.h"
17dff81c 122#endif
c801d85f 123
a339970a 124IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList)
8a0a092b 125
e90c1d2a
VZ
126// ----------------------------------------------------------------------------
127// private globals
128// ----------------------------------------------------------------------------
129
130static wxChar wxFileFunctionsBuffer[4*_MAXPATHLEN];
131
5d33ed2c
DW
132#if defined(__VISAGECPP__) && __IBMCPP__ >= 400
133//
32e768ae 134// VisualAge C++ V4.0 cannot have any external linkage const decs
5d33ed2c
DW
135// in headers included by more than one primary source
136//
137const off_t wxInvalidOffset = (off_t)-1;
138#endif
139
a339970a
VZ
140// ----------------------------------------------------------------------------
141// macros
142// ----------------------------------------------------------------------------
143
144// we need to translate Mac filenames before passing them to OS functions
145#ifdef __MAC__
146 #define OS_FILENAME(s) (wxUnix2MacFilename(s))
147#else
148 #define OS_FILENAME(s) (s.fn_str())
149#endif
150
e90c1d2a
VZ
151// ============================================================================
152// implementation
153// ============================================================================
154
c801d85f
KB
155void wxPathList::Add (const wxString& path)
156{
50920146 157 wxStringList::Add (WXSTRINGCAST path);
c801d85f
KB
158}
159
160// Add paths e.g. from the PATH environment variable
161void wxPathList::AddEnvList (const wxString& envVariable)
162{
50920146 163 static const wxChar PATH_TOKS[] =
34138703 164#ifdef __WINDOWS__
223d09f6 165 wxT(" ;"); // Don't seperate with colon in DOS (used for drive)
c801d85f 166#else
223d09f6 167 wxT(" :;");
c801d85f
KB
168#endif
169
50920146 170 wxChar *val = wxGetenv (WXSTRINGCAST envVariable);
c801d85f
KB
171 if (val && *val)
172 {
50920146
OK
173 wxChar *s = copystring (val);
174 wxChar *save_ptr, *token = wxStrtok (s, PATH_TOKS, &save_ptr);
c801d85f
KB
175
176 if (token)
a17e237f 177 {
3f4a0c5b
VZ
178 Add (copystring (token));
179 while (token)
a17e237f 180 {
50920146 181 if ((token = wxStrtok ((wxChar *) NULL, PATH_TOKS, &save_ptr)) != NULL)
a17e237f
VZ
182 Add (wxString(token));
183 }
184 }
185
186 // suppress warning about unused variable save_ptr when wxStrtok() is a
187 // macro which throws away its third argument
188 save_ptr = token;
189
190 delete [] s;
c801d85f
KB
191 }
192}
193
194// Given a full filename (with path), ensure that that file can
195// be accessed again USING FILENAME ONLY by adding the path
196// to the list if not already there.
197void wxPathList::EnsureFileAccessible (const wxString& path)
198{
7af89395
VZ
199 wxString path_only(wxPathOnly(path));
200 if ( !path_only.IsEmpty() )
201 {
202 if ( !Member(path_only) )
203 Add(path_only);
204 }
c801d85f
KB
205}
206
207bool wxPathList::Member (const wxString& path)
208{
209 for (wxNode * node = First (); node != NULL; node = node->Next ())
210 {
50920146 211 wxString path2((wxChar *) node->Data ());
c801d85f 212 if (
17dff81c 213#if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__)
c801d85f 214 // Case INDEPENDENT
3f4a0c5b 215 path.CompareTo (path2, wxString::ignoreCase) == 0
c801d85f 216#else
3f4a0c5b
VZ
217 // Case sensitive File System
218 path.CompareTo (path2) == 0
c801d85f 219#endif
3f4a0c5b
VZ
220 )
221 return TRUE;
c801d85f
KB
222 }
223 return FALSE;
224}
225
226wxString wxPathList::FindValidPath (const wxString& file)
227{
e90c1d2a
VZ
228 if (wxFileExists (wxExpandPath(wxFileFunctionsBuffer, file)))
229 return wxString(wxFileFunctionsBuffer);
c801d85f 230
50920146 231 wxChar buf[_MAXPATHLEN];
e90c1d2a 232 wxStrcpy(buf, wxFileFunctionsBuffer);
c801d85f 233
50920146
OK
234 wxChar *filename = (wxChar*) NULL; /* shut up buggy egcs warning */
235 filename = IsAbsolutePath (buf) ? wxFileNameFromPath (buf) : (wxChar *)buf;
c801d85f
KB
236
237 for (wxNode * node = First (); node; node = node->Next ())
238 {
50920146 239 wxChar *path = (wxChar *) node->Data ();
e90c1d2a
VZ
240 wxStrcpy (wxFileFunctionsBuffer, path);
241 wxChar ch = wxFileFunctionsBuffer[wxStrlen(wxFileFunctionsBuffer)-1];
223d09f6
KB
242 if (ch != wxT('\\') && ch != wxT('/'))
243 wxStrcat (wxFileFunctionsBuffer, wxT("/"));
e90c1d2a 244 wxStrcat (wxFileFunctionsBuffer, filename);
34138703 245#ifdef __WINDOWS__
e90c1d2a 246 Unix2DosFilename (wxFileFunctionsBuffer);
c801d85f 247#endif
e90c1d2a 248 if (wxFileExists (wxFileFunctionsBuffer))
c801d85f 249 {
e90c1d2a 250 return wxString(wxFileFunctionsBuffer); // Found!
c801d85f 251 }
3f4a0c5b 252 } // for()
c801d85f 253
223d09f6 254 return wxString(wxT("")); // Not found
c801d85f
KB
255}
256
257wxString wxPathList::FindAbsoluteValidPath (const wxString& file)
258{
e90c1d2a
VZ
259 wxString f = FindValidPath(file);
260 if ( wxIsAbsolutePath(f) )
261 return f;
262
263 wxString buf;
264 wxGetWorkingDirectory(buf.GetWriteBuf(_MAXPATHLEN), _MAXPATHLEN - 1);
265 buf.UngetWriteBuf();
266 if ( !wxEndsWithPathSeparator(buf) )
c801d85f 267 {
e90c1d2a 268 buf += wxFILE_SEP_PATH;
c801d85f 269 }
e90c1d2a
VZ
270 buf += f;
271
272 return buf;
c801d85f
KB
273}
274
3f4a0c5b 275bool
c801d85f
KB
276wxFileExists (const wxString& filename)
277{
6b037754 278#ifdef __GNUWIN32__ // (fix a B20 bug)
a339970a 279 return GetFileAttributes(filename) != 0xFFFFFFFF;
6b037754 280#else
a339970a
VZ
281 wxStructStat stbuf;
282 if ( !filename.empty() && wxStat (OS_FILENAME(filename), &stbuf) == 0 )
283 return TRUE;
a3ef5bf5 284
a339970a 285 return FALSE;
6b037754 286#endif
c801d85f
KB
287}
288
3f4a0c5b 289bool
c801d85f
KB
290wxIsAbsolutePath (const wxString& filename)
291{
223d09f6 292 if (filename != wxT(""))
c801d85f 293 {
223d09f6 294 if (filename[0] == wxT('/')
c801d85f 295#ifdef __VMS__
223d09f6 296 || (filename[0] == wxT('[') && filename[1] != wxT('.'))
c801d85f 297#endif
34138703 298#ifdef __WINDOWS__
c801d85f 299 /* MSDOS */
223d09f6 300 || filename[0] == wxT('\\') || (wxIsalpha (filename[0]) && filename[1] == wxT(':'))
c801d85f 301#endif
3f4a0c5b
VZ
302 )
303 return TRUE;
c801d85f
KB
304 }
305 return FALSE;
306}
307
308/*
309 * Strip off any extension (dot something) from end of file,
310 * IF one exists. Inserts zero into buffer.
311 *
312 */
3f4a0c5b 313
50920146 314void wxStripExtension(wxChar *buffer)
c801d85f 315{
50920146 316 int len = wxStrlen(buffer);
c801d85f
KB
317 int i = len-1;
318 while (i > 0)
319 {
223d09f6 320 if (buffer[i] == wxT('.'))
c801d85f
KB
321 {
322 buffer[i] = 0;
323 break;
324 }
325 i --;
326 }
327}
328
47fa7969
JS
329void wxStripExtension(wxString& buffer)
330{
331 size_t len = buffer.Length();
332 size_t i = len-1;
333 while (i > 0)
334 {
223d09f6 335 if (buffer.GetChar(i) == wxT('.'))
47fa7969
JS
336 {
337 buffer = buffer.Left(i);
338 break;
339 }
340 i --;
341 }
342}
343
c801d85f 344// Destructive removal of /./ and /../ stuff
50920146 345wxChar *wxRealPath (wxChar *path)
c801d85f 346{
2049ba38 347#ifdef __WXMSW__
223d09f6 348 static const wxChar SEP = wxT('\\');
c801d85f
KB
349 Unix2DosFilename(path);
350#else
223d09f6 351 static const wxChar SEP = wxT('/');
c801d85f
KB
352#endif
353 if (path[0] && path[1]) {
354 /* MATTHEW: special case "/./x" */
50920146 355 wxChar *p;
223d09f6 356 if (path[2] == SEP && path[1] == wxT('.'))
c801d85f
KB
357 p = &path[0];
358 else
359 p = &path[2];
360 for (; *p; p++)
361 {
3f4a0c5b
VZ
362 if (*p == SEP)
363 {
223d09f6 364 if (p[1] == wxT('.') && p[2] == wxT('.') && (p[3] == SEP || p[3] == wxT('\0')))
3f4a0c5b 365 {
50920146 366 wxChar *q;
3f4a0c5b 367 for (q = p - 1; q >= path && *q != SEP; q--);
223d09f6 368 if (q[0] == SEP && (q[1] != wxT('.') || q[2] != wxT('.') || q[3] != SEP)
3f4a0c5b
VZ
369 && (q - 1 <= path || q[-1] != SEP))
370 {
50920146 371 wxStrcpy (q, p + 3);
223d09f6 372 if (path[0] == wxT('\0'))
3f4a0c5b
VZ
373 {
374 path[0] = SEP;
223d09f6 375 path[1] = wxT('\0');
3f4a0c5b 376 }
2049ba38 377#ifdef __WXMSW__
3f4a0c5b 378 /* Check that path[2] is NULL! */
223d09f6 379 else if (path[1] == wxT(':') && !path[2])
3f4a0c5b
VZ
380 {
381 path[2] = SEP;
223d09f6 382 path[3] = wxT('\0');
3f4a0c5b
VZ
383 }
384#endif
385 p = q - 1;
386 }
387 }
223d09f6 388 else if (p[1] == wxT('.') && (p[2] == SEP || p[2] == wxT('\0')))
50920146 389 wxStrcpy (p, p + 2);
3f4a0c5b 390 }
c801d85f
KB
391 }
392 }
393 return path;
394}
395
396// Must be destroyed
50920146 397wxChar *wxCopyAbsolutePath(const wxString& filename)
c801d85f 398{
223d09f6 399 if (filename == wxT(""))
50920146 400 return (wxChar *) NULL;
c801d85f 401
e90c1d2a 402 if (! IsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer, filename))) {
50920146 403 wxChar buf[_MAXPATHLEN];
223d09f6 404 buf[0] = wxT('\0');
7af89395 405 wxGetWorkingDirectory(buf, WXSIZEOF(buf));
50920146 406 wxChar ch = buf[wxStrlen(buf) - 1];
2049ba38 407#ifdef __WXMSW__
223d09f6
KB
408 if (ch != wxT('\\') && ch != wxT('/'))
409 wxStrcat(buf, wxT("\\"));
c801d85f 410#else
223d09f6
KB
411 if (ch != wxT('/'))
412 wxStrcat(buf, wxT("/"));
c801d85f 413#endif
e90c1d2a 414 wxStrcat(buf, wxFileFunctionsBuffer);
c801d85f
KB
415 return copystring( wxRealPath(buf) );
416 }
e90c1d2a 417 return copystring( wxFileFunctionsBuffer );
c801d85f
KB
418}
419
420/*-
421 Handles:
422 ~/ => home dir
423 ~user/ => user's home dir
424 If the environment variable a = "foo" and b = "bar" then:
425 Unix:
3f4a0c5b
VZ
426 $a => foo
427 $a$b => foobar
428 $a.c => foo.c
429 xxx$a => xxxfoo
430 ${a}! => foo!
431 $(b)! => bar!
432 \$a => \$a
c801d85f 433 MSDOS:
3f4a0c5b
VZ
434 $a ==> $a
435 $(a) ==> foo
436 $(a)$b ==> foo$b
437 $(a)$(b)==> foobar
438 test.$$ ==> test.$$
c801d85f
KB
439 */
440
441/* input name in name, pathname output to buf. */
442
50920146 443wxChar *wxExpandPath(wxChar *buf, const wxChar *name)
c801d85f 444{
50920146
OK
445 register wxChar *d, *s, *nm;
446 wxChar lnm[_MAXPATHLEN];
33ac7e6f 447 int q;
c801d85f
KB
448
449 // Some compilers don't like this line.
223d09f6 450// const wxChar trimchars[] = wxT("\n \t");
c801d85f 451
50920146 452 wxChar trimchars[4];
223d09f6
KB
453 trimchars[0] = wxT('\n');
454 trimchars[1] = wxT(' ');
455 trimchars[2] = wxT('\t');
c801d85f
KB
456 trimchars[3] = 0;
457
2049ba38 458#ifdef __WXMSW__
223d09f6 459 const wxChar SEP = wxT('\\');
c801d85f 460#else
223d09f6 461 const wxChar SEP = wxT('/');
c801d85f 462#endif
223d09f6
KB
463 buf[0] = wxT('\0');
464 if (name == NULL || *name == wxT('\0'))
3f4a0c5b 465 return buf;
c801d85f 466 nm = copystring(name); // Make a scratch copy
50920146 467 wxChar *nm_tmp = nm;
c801d85f
KB
468
469 /* Skip leading whitespace and cr */
50920146 470 while (wxStrchr((wxChar *)trimchars, *nm) != NULL)
3f4a0c5b 471 nm++;
c801d85f 472 /* And strip off trailing whitespace and cr */
50920146
OK
473 s = nm + (q = wxStrlen(nm)) - 1;
474 while (q-- && wxStrchr((wxChar *)trimchars, *s) != NULL)
223d09f6 475 *s = wxT('\0');
c801d85f
KB
476
477 s = nm;
478 d = lnm;
2049ba38 479#ifdef __WXMSW__
c801d85f
KB
480 q = FALSE;
481#else
223d09f6 482 q = nm[0] == wxT('\\') && nm[1] == wxT('~');
c801d85f
KB
483#endif
484
485 /* Expand inline environment variables */
c2ff79b1
DW
486#ifdef __VISAGECPP__
487 while (*d)
488 {
489 *d++ = *s;
223d09f6 490 if(*s == wxT('\\'))
c2ff79b1
DW
491 {
492 *(d - 1) = *++s;
493 if (*d)
494 {
495 s++;
496 continue;
497 }
498 else
499 break;
500 }
501 else
502#else
22394d24 503 while ((*d++ = *s) != 0) {
c2ff79b1 504# ifndef __WXMSW__
223d09f6 505 if (*s == wxT('\\')) {
3f4a0c5b
VZ
506 if ((*(d - 1) = *++s)) {
507 s++;
508 continue;
509 } else
510 break;
511 } else
c2ff79b1 512# endif
c801d85f 513#endif
2049ba38 514#ifdef __WXMSW__
223d09f6 515 if (*s++ == wxT('$') && (*s == wxT('{') || *s == wxT(')')))
c801d85f 516#else
223d09f6 517 if (*s++ == wxT('$'))
3f4a0c5b
VZ
518#endif
519 {
50920146 520 register wxChar *start = d;
223d09f6 521 register int braces = (*s == wxT('{') || *s == wxT('('));
50920146 522 register wxChar *value;
22394d24 523 while ((*d++ = *s) != 0)
223d09f6 524 if (braces ? (*s == wxT('}') || *s == wxT(')')) : !(wxIsalnum(*s) || *s == wxT('_')) )
3f4a0c5b
VZ
525 break;
526 else
527 s++;
528 *--d = 0;
50920146 529 value = wxGetenv(braces ? start + 1 : start);
3f4a0c5b 530 if (value) {
22394d24 531 for ((d = start - 1); (*d++ = *value++) != 0;);
3f4a0c5b
VZ
532 d--;
533 if (braces && *s)
534 s++;
535 }
536 }
c801d85f
KB
537 }
538
539 /* Expand ~ and ~user */
540 nm = lnm;
223d09f6
KB
541 s = wxT("");
542 if (nm[0] == wxT('~') && !q)
c801d85f 543 {
3f4a0c5b
VZ
544 /* prefix ~ */
545 if (nm[1] == SEP || nm[1] == 0)
546 { /* ~/filename */
f6bcfd97 547 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
223d09f6 548 if ((s = WXSTRINGCAST wxGetUserHome(wxT(""))) != NULL) {
3f4a0c5b
VZ
549 if (*++nm)
550 nm++;
551 }
c801d85f 552 } else
3f4a0c5b 553 { /* ~user/filename */
50920146
OK
554 register wxChar *nnm;
555 register wxChar *home;
3f4a0c5b
VZ
556 for (s = nm; *s && *s != SEP; s++);
557 int was_sep; /* MATTHEW: Was there a separator, or NULL? */
c801d85f 558 was_sep = (*s == SEP);
3f4a0c5b
VZ
559 nnm = *s ? s + 1 : s;
560 *s = 0;
f6bcfd97 561 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
0e6667e2 562 if ((home = WXSTRINGCAST wxGetUserHome(wxString(nm + 1))) == NULL) {
c801d85f 563 if (was_sep) /* replace only if it was there: */
3f4a0c5b 564 *s = SEP;
223d09f6 565 s = wxT("");
3f4a0c5b
VZ
566 } else {
567 nm = nnm;
568 s = home;
569 }
570 }
c801d85f
KB
571 }
572
573 d = buf;
574 if (s && *s) { /* MATTHEW: s could be NULL if user '~' didn't exist */
3f4a0c5b 575 /* Copy home dir */
223d09f6 576 while (wxT('\0') != (*d++ = *s++))
3f4a0c5b
VZ
577 /* loop */;
578 // Handle root home
579 if (d - 1 > buf && *(d - 2) != SEP)
580 *(d - 1) = SEP;
c801d85f
KB
581 }
582 s = nm;
22394d24 583 while ((*d++ = *s++) != 0);
c801d85f
KB
584 delete[] nm_tmp; // clean up alloc
585 /* Now clean up the buffer */
586 return wxRealPath(buf);
587}
588
c801d85f
KB
589/* Contract Paths to be build upon an environment variable
590 component:
591
592 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
593
594 The call wxExpandPath can convert these back!
595 */
50920146 596wxChar *
c801d85f
KB
597wxContractPath (const wxString& filename, const wxString& envname, const wxString& user)
598{
50920146 599 static wxChar dest[_MAXPATHLEN];
c801d85f 600
223d09f6 601 if (filename == wxT(""))
50920146 602 return (wxChar *) NULL;
c801d85f 603
50920146 604 wxStrcpy (dest, WXSTRINGCAST filename);
2049ba38 605#ifdef __WXMSW__
c801d85f
KB
606 Unix2DosFilename(dest);
607#endif
608
609 // Handle environment
0e6667e2 610 const wxChar *val = (const wxChar *) NULL;
50920146
OK
611 wxChar *tcp = (wxChar *) NULL;
612 if (envname != WXSTRINGCAST NULL && (val = wxGetenv (WXSTRINGCAST envname)) != NULL &&
613 (tcp = wxStrstr (dest, val)) != NULL)
c801d85f 614 {
e90c1d2a 615 wxStrcpy (wxFileFunctionsBuffer, tcp + wxStrlen (val));
223d09f6
KB
616 *tcp++ = wxT('$');
617 *tcp++ = wxT('{');
50920146 618 wxStrcpy (tcp, WXSTRINGCAST envname);
223d09f6 619 wxStrcat (tcp, wxT("}"));
e90c1d2a 620 wxStrcat (tcp, wxFileFunctionsBuffer);
c801d85f
KB
621 }
622
623 // Handle User's home (ignore root homes!)
624 size_t len = 0;
625 if ((val = wxGetUserHome (user)) != NULL &&
50920146
OK
626 (len = wxStrlen(val)) > 2 &&
627 wxStrncmp(dest, val, len) == 0)
c801d85f 628 {
223d09f6
KB
629 wxStrcpy(wxFileFunctionsBuffer, wxT("~"));
630 if (user != wxT(""))
e90c1d2a 631 wxStrcat(wxFileFunctionsBuffer, (const wxChar*) user);
2049ba38 632#ifdef __WXMSW__
e90c1d2a 633// strcat(wxFileFunctionsBuffer, "\\");
c801d85f 634#else
e90c1d2a 635// strcat(wxFileFunctionsBuffer, "/");
c801d85f 636#endif
e90c1d2a
VZ
637 wxStrcat(wxFileFunctionsBuffer, dest + len);
638 wxStrcpy (dest, wxFileFunctionsBuffer);
c801d85f
KB
639 }
640
641 return dest;
642}
643
644// Return just the filename, not the path
645// (basename)
50920146 646wxChar *wxFileNameFromPath (wxChar *path)
c801d85f
KB
647{
648 if (path)
649 {
50920146 650 register wxChar *tcp;
c801d85f 651
50920146 652 tcp = path + wxStrlen (path);
c801d85f 653 while (--tcp >= path)
3f4a0c5b 654 {
223d09f6 655 if (*tcp == wxT('/') || *tcp == wxT('\\')
c801d85f 656#ifdef __VMS__
223d09f6 657 || *tcp == wxT(':') || *tcp == wxT(']'))
c801d85f
KB
658#else
659 )
660#endif
3f4a0c5b
VZ
661 return tcp + 1;
662 } /* while */
c2ff79b1 663#if defined(__WXMSW__) || defined(__WXPM__)
223d09f6 664 if (wxIsalpha (*path) && *(path + 1) == wxT(':'))
3f4a0c5b 665 return path + 2;
c801d85f
KB
666#endif
667 }
668 return path;
669}
670
671wxString wxFileNameFromPath (const wxString& path1)
672{
223d09f6 673 if (path1 != wxT(""))
c801d85f
KB
674 {
675
50920146
OK
676 wxChar *path = WXSTRINGCAST path1 ;
677 register wxChar *tcp;
c801d85f 678
50920146 679 tcp = path + wxStrlen (path);
c801d85f 680 while (--tcp >= path)
3f4a0c5b 681 {
223d09f6 682 if (*tcp == wxT('/') || *tcp == wxT('\\')
c801d85f 683#ifdef __VMS__
223d09f6 684 || *tcp == wxT(':') || *tcp == wxT(']'))
c801d85f
KB
685#else
686 )
687#endif
3f4a0c5b
VZ
688 return wxString(tcp + 1);
689 } /* while */
c2ff79b1 690#if defined(__WXMSW__) || defined(__WXPM__)
223d09f6 691 if (wxIsalpha (*path) && *(path + 1) == wxT(':'))
3f4a0c5b 692 return wxString(path + 2);
c801d85f
KB
693#endif
694 }
dface61c
JS
695 // Yes, this should return the path, not an empty string, otherwise
696 // we get "thing.txt" -> "".
697 return path1;
c801d85f
KB
698}
699
700// Return just the directory, or NULL if no directory
50920146
OK
701wxChar *
702wxPathOnly (wxChar *path)
c801d85f
KB
703{
704 if (path && *path)
705 {
50920146 706 static wxChar buf[_MAXPATHLEN];
c801d85f
KB
707
708 // Local copy
50920146 709 wxStrcpy (buf, path);
c801d85f 710
50920146 711 int l = wxStrlen(path);
c801d85f
KB
712 bool done = FALSE;
713
714 int i = l - 1;
715
716 // Search backward for a backward or forward slash
717 while (!done && i > -1)
718 {
719 // ] is for VMS
223d09f6 720 if (path[i] == wxT('/') || path[i] == wxT('\\') || path[i] == wxT(']'))
c801d85f
KB
721 {
722 done = TRUE;
723#ifdef __VMS__
a339970a
VZ
724 if ( path[i] == wxT(']') )
725 buf[i+1] = 0;
726 else
727#endif
c801d85f 728 buf[i] = 0;
c801d85f
KB
729
730 return buf;
731 }
732 else i --;
733 }
734
c2ff79b1 735#if defined(__WXMSW__) || defined(__WXPM__)
c801d85f 736 // Try Drive specifier
223d09f6 737 if (wxIsalpha (buf[0]) && buf[1] == wxT(':'))
3f4a0c5b
VZ
738 {
739 // A:junk --> A:. (since A:.\junk Not A:\junk)
223d09f6
KB
740 buf[2] = wxT('.');
741 buf[3] = wxT('\0');
3f4a0c5b
VZ
742 return buf;
743 }
c801d85f
KB
744#endif
745 }
746
50920146 747 return (wxChar *) NULL;
c801d85f
KB
748}
749
750// Return just the directory, or NULL if no directory
751wxString wxPathOnly (const wxString& path)
752{
223d09f6 753 if (path != wxT(""))
c801d85f 754 {
50920146 755 wxChar buf[_MAXPATHLEN];
c801d85f
KB
756
757 // Local copy
50920146 758 wxStrcpy (buf, WXSTRINGCAST path);
c801d85f
KB
759
760 int l = path.Length();
761 bool done = FALSE;
762
763 int i = l - 1;
764
765 // Search backward for a backward or forward slash
766 while (!done && i > -1)
767 {
768 // ] is for VMS
223d09f6 769 if (path[i] == wxT('/') || path[i] == wxT('\\') || path[i] == wxT(']'))
c801d85f
KB
770 {
771 done = TRUE;
772#ifdef __VMS__
a339970a
VZ
773 if ( path[i] == wxT(']') )
774 buf[i+1] = 0;
775 else
776#endif
c801d85f 777 buf[i] = 0;
c801d85f
KB
778
779 return wxString(buf);
780 }
781 else i --;
782 }
783
c2ff79b1 784#if defined(__WXMSW__) || defined(__WXPM__)
c801d85f 785 // Try Drive specifier
223d09f6 786 if (wxIsalpha (buf[0]) && buf[1] == wxT(':'))
3f4a0c5b
VZ
787 {
788 // A:junk --> A:. (since A:.\junk Not A:\junk)
223d09f6
KB
789 buf[2] = wxT('.');
790 buf[3] = wxT('\0');
3f4a0c5b
VZ
791 return wxString(buf);
792 }
c801d85f
KB
793#endif
794 }
795
223d09f6 796 return wxString(wxT(""));
c801d85f
KB
797}
798
799// Utility for converting delimiters in DOS filenames to UNIX style
800// and back again - or we get nasty problems with delimiters.
801// Also, convert to lower case, since case is significant in UNIX.
802
03e11df5 803#if defined(__WXMAC__) && !defined(__UNIX__)
e7549107
SC
804
805static char sMacFileNameConversion[ 1000 ] ;
806
807wxString wxMac2UnixFilename (const char *str)
17dff81c 808{
f6bcfd97
BP
809 char *s = sMacFileNameConversion ;
810 strcpy( s , str ) ;
e7549107
SC
811 if (s)
812 {
813 memmove( s+1 , s ,strlen( s ) + 1) ;
814 if ( *s == ':' )
815 *s = '.' ;
816 else
817 *s = '/' ;
818
819 while (*s)
820 {
821 if (*s == ':')
822 *s = '/';
823 else
824 *s = wxTolower (*s); // Case INDEPENDENT
825 s++;
826 }
827 }
828 return wxString (sMacFileNameConversion) ;
17dff81c
SC
829}
830
e7549107 831wxString wxUnix2MacFilename (const char *str)
17dff81c 832{
f6bcfd97
BP
833 char *s = sMacFileNameConversion ;
834 strcpy( s , str ) ;
e7549107
SC
835 if (s)
836 {
837 if ( *s == '.' )
838 {
839 // relative path , since it goes on with slash which is translated to a :
840 memmove( s , s+1 ,strlen( s ) ) ;
841 }
842 else if ( *s == '/' )
843 {
844 // absolute path -> on mac just start with the drive name
845 memmove( s , s+1 ,strlen( s ) ) ;
846 }
847 else
848 {
849 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
850 }
851 while (*s)
852 {
853 if (*s == '/' || *s == '\\')
854 {
f6bcfd97
BP
855 // convert any back-directory situations
856 if ( *(s+1) == '.' && *(s+2) == '.' && ( (*(s+3) == '/' || *(s+3) == '\\') ) )
857 {
e7549107 858 *s = ':';
f6bcfd97
BP
859 memmove( s+1 , s+3 ,strlen( s+3 ) + 1 ) ;
860 }
861 else
e7549107
SC
862 *s = ':';
863 }
864
865 s++ ;
866 }
867 }
868 return wxString (sMacFileNameConversion) ;
17dff81c 869}
e7549107 870
f28538c5 871wxString wxMacFSSpec2MacFilename( const FSSpec *spec )
e7549107 872{
f6bcfd97
BP
873 Handle myPath ;
874 short length ;
875
876 FSpGetFullPath( spec , &length , &myPath ) ;
877 ::SetHandleSize( myPath , length + 1 ) ;
878 ::HLock( myPath ) ;
879 (*myPath)[length] = 0 ;
880 if ( length > 0 && (*myPath)[length-1] ==':' )
881 (*myPath)[length-1] = 0 ;
882
883 wxString result( (char*) *myPath ) ;
884 ::HUnlock( myPath ) ;
885 ::DisposeHandle( myPath ) ;
886 return result ;
e7549107
SC
887}
888
889wxString wxMacFSSpec2UnixFilename( const FSSpec *spec )
890{
f6bcfd97 891 return wxMac2UnixFilename( wxMacFSSpec2MacFilename( spec) ) ;
e7549107
SC
892}
893
894void wxMacFilename2FSSpec( const char *path , FSSpec *spec )
895{
f6bcfd97 896 FSpLocationFromFullPath( strlen(path ) , path , spec ) ;
e7549107
SC
897}
898
899void wxUnixFilename2FSSpec( const char *path , FSSpec *spec )
900{
f6bcfd97
BP
901 wxString var = wxUnix2MacFilename( path ) ;
902 wxMacFilename2FSSpec( var , spec ) ;
e7549107
SC
903}
904
17dff81c 905#endif
3f4a0c5b 906void
e7549107 907wxDos2UnixFilename (char *s)
c801d85f
KB
908{
909 if (s)
910 while (*s)
911 {
e7549107
SC
912 if (*s == '\\')
913 *s = '/';
914#ifdef __WXMSW__
3f4a0c5b 915 else
e7549107 916 *s = wxTolower (*s); // Case INDEPENDENT
c801d85f 917#endif
3f4a0c5b 918 s++;
c801d85f
KB
919 }
920}
921
3f4a0c5b 922void
f28538c5 923#if defined(__WXMSW__) || defined(__WXPM__)
5b735202 924wxUnix2DosFilename (wxChar *s)
46dc76ba 925#else
5b735202 926wxUnix2DosFilename (wxChar *WXUNUSED(s) )
46dc76ba 927#endif
c801d85f
KB
928{
929// Yes, I really mean this to happen under DOS only! JACS
c2ff79b1 930#if defined(__WXMSW__) || defined(__WXPM__)
c801d85f
KB
931 if (s)
932 while (*s)
933 {
223d09f6
KB
934 if (*s == wxT('/'))
935 *s = wxT('\\');
3f4a0c5b 936 s++;
c801d85f
KB
937 }
938#endif
939}
940
941// Concatenate two files to form third
3f4a0c5b 942bool
c801d85f
KB
943wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& file3)
944{
a339970a
VZ
945 wxString outfile;
946 if ( !wxGetTempFileName("cat", outfile) )
947 return FALSE;
c801d85f 948
c67daf87
UR
949 FILE *fp1 = (FILE *) NULL;
950 FILE *fp2 = (FILE *) NULL;
951 FILE *fp3 = (FILE *) NULL;
c801d85f 952 // Open the inputs and outputs
a339970a
VZ
953 if ((fp1 = fopen (OS_FILENAME( file1 ), "rb")) == NULL ||
954 (fp2 = fopen (OS_FILENAME( file2 ), "rb")) == NULL ||
955 (fp3 = fopen (OS_FILENAME( outfile ), "wb")) == NULL)
c801d85f
KB
956 {
957 if (fp1)
3f4a0c5b 958 fclose (fp1);
c801d85f 959 if (fp2)
3f4a0c5b 960 fclose (fp2);
c801d85f 961 if (fp3)
3f4a0c5b 962 fclose (fp3);
c801d85f
KB
963 return FALSE;
964 }
965
966 int ch;
967 while ((ch = getc (fp1)) != EOF)
968 (void) putc (ch, fp3);
969 fclose (fp1);
970
971 while ((ch = getc (fp2)) != EOF)
972 (void) putc (ch, fp3);
973 fclose (fp2);
974
975 fclose (fp3);
976 bool result = wxRenameFile(outfile, file3);
c801d85f
KB
977 return result;
978}
979
980// Copy files
3f4a0c5b 981bool
4658c44e 982wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite)
c801d85f 983{
4658c44e
VZ
984#if defined(__WIN32__)
985 // CopyFile() copies file attributes and modification time too, so use it
986 // instead of our code if available
987 //
988 // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite
7a656506 989 return ::CopyFile(file1, file2, !overwrite) != 0;
4658c44e 990#else // !Win32
32f31043
VZ
991 wxStructStat fbuf;
992
993 // get permissions of file1
c3396917 994 if ( wxStat(OS_FILENAME(file1), &fbuf) != 0 )
32f31043
VZ
995 {
996 // the file probably doesn't exist or we haven't the rights to read
997 // from it anyhow
998 wxLogSysError(_("Impossible to get permissions for file '%s'"),
999 file1.c_str());
1000 return FALSE;
1001 }
1002
1003 // open file1 for reading
1004 wxFile fileIn(file1, wxFile::read);
a339970a
VZ
1005 if ( !fileIn.IsOpened() )
1006 return FALSE;
c801d85f 1007
32f31043
VZ
1008 // remove file2, if it exists. This is needed for creating
1009 // file2 with the correct permissions in the next step
4658c44e 1010 if ( wxFileExists(file2) && (!overwrite || !wxRemoveFile(file2)))
32f31043
VZ
1011 {
1012 wxLogSysError(_("Impossible to overwrite the file '%s'"),
1013 file2.c_str());
1014 return FALSE;
1015 }
1016
1017#ifdef __UNIX__
1018 // reset the umask as we want to create the file with exactly the same
1019 // permissions as the original one
1020 mode_t oldUmask = umask( 0 );
1021#endif // __UNIX__
1022
1023 // create file2 with the same permissions than file1 and open it for
1024 // writing
1025 wxFile fileOut;
4658c44e 1026 if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) )
a339970a 1027 return FALSE;
c801d85f 1028
32f31043
VZ
1029#ifdef __UNIX__
1030 /// restore the old umask
1031 umask(oldUmask);
1032#endif // __UNIX__
1033
1034 // copy contents of file1 to file2
a339970a
VZ
1035 char buf[4096];
1036 size_t count;
1037 for ( ;; )
c7386783 1038 {
a339970a
VZ
1039 count = fileIn.Read(buf, WXSIZEOF(buf));
1040 if ( fileIn.Error() )
1041 return FALSE;
1042
1043 // end of file?
1044 if ( !count )
1045 break;
1046
1047 if ( fileOut.Write(buf, count) < count )
1048 return FALSE;
c7386783 1049 }
c801d85f 1050
abb74e97
VZ
1051 // we can expect fileIn to be closed successfully, but we should ensure
1052 // that fileOut was closed as some write errors (disk full) might not be
1053 // detected before doing this
1054 if ( !fileIn.Close() || !fileOut.Close() )
1055 return FALSE;
1056
5fde6fcc 1057#if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__)
abb74e97
VZ
1058 // no chmod in VA. Should be some permission API for HPFS386 partitions
1059 // however
c3396917 1060 if ( chmod(OS_FILENAME(file2), fbuf.st_mode) != 0 )
32f31043
VZ
1061 {
1062 wxLogSysError(_("Impossible to set permissions for the file '%s'"),
1063 file2.c_str());
1064 return FALSE;
1065 }
4658c44e
VZ
1066#endif // OS/2 || Mac
1067
a339970a 1068 return TRUE;
4658c44e 1069#endif // __WXMSW__ && __WIN32__
c801d85f
KB
1070}
1071
3f4a0c5b 1072bool
c801d85f
KB
1073wxRenameFile (const wxString& file1, const wxString& file2)
1074{
1075 // Normal system call
c3396917 1076 if ( wxRename (file1, file2) == 0 )
c801d85f 1077 return TRUE;
a339970a 1078
c801d85f
KB
1079 // Try to copy
1080 if (wxCopyFile(file1, file2)) {
1081 wxRemoveFile(file1);
1082 return TRUE;
1083 }
1084 // Give up
1085 return FALSE;
1086}
1087
1088bool wxRemoveFile(const wxString& file)
1089{
3f4a0c5b 1090#if defined(__VISUALC__) || defined(__BORLANDC__) || defined(__WATCOMC__)
a339970a 1091 int res = wxRemove(file);
c801d85f 1092#else
a339970a 1093 int res = unlink(OS_FILENAME(file));
c801d85f 1094#endif
a339970a
VZ
1095
1096 return res == 0;
c801d85f
KB
1097}
1098
1a33c3ba 1099bool wxMkdir(const wxString& dir, int perm)
c801d85f 1100{
5fde6fcc 1101#if defined(__WXMAC__) && !defined(__UNIX__)
e7549107 1102 return (mkdir(wxUnix2MacFilename( dir ) , 0 ) == 0);
7708abe9 1103#else // !Mac
50920146 1104 const wxChar *dirname = dir.c_str();
1a33c3ba 1105
c2ff79b1 1106 // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too
7708abe9 1107 // for the GNU compiler
f6bcfd97 1108#if (!(defined(__WXMSW__) || defined(__WXPM__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WXWINE__)
50920146 1109 if ( mkdir(wxFNCONV(dirname), perm) != 0 )
f6bcfd97
BP
1110#elif defined(__WXPM__)
1111 if (::DosCreateDir((PSZ)dirname, NULL) != 0) // enhance for EAB's??
6fdbbb00 1112#else // !MSW and !OS/2 VAC++
33ac7e6f 1113 (void)perm;
f6bcfd97 1114 if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 )
7708abe9 1115#endif // !MSW/MSW
1a33c3ba
VZ
1116 {
1117 wxLogSysError(_("Directory '%s' couldn't be created"), dirname);
1118
1119 return FALSE;
1120 }
1121
1122 return TRUE;
e7549107 1123#endif // Mac/!Mac
c801d85f
KB
1124}
1125
1126bool wxRmdir(const wxString& dir, int WXUNUSED(flags))
1127{
1128#ifdef __VMS__
acdc3b6b 1129 return FALSE; //to be changed since rmdir exists in VMS7.x
f6bcfd97
BP
1130#elif defined(__WXPM__)
1131 return (::DosDeleteDir((PSZ)dir.c_str()) == 0);
c801d85f 1132#else
a3ef5bf5
JS
1133
1134#ifdef __SALFORDC__
1135 return FALSE; // What to do?
1136#else
a339970a 1137 return (wxRmDir(OS_FILENAME(dir)) == 0);
c801d85f
KB
1138#endif
1139
1140#endif
1141}
1142
c801d85f 1143// does the path exists? (may have or not '/' or '\\' at the end)
50920146 1144bool wxPathExists(const wxChar *pszPathName)
c801d85f 1145{
f6bcfd97
BP
1146 wxString strPath(pszPathName);
1147#ifdef __WINDOWS__
1148 // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists,
1149 // so remove all trailing backslashes from the path - but don't do this for
1150 // the pathes "d:\" (which are different from "d:") nor for just "\"
1151 while ( wxEndsWithPathSeparator(strPath) )
1152 {
1153 size_t len = strPath.length();
1154 if ( len == 1 || strPath[len - 1] == _T(':') )
1155 break;
c801d85f 1156
f6bcfd97
BP
1157 strPath.Truncate(len - 1);
1158 }
1159#endif // __WINDOWS__
1160
1161 wxStructStat st;
71c97a89 1162#ifndef __VISAGECPP__
f6bcfd97
BP
1163 return wxStat(wxFNSTRINGCAST strPath.fn_str(), &st) == 0 &&
1164 ((st.st_mode & S_IFMT) == S_IFDIR);
71c97a89
DW
1165#else
1166 // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only
1167 return wxStat(wxFNSTRINGCAST strPath.fn_str(), &st) == 0 &&
1168 (st.st_mode == S_IFDIR);
1169#endif
1170
c801d85f
KB
1171}
1172
1173// Get a temporary filename, opening and closing the file.
50920146 1174wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf)
c801d85f 1175{
34138703 1176#ifdef __WINDOWS__
c801d85f 1177
3f4a0c5b 1178#ifndef __WIN32__
50920146 1179 wxChar tmp[144];
c801d85f
KB
1180 ::GetTempFileName(0, WXSTRINGCAST prefix, 0, tmp);
1181#else
50920146
OK
1182 wxChar tmp[MAX_PATH];
1183 wxChar tmpPath[MAX_PATH];
c801d85f
KB
1184 ::GetTempPath(MAX_PATH, tmpPath);
1185 ::GetTempFileName(tmpPath, WXSTRINGCAST prefix, 0, tmp);
1186#endif
50920146 1187 if (buf) wxStrcpy(buf, tmp);
c801d85f
KB
1188 else buf = copystring(tmp);
1189 return buf;
1190
1191#else
3f4a0c5b 1192 static short last_temp = 0; // cache last to speed things a bit
c801d85f 1193 // At most 1000 temp files to a process! We use a ring count.
50920146 1194 wxChar tmp[100]; // FIXME static buffer
c801d85f
KB
1195
1196 for (short suffix = last_temp + 1; suffix != last_temp; ++suffix %= 1000)
1197 {
223d09f6 1198 wxSprintf (tmp, wxT("/tmp/%s%d.%03x"), WXSTRINGCAST prefix, (int) getpid (), (int) suffix);
c801d85f 1199 if (!wxFileExists( tmp ))
3f4a0c5b
VZ
1200 {
1201 // Touch the file to create it (reserve name)
50920146 1202 FILE *fd = fopen (wxFNCONV(tmp), "w");
3f4a0c5b
VZ
1203 if (fd)
1204 fclose (fd);
1205 last_temp = suffix;
c801d85f 1206 if (buf)
50920146 1207 wxStrcpy( buf, tmp);
3f4a0c5b
VZ
1208 else
1209 buf = copystring( tmp );
1210 return buf;
1211 }
c801d85f 1212 }
f5abe911 1213 wxLogError( _("wxWindows: error finding temporary file name.\n") );
c801d85f 1214 if (buf) buf[0] = 0;
50920146 1215 return (wxChar *) NULL;
c801d85f
KB
1216#endif
1217}
1218
c0ab6adf
JS
1219bool wxGetTempFileName(const wxString& prefix, wxString& buf)
1220{
1221 wxChar buf2[512];
1222 if (wxGetTempFileName(prefix, buf2) != (wxChar*) NULL)
1223 {
1224 buf = buf2;
1225 return TRUE;
1226 }
1227 else
1228 return FALSE;
1229}
1230
c801d85f
KB
1231// Get first file name matching given wild card.
1232
1233#ifdef __UNIX__
1234
1235// Get first file name matching given wild card.
1236// Flags are reserved for future use.
1237
acdc3b6b 1238#if !defined( __VMS__ ) || ( __VMS_VER >= 70000000 )
7af89395
VZ
1239 static DIR *gs_dirStream = (DIR *) NULL;
1240 static wxString gs_strFileSpec;
1241 static int gs_findFlags = 0;
c801d85f
KB
1242#endif
1243
50920146 1244wxString wxFindFirstFile(const wxChar *spec, int flags)
c801d85f 1245{
7af89395 1246 wxString result;
03c046d1
JJ
1247#ifdef __VMS
1248 wxChar *specvms = NULL;
1249#endif
a339970a 1250
acdc3b6b 1251#if !defined( __VMS__ ) || ( __VMS_VER >= 70000000 )
7af89395
VZ
1252 if (gs_dirStream)
1253 closedir(gs_dirStream); // edz 941103: better housekeping
c801d85f 1254
7af89395 1255 gs_findFlags = flags;
c801d85f 1256
7af89395 1257 gs_strFileSpec = spec;
c801d85f 1258
7af89395
VZ
1259 // Find path only so we can concatenate
1260 // found file onto path
1261 wxString path(wxPathOnly(gs_strFileSpec));
c801d85f 1262
7af89395 1263 // special case: path is really "/"
223d09f6 1264 if ( !path && gs_strFileSpec[0u] == wxT('/') )
03c046d1
JJ
1265#ifdef __VMS
1266 {
a339970a
VZ
1267 wxStrcpy( specvms , wxT( "[000000]" ) );
1268 gs_strFileSpec = specvms;
1269 wxString path_vms(wxPathOnly(gs_strFileSpec));
1270 path = path_vms;
03c046d1
JJ
1271 }
1272#else
1273 path = wxT('/');
1274#endif
1275 // path is empty => Local directory
7af89395 1276 if ( !path )
03c046d1
JJ
1277#ifdef __VMS
1278 {
a339970a
VZ
1279 wxStrcpy( specvms , wxT( "[]" ) );
1280 gs_strFileSpec = specvms;
1281 wxString path_vms1(wxPathOnly(gs_strFileSpec));
1282 path = path_vms1;
03c046d1
JJ
1283 }
1284#else
1285 path = wxT('.');
1286#endif
a339970a 1287
50920146 1288 gs_dirStream = opendir(path.fn_str());
7af89395
VZ
1289 if ( !gs_dirStream )
1290 {
1291 wxLogSysError(_("Can not enumerate files in directory '%s'"),
1292 path.c_str());
1293 }
1294 else
1295 {
1296 result = wxFindNextFile();
1297 }
acdc3b6b 1298#endif // !VMS6.x or earlier
c801d85f 1299
7af89395 1300 return result;
c801d85f
KB
1301}
1302
7af89395 1303wxString wxFindNextFile()
c801d85f 1304{
7af89395 1305 wxString result;
c801d85f 1306
acdc3b6b 1307#if !defined( __VMS__ ) || ( __VMS_VER >= 70000000 )
223d09f6 1308 wxCHECK_MSG( gs_dirStream, result, wxT("must call wxFindFirstFile first") );
7af89395
VZ
1309
1310 // Find path only so we can concatenate
1311 // found file onto path
1312 wxString path(wxPathOnly(gs_strFileSpec));
1313 wxString name(wxFileNameFromPath(gs_strFileSpec));
1314
1315 /* MATTHEW: special case: path is really "/" */
223d09f6
KB
1316 if ( !path && gs_strFileSpec[0u] == wxT('/'))
1317 path = wxT('/');
7af89395
VZ
1318
1319 // Do the reading
1320 struct dirent *nextDir;
1321 for ( nextDir = readdir(gs_dirStream);
1322 nextDir != NULL;
1323 nextDir = readdir(gs_dirStream) )
1324 {
bf9e3e73 1325 if (wxMatchWild(name, nextDir->d_name, FALSE) && // RR: added FALSE to find hidden files
f6bcfd97
BP
1326 strcmp(nextDir->d_name, ".") &&
1327 strcmp(nextDir->d_name, "..") )
7af89395
VZ
1328 {
1329 result.Empty();
1330 if ( !path.IsEmpty() )
1331 {
1332 result = path;
223d09f6
KB
1333 if ( path != wxT('/') )
1334 result += wxT('/');
7af89395 1335 }
c801d85f 1336
7af89395 1337 result += nextDir->d_name;
c801d85f 1338
7af89395
VZ
1339 // Only return "." and ".." when they match
1340 bool isdir;
1341 if ( (strcmp(nextDir->d_name, ".") == 0) ||
1342 (strcmp(nextDir->d_name, "..") == 0))
1343 {
1344 if ( (gs_findFlags & wxDIR) != 0 )
1345 isdir = TRUE;
1346 else
1347 continue;
1348 }
1349 else
1350 isdir = wxDirExists(result);
c801d85f 1351
7af89395
VZ
1352 // and only return directories when flags & wxDIR
1353 if ( !gs_findFlags ||
1354 ((gs_findFlags & wxDIR) && isdir) ||
1355 ((gs_findFlags & wxFILE) && !isdir) )
1356 {
1357 return result;
1358 }
1359 }
1360 }
c801d85f 1361
7af89395 1362 result.Empty(); // not found
c801d85f 1363
7af89395
VZ
1364 closedir(gs_dirStream);
1365 gs_dirStream = (DIR *) NULL;
acdc3b6b 1366#endif // !VMS6.2 or earlier
c801d85f 1367
7af89395 1368 return result;
c801d85f
KB
1369}
1370
7c74e7fe
SC
1371#elif defined(__WXMAC__)
1372
1373struct MacDirectoryIterator
1374{
f6bcfd97
BP
1375 CInfoPBRec m_CPB ;
1376 wxInt16 m_index ;
1377 long m_dirId ;
1378 Str255 m_name ;
7c74e7fe
SC
1379} ;
1380
1381static int g_iter_flags ;
1382
1383static MacDirectoryIterator g_iter ;
1384
1385wxString wxFindFirstFile(const wxChar *spec, int flags)
1386{
1387 wxString result;
1388
1389 g_iter_flags = flags; /* MATTHEW: [5] Remember flags */
1390
1391 // Find path only so we can concatenate found file onto path
1392 wxString path(wxPathOnly(spec));
1393 if ( !path.IsEmpty() )
1394 result << path << wxT('\\');
1395
f6bcfd97
BP
1396 FSSpec fsspec ;
1397
1398 wxUnixFilename2FSSpec( result , &fsspec ) ;
1399 g_iter.m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ;
1400 g_iter.m_CPB.hFileInfo.ioNamePtr = g_iter.m_name ;
1401 g_iter.m_index = 0 ;
1402
1403 Boolean isDir ;
1404 FSpGetDirectoryID( &fsspec , &g_iter.m_dirId , &isDir ) ;
1405 if ( !isDir )
1406 return wxEmptyString ;
1407
1408 return wxFindNextFile( ) ;
7c74e7fe
SC
1409}
1410
1411wxString wxFindNextFile()
1412{
1413 wxString result;
1414
f6bcfd97
BP
1415 short err = noErr ;
1416
1417 while ( err == noErr )
1418 {
1419 g_iter.m_index++ ;
1420 g_iter.m_CPB.dirInfo.ioFDirIndex = g_iter.m_index;
1421 g_iter.m_CPB.dirInfo.ioDrDirID = g_iter.m_dirId; /* we need to do this every time */
1422 err = PBGetCatInfoSync((CInfoPBPtr)&g_iter.m_CPB);
1423 if ( err != noErr )
1424 break ;
1425
1426 if ( ( g_iter.m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (g_iter_flags & wxDIR) ) // we have a directory
1427 break ;
1428
1429 if ( ( g_iter.m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(g_iter_flags & wxFILE ) )
1430 continue ;
1431
1432 // hit !
1433 break ;
1434 }
1435 if ( err != noErr )
1436 {
1437 return wxEmptyString ;
1438 }
1439 FSSpec spec ;
1440
1441 FSMakeFSSpecCompat(g_iter.m_CPB.hFileInfo.ioVRefNum,
1442 g_iter.m_dirId,
1443 g_iter.m_name,
1444 &spec) ;
1445
1446 return wxMacFSSpec2UnixFilename( &spec ) ;
7c74e7fe
SC
1447}
1448
2049ba38 1449#elif defined(__WXMSW__)
c801d85f
KB
1450
1451#ifdef __WIN32__
7af89395
VZ
1452 static HANDLE gs_hFileStruct = INVALID_HANDLE_VALUE;
1453 static WIN32_FIND_DATA gs_findDataStruct;
1454#else // Win16
1455 #ifdef __BORLANDC__
1456 static struct ffblk gs_findDataStruct;
1457 #else
1458 static struct _find_t gs_findDataStruct;
1459 #endif // Borland
1460#endif // Win32/16
1461
1462static wxString gs_strFileSpec;
1463static int gs_findFlags = 0;
1464
50920146 1465wxString wxFindFirstFile(const wxChar *spec, int flags)
c801d85f 1466{
7af89395
VZ
1467 wxString result;
1468
1469 gs_strFileSpec = spec;
1470 gs_findFlags = flags; /* MATTHEW: [5] Remember flags */
1471
1472 // Find path only so we can concatenate found file onto path
1473 wxString path(wxPathOnly(gs_strFileSpec));
1474 if ( !path.IsEmpty() )
223d09f6 1475 result << path << wxT('\\');
c801d85f
KB
1476
1477#ifdef __WIN32__
7af89395
VZ
1478 if ( gs_hFileStruct != INVALID_HANDLE_VALUE )
1479 FindClose(gs_hFileStruct);
c801d85f 1480
7af89395 1481 gs_hFileStruct = ::FindFirstFile(WXSTRINGCAST spec, &gs_findDataStruct);
c801d85f 1482
7af89395
VZ
1483 if ( gs_hFileStruct == INVALID_HANDLE_VALUE )
1484 {
7af89395 1485 result.Empty();
c801d85f 1486
7af89395
VZ
1487 return result;
1488 }
c801d85f 1489
7af89395
VZ
1490 bool isdir = !!(gs_findDataStruct.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1491
1492 if (isdir && !(flags & wxDIR))
1493 return wxFindNextFile();
1494 else if (!isdir && flags && !(flags & wxFILE))
1495 return wxFindNextFile();
c801d85f 1496
7af89395
VZ
1497 result += gs_findDataStruct.cFileName;
1498
1499 return result;
f6bcfd97 1500#else // !Win32
7af89395 1501 int flag = _A_NORMAL;
f6bcfd97 1502 if (flags & wxDIR)
7af89395 1503 flag = _A_SUBDIR;
c801d85f
KB
1504
1505#ifdef __BORLANDC__
7af89395 1506 if (findfirst(WXSTRINGCAST spec, &gs_findDataStruct, flag) == 0)
c801d85f 1507#else
7af89395 1508 if (_dos_findfirst(WXSTRINGCAST spec, flag, &gs_findDataStruct) == 0)
c801d85f 1509#endif
7af89395 1510 {
7af89395 1511 char attrib;
c801d85f
KB
1512
1513#ifdef __BORLANDC__
7af89395 1514 attrib = gs_findDataStruct.ff_attrib;
c801d85f 1515#else
7af89395 1516 attrib = gs_findDataStruct.attrib;
c801d85f
KB
1517#endif
1518
7af89395
VZ
1519 if (attrib & _A_SUBDIR) {
1520 if (!(gs_findFlags & wxDIR))
1521 return wxFindNextFile();
1522 } else if (gs_findFlags && !(gs_findFlags & wxFILE))
3f4a0c5b 1523 return wxFindNextFile();
c801d85f 1524
7af89395 1525 result +=
c801d85f 1526#ifdef __BORLANDC__
7af89395 1527 gs_findDataStruct.ff_name
c801d85f 1528#else
7af89395 1529 gs_findDataStruct.name
c801d85f 1530#endif
7af89395
VZ
1531 ;
1532 }
7af89395
VZ
1533
1534 return result;
f6bcfd97 1535#endif // __WIN32__
c801d85f
KB
1536}
1537
32e768ae 1538
7af89395 1539wxString wxFindNextFile()
c801d85f 1540{
7af89395 1541 wxString result;
3f4a0c5b 1542
7af89395
VZ
1543 // Find path only so we can concatenate found file onto path
1544 wxString path(wxPathOnly(gs_strFileSpec));
1545
1546try_again:
c801d85f
KB
1547
1548#ifdef __WIN32__
7af89395 1549 if (gs_hFileStruct == INVALID_HANDLE_VALUE)
59fcb8f8 1550 return result;
c801d85f 1551
7af89395
VZ
1552 bool success = (FindNextFile(gs_hFileStruct, &gs_findDataStruct) != 0);
1553 if (!success)
1554 {
1555 FindClose(gs_hFileStruct);
1556 gs_hFileStruct = INVALID_HANDLE_VALUE;
1557 }
1558 else
1559 {
1560 bool isdir = !!(gs_findDataStruct.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
c801d85f 1561
7af89395
VZ
1562 if (isdir && !(gs_findFlags & wxDIR))
1563 goto try_again;
1564 else if (!isdir && gs_findFlags && !(gs_findFlags & wxFILE))
1565 goto try_again;
c801d85f 1566
7af89395 1567 if ( !path.IsEmpty() )
223d09f6 1568 result << path << wxT('\\');
7af89395
VZ
1569 result << gs_findDataStruct.cFileName;
1570 }
1571
1572 return result;
1573#else // Win16
c801d85f
KB
1574
1575#ifdef __BORLANDC__
7af89395 1576 if (findnext(&gs_findDataStruct) == 0)
c801d85f 1577#else
7af89395 1578 if (_dos_findnext(&gs_findDataStruct) == 0)
c801d85f 1579#endif
7af89395
VZ
1580 {
1581 /* MATTHEW: [5] Check directory flag */
1582 char attrib;
c801d85f
KB
1583
1584#ifdef __BORLANDC__
7af89395 1585 attrib = gs_findDataStruct.ff_attrib;
c801d85f 1586#else
7af89395 1587 attrib = gs_findDataStruct.attrib;
c801d85f
KB
1588#endif
1589
7af89395
VZ
1590 if (attrib & _A_SUBDIR) {
1591 if (!(gs_findFlags & wxDIR))
1592 goto try_again;
1593 } else if (gs_findFlags && !(gs_findFlags & wxFILE))
1594 goto try_again;
c801d85f
KB
1595
1596
7af89395 1597 result +=
c801d85f 1598#ifdef __BORLANDC__
7af89395 1599 gs_findDataStruct.ff_name
c801d85f 1600#else
7af89395 1601 gs_findDataStruct.name
c801d85f 1602#endif
7af89395
VZ
1603 ;
1604 }
7af89395
VZ
1605
1606 return result;
f6bcfd97 1607#endif // Win32/16
c801d85f
KB
1608}
1609
32e768ae
DW
1610#elif defined(__WXPM__)
1611
1612wxString wxFindFirstFile(const wxChar *spec, int flags)
1613{
1614 wxString result;
1615
1616 /*
1617 // TODO: figure something out here for OS/2
1618 gs_strFileSpec = spec;
1619 gs_findFlags = flags;
1620
1621 // Find path only so we can concatenate found file onto path
1622 wxString path(wxPathOnly(gs_strFileSpec));
1623 if ( !path.IsEmpty() )
1624 result << path << wxT('\\');
1625
1626 int flag = _A_NORMAL;
1627 if (flags & wxDIR)
1628 flag = _A_SUBDIR;
1629
1630 if (_dos_findfirst(WXSTRINGCAST spec, flag, &gs_findDataStruct) == 0)
1631 {
1632 char attrib;
1633 attrib = gs_findDataStruct.attrib;
1634
1635 if (attrib & _A_SUBDIR) {
1636 if (!(gs_findFlags & wxDIR))
1637 return wxFindNextFile();
1638 } else if (gs_findFlags && !(gs_findFlags & wxFILE))
1639 return wxFindNextFile();
1640
1641 result += gs_findDataStruct.name;
1642 }
1643 */
1644 return result;
1645}
1646
1647wxString wxFindNextFile()
1648{
1649 wxString result;
1650 // TODO:
1651 return result;
1652}
1653
1654#endif // Unix/Windows/OS/2
c801d85f
KB
1655
1656// Get current working directory.
1657// If buf is NULL, allocates space using new, else
1658// copies into buf.
50920146 1659wxChar *wxGetWorkingDirectory(wxChar *buf, int sz)
c801d85f
KB
1660{
1661 if (!buf)
50920146
OK
1662 buf = new wxChar[sz+1];
1663#if wxUSE_UNICODE
1664 char *cbuf = new char[sz+1];
1665#ifdef _MSC_VER
1666 if (_getcwd(cbuf, sz) == NULL) {
5fde6fcc 1667#elif defined(__WXMAC__) && !defined(__UNIX__)
f6bcfd97
BP
1668 enum
1669 {
1670 SFSaveDisk = 0x214, CurDirStore = 0x398
1671 };
1672 FSSpec cwdSpec ;
1673
1674 FSMakeFSSpec( - *(short *) SFSaveDisk , *(long *) CurDirStore , NULL , &cwdSpec ) ;
1675 wxString res = wxMacFSSpec2UnixFilename( &cwdSpec ) ;
1676 strcpy( buf , res ) ;
1677 if (0) {
50920146
OK
1678#else
1679 if (getcwd(cbuf, sz) == NULL) {
1680#endif
1681 delete [] cbuf;
7c74e7fe 1682#else // wxUnicode
3f4a0c5b 1683#ifdef _MSC_VER
c801d85f 1684 if (_getcwd(buf, sz) == NULL) {
5fde6fcc 1685#elif defined(__WXMAC__) && !defined(__UNIX__)
5b781a67
SC
1686 FSSpec cwdSpec ;
1687 FCBPBRec pb;
1688 OSErr error;
1689 Str255 fileName ;
1690 pb.ioNamePtr = (StringPtr) &fileName;
1691 pb.ioVRefNum = 0;
1692 pb.ioRefNum = LMGetCurApRefNum();
1693 pb.ioFCBIndx = 0;
1694 error = PBGetFCBInfoSync(&pb);
33ac7e6f 1695 if ( error == noErr )
5b781a67
SC
1696 {
1697 cwdSpec.vRefNum = pb.ioFCBVRefNum;
1698 cwdSpec.parID = pb.ioFCBParID;
1699 cwdSpec.name[0] = 0 ;
1700 wxString res = wxMacFSSpec2UnixFilename( &cwdSpec ) ;
1701
1702 strcpy( buf , res ) ;
1703 buf[res.length()-1]=0 ;
1704 }
1705 else
1706 buf[0] = 0 ;
1707 /*
1708 this version will not always give back the application directory on mac
33ac7e6f
KB
1709 enum
1710 {
1711 SFSaveDisk = 0x214, CurDirStore = 0x398
5b781a67
SC
1712 };
1713 FSSpec cwdSpec ;
1714
1715 FSMakeFSSpec( - *(short *) SFSaveDisk , *(long *) CurDirStore , NULL , &cwdSpec ) ;
1716 wxString res = wxMacFSSpec2UnixFilename( &cwdSpec ) ;
1717 strcpy( buf , res ) ;
1718 */
1719 if (0) {
f6bcfd97
BP
1720#elif(__VISAGECPP__)
1721 APIRET rc;
1722 rc = ::DosQueryCurrentDir( 0 // current drive
1723 ,buf
1724 ,(PULONG)&sz
1725 );
1726 if (rc != 0) {
c801d85f
KB
1727#else
1728 if (getcwd(buf, sz) == NULL) {
1729#endif
50920146 1730#endif
223d09f6
KB
1731 buf[0] = wxT('.');
1732 buf[1] = wxT('\0');
50920146
OK
1733 }
1734#if wxUSE_UNICODE
1735 else {
dcf924a3 1736 wxConvFile.MB2WC(buf, cbuf, sz);
50920146 1737 delete [] cbuf;
c801d85f 1738 }
50920146 1739#endif
c801d85f
KB
1740 return buf;
1741}
1742
7af89395
VZ
1743wxString wxGetCwd()
1744{
eac2aeb0
VZ
1745 static const size_t maxPathLen = 1024;
1746
1747 wxString str;
1748 wxGetWorkingDirectory(str.GetWriteBuf(maxPathLen), maxPathLen);
1749 str.UngetWriteBuf();
1750
1751 return str;
7af89395
VZ
1752}
1753
c801d85f
KB
1754bool wxSetWorkingDirectory(const wxString& d)
1755{
f6bcfd97 1756#if defined( __UNIX__ ) || defined( __WXMAC__ )
e90c1d2a 1757 return (chdir(wxFNSTRINGCAST d.fn_str()) == 0);
f6bcfd97
BP
1758#elif defined(__WXPM__)
1759 return (::DosSetCurrentDir((PSZ)d.c_str()) == 0);
34138703 1760#elif defined(__WINDOWS__)
c801d85f
KB
1761
1762#ifdef __WIN32__
1763 return (bool)(SetCurrentDirectory(d) != 0);
1764#else
1765 // Must change drive, too.
1766 bool isDriveSpec = ((strlen(d) > 1) && (d[1] == ':'));
1767 if (isDriveSpec)
1768 {
50920146 1769 wxChar firstChar = d[0];
c801d85f
KB
1770
1771 // To upper case
1772 if (firstChar > 90)
1773 firstChar = firstChar - 32;
1774
1775 // To a drive number
1776 unsigned int driveNo = firstChar - 64;
1777 if (driveNo > 0)
1778 {
1779 unsigned int noDrives;
1780 _dos_setdrive(driveNo, &noDrives);
1781 }
1782 }
1783 bool success = (chdir(WXSTRINGCAST d) == 0);
1784
1785 return success;
1786#endif
1787
1788#endif
1789}
1790
631f1bfe
JS
1791// Get the OS directory if appropriate (such as the Windows directory).
1792// On non-Windows platform, probably just return the empty string.
1793wxString wxGetOSDirectory()
1794{
1795#ifdef __WINDOWS__
50920146 1796 wxChar buf[256];
631f1bfe
JS
1797 GetWindowsDirectory(buf, 256);
1798 return wxString(buf);
1799#else
1800 return wxEmptyString;
1801#endif
1802}
1803
a907da15 1804bool wxEndsWithPathSeparator(const wxChar *pszFileName)
c801d85f 1805{
a907da15 1806 size_t len = wxStrlen(pszFileName);
c801d85f
KB
1807 if ( len == 0 )
1808 return FALSE;
1809 else
1810 return wxIsPathSeparator(pszFileName[len - 1]);
1811}
1812
1813// find a file in a list of directories, returns false if not found
50920146 1814bool wxFindFileInPath(wxString *pStr, const wxChar *pszPath, const wxChar *pszFile)
c801d85f 1815{
a17e237f
VZ
1816 // we assume that it's not empty
1817 wxCHECK_MSG( !wxIsEmpty(pszFile), FALSE,
f6bcfd97 1818 _T("empty file name in wxFindFileInPath"));
a17e237f
VZ
1819
1820 // skip path separator in the beginning of the file name if present
1821 if ( wxIsPathSeparator(*pszFile) )
1822 pszFile++;
1823
1824 // copy the path (strtok will modify it)
1825 wxChar *szPath = new wxChar[wxStrlen(pszPath) + 1];
1826 wxStrcpy(szPath, pszPath);
1827
1828 wxString strFile;
1829 wxChar *pc, *save_ptr;
1830 for ( pc = wxStrtok(szPath, wxPATH_SEP, &save_ptr);
1831 pc != NULL;
1832 pc = wxStrtok((wxChar *) NULL, wxPATH_SEP, &save_ptr) )
1833 {
1834 // search for the file in this directory
1835 strFile = pc;
1836 if ( !wxEndsWithPathSeparator(pc) )
1837 strFile += wxFILE_SEP_PATH;
1838 strFile += pszFile;
1839
1840 if ( FileExists(strFile) ) {
1841 *pStr = strFile;
1842 break;
1843 }
c801d85f 1844 }
c801d85f 1845
a17e237f
VZ
1846 // suppress warning about unused variable save_ptr when wxStrtok() is a
1847 // macro which throws away its third argument
1848 save_ptr = pc;
1849
1850 delete [] szPath;
c801d85f 1851
a17e237f 1852 return pc != NULL; // if true => we breaked from the loop
c801d85f
KB
1853}
1854
50920146 1855void WXDLLEXPORT wxSplitPath(const wxChar *pszFileName,
3826db3e
VZ
1856 wxString *pstrPath,
1857 wxString *pstrName,
1858 wxString *pstrExt)
1859{
d37fd2fa 1860 // it can be empty, but it shouldn't be NULL
223d09f6 1861 wxCHECK_RET( pszFileName, wxT("NULL file name in wxSplitPath") );
3826db3e 1862
9e8d8607 1863 wxFileName::SplitPath(pszFileName, pstrPath, pstrName, pstrExt);
3826db3e 1864}
7f555861 1865
a47ce4a7
VS
1866time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename)
1867{
f6bcfd97 1868 wxStructStat buf;
a47ce4a7 1869
f6bcfd97 1870 wxStat(filename.fn_str(), &buf);
a47ce4a7
VS
1871 return buf.st_mtime;
1872}
1873
1874
7f555861
JS
1875//------------------------------------------------------------------------
1876// wild character routines
1877//------------------------------------------------------------------------
1878
1879bool wxIsWild( const wxString& pattern )
1880{
1881 wxString tmp = pattern;
50920146 1882 wxChar *pat = WXSTRINGCAST(tmp);
7f555861 1883 while (*pat) {
3f4a0c5b 1884 switch (*pat++) {
223d09f6 1885 case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'):
3f4a0c5b 1886 return TRUE;
223d09f6 1887 case wxT('\\'):
3f4a0c5b
VZ
1888 if (!*pat++)
1889 return FALSE;
1890 }
7f555861
JS
1891 }
1892 return FALSE;
1893};
1894
b112d152 1895bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
99cc00ed 1896
7be1f0d9 1897#if defined(HAVE_FNMATCH_H)
dfcb1ae0 1898{
50920146 1899// this probably won't work well for multibyte chars in Unicode mode?
b112d152 1900 if(dot_special)
50920146 1901 return fnmatch(pat.fn_str(), text.fn_str(), FNM_PERIOD) == 0;
b112d152 1902 else
50920146 1903 return fnmatch(pat.fn_str(), text.fn_str(), 0) == 0;
dfcb1ae0
KB
1904}
1905#else
1906
7be1f0d9
JS
1907// #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1908
dfcb1ae0
KB
1909 /*
1910 * WARNING: this code is broken!
1911 */
7f555861
JS
1912{
1913 wxString tmp1 = pat;
50920146 1914 wxChar *pattern = WXSTRINGCAST(tmp1);
7f555861 1915 wxString tmp2 = text;
50920146
OK
1916 wxChar *str = WXSTRINGCAST(tmp2);
1917 wxChar c;
1918 wxChar *cp;
7f555861
JS
1919 bool done = FALSE, ret_code, ok;
1920 // Below is for vi fans
223d09f6 1921 const wxChar OB = wxT('{'), CB = wxT('}');
7f555861
JS
1922
1923 // dot_special means '.' only matches '.'
223d09f6 1924 if (dot_special && *str == wxT('.') && *pattern != *str)
3f4a0c5b 1925 return FALSE;
7f555861 1926
223d09f6
KB
1927 while ((*pattern != wxT('\0')) && (!done)
1928 && (((*str==wxT('\0'))&&((*pattern==OB)||(*pattern==wxT('*'))))||(*str!=wxT('\0')))) {
3f4a0c5b 1929 switch (*pattern) {
223d09f6 1930 case wxT('\\'):
3f4a0c5b 1931 pattern++;
223d09f6 1932 if (*pattern != wxT('\0'))
3f4a0c5b
VZ
1933 pattern++;
1934 break;
223d09f6 1935 case wxT('*'):
3f4a0c5b
VZ
1936 pattern++;
1937 ret_code = FALSE;
223d09f6 1938 while ((*str!=wxT('\0'))
33ac7e6f 1939 && ((ret_code=wxMatchWild(pattern, str++, FALSE)) == 0))
3f4a0c5b
VZ
1940 /*loop*/;
1941 if (ret_code) {
223d09f6 1942 while (*str != wxT('\0'))
3f4a0c5b 1943 str++;
223d09f6 1944 while (*pattern != wxT('\0'))
3f4a0c5b
VZ
1945 pattern++;
1946 }
1947 break;
223d09f6 1948 case wxT('['):
3f4a0c5b
VZ
1949 pattern++;
1950 repeat:
223d09f6 1951 if ((*pattern == wxT('\0')) || (*pattern == wxT(']'))) {
3f4a0c5b
VZ
1952 done = TRUE;
1953 break;
1954 }
223d09f6 1955 if (*pattern == wxT('\\')) {
3f4a0c5b 1956 pattern++;
223d09f6 1957 if (*pattern == wxT('\0')) {
3f4a0c5b
VZ
1958 done = TRUE;
1959 break;
1960 }
1961 }
223d09f6 1962 if (*(pattern + 1) == wxT('-')) {
3f4a0c5b
VZ
1963 c = *pattern;
1964 pattern += 2;
223d09f6 1965 if (*pattern == wxT(']')) {
3f4a0c5b
VZ
1966 done = TRUE;
1967 break;
1968 }
223d09f6 1969 if (*pattern == wxT('\\')) {
3f4a0c5b 1970 pattern++;
223d09f6 1971 if (*pattern == wxT('\0')) {
3f4a0c5b
VZ
1972 done = TRUE;
1973 break;
1974 }
1975 }
1976 if ((*str < c) || (*str > *pattern)) {
1977 pattern++;
1978 goto repeat;
1979 }
1980 } else if (*pattern != *str) {
1981 pattern++;
1982 goto repeat;
1983 }
1984 pattern++;
223d09f6
KB
1985 while ((*pattern != wxT(']')) && (*pattern != wxT('\0'))) {
1986 if ((*pattern == wxT('\\')) && (*(pattern + 1) != wxT('\0')))
3f4a0c5b
VZ
1987 pattern++;
1988 pattern++;
1989 }
223d09f6 1990 if (*pattern != wxT('\0')) {
3f4a0c5b
VZ
1991 pattern++, str++;
1992 }
1993 break;
223d09f6 1994 case wxT('?'):
3f4a0c5b
VZ
1995 pattern++;
1996 str++;
1997 break;
1998 case OB:
1999 pattern++;
223d09f6 2000 while ((*pattern != CB) && (*pattern != wxT('\0'))) {
3f4a0c5b
VZ
2001 cp = str;
2002 ok = TRUE;
223d09f6
KB
2003 while (ok && (*cp != wxT('\0')) && (*pattern != wxT('\0'))
2004 && (*pattern != wxT(',')) && (*pattern != CB)) {
2005 if (*pattern == wxT('\\'))
3f4a0c5b
VZ
2006 pattern++;
2007 ok = (*pattern++ == *cp++);
2008 }
223d09f6 2009 if (*pattern == wxT('\0')) {
3f4a0c5b
VZ
2010 ok = FALSE;
2011 done = TRUE;
2012 break;
2013 } else if (ok) {
2014 str = cp;
223d09f6
KB
2015 while ((*pattern != CB) && (*pattern != wxT('\0'))) {
2016 if (*++pattern == wxT('\\')) {
3f4a0c5b
VZ
2017 if (*++pattern == CB)
2018 pattern++;
2019 }
2020 }
2021 } else {
223d09f6
KB
2022 while (*pattern!=CB && *pattern!=wxT(',') && *pattern!=wxT('\0')) {
2023 if (*++pattern == wxT('\\')) {
2024 if (*++pattern == CB || *pattern == wxT(','))
3f4a0c5b
VZ
2025 pattern++;
2026 }
2027 }
2028 }
223d09f6 2029 if (*pattern != wxT('\0'))
3f4a0c5b
VZ
2030 pattern++;
2031 }
2032 break;
2033 default:
2034 if (*str == *pattern) {
2035 str++, pattern++;
2036 } else {
2037 done = TRUE;
2038 }
2039 }
7f555861 2040 }
223d09f6 2041 while (*pattern == wxT('*'))
3f4a0c5b 2042 pattern++;
223d09f6 2043 return ((*str == wxT('\0')) && (*pattern == wxT('\0')));
7f555861 2044};
fd3f686c 2045
dfcb1ae0 2046#endif
7f555861 2047
3f4a0c5b 2048#ifdef __VISUALC__
fd3f686c 2049 #pragma warning(default:4706) // assignment within conditional expression
53c6e7cc 2050#endif // VC++