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