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