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