]> git.saurik.com Git - wxWidgets.git/blame - src/common/filefn.cpp
reverted, the problem is deeper than that
[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
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
e90c1d2a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
7af89395 21 #pragma implementation "filefn.h"
c801d85f
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26#include "wx/defs.h"
27
28#ifdef __BORLANDC__
7af89395 29 #pragma hdrstop
c801d85f
KB
30#endif
31
c801d85f 32#include "wx/utils.h"
3096bd2f 33#include "wx/intl.h"
6294ac2e 34#include "wx/file.h" // This does include filefn.h
9e8d8607 35#include "wx/filename.h"
8ff12342 36#include "wx/dir.h"
c801d85f 37
fd3f686c 38// there are just too many of those...
3f4a0c5b 39#ifdef __VISUALC__
fd3f686c
VZ
40 #pragma warning(disable:4706) // assignment within conditional expression
41#endif // VC++
42
c801d85f
KB
43#include <ctype.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#if !defined(__WATCOMC__)
3f4a0c5b
VZ
48 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
49 #include <errno.h>
50 #endif
c801d85f 51#endif
3f4a0c5b 52
76a5e5d2 53#if defined(__WXMAC__)
28301089 54 #include "wx/mac/private.h" // includes mac headers
76a5e5d2
SC
55#endif
56
f5abe911 57#include "wx/log.h"
99cc00ed 58
7be1f0d9
JS
59// No, Cygwin doesn't appear to have fnmatch.h after all.
60#if defined(HAVE_FNMATCH_H)
7af89395 61 #include "fnmatch.h"
dfcb1ae0
KB
62#endif
63
34138703 64#ifdef __WINDOWS__
9ed0d735 65 #include "wx/msw/wrapwin.h"
3d5231db 66 #include "wx/msw/mslu.h"
b0091f58 67
3ffbc733
VZ
68 // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path()
69 //
70 // note that it must be included after <windows.h>
71 #ifdef __GNUWIN32__
e02e8816
MB
72 #ifdef __CYGWIN__
73 #include <sys/cygwin.h>
74 #endif
3ffbc733
VZ
75 #endif // __GNUWIN32__
76#endif // __WINDOWS__
c801d85f 77
13b1f8a7
VZ
78// TODO: Borland probably has _wgetcwd as well?
79#ifdef _MSC_VER
80 #define HAVE_WGETCWD
81#endif
82
e90c1d2a
VZ
83// ----------------------------------------------------------------------------
84// constants
85// ----------------------------------------------------------------------------
86
13b1f8a7
VZ
87#ifndef _MAXPATHLEN
88 #define _MAXPATHLEN 1024
89#endif
c801d85f 90
da2b4b7a 91#ifdef __WXMAC__
2d4e4f80 92# include "MoreFilesX.h"
17dff81c 93#endif
c801d85f 94
e90c1d2a
VZ
95// ----------------------------------------------------------------------------
96// private globals
97// ----------------------------------------------------------------------------
98
1f1070e2 99// MT-FIXME: get rid of this horror and all code using it
e90c1d2a
VZ
100static wxChar wxFileFunctionsBuffer[4*_MAXPATHLEN];
101
5d33ed2c
DW
102#if defined(__VISAGECPP__) && __IBMCPP__ >= 400
103//
32e768ae 104// VisualAge C++ V4.0 cannot have any external linkage const decs
5d33ed2c
DW
105// in headers included by more than one primary source
106//
30984dea 107const int wxInvalidOffset = -1;
5d33ed2c
DW
108#endif
109
a339970a
VZ
110// ----------------------------------------------------------------------------
111// macros
112// ----------------------------------------------------------------------------
113
114// we need to translate Mac filenames before passing them to OS functions
334d2448 115#define OS_FILENAME(s) (s.fn_str())
a339970a 116
e90c1d2a
VZ
117// ============================================================================
118// implementation
119// ============================================================================
120
92980e90
RR
121#ifdef wxNEED_WX_UNISTD_H
122
123WXDLLEXPORT int wxStat( const wxChar *file_name, wxStructStat *buf )
124{
125 return stat( wxConvFile.cWX2MB( file_name ), buf );
126}
127
128WXDLLEXPORT int wxAccess( const wxChar *pathname, int mode )
129{
130 return access( wxConvFile.cWX2MB( pathname ), mode );
131}
132
133WXDLLEXPORT int wxOpen( const wxChar *pathname, int flags, mode_t mode )
134{
135 return open( wxConvFile.cWX2MB( pathname ), flags, mode );
136}
137
138#endif
139 // wxNEED_WX_UNISTD_H
140
141// ----------------------------------------------------------------------------
142// wxPathList
143// ----------------------------------------------------------------------------
144
df5168c4 145// IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList)
92980e90 146
f526f752
MB
147static inline wxChar* MYcopystring(const wxString& s)
148{
149 wxChar* copy = new wxChar[s.length() + 1];
150 return wxStrcpy(copy, s.c_str());
151}
152
153static inline wxChar* MYcopystring(const wxChar* s)
154{
155 wxChar* copy = new wxChar[wxStrlen(s) + 1];
156 return wxStrcpy(copy, s);
157}
158
c801d85f
KB
159void wxPathList::Add (const wxString& path)
160{
fd82f4e6 161 wxStringList::Add (WXSTRINGCAST path);
c801d85f
KB
162}
163
164// Add paths e.g. from the PATH environment variable
165void wxPathList::AddEnvList (const wxString& envVariable)
166{
1c193821
JS
167 // No environment variables on WinCE
168#ifndef __WXWINCE__
db4444f0 169 static const wxChar PATH_TOKS[] =
5a3912f2 170#if defined(__WINDOWS__) || defined(__OS2__)
db4444f0
CE
171 /*
172 The space has been removed from the tokenizers, otherwise a
173 path such as "C:\Program Files" would be split into 2 paths:
174 "C:\Program" and "Files"
175 */
176// wxT(" ;"); // Don't seperate with colon in DOS (used for drive)
177 wxT(";"); // Don't seperate with colon in DOS (used for drive)
c801d85f 178#else
223d09f6 179 wxT(" :;");
c801d85f
KB
180#endif
181
db4444f0
CE
182 wxChar *val = wxGetenv (WXSTRINGCAST envVariable);
183 if (val && *val)
c801d85f 184 {
f526f752 185 wxChar *s = MYcopystring (val);
db4444f0
CE
186 wxChar *save_ptr, *token = wxStrtok (s, PATH_TOKS, &save_ptr);
187
188 if (token)
189 {
190 Add(token);
191 while (token)
192 {
193 if ( (token = wxStrtok ((wxChar *) NULL, PATH_TOKS, &save_ptr))
194 != NULL )
195 {
196 Add(token);
197 }
198 }
199 }
200
201 // suppress warning about unused variable save_ptr when wxStrtok() is a
202 // macro which throws away its third argument
203 save_ptr = token;
204
205 delete [] s;
c801d85f 206 }
1c193821 207#endif
c801d85f
KB
208}
209
210// Given a full filename (with path), ensure that that file can
211// be accessed again USING FILENAME ONLY by adding the path
212// to the list if not already there.
213void wxPathList::EnsureFileAccessible (const wxString& path)
214{
7af89395
VZ
215 wxString path_only(wxPathOnly(path));
216 if ( !path_only.IsEmpty() )
217 {
218 if ( !Member(path_only) )
219 Add(path_only);
220 }
c801d85f
KB
221}
222
223bool wxPathList::Member (const wxString& path)
224{
df5168c4 225 for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext())
c801d85f 226 {
b1d4dd7a 227 wxString path2( node->GetData() );
c801d85f 228 if (
5a3912f2 229#if defined(__WINDOWS__) || defined(__OS2__) || defined(__VMS__) || defined (__WXMAC__)
c801d85f 230 // Case INDEPENDENT
3f4a0c5b 231 path.CompareTo (path2, wxString::ignoreCase) == 0
c801d85f 232#else
3f4a0c5b
VZ
233 // Case sensitive File System
234 path.CompareTo (path2) == 0
c801d85f 235#endif
3f4a0c5b 236 )
79e162f5 237 return true;
c801d85f 238 }
79e162f5 239 return false;
c801d85f
KB
240}
241
242wxString wxPathList::FindValidPath (const wxString& file)
243{
e90c1d2a
VZ
244 if (wxFileExists (wxExpandPath(wxFileFunctionsBuffer, file)))
245 return wxString(wxFileFunctionsBuffer);
c801d85f 246
50920146 247 wxChar buf[_MAXPATHLEN];
e90c1d2a 248 wxStrcpy(buf, wxFileFunctionsBuffer);
c801d85f 249
999836aa 250 wxChar *filename = wxIsAbsolutePath (buf) ? wxFileNameFromPath (buf) : (wxChar *)buf;
c801d85f 251
df5168c4 252 for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext())
c801d85f 253 {
df5168c4 254 const wxChar *path = node->GetData();
e90c1d2a
VZ
255 wxStrcpy (wxFileFunctionsBuffer, path);
256 wxChar ch = wxFileFunctionsBuffer[wxStrlen(wxFileFunctionsBuffer)-1];
223d09f6
KB
257 if (ch != wxT('\\') && ch != wxT('/'))
258 wxStrcat (wxFileFunctionsBuffer, wxT("/"));
e90c1d2a 259 wxStrcat (wxFileFunctionsBuffer, filename);
34138703 260#ifdef __WINDOWS__
2b5f62a0 261 wxUnix2DosFilename (wxFileFunctionsBuffer);
c801d85f 262#endif
e90c1d2a 263 if (wxFileExists (wxFileFunctionsBuffer))
c801d85f 264 {
e90c1d2a 265 return wxString(wxFileFunctionsBuffer); // Found!
c801d85f 266 }
3f4a0c5b 267 } // for()
c801d85f 268
b1d4dd7a 269 return wxEmptyString; // Not found
c801d85f
KB
270}
271
272wxString wxPathList::FindAbsoluteValidPath (const wxString& file)
273{
e90c1d2a 274 wxString f = FindValidPath(file);
fc447c7f 275 if ( f.empty() || wxIsAbsolutePath(f) )
e90c1d2a
VZ
276 return f;
277
278 wxString buf;
13b1f8a7
VZ
279 wxGetWorkingDirectory(wxStringBuffer(buf, _MAXPATHLEN), _MAXPATHLEN);
280
e90c1d2a 281 if ( !wxEndsWithPathSeparator(buf) )
c801d85f 282 {
e90c1d2a 283 buf += wxFILE_SEP_PATH;
c801d85f 284 }
e90c1d2a
VZ
285 buf += f;
286
287 return buf;
c801d85f
KB
288}
289
3f4a0c5b 290bool
c801d85f
KB
291wxFileExists (const wxString& filename)
292{
4ea2c29f
VZ
293 // we must use GetFileAttributes() instead of the ANSI C functions because
294 // it can cope with network (UNC) paths unlike them
2db300c6 295#if defined(__WIN32__) && !defined(__WXMICROWIN__)
2db300c6 296 DWORD ret = ::GetFileAttributes(filename);
2db300c6 297
a28ae409 298 return (ret != (DWORD)-1) && !(ret & FILE_ATTRIBUTE_DIRECTORY);
4ea2c29f
VZ
299#else // !__WIN32__
300 wxStructStat st;
f3660dcb
SC
301#ifndef wxNEED_WX_UNISTD_H
302 return wxStat( filename.fn_str() , &st) == 0 && (st.st_mode & S_IFREG);
303#else
304 return wxStat( filename , &st) == 0 && (st.st_mode & S_IFREG);
305#endif
4ea2c29f 306#endif // __WIN32__/!__WIN32__
c801d85f
KB
307}
308
3f4a0c5b 309bool
c801d85f
KB
310wxIsAbsolutePath (const wxString& filename)
311{
2985ad5d
RR
312 if (filename != wxT(""))
313 {
e8e1d09e
GD
314#if defined(__WXMAC__) && !defined(__DARWIN__)
315 // Classic or Carbon CodeWarrior like
316 // Carbon with Apple DevTools is Unix like
2db300c6 317
2985ad5d
RR
318 // This seems wrong to me, but there is no fix. since
319 // "MacOS:MyText.txt" is absolute whereas "MyDir:MyText.txt"
320 // is not. Or maybe ":MyDir:MyText.txt" has to be used? RR.
2985ad5d 321 if (filename.Find(':') != wxNOT_FOUND && filename[0] != ':')
79e162f5 322 return true ;
cd378f94 323#else
e8e1d09e
GD
324 // Unix like or Windows
325 if (filename[0] == wxT('/'))
79e162f5 326 return true;
e8e1d09e 327#endif
c801d85f 328#ifdef __VMS__
e8e1d09e 329 if ((filename[0] == wxT('[') && filename[1] != wxT('.')))
79e162f5 330 return true;
c801d85f 331#endif
5a3912f2 332#if defined(__WINDOWS__) || defined(__OS2__)
e8e1d09e
GD
333 // MSDOS like
334 if (filename[0] == wxT('\\') || (wxIsalpha (filename[0]) && filename[1] == wxT(':')))
79e162f5 335 return true;
c801d85f 336#endif
c801d85f 337 }
79e162f5 338 return false ;
c801d85f
KB
339}
340
341/*
342 * Strip off any extension (dot something) from end of file,
343 * IF one exists. Inserts zero into buffer.
344 *
345 */
3f4a0c5b 346
50920146 347void wxStripExtension(wxChar *buffer)
c801d85f 348{
50920146 349 int len = wxStrlen(buffer);
c801d85f
KB
350 int i = len-1;
351 while (i > 0)
352 {
223d09f6 353 if (buffer[i] == wxT('.'))
c801d85f
KB
354 {
355 buffer[i] = 0;
356 break;
357 }
358 i --;
359 }
360}
361
47fa7969
JS
362void wxStripExtension(wxString& buffer)
363{
364 size_t len = buffer.Length();
365 size_t i = len-1;
366 while (i > 0)
367 {
223d09f6 368 if (buffer.GetChar(i) == wxT('.'))
47fa7969
JS
369 {
370 buffer = buffer.Left(i);
371 break;
372 }
373 i --;
374 }
375}
376
c801d85f 377// Destructive removal of /./ and /../ stuff
50920146 378wxChar *wxRealPath (wxChar *path)
c801d85f 379{
2049ba38 380#ifdef __WXMSW__
223d09f6 381 static const wxChar SEP = wxT('\\');
2b5f62a0 382 wxUnix2DosFilename(path);
c801d85f 383#else
223d09f6 384 static const wxChar SEP = wxT('/');
c801d85f
KB
385#endif
386 if (path[0] && path[1]) {
387 /* MATTHEW: special case "/./x" */
50920146 388 wxChar *p;
223d09f6 389 if (path[2] == SEP && path[1] == wxT('.'))
c801d85f
KB
390 p = &path[0];
391 else
392 p = &path[2];
393 for (; *p; p++)
394 {
3f4a0c5b
VZ
395 if (*p == SEP)
396 {
223d09f6 397 if (p[1] == wxT('.') && p[2] == wxT('.') && (p[3] == SEP || p[3] == wxT('\0')))
3f4a0c5b 398 {
50920146 399 wxChar *q;
f0e1c343
JS
400 for (q = p - 1; q >= path && *q != SEP; q--)
401 {
402 // Empty
403 }
404
223d09f6 405 if (q[0] == SEP && (q[1] != wxT('.') || q[2] != wxT('.') || q[3] != SEP)
3f4a0c5b
VZ
406 && (q - 1 <= path || q[-1] != SEP))
407 {
50920146 408 wxStrcpy (q, p + 3);
223d09f6 409 if (path[0] == wxT('\0'))
3f4a0c5b
VZ
410 {
411 path[0] = SEP;
223d09f6 412 path[1] = wxT('\0');
3f4a0c5b 413 }
5a3912f2 414#if defined(__WXMSW__) || defined(__OS2__)
3f4a0c5b 415 /* Check that path[2] is NULL! */
223d09f6 416 else if (path[1] == wxT(':') && !path[2])
3f4a0c5b
VZ
417 {
418 path[2] = SEP;
223d09f6 419 path[3] = wxT('\0');
3f4a0c5b
VZ
420 }
421#endif
422 p = q - 1;
423 }
424 }
223d09f6 425 else if (p[1] == wxT('.') && (p[2] == SEP || p[2] == wxT('\0')))
50920146 426 wxStrcpy (p, p + 2);
3f4a0c5b 427 }
c801d85f
KB
428 }
429 }
430 return path;
431}
432
433// Must be destroyed
50920146 434wxChar *wxCopyAbsolutePath(const wxString& filename)
c801d85f 435{
223d09f6 436 if (filename == wxT(""))
50920146 437 return (wxChar *) NULL;
c801d85f 438
2b5f62a0 439 if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer, filename))) {
50920146 440 wxChar buf[_MAXPATHLEN];
223d09f6 441 buf[0] = wxT('\0');
7af89395 442 wxGetWorkingDirectory(buf, WXSIZEOF(buf));
50920146 443 wxChar ch = buf[wxStrlen(buf) - 1];
2049ba38 444#ifdef __WXMSW__
223d09f6
KB
445 if (ch != wxT('\\') && ch != wxT('/'))
446 wxStrcat(buf, wxT("\\"));
c801d85f 447#else
223d09f6
KB
448 if (ch != wxT('/'))
449 wxStrcat(buf, wxT("/"));
c801d85f 450#endif
e90c1d2a 451 wxStrcat(buf, wxFileFunctionsBuffer);
f526f752 452 return MYcopystring( wxRealPath(buf) );
c801d85f 453 }
f526f752 454 return MYcopystring( wxFileFunctionsBuffer );
c801d85f
KB
455}
456
457/*-
458 Handles:
459 ~/ => home dir
460 ~user/ => user's home dir
461 If the environment variable a = "foo" and b = "bar" then:
462 Unix:
3f4a0c5b
VZ
463 $a => foo
464 $a$b => foobar
465 $a.c => foo.c
466 xxx$a => xxxfoo
467 ${a}! => foo!
468 $(b)! => bar!
469 \$a => \$a
c801d85f 470 MSDOS:
3f4a0c5b
VZ
471 $a ==> $a
472 $(a) ==> foo
473 $(a)$b ==> foo$b
474 $(a)$(b)==> foobar
475 test.$$ ==> test.$$
c801d85f
KB
476 */
477
478/* input name in name, pathname output to buf. */
479
50920146 480wxChar *wxExpandPath(wxChar *buf, const wxChar *name)
c801d85f 481{
50920146
OK
482 register wxChar *d, *s, *nm;
483 wxChar lnm[_MAXPATHLEN];
33ac7e6f 484 int q;
c801d85f
KB
485
486 // Some compilers don't like this line.
223d09f6 487// const wxChar trimchars[] = wxT("\n \t");
c801d85f 488
50920146 489 wxChar trimchars[4];
223d09f6
KB
490 trimchars[0] = wxT('\n');
491 trimchars[1] = wxT(' ');
492 trimchars[2] = wxT('\t');
c801d85f
KB
493 trimchars[3] = 0;
494
2049ba38 495#ifdef __WXMSW__
a62848fd 496 const wxChar SEP = wxT('\\');
c801d85f 497#else
a62848fd 498 const wxChar SEP = wxT('/');
c801d85f 499#endif
223d09f6
KB
500 buf[0] = wxT('\0');
501 if (name == NULL || *name == wxT('\0'))
3f4a0c5b 502 return buf;
f526f752 503 nm = MYcopystring(name); // Make a scratch copy
50920146 504 wxChar *nm_tmp = nm;
c801d85f
KB
505
506 /* Skip leading whitespace and cr */
50920146 507 while (wxStrchr((wxChar *)trimchars, *nm) != NULL)
3f4a0c5b 508 nm++;
c801d85f 509 /* And strip off trailing whitespace and cr */
50920146
OK
510 s = nm + (q = wxStrlen(nm)) - 1;
511 while (q-- && wxStrchr((wxChar *)trimchars, *s) != NULL)
223d09f6 512 *s = wxT('\0');
c801d85f
KB
513
514 s = nm;
515 d = lnm;
2049ba38 516#ifdef __WXMSW__
dc259b79 517 q = FALSE;
c801d85f 518#else
223d09f6 519 q = nm[0] == wxT('\\') && nm[1] == wxT('~');
c801d85f
KB
520#endif
521
522 /* Expand inline environment variables */
c2ff79b1
DW
523#ifdef __VISAGECPP__
524 while (*d)
525 {
526 *d++ = *s;
223d09f6 527 if(*s == wxT('\\'))
c2ff79b1
DW
528 {
529 *(d - 1) = *++s;
530 if (*d)
531 {
532 s++;
533 continue;
534 }
535 else
536 break;
537 }
538 else
539#else
22394d24 540 while ((*d++ = *s) != 0) {
c2ff79b1 541# ifndef __WXMSW__
223d09f6 542 if (*s == wxT('\\')) {
3f4a0c5b
VZ
543 if ((*(d - 1) = *++s)) {
544 s++;
545 continue;
546 } else
547 break;
548 } else
c2ff79b1 549# endif
c801d85f 550#endif
1c193821
JS
551 // No env variables on WinCE
552#ifndef __WXWINCE__
2049ba38 553#ifdef __WXMSW__
223d09f6 554 if (*s++ == wxT('$') && (*s == wxT('{') || *s == wxT(')')))
c801d85f 555#else
223d09f6 556 if (*s++ == wxT('$'))
3f4a0c5b
VZ
557#endif
558 {
50920146 559 register wxChar *start = d;
223d09f6 560 register int braces = (*s == wxT('{') || *s == wxT('('));
50920146 561 register wxChar *value;
22394d24 562 while ((*d++ = *s) != 0)
223d09f6 563 if (braces ? (*s == wxT('}') || *s == wxT(')')) : !(wxIsalnum(*s) || *s == wxT('_')) )
3f4a0c5b
VZ
564 break;
565 else
566 s++;
567 *--d = 0;
50920146 568 value = wxGetenv(braces ? start + 1 : start);
3f4a0c5b 569 if (value) {
f0e1c343
JS
570 for ((d = start - 1); (*d++ = *value++) != 0;)
571 {
572 // Empty
573 }
574
3f4a0c5b
VZ
575 d--;
576 if (braces && *s)
577 s++;
578 }
579 }
1c193821
JS
580#endif
581 // __WXWINCE__
c801d85f
KB
582 }
583
584 /* Expand ~ and ~user */
585 nm = lnm;
223d09f6 586 if (nm[0] == wxT('~') && !q)
c801d85f 587 {
3f4a0c5b
VZ
588 /* prefix ~ */
589 if (nm[1] == SEP || nm[1] == 0)
590 { /* ~/filename */
f6bcfd97 591 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
223d09f6 592 if ((s = WXSTRINGCAST wxGetUserHome(wxT(""))) != NULL) {
3f4a0c5b
VZ
593 if (*++nm)
594 nm++;
595 }
c801d85f 596 } else
3f4a0c5b 597 { /* ~user/filename */
50920146
OK
598 register wxChar *nnm;
599 register wxChar *home;
f0e1c343
JS
600 for (s = nm; *s && *s != SEP; s++)
601 {
602 // Empty
603 }
3f4a0c5b 604 int was_sep; /* MATTHEW: Was there a separator, or NULL? */
c801d85f 605 was_sep = (*s == SEP);
3f4a0c5b
VZ
606 nnm = *s ? s + 1 : s;
607 *s = 0;
f6bcfd97 608 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
0e6667e2 609 if ((home = WXSTRINGCAST wxGetUserHome(wxString(nm + 1))) == NULL) {
c801d85f 610 if (was_sep) /* replace only if it was there: */
3f4a0c5b 611 *s = SEP;
295272bd 612 s = NULL;
3f4a0c5b
VZ
613 } else {
614 nm = nnm;
615 s = home;
616 }
617 }
c801d85f
KB
618 }
619
620 d = buf;
621 if (s && *s) { /* MATTHEW: s could be NULL if user '~' didn't exist */
3f4a0c5b 622 /* Copy home dir */
223d09f6 623 while (wxT('\0') != (*d++ = *s++))
3f4a0c5b
VZ
624 /* loop */;
625 // Handle root home
626 if (d - 1 > buf && *(d - 2) != SEP)
627 *(d - 1) = SEP;
c801d85f
KB
628 }
629 s = nm;
f0e1c343
JS
630 while ((*d++ = *s++) != 0)
631 {
632 // Empty
633 }
c801d85f
KB
634 delete[] nm_tmp; // clean up alloc
635 /* Now clean up the buffer */
636 return wxRealPath(buf);
637}
638
c801d85f
KB
639/* Contract Paths to be build upon an environment variable
640 component:
641
642 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
643
644 The call wxExpandPath can convert these back!
645 */
50920146 646wxChar *
c801d85f
KB
647wxContractPath (const wxString& filename, const wxString& envname, const wxString& user)
648{
50920146 649 static wxChar dest[_MAXPATHLEN];
c801d85f 650
223d09f6 651 if (filename == wxT(""))
50920146 652 return (wxChar *) NULL;
c801d85f 653
50920146 654 wxStrcpy (dest, WXSTRINGCAST filename);
2049ba38 655#ifdef __WXMSW__
2b5f62a0 656 wxUnix2DosFilename(dest);
c801d85f
KB
657#endif
658
659 // Handle environment
999836aa 660 const wxChar *val;
1c193821 661#ifndef __WXWINCE__
999836aa 662 wxChar *tcp;
50920146
OK
663 if (envname != WXSTRINGCAST NULL && (val = wxGetenv (WXSTRINGCAST envname)) != NULL &&
664 (tcp = wxStrstr (dest, val)) != NULL)
c801d85f 665 {
e90c1d2a 666 wxStrcpy (wxFileFunctionsBuffer, tcp + wxStrlen (val));
223d09f6
KB
667 *tcp++ = wxT('$');
668 *tcp++ = wxT('{');
50920146 669 wxStrcpy (tcp, WXSTRINGCAST envname);
223d09f6 670 wxStrcat (tcp, wxT("}"));
e90c1d2a 671 wxStrcat (tcp, wxFileFunctionsBuffer);
c801d85f 672 }
1c193821 673#endif
c801d85f
KB
674
675 // Handle User's home (ignore root homes!)
844ca096
VZ
676 val = wxGetUserHome (user);
677 if (!val)
678 return dest;
679
680 const size_t len = wxStrlen(val);
681 if (len <= 2)
682 return dest;
683
684 if (wxStrncmp(dest, val, len) == 0)
685 {
686 wxStrcpy(wxFileFunctionsBuffer, wxT("~"));
687 if (user != wxT(""))
688 wxStrcat(wxFileFunctionsBuffer, (const wxChar*) user);
689 wxStrcat(wxFileFunctionsBuffer, dest + len);
690 wxStrcpy (dest, wxFileFunctionsBuffer);
691 }
c801d85f
KB
692
693 return dest;
694}
695
1f1070e2 696// Return just the filename, not the path (basename)
50920146 697wxChar *wxFileNameFromPath (wxChar *path)
c801d85f 698{
1f1070e2
VZ
699 wxString p = path;
700 wxString n = wxFileNameFromPath(p);
2db300c6 701
1f1070e2 702 return path + p.length() - n.length();
c801d85f
KB
703}
704
1f1070e2 705wxString wxFileNameFromPath (const wxString& path)
c801d85f 706{
1f1070e2
VZ
707 wxString name, ext;
708 wxFileName::SplitPath(path, NULL, &name, &ext);
2db300c6 709
1f1070e2
VZ
710 wxString fullname = name;
711 if ( !ext.empty() )
712 {
713 fullname << wxFILE_SEP_EXT << ext;
c801d85f 714 }
1f1070e2
VZ
715
716 return fullname;
c801d85f
KB
717}
718
719// Return just the directory, or NULL if no directory
50920146
OK
720wxChar *
721wxPathOnly (wxChar *path)
c801d85f 722{
e8e1d09e 723 if (path && *path)
c801d85f 724 {
e8e1d09e 725 static wxChar buf[_MAXPATHLEN];
2db300c6 726
e8e1d09e
GD
727 // Local copy
728 wxStrcpy (buf, path);
2db300c6 729
e8e1d09e
GD
730 int l = wxStrlen(path);
731 int i = l - 1;
2db300c6 732
e8e1d09e
GD
733 // Search backward for a backward or forward slash
734 while (i > -1)
735 {
736#if defined(__WXMAC__) && !defined(__DARWIN__)
737 // Classic or Carbon CodeWarrior like
738 // Carbon with Apple DevTools is Unix like
739 if (path[i] == wxT(':') )
740 {
741 buf[i] = 0;
742 return buf;
743 }
bedaf53e 744#else
e8e1d09e
GD
745 // Unix like or Windows
746 if (path[i] == wxT('/') || path[i] == wxT('\\'))
747 {
748 buf[i] = 0;
749 return buf;
750 }
bedaf53e 751#endif
c801d85f 752#ifdef __VMS__
e8e1d09e
GD
753 if (path[i] == wxT(']'))
754 {
755 buf[i+1] = 0;
756 return buf;
757 }
a339970a 758#endif
e8e1d09e 759 i --;
c801d85f 760 }
2db300c6 761
5a3912f2 762#if defined(__WXMSW__) || defined(__OS2__)
e8e1d09e
GD
763 // Try Drive specifier
764 if (wxIsalpha (buf[0]) && buf[1] == wxT(':'))
3f4a0c5b 765 {
e8e1d09e
GD
766 // A:junk --> A:. (since A:.\junk Not A:\junk)
767 buf[2] = wxT('.');
768 buf[3] = wxT('\0');
769 return buf;
3f4a0c5b 770 }
c801d85f
KB
771#endif
772 }
e8e1d09e 773 return (wxChar *) NULL;
c801d85f
KB
774}
775
776// Return just the directory, or NULL if no directory
777wxString wxPathOnly (const wxString& path)
778{
e8e1d09e 779 if (path != wxT(""))
c801d85f 780 {
e8e1d09e 781 wxChar buf[_MAXPATHLEN];
2db300c6 782
e8e1d09e
GD
783 // Local copy
784 wxStrcpy (buf, WXSTRINGCAST path);
2db300c6 785
e8e1d09e
GD
786 int l = path.Length();
787 int i = l - 1;
788
789 // Search backward for a backward or forward slash
790 while (i > -1)
791 {
792#if defined(__WXMAC__) && !defined(__DARWIN__)
793 // Classic or Carbon CodeWarrior like
794 // Carbon with Apple DevTools is Unix like
795 if (path[i] == wxT(':') )
796 {
797 buf[i] = 0;
798 return wxString(buf);
799 }
bedaf53e 800#else
e8e1d09e
GD
801 // Unix like or Windows
802 if (path[i] == wxT('/') || path[i] == wxT('\\'))
803 {
5c760b84
JS
804 // Don't return an empty string
805 if (i == 0)
806 i ++;
e8e1d09e
GD
807 buf[i] = 0;
808 return wxString(buf);
809 }
bedaf53e 810#endif
c801d85f 811#ifdef __VMS__
e8e1d09e
GD
812 if (path[i] == wxT(']'))
813 {
814 buf[i+1] = 0;
815 return wxString(buf);
816 }
a339970a 817#endif
e8e1d09e 818 i --;
c801d85f 819 }
2db300c6 820
5a3912f2 821#if defined(__WXMSW__) || defined(__OS2__)
e8e1d09e
GD
822 // Try Drive specifier
823 if (wxIsalpha (buf[0]) && buf[1] == wxT(':'))
3f4a0c5b 824 {
e8e1d09e
GD
825 // A:junk --> A:. (since A:.\junk Not A:\junk)
826 buf[2] = wxT('.');
827 buf[3] = wxT('\0');
828 return wxString(buf);
3f4a0c5b 829 }
c801d85f
KB
830#endif
831 }
e8e1d09e 832 return wxString(wxT(""));
c801d85f
KB
833}
834
835// Utility for converting delimiters in DOS filenames to UNIX style
836// and back again - or we get nasty problems with delimiters.
837// Also, convert to lower case, since case is significant in UNIX.
838
da2b4b7a 839#if defined(__WXMAC__)
b0091f58 840
a2b77260
SC
841#if TARGET_API_MAC_OSX
842#define kDefaultPathStyle kCFURLPOSIXPathStyle
a1c34a78 843#else
a2b77260 844#define kDefaultPathStyle kCFURLHFSPathStyle
a1c34a78
GD
845#endif
846
a62848fd 847wxString wxMacFSRefToPath( const FSRef *fsRef , CFStringRef additionalPathComponent )
a2b77260 848{
a62848fd 849 CFURLRef fullURLRef;
a2b77260
SC
850 fullURLRef = CFURLCreateFromFSRef(NULL, fsRef);
851 if ( additionalPathComponent )
852 {
853 CFURLRef parentURLRef = fullURLRef ;
854 fullURLRef = CFURLCreateCopyAppendingPathComponent(NULL, parentURLRef,
855 additionalPathComponent,false);
856 CFRelease( parentURLRef ) ;
857 }
a62848fd
WS
858 CFStringRef cfString = CFURLCopyFileSystemPath(fullURLRef, kDefaultPathStyle);
859 CFRelease( fullURLRef ) ;
860 return wxMacCFStringHolder(cfString).AsString(wxLocale::GetSystemEncoding());
bedaf53e 861}
e7549107 862
a62848fd 863OSStatus wxMacPathToFSRef( const wxString&path , FSRef *fsRef )
bedaf53e 864{
b1d4dd7a 865 OSStatus err = noErr ;
a62848fd
WS
866 CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, wxMacCFStringHolder(path ,wxLocale::GetSystemEncoding() ) , kDefaultPathStyle, false);
867 if ( NULL != url )
868 {
869 if ( CFURLGetFSRef(url, fsRef) == false )
870 err = fnfErr ;
a2b77260 871 CFRelease( url ) ;
bfc2bf62
SC
872 }
873 else
874 {
a2b77260 875 err = fnfErr ;
bfc2bf62 876 }
a2b77260 877 return err ;
bedaf53e
SC
878}
879
a62848fd 880wxString wxMacHFSUniStrToString( ConstHFSUniStr255Param uniname )
c4e41ce3 881{
a2b77260
SC
882 CFStringRef cfname = CFStringCreateWithCharacters( kCFAllocatorDefault,
883 uniname->unicode,
884 uniname->length );
885 return wxMacCFStringHolder(cfname).AsString() ;
c4e41ce3 886}
e7549107 887
a2b77260 888wxString wxMacFSSpec2MacFilename( const FSSpec *spec )
17dff81c 889{
a2b77260
SC
890 FSRef fsRef ;
891 if ( FSpMakeFSRef( spec , &fsRef) == noErr )
e7549107 892 {
a2b77260 893 return wxMacFSRefToPath( &fsRef ) ;
e7549107 894 }
a2b77260 895 return wxEmptyString ;
17dff81c 896}
e7549107 897
a2b77260 898void wxMacFilename2FSSpec( const wxString& path , FSSpec *spec )
e7549107 899{
a2b77260
SC
900 OSStatus err = noErr ;
901 FSRef fsRef ;
902 wxMacPathToFSRef( path , &fsRef ) ;
903 err = FSRefMakeFSSpec( &fsRef , spec ) ;
e7549107 904}
12d67f8a 905
e8e1d09e
GD
906#endif // __WXMAC__
907
3f4a0c5b 908void
4603b4f9 909wxDos2UnixFilename (wxChar *s)
c801d85f
KB
910{
911 if (s)
912 while (*s)
913 {
4603b4f9
JS
914 if (*s == _T('\\'))
915 *s = _T('/');
e7549107 916#ifdef __WXMSW__
3f4a0c5b 917 else
254a2129 918 *s = (wxChar)wxTolower (*s); // Case INDEPENDENT
c801d85f 919#endif
3f4a0c5b 920 s++;
c801d85f
KB
921 }
922}
923
3f4a0c5b 924void
5a3912f2 925#if defined(__WXMSW__) || defined(__OS2__)
5b735202 926wxUnix2DosFilename (wxChar *s)
46dc76ba 927#else
5b735202 928wxUnix2DosFilename (wxChar *WXUNUSED(s) )
46dc76ba 929#endif
c801d85f
KB
930{
931// Yes, I really mean this to happen under DOS only! JACS
5a3912f2 932#if defined(__WXMSW__) || defined(__OS2__)
c801d85f
KB
933 if (s)
934 while (*s)
935 {
223d09f6
KB
936 if (*s == wxT('/'))
937 *s = wxT('\\');
3f4a0c5b 938 s++;
c801d85f
KB
939 }
940#endif
941}
942
943// Concatenate two files to form third
3f4a0c5b 944bool
c801d85f
KB
945wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& file3)
946{
a339970a 947 wxString outfile;
2b5f62a0 948 if ( !wxGetTempFileName( wxT("cat"), outfile) )
79e162f5 949 return false;
c801d85f 950
999836aa 951 FILE *fp1 wxDUMMY_INITIALIZE(NULL);
044bbeb0
MB
952 FILE *fp2 = NULL;
953 FILE *fp3 = NULL;
c801d85f 954 // Open the inputs and outputs
401eb3de
RR
955 if ((fp1 = wxFopen ( file1, wxT("rb"))) == NULL ||
956 (fp2 = wxFopen ( file2, wxT("rb"))) == NULL ||
957 (fp3 = wxFopen ( outfile, wxT("wb"))) == NULL)
c801d85f
KB
958 {
959 if (fp1)
3f4a0c5b 960 fclose (fp1);
c801d85f 961 if (fp2)
3f4a0c5b 962 fclose (fp2);
c801d85f 963 if (fp3)
3f4a0c5b 964 fclose (fp3);
79e162f5 965 return false;
c801d85f
KB
966 }
967
968 int ch;
969 while ((ch = getc (fp1)) != EOF)
970 (void) putc (ch, fp3);
971 fclose (fp1);
972
973 while ((ch = getc (fp2)) != EOF)
974 (void) putc (ch, fp3);
975 fclose (fp2);
976
977 fclose (fp3);
978 bool result = wxRenameFile(outfile, file3);
c801d85f
KB
979 return result;
980}
981
982// Copy files
3f4a0c5b 983bool
4658c44e 984wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite)
c801d85f 985{
04ef50df 986#if defined(__WIN32__) && !defined(__WXMICROWIN__)
4658c44e
VZ
987 // CopyFile() copies file attributes and modification time too, so use it
988 // instead of our code if available
989 //
990 // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite
564225a1
VZ
991 if ( !::CopyFile(file1, file2, !overwrite) )
992 {
993 wxLogSysError(_("Failed to copy the file '%s' to '%s'"),
994 file1.c_str(), file2.c_str());
995
79e162f5 996 return false;
564225a1 997 }
5a3912f2 998#elif defined(__OS2__)
564225a1 999 if ( ::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) != 0 )
79e162f5 1000 return false;
4658c44e 1001#else // !Win32
32f31043 1002
b1ac3b56 1003 wxStructStat fbuf;
32f31043 1004 // get permissions of file1
b1ac3b56 1005 if ( wxStat( file1.c_str(), &fbuf) != 0 )
32f31043
VZ
1006 {
1007 // the file probably doesn't exist or we haven't the rights to read
1008 // from it anyhow
1009 wxLogSysError(_("Impossible to get permissions for file '%s'"),
1010 file1.c_str());
79e162f5 1011 return false;
32f31043
VZ
1012 }
1013
1014 // open file1 for reading
1015 wxFile fileIn(file1, wxFile::read);
a339970a 1016 if ( !fileIn.IsOpened() )
79e162f5 1017 return false;
c801d85f 1018
32f31043
VZ
1019 // remove file2, if it exists. This is needed for creating
1020 // file2 with the correct permissions in the next step
4658c44e 1021 if ( wxFileExists(file2) && (!overwrite || !wxRemoveFile(file2)))
32f31043
VZ
1022 {
1023 wxLogSysError(_("Impossible to overwrite the file '%s'"),
1024 file2.c_str());
79e162f5 1025 return false;
32f31043
VZ
1026 }
1027
32f31043
VZ
1028 // reset the umask as we want to create the file with exactly the same
1029 // permissions as the original one
8482e4bd 1030 wxCHANGE_UMASK(0);
32f31043
VZ
1031
1032 // create file2 with the same permissions than file1 and open it for
1033 // writing
dc259b79 1034
32f31043 1035 wxFile fileOut;
4658c44e 1036 if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) )
79e162f5 1037 return false;
c801d85f 1038
32f31043 1039 // copy contents of file1 to file2
a339970a
VZ
1040 char buf[4096];
1041 size_t count;
1042 for ( ;; )
c7386783 1043 {
a339970a
VZ
1044 count = fileIn.Read(buf, WXSIZEOF(buf));
1045 if ( fileIn.Error() )
79e162f5 1046 return false;
a339970a
VZ
1047
1048 // end of file?
1049 if ( !count )
1050 break;
1051
1052 if ( fileOut.Write(buf, count) < count )
79e162f5 1053 return false;
c7386783 1054 }
c801d85f 1055
abb74e97
VZ
1056 // we can expect fileIn to be closed successfully, but we should ensure
1057 // that fileOut was closed as some write errors (disk full) might not be
1058 // detected before doing this
1059 if ( !fileIn.Close() || !fileOut.Close() )
79e162f5 1060 return false;
abb74e97 1061
5fde6fcc 1062#if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__)
abb74e97
VZ
1063 // no chmod in VA. Should be some permission API for HPFS386 partitions
1064 // however
c3396917 1065 if ( chmod(OS_FILENAME(file2), fbuf.st_mode) != 0 )
32f31043
VZ
1066 {
1067 wxLogSysError(_("Impossible to set permissions for the file '%s'"),
1068 file2.c_str());
79e162f5 1069 return false;
32f31043 1070 }
4658c44e 1071#endif // OS/2 || Mac
564225a1 1072#endif // __WXMSW__ && __WIN32__
4658c44e 1073
79e162f5 1074 return true;
c801d85f
KB
1075}
1076
3f4a0c5b 1077bool
c801d85f
KB
1078wxRenameFile (const wxString& file1, const wxString& file2)
1079{
1c193821
JS
1080#ifndef __WXWINCE__
1081 // Normal system call
c3396917 1082 if ( wxRename (file1, file2) == 0 )
79e162f5 1083 return true;
1c193821 1084#endif
a339970a 1085
c801d85f
KB
1086 // Try to copy
1087 if (wxCopyFile(file1, file2)) {
1088 wxRemoveFile(file1);
79e162f5 1089 return true;
c801d85f
KB
1090 }
1091 // Give up
79e162f5 1092 return false;
c801d85f
KB
1093}
1094
1095bool wxRemoveFile(const wxString& file)
1096{
161f4f73
VZ
1097#if defined(__VISUALC__) \
1098 || defined(__BORLANDC__) \
1099 || defined(__WATCOMC__) \
ba1e9d6c 1100 || defined(__DMC__) \
e874f867
SC
1101 || defined(__GNUWIN32__) \
1102 || (defined(__MWERKS__) && defined(__MSL__))
a339970a 1103 int res = wxRemove(file);
c4e41ce3
SC
1104#elif defined(__WXMAC__)
1105 int res = unlink(wxFNCONV(file));
c801d85f 1106#else
a339970a 1107 int res = unlink(OS_FILENAME(file));
c801d85f 1108#endif
a339970a
VZ
1109
1110 return res == 0;
c801d85f
KB
1111}
1112
1a33c3ba 1113bool wxMkdir(const wxString& dir, int perm)
c801d85f 1114{
5fde6fcc 1115#if defined(__WXMAC__) && !defined(__UNIX__)
c4e41ce3 1116 return (mkdir( wxFNCONV(dir) , 0 ) == 0);
7708abe9 1117#else // !Mac
50920146 1118 const wxChar *dirname = dir.c_str();
1a33c3ba 1119
c2ff79b1 1120 // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too
7708abe9 1121 // for the GNU compiler
5a3912f2 1122#if (!(defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__)
9c8cd106 1123 #if defined(MSVCRT)
79e162f5 1124 wxUnusedVar(perm);
2e38557f 1125 if ( mkdir(wxFNCONV(dirname)) != 0 )
9c8cd106
WS
1126 #else
1127 if ( mkdir(wxFNCONV(dirname), perm) != 0 )
2e38557f 1128 #endif
5a3912f2 1129#elif defined(__OS2__)
f6bcfd97 1130 if (::DosCreateDir((PSZ)dirname, NULL) != 0) // enhance for EAB's??
b916f809
VS
1131#elif defined(__DOS__)
1132 #if defined(__WATCOMC__)
1133 (void)perm;
1134 if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 )
1135 #elif defined(__DJGPP__)
1136 if ( mkdir(wxFNCONV(dirname), perm) != 0 )
1137 #else
1138 #error "Unsupported DOS compiler!"
1139 #endif
1140#else // !MSW, !DOS and !OS/2 VAC++
19193a2c 1141 (void)perm;
1c193821
JS
1142#ifdef __WXWINCE__
1143 if ( !CreateDirectory(dirname, NULL) )
1144#else
f6bcfd97 1145 if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 )
1c193821 1146#endif
7708abe9 1147#endif // !MSW/MSW
1a33c3ba
VZ
1148 {
1149 wxLogSysError(_("Directory '%s' couldn't be created"), dirname);
1150
79e162f5 1151 return false;
1a33c3ba
VZ
1152 }
1153
79e162f5 1154 return true;
e7549107 1155#endif // Mac/!Mac
c801d85f
KB
1156}
1157
1158bool wxRmdir(const wxString& dir, int WXUNUSED(flags))
1159{
1160#ifdef __VMS__
79e162f5 1161 return false; //to be changed since rmdir exists in VMS7.x
5a3912f2 1162#elif defined(__OS2__)
1c193821 1163 return (::DosDeleteDir((PSZ)dir.c_str()) == 0);
c801d85f 1164#else
a3ef5bf5 1165
1c193821
JS
1166#ifdef __WXWINCE__
1167 return (CreateDirectory(dir, NULL) != 0);
a3ef5bf5 1168#else
1c193821 1169 return (wxRmDir(OS_FILENAME(dir)) == 0);
c801d85f
KB
1170#endif
1171
1172#endif
1173}
1174
c801d85f 1175// does the path exists? (may have or not '/' or '\\' at the end)
50920146 1176bool wxPathExists(const wxChar *pszPathName)
c801d85f 1177{
f6bcfd97 1178 wxString strPath(pszPathName);
e32d659d 1179
5a3912f2 1180#if defined(__WINDOWS__) || defined(__OS2__)
f6bcfd97
BP
1181 // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists,
1182 // so remove all trailing backslashes from the path - but don't do this for
1183 // the pathes "d:\" (which are different from "d:") nor for just "\"
1184 while ( wxEndsWithPathSeparator(strPath) )
1185 {
1186 size_t len = strPath.length();
cc4a1ce1 1187 if ( len == 1 || (len == 3 && strPath[len - 2] == _T(':')) )
f6bcfd97 1188 break;
c801d85f 1189
f6bcfd97
BP
1190 strPath.Truncate(len - 1);
1191 }
1192#endif // __WINDOWS__
1193
5a3912f2
SN
1194#ifdef __OS2__
1195 // OS/2 can't handle "d:", it wants either "d:\" or "d:."
1d4f9cb7 1196 if (strPath.length() == 2 && strPath[1u] == _T(':'))
5a3912f2
SN
1197 strPath << _T('.');
1198#endif
1199
2db300c6 1200#if defined(__WIN32__) && !defined(__WXMICROWIN__)
e32d659d
VZ
1201 // stat() can't cope with network paths
1202 DWORD ret = ::GetFileAttributes(strPath);
2db300c6 1203
a28ae409 1204 return (ret != (DWORD)-1) && (ret & FILE_ATTRIBUTE_DIRECTORY);
e32d659d 1205#else // !__WIN32__
4c97e024 1206
f6bcfd97 1207 wxStructStat st;
71c97a89 1208#ifndef __VISAGECPP__
5a3912f2 1209 return wxStat(strPath.c_str(), &st) == 0 && ((st.st_mode & S_IFMT) == S_IFDIR);
71c97a89
DW
1210#else
1211 // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only
92980e90 1212 return wxStat(pszPathName, &st) == 0 && (st.st_mode == S_IFDIR);
71c97a89
DW
1213#endif
1214
e32d659d 1215#endif // __WIN32__/!__WIN32__
c801d85f
KB
1216}
1217
1218// Get a temporary filename, opening and closing the file.
50920146 1219wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf)
c801d85f 1220{
ade35f11
VZ
1221 wxString filename = wxFileName::CreateTempFileName(prefix);
1222 if ( filename.empty() )
1223 return NULL;
c801d85f 1224
ade35f11
VZ
1225 if ( buf )
1226 wxStrcpy(buf, filename);
1227 else
f526f752 1228 buf = MYcopystring(filename);
c801d85f 1229
ade35f11 1230 return buf;
c801d85f
KB
1231}
1232
c0ab6adf
JS
1233bool wxGetTempFileName(const wxString& prefix, wxString& buf)
1234{
ade35f11
VZ
1235 buf = wxFileName::CreateTempFileName(prefix);
1236
1237 return !buf.empty();
c0ab6adf
JS
1238}
1239
c801d85f
KB
1240// Get first file name matching given wild card.
1241
8ff12342
VS
1242static wxDir *gs_dir = NULL;
1243static wxString gs_dirPath;
1244
1245wxString wxFindFirstFile(const wxChar *spec, int flags)
1246{
9a5ead1e 1247 wxSplitPath(spec, &gs_dirPath, NULL, NULL);
8ff12342
VS
1248 if ( gs_dirPath.IsEmpty() )
1249 gs_dirPath = wxT(".");
083f7497 1250 if ( !wxEndsWithPathSeparator(gs_dirPath ) )
8ff12342
VS
1251 gs_dirPath << wxFILE_SEP_PATH;
1252
1253 if (gs_dir)
1254 delete gs_dir;
1255 gs_dir = new wxDir(gs_dirPath);
2db300c6 1256
8ff12342
VS
1257 if ( !gs_dir->IsOpened() )
1258 {
1259 wxLogSysError(_("Can not enumerate files '%s'"), spec);
1260 return wxEmptyString;
1261 }
2db300c6 1262
999836aa 1263 int dirFlags;
8ff12342
VS
1264 switch (flags)
1265 {
1266 case wxDIR: dirFlags = wxDIR_DIRS; break;
1267 case wxFILE: dirFlags = wxDIR_FILES; break;
1268 default: dirFlags = wxDIR_DIRS | wxDIR_FILES; break;
1269 }
2db300c6 1270
8ff12342 1271 wxString result;
1f1070e2 1272 gs_dir->GetFirst(&result, wxFileNameFromPath(wxString(spec)), dirFlags);
8ff12342 1273 if ( result.IsEmpty() )
e168b6ac 1274 {
8ff12342 1275 wxDELETE(gs_dir);
e168b6ac
VS
1276 return result;
1277 }
8ff12342
VS
1278
1279 return gs_dirPath + result;
1280}
1281
1282wxString wxFindNextFile()
1283{
1284 wxASSERT_MSG( gs_dir, wxT("You must call wxFindFirstFile before!") );
1285
1286 wxString result;
1287 gs_dir->GetNext(&result);
2db300c6 1288
8ff12342 1289 if ( result.IsEmpty() )
e168b6ac 1290 {
8ff12342 1291 wxDELETE(gs_dir);
e168b6ac
VS
1292 return result;
1293 }
2db300c6 1294
8ff12342
VS
1295 return gs_dirPath + result;
1296}
1297
c801d85f
KB
1298
1299// Get current working directory.
1300// If buf is NULL, allocates space using new, else
1301// copies into buf.
50920146 1302wxChar *wxGetWorkingDirectory(wxChar *buf, int sz)
c801d85f 1303{
1c193821
JS
1304#ifdef __WXWINCE__
1305 return NULL;
1306#else
13b1f8a7 1307 if ( !buf )
f6bcfd97 1308 {
13b1f8a7
VZ
1309 buf = new wxChar[sz + 1];
1310 }
1311
79e162f5 1312 bool ok wxDUMMY_INITIALIZE(false);
13b1f8a7
VZ
1313
1314 // for the compilers which have Unicode version of _getcwd(), call it
1315 // directly, for the others call the ANSI version and do the translation
dbc38199 1316#if !wxUSE_UNICODE
247f8a77 1317 #define cbuf buf
dbc38199 1318#else // wxUSE_UNICODE
79e162f5 1319 bool needsANSI = true;
dbc38199 1320
247f8a77 1321 #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU
dc259b79 1322 // This is not legal code as the compiler
c35c94f6
RR
1323 // is allowed destroy the wxCharBuffer.
1324 // wxCharBuffer c_buffer(sz);
1325 // char *cbuf = (char*)(const char*)c_buffer;
1326 char cbuf[_MAXPATHLEN];
247f8a77 1327 #endif
dbc38199 1328
13b1f8a7 1329 #ifdef HAVE_WGETCWD
247f8a77
VS
1330 #if wxUSE_UNICODE_MSLU
1331 if ( wxGetOsVersion() != wxWIN95 )
f5c0e657 1332 #else
79e162f5 1333 char *cbuf = NULL; // never really used because needsANSI will always be false
247f8a77
VS
1334 #endif
1335 {
1336 ok = _wgetcwd(buf, sz) != NULL;
79e162f5 1337 needsANSI = false;
247f8a77 1338 }
13b1f8a7 1339 #endif
13b1f8a7 1340
247f8a77 1341 if ( needsANSI )
dbc38199 1342#endif // wxUSE_UNICODE
247f8a77 1343 {
29d0a26e 1344 #if defined(_MSC_VER) || defined(__MINGW32__)
dbc38199 1345 ok = _getcwd(cbuf, sz) != NULL;
13b1f8a7 1346 #elif defined(__WXMAC__) && !defined(__DARWIN__)
a2b77260
SC
1347 char lbuf[1024] ;
1348 if ( getcwd( lbuf , sizeof( lbuf ) ) )
13b1f8a7 1349 {
a2b77260 1350 wxString res( lbuf , *wxConvCurrent ) ;
a62848fd 1351 wxStrcpy( buf , res ) ;
79e162f5 1352 ok = true;
13b1f8a7
VZ
1353 }
1354 else
a2b77260 1355 ok = false ;
5a3912f2 1356 #elif defined(__OS2__)
13b1f8a7 1357 APIRET rc;
5a3912f2
SN
1358 ULONG ulDriveNum = 0;
1359 ULONG ulDriveMap = 0;
a62848fd 1360 rc = ::DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap);
5a3912f2 1361 ok = rc == 0;
a62848fd
WS
1362 if (ok)
1363 {
1364 sz -= 3;
1365 rc = ::DosQueryCurrentDir( 0 // current drive
5a3912f2
SN
1366 ,cbuf + 3
1367 ,(PULONG)&sz
1368 );
1369 cbuf[0] = 'A' + (ulDriveNum - 1);
1370 cbuf[1] = ':';
1371 cbuf[2] = '\\';
1372 ok = rc == 0;
1373 }
13b1f8a7 1374 #else // !Win32/VC++ !Mac !OS2
dbc38199 1375 ok = getcwd(cbuf, sz) != NULL;
13b1f8a7 1376 #endif // platform
247f8a77 1377
7a21e692 1378 #if wxUSE_UNICODE && !(defined(__WXMAC__) && !defined(__DARWIN__))
247f8a77
VS
1379 // finally convert the result to Unicode if needed
1380 wxConvFile.MB2WC(buf, cbuf, sz);
1381 #endif // wxUSE_UNICODE
1382 }
13b1f8a7
VZ
1383
1384 if ( !ok )
19193a2c 1385 {
13b1f8a7 1386 wxLogSysError(_("Failed to get the working directory"));
19193a2c 1387
13b1f8a7
VZ
1388 // VZ: the old code used to return "." on error which didn't make any
1389 // sense at all to me - empty string is a better error indicator
1390 // (NULL might be even better but I'm afraid this could lead to
1391 // problems with the old code assuming the return is never NULL)
1392 buf[0] = _T('\0');
19193a2c 1393 }
13b1f8a7 1394 else // ok, but we might need to massage the path into the right format
19193a2c 1395 {
4b00a538 1396#ifdef __DJGPP__
13b1f8a7
VZ
1397 // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths
1398 // with / deliminers. We don't like that.
1399 for (wxChar *ch = buf; *ch; ch++)
1400 {
1401 if (*ch == wxT('/'))
1402 *ch = wxT('\\');
1403 }
1404#endif // __DJGPP__
1405
17234b26
MB
1406// MBN: we hope that in the case the user is compiling a GTK+/Motif app,
1407// he needs Unix as opposed to Win32 pathnames
1408#if defined( __CYGWIN__ ) && defined( __WINDOWS__ )
3ffbc733 1409 // another example of DOS/Unix mix (Cygwin)
13b1f8a7
VZ
1410 wxString pathUnix = buf;
1411 cygwin_conv_to_full_win32_path(pathUnix, buf);
e02e8816 1412#endif // __CYGWIN__
13b1f8a7 1413 }
4b00a538 1414
13b1f8a7 1415 return buf;
dbc38199
VS
1416
1417#if !wxUSE_UNICODE
247f8a77 1418 #undef cbuf
dbc38199 1419#endif
1c193821
JS
1420
1421#endif
1422 // __WXWINCE__
c801d85f
KB
1423}
1424
7af89395
VZ
1425wxString wxGetCwd()
1426{
4bc6e5c5
RR
1427 wxChar *buffer = new wxChar[_MAXPATHLEN];
1428 wxGetWorkingDirectory(buffer, _MAXPATHLEN);
1429 wxString str( buffer );
1430 delete [] buffer;
dc259b79 1431
eac2aeb0 1432 return str;
7af89395
VZ
1433}
1434
c801d85f
KB
1435bool wxSetWorkingDirectory(const wxString& d)
1436{
5a3912f2 1437#if defined(__OS2__)
1c193821 1438 return (::DosSetCurrentDir((PSZ)d.c_str()) == 0);
5a3912f2
SN
1439#elif defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__)
1440 return (chdir(wxFNSTRINGCAST d.fn_str()) == 0);
34138703 1441#elif defined(__WINDOWS__)
a62848fd 1442
c801d85f 1443#ifdef __WIN32__
1c193821
JS
1444#ifdef __WXWINCE__
1445 // No equivalent in WinCE
79e162f5 1446 return false;
c801d85f 1447#else
1c193821
JS
1448 return (bool)(SetCurrentDirectory(d) != 0);
1449#endif
1450#else
1451 // Must change drive, too.
1452 bool isDriveSpec = ((strlen(d) > 1) && (d[1] == ':'));
1453 if (isDriveSpec)
c801d85f 1454 {
1c193821 1455 wxChar firstChar = d[0];
a62848fd 1456
1c193821
JS
1457 // To upper case
1458 if (firstChar > 90)
1459 firstChar = firstChar - 32;
a62848fd 1460
1c193821
JS
1461 // To a drive number
1462 unsigned int driveNo = firstChar - 64;
1463 if (driveNo > 0)
1464 {
1465 unsigned int noDrives;
1466 _dos_setdrive(driveNo, &noDrives);
1467 }
c801d85f 1468 }
1c193821 1469 bool success = (chdir(WXSTRINGCAST d) == 0);
a62848fd 1470
1c193821 1471 return success;
c801d85f 1472#endif
a62848fd 1473
c801d85f
KB
1474#endif
1475}
1476
631f1bfe
JS
1477// Get the OS directory if appropriate (such as the Windows directory).
1478// On non-Windows platform, probably just return the empty string.
1479wxString wxGetOSDirectory()
1480{
1c193821
JS
1481#ifdef __WXWINCE__
1482 return wxString(wxT("\\Windows"));
1483#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
50920146 1484 wxChar buf[256];
631f1bfe
JS
1485 GetWindowsDirectory(buf, 256);
1486 return wxString(buf);
2d3441d7
GD
1487#elif defined(__WXMAC__)
1488 return wxMacFindFolder(kOnSystemDisk, 'macs', false);
631f1bfe
JS
1489#else
1490 return wxEmptyString;
1491#endif
1492}
1493
a907da15 1494bool wxEndsWithPathSeparator(const wxChar *pszFileName)
c801d85f 1495{
903b61cc
VZ
1496 size_t len = wxStrlen(pszFileName);
1497
1498 return len && wxIsPathSeparator(pszFileName[len - 1]);
c801d85f
KB
1499}
1500
79e162f5 1501// find a file in a list of directories, returns false if not found
50920146 1502bool wxFindFileInPath(wxString *pStr, const wxChar *pszPath, const wxChar *pszFile)
c801d85f 1503{
a17e237f 1504 // we assume that it's not empty
79e162f5 1505 wxCHECK_MSG( !wxIsEmpty(pszFile), false,
f6bcfd97 1506 _T("empty file name in wxFindFileInPath"));
a17e237f
VZ
1507
1508 // skip path separator in the beginning of the file name if present
1509 if ( wxIsPathSeparator(*pszFile) )
1510 pszFile++;
1511
1512 // copy the path (strtok will modify it)
1513 wxChar *szPath = new wxChar[wxStrlen(pszPath) + 1];
1514 wxStrcpy(szPath, pszPath);
1515
1516 wxString strFile;
1517 wxChar *pc, *save_ptr;
1518 for ( pc = wxStrtok(szPath, wxPATH_SEP, &save_ptr);
1519 pc != NULL;
1520 pc = wxStrtok((wxChar *) NULL, wxPATH_SEP, &save_ptr) )
1521 {
1522 // search for the file in this directory
1523 strFile = pc;
1524 if ( !wxEndsWithPathSeparator(pc) )
1525 strFile += wxFILE_SEP_PATH;
1526 strFile += pszFile;
1527
2b5f62a0 1528 if ( wxFileExists(strFile) ) {
a17e237f
VZ
1529 *pStr = strFile;
1530 break;
1531 }
c801d85f 1532 }
c801d85f 1533
a17e237f
VZ
1534 // suppress warning about unused variable save_ptr when wxStrtok() is a
1535 // macro which throws away its third argument
1536 save_ptr = pc;
1537
1538 delete [] szPath;
c801d85f 1539
79e162f5 1540 return pc != NULL; // if true => we breaked from the loop
c801d85f
KB
1541}
1542
50920146 1543void WXDLLEXPORT wxSplitPath(const wxChar *pszFileName,
3826db3e
VZ
1544 wxString *pstrPath,
1545 wxString *pstrName,
1546 wxString *pstrExt)
1547{
d37fd2fa 1548 // it can be empty, but it shouldn't be NULL
223d09f6 1549 wxCHECK_RET( pszFileName, wxT("NULL file name in wxSplitPath") );
3826db3e 1550
9e8d8607 1551 wxFileName::SplitPath(pszFileName, pstrPath, pstrName, pstrExt);
3826db3e 1552}
7f555861 1553
a47ce4a7
VS
1554time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename)
1555{
1c193821
JS
1556#ifdef __WXWINCE__
1557 FILETIME creationTime, lastAccessTime, lastWriteTime;
1558 HANDLE fileHandle = ::CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1559 0, FILE_ATTRIBUTE_NORMAL, 0);
1560 if (fileHandle == INVALID_HANDLE_VALUE)
1561 return 0;
1562 else
1563 {
1564 if (GetFileTime(fileHandle, & creationTime, & lastAccessTime, & lastWriteTime))
1565 {
1566 CloseHandle(fileHandle);
1567
1568 wxDateTime dateTime;
1569 FILETIME ftLocal;
1570 if ( !::FileTimeToLocalFileTime(&lastWriteTime, &ftLocal) )
1571 {
1572 wxLogLastError(_T("FileTimeToLocalFileTime"));
1573 }
a62848fd 1574
1c193821
JS
1575 SYSTEMTIME st;
1576 if ( !::FileTimeToSystemTime(&ftLocal, &st) )
1577 {
1578 wxLogLastError(_T("FileTimeToSystemTime"));
1579 }
a62848fd 1580
1c193821
JS
1581 dateTime.Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear,
1582 st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
1583 return dateTime.GetTicks();
1584 }
1585 else
1586 return 0;
1587 }
1588#else
f6bcfd97 1589 wxStructStat buf;
92980e90 1590 wxStat( filename, &buf);
dc259b79 1591
a47ce4a7 1592 return buf.st_mtime;
1c193821 1593#endif
a47ce4a7
VS
1594}
1595
1596
9e152a55
WS
1597// Parses the filterStr, returning the number of filters.
1598// Returns 0 if none or if there's a problem.
daf32463 1599// filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpeg"
9e152a55 1600
daf32463 1601int WXDLLEXPORT wxParseCommonDialogsFilter(const wxString& filterStr, wxArrayString& descriptions, wxArrayString& filters)
9e152a55
WS
1602{
1603 descriptions.Clear();
1604 filters.Clear();
1605
1606 wxString str(filterStr);
1607
1608 wxString description, filter;
1609 int pos = 0;
1610 while( pos != wxNOT_FOUND )
1611 {
1612 pos = str.Find(wxT('|'));
1613 if ( pos == wxNOT_FOUND )
1614 {
1615 // if there are no '|'s at all in the string just take the entire
daf32463 1616 // string as filter and make description empty for later autocompletion
9e152a55
WS
1617 if ( filters.IsEmpty() )
1618 {
daf32463 1619 descriptions.Add(wxEmptyString);
9e152a55
WS
1620 filters.Add(filterStr);
1621 }
1622 else
1623 {
1624 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
1625 }
1626
1627 break;
1628 }
1629
1630 description = str.Left(pos);
1631 str = str.Mid(pos + 1);
1632 pos = str.Find(wxT('|'));
1633 if ( pos == wxNOT_FOUND )
1634 {
1635 filter = str;
1636 }
1637 else
1638 {
1639 filter = str.Left(pos);
1640 str = str.Mid(pos + 1);
1641 }
1642
1643 descriptions.Add(description);
1644 filters.Add(filter);
1645 }
1646
daf32463
WS
1647#if defined(__WXMOTIF__)
1648 // split it so there is one wildcard per entry
1649 for( size_t i = 0 ; i < descriptions.GetCount() ; i++ )
1650 {
1651 pos = filters[i].Find(wxT(';'));
1652 if (pos != wxNOT_FOUND)
1653 {
1654 // first split only filters
1655 descriptions.Insert(descriptions[i],i+1);
1656 filters.Insert(filters[i].Mid(pos+1),i+1);
1657 filters[i]=filters[i].Left(pos);
1658
1659 // autoreplace new filter in description with pattern:
1660 // C/C++ Files(*.cpp;*.c;*.h)|*.cpp;*.c;*.h
1661 // cause split into:
1662 // C/C++ Files(*.cpp)|*.cpp
1663 // C/C++ Files(*.c;*.h)|*.c;*.h
1664 // and next iteration cause another split into:
1665 // C/C++ Files(*.cpp)|*.cpp
1666 // C/C++ Files(*.c)|*.c
1667 // C/C++ Files(*.h)|*.h
1668 for ( size_t k=i;k<i+2;k++ )
1669 {
1670 pos = descriptions[k].Find(filters[k]);
1671 if (pos != wxNOT_FOUND)
1672 {
1673 wxString before = descriptions[k].Left(pos);
1674 wxString after = descriptions[k].Mid(pos+filters[k].Len());
1675 pos = before.Find(_T('('),true);
1676 if (pos>before.Find(_T(')'),true))
1677 {
1678 before = before.Left(pos+1);
1679 before << filters[k];
1680 pos = after.Find(_T(')'));
1681 int pos1 = after.Find(_T('('));
1682 if (pos != wxNOT_FOUND && (pos<pos1 || pos1==wxNOT_FOUND))
1683 {
1684 before << after.Mid(pos);
1685 descriptions[k] = before;
1686 }
1687 }
1688 }
1689 }
1690 }
1691 }
1692#endif
1693
1694 // autocompletion
1695 for( size_t j = 0 ; j < descriptions.GetCount() ; j++ )
1696 {
1697 if ( descriptions[j] == wxEmptyString && filters[j] != wxEmptyString )
1698 {
1699 descriptions[j].Printf(_("Files (%s)"), filters[j].c_str());
1700 }
1701 }
1702
9e152a55
WS
1703 return filters.GetCount();
1704}
1705
1706
7f555861
JS
1707//------------------------------------------------------------------------
1708// wild character routines
1709//------------------------------------------------------------------------
1710
1711bool wxIsWild( const wxString& pattern )
1712{
2b5f62a0
VZ
1713 wxString tmp = pattern;
1714 wxChar *pat = WXSTRINGCAST(tmp);
dc259b79 1715 while (*pat)
2b5f62a0 1716 {
dc259b79 1717 switch (*pat++)
2b5f62a0 1718 {
223d09f6 1719 case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'):
79e162f5 1720 return true;
223d09f6 1721 case wxT('\\'):
3f4a0c5b 1722 if (!*pat++)
79e162f5 1723 return false;
3f4a0c5b 1724 }
7f555861 1725 }
79e162f5 1726 return false;
dfcb1ae0 1727}
dfcb1ae0 1728
2b5f62a0
VZ
1729/*
1730* Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU>
1731*
1732* The match procedure is public domain code (from ircII's reg.c)
1733*/
7be1f0d9 1734
2b5f62a0 1735bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
7f555861 1736{
2b5f62a0
VZ
1737 if (text.empty())
1738 {
1739 /* Match if both are empty. */
1740 return pat.empty();
1741 }
dc259b79 1742
2b5f62a0
VZ
1743 const wxChar *m = pat.c_str(),
1744 *n = text.c_str(),
1745 *ma = NULL,
1746 *na = NULL,
1747 *mp = NULL,
1748 *np = NULL;
1749 int just = 0,
1750 pcount = 0,
1751 acount = 0,
1752 count = 0;
1753
1754 if (dot_special && (*n == wxT('.')))
1755 {
dc259b79 1756 /* Never match so that hidden Unix files
2b5f62a0 1757 * are never found. */
79e162f5 1758 return false;
2b5f62a0 1759 }
7f555861 1760
2b5f62a0
VZ
1761 for (;;)
1762 {
1763 if (*m == wxT('*'))
1764 {
1765 ma = ++m;
1766 na = n;
1767 just = 1;
1768 mp = NULL;
1769 acount = count;
3f4a0c5b 1770 }
2b5f62a0
VZ
1771 else if (*m == wxT('?'))
1772 {
1773 m++;
1774 if (!*n++)
79e162f5 1775 return false;
3f4a0c5b 1776 }
2b5f62a0
VZ
1777 else
1778 {
1779 if (*m == wxT('\\'))
1780 {
1781 m++;
1782 /* Quoting "nothing" is a bad thing */
1783 if (!*m)
79e162f5 1784 return false;
3f4a0c5b 1785 }
2b5f62a0
VZ
1786 if (!*m)
1787 {
1788 /*
1789 * If we are out of both strings or we just
1790 * saw a wildcard, then we can say we have a
1791 * match
1792 */
1793 if (!*n)
79e162f5 1794 return true;
2b5f62a0 1795 if (just)
79e162f5 1796 return true;
2b5f62a0
VZ
1797 just = 0;
1798 goto not_matched;
1799 }
1800 /*
1801 * We could check for *n == NULL at this point, but
1802 * since it's more common to have a character there,
1803 * check to see if they match first (m and n) and
1804 * then if they don't match, THEN we can check for
1805 * the NULL of n
1806 */
1807 just = 0;
1808 if (*m == *n)
1809 {
1810 m++;
1811 if (*n == wxT(' '))
1812 mp = NULL;
1813 count++;
1814 n++;
1815 }
1816 else
1817 {
1818
1819 not_matched:
1820
1821 /*
1822 * If there are no more characters in the
1823 * string, but we still need to find another
1824 * character (*m != NULL), then it will be
1825 * impossible to match it
1826 */
1827 if (!*n)
79e162f5 1828 return false;
2b5f62a0
VZ
1829 if (mp)
1830 {
1831 m = mp;
1832 if (*np == wxT(' '))
1833 {
1834 mp = NULL;
1835 goto check_percent;
1836 }
1837 n = ++np;
1838 count = pcount;
1839 }
1840 else
1841 check_percent:
1842
1843 if (ma)
1844 {
1845 m = ma;
1846 n = ++na;
1847 count = acount;
1848 }
1849 else
79e162f5 1850 return false;
3f4a0c5b 1851 }
3f4a0c5b 1852 }
3f4a0c5b 1853 }
2b5f62a0 1854}
fd3f686c 1855
7f555861 1856
3f4a0c5b 1857#ifdef __VISUALC__
fd3f686c 1858 #pragma warning(default:4706) // assignment within conditional expression
53c6e7cc 1859#endif // VC++