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