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