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