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