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