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