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