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