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