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