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