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