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