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