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