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