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