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