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