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