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