]>
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 | ) |
dc259b79 | 275 | return TRUE; |
c801d85f | 276 | } |
dc259b79 | 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] != ':') |
dc259b79 | 357 | return TRUE ; |
cd378f94 | 358 | #else |
e8e1d09e GD |
359 | // Unix like or Windows |
360 | if (filename[0] == wxT('/')) | |
dc259b79 | 361 | return TRUE; |
e8e1d09e | 362 | #endif |
c801d85f | 363 | #ifdef __VMS__ |
e8e1d09e | 364 | if ((filename[0] == wxT('[') && filename[1] != wxT('.'))) |
dc259b79 | 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(':'))) | |
dc259b79 | 370 | return TRUE; |
c801d85f | 371 | #endif |
c801d85f | 372 | } |
dc259b79 | 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__ |
dc259b79 | 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; | |
dc259b79 | 868 | Boolean isDirectory = FALSE; |
b1d4dd7a | 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) ) |
dc259b79 | 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); |
dc259b79 | 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 | ||
dc259b79 | 1138 | return FALSE; |
564225a1 | 1139 | } |
19193a2c | 1140 | #elif defined(__WXPM__) |
564225a1 | 1141 | if ( ::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) != 0 ) |
dc259b79 | 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()); | |
dc259b79 | 1153 | return FALSE; |
32f31043 VZ |
1154 | } |
1155 | ||
1156 | // open file1 for reading | |
1157 | wxFile fileIn(file1, wxFile::read); | |
a339970a | 1158 | if ( !fileIn.IsOpened() ) |
dc259b79 | 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()); | |
dc259b79 | 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 | |
dc259b79 | 1178 | |
32f31043 | 1179 | wxFile fileOut; |
4658c44e | 1180 | if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) ) |
dc259b79 | 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() ) | |
dc259b79 | 1195 | return FALSE; |
a339970a VZ |
1196 | |
1197 | // end of file? | |
1198 | if ( !count ) | |
1199 | break; | |
1200 | ||
1201 | if ( fileOut.Write(buf, count) < count ) | |
dc259b79 | 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() ) | |
dc259b79 | 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()); | |
dc259b79 | 1218 | return FALSE; |
32f31043 | 1219 | } |
4658c44e | 1220 | #endif // OS/2 || Mac |
564225a1 | 1221 | #endif // __WXMSW__ && __WIN32__ |
4658c44e | 1222 | |
dc259b79 | 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 ) |
dc259b79 | 1231 | return TRUE; |
a339970a | 1232 | |
c801d85f KB |
1233 | // Try to copy |
1234 | if (wxCopyFile(file1, file2)) { | |
1235 | wxRemoveFile(file1); | |
dc259b79 | 1236 | return TRUE; |
c801d85f KB |
1237 | } |
1238 | // Give up | |
dc259b79 | 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__) |
2e38557f | 1266 | #ifndef MSVCRT |
50920146 | 1267 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) |
2e38557f JS |
1268 | #else |
1269 | if ( mkdir(wxFNCONV(dirname)) != 0 ) | |
1270 | #endif | |
f6bcfd97 BP |
1271 | #elif defined(__WXPM__) |
1272 | if (::DosCreateDir((PSZ)dirname, NULL) != 0) // enhance for EAB's?? | |
b916f809 VS |
1273 | #elif defined(__DOS__) |
1274 | #if defined(__WATCOMC__) | |
1275 | (void)perm; | |
1276 | if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) | |
1277 | #elif defined(__DJGPP__) | |
1278 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) | |
1279 | #else | |
1280 | #error "Unsupported DOS compiler!" | |
1281 | #endif | |
1282 | #else // !MSW, !DOS and !OS/2 VAC++ | |
19193a2c | 1283 | (void)perm; |
f6bcfd97 | 1284 | if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) |
7708abe9 | 1285 | #endif // !MSW/MSW |
1a33c3ba VZ |
1286 | { |
1287 | wxLogSysError(_("Directory '%s' couldn't be created"), dirname); | |
1288 | ||
dc259b79 | 1289 | return FALSE; |
1a33c3ba VZ |
1290 | } |
1291 | ||
dc259b79 | 1292 | return TRUE; |
e7549107 | 1293 | #endif // Mac/!Mac |
c801d85f KB |
1294 | } |
1295 | ||
1296 | bool wxRmdir(const wxString& dir, int WXUNUSED(flags)) | |
1297 | { | |
1298 | #ifdef __VMS__ | |
dc259b79 | 1299 | return FALSE; //to be changed since rmdir exists in VMS7.x |
f6bcfd97 BP |
1300 | #elif defined(__WXPM__) |
1301 | return (::DosDeleteDir((PSZ)dir.c_str()) == 0); | |
c801d85f | 1302 | #else |
a3ef5bf5 JS |
1303 | |
1304 | #ifdef __SALFORDC__ | |
dc259b79 | 1305 | return FALSE; // What to do? |
a3ef5bf5 | 1306 | #else |
a339970a | 1307 | return (wxRmDir(OS_FILENAME(dir)) == 0); |
c801d85f KB |
1308 | #endif |
1309 | ||
1310 | #endif | |
1311 | } | |
1312 | ||
c801d85f | 1313 | // does the path exists? (may have or not '/' or '\\' at the end) |
50920146 | 1314 | bool wxPathExists(const wxChar *pszPathName) |
c801d85f | 1315 | { |
f6bcfd97 | 1316 | wxString strPath(pszPathName); |
e32d659d | 1317 | |
f6bcfd97 BP |
1318 | #ifdef __WINDOWS__ |
1319 | // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists, | |
1320 | // so remove all trailing backslashes from the path - but don't do this for | |
1321 | // the pathes "d:\" (which are different from "d:") nor for just "\" | |
1322 | while ( wxEndsWithPathSeparator(strPath) ) | |
1323 | { | |
1324 | size_t len = strPath.length(); | |
cc4a1ce1 | 1325 | if ( len == 1 || (len == 3 && strPath[len - 2] == _T(':')) ) |
f6bcfd97 | 1326 | break; |
c801d85f | 1327 | |
f6bcfd97 BP |
1328 | strPath.Truncate(len - 1); |
1329 | } | |
1330 | #endif // __WINDOWS__ | |
1331 | ||
2db300c6 | 1332 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
e32d659d VZ |
1333 | // stat() can't cope with network paths |
1334 | DWORD ret = ::GetFileAttributes(strPath); | |
2db300c6 | 1335 | |
a28ae409 | 1336 | return (ret != (DWORD)-1) && (ret & FILE_ATTRIBUTE_DIRECTORY); |
e32d659d | 1337 | #else // !__WIN32__ |
4c97e024 | 1338 | |
f6bcfd97 | 1339 | wxStructStat st; |
71c97a89 | 1340 | #ifndef __VISAGECPP__ |
92980e90 | 1341 | return wxStat(pszPathName, &st) == 0 && ((st.st_mode & S_IFMT) == S_IFDIR); |
71c97a89 DW |
1342 | #else |
1343 | // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only | |
92980e90 | 1344 | return wxStat(pszPathName, &st) == 0 && (st.st_mode == S_IFDIR); |
71c97a89 DW |
1345 | #endif |
1346 | ||
e32d659d | 1347 | #endif // __WIN32__/!__WIN32__ |
c801d85f KB |
1348 | } |
1349 | ||
1350 | // Get a temporary filename, opening and closing the file. | |
50920146 | 1351 | wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf) |
c801d85f | 1352 | { |
ade35f11 VZ |
1353 | wxString filename = wxFileName::CreateTempFileName(prefix); |
1354 | if ( filename.empty() ) | |
1355 | return NULL; | |
c801d85f | 1356 | |
ade35f11 VZ |
1357 | if ( buf ) |
1358 | wxStrcpy(buf, filename); | |
1359 | else | |
1360 | buf = copystring(filename); | |
c801d85f | 1361 | |
ade35f11 | 1362 | return buf; |
c801d85f KB |
1363 | } |
1364 | ||
c0ab6adf JS |
1365 | bool wxGetTempFileName(const wxString& prefix, wxString& buf) |
1366 | { | |
ade35f11 VZ |
1367 | buf = wxFileName::CreateTempFileName(prefix); |
1368 | ||
1369 | return !buf.empty(); | |
c0ab6adf JS |
1370 | } |
1371 | ||
c801d85f KB |
1372 | // Get first file name matching given wild card. |
1373 | ||
8ff12342 VS |
1374 | static wxDir *gs_dir = NULL; |
1375 | static wxString gs_dirPath; | |
1376 | ||
1377 | wxString wxFindFirstFile(const wxChar *spec, int flags) | |
1378 | { | |
9a5ead1e | 1379 | wxSplitPath(spec, &gs_dirPath, NULL, NULL); |
8ff12342 VS |
1380 | if ( gs_dirPath.IsEmpty() ) |
1381 | gs_dirPath = wxT("."); | |
1382 | if ( gs_dirPath.Last() != wxFILE_SEP_PATH ) | |
1383 | gs_dirPath << wxFILE_SEP_PATH; | |
1384 | ||
1385 | if (gs_dir) | |
1386 | delete gs_dir; | |
1387 | gs_dir = new wxDir(gs_dirPath); | |
2db300c6 | 1388 | |
8ff12342 VS |
1389 | if ( !gs_dir->IsOpened() ) |
1390 | { | |
1391 | wxLogSysError(_("Can not enumerate files '%s'"), spec); | |
1392 | return wxEmptyString; | |
1393 | } | |
2db300c6 | 1394 | |
8ff12342 VS |
1395 | int dirFlags = 0; |
1396 | switch (flags) | |
1397 | { | |
1398 | case wxDIR: dirFlags = wxDIR_DIRS; break; | |
1399 | case wxFILE: dirFlags = wxDIR_FILES; break; | |
1400 | default: dirFlags = wxDIR_DIRS | wxDIR_FILES; break; | |
1401 | } | |
2db300c6 | 1402 | |
8ff12342 | 1403 | wxString result; |
1f1070e2 | 1404 | gs_dir->GetFirst(&result, wxFileNameFromPath(wxString(spec)), dirFlags); |
8ff12342 | 1405 | if ( result.IsEmpty() ) |
e168b6ac | 1406 | { |
8ff12342 | 1407 | wxDELETE(gs_dir); |
e168b6ac VS |
1408 | return result; |
1409 | } | |
8ff12342 VS |
1410 | |
1411 | return gs_dirPath + result; | |
1412 | } | |
1413 | ||
1414 | wxString wxFindNextFile() | |
1415 | { | |
1416 | wxASSERT_MSG( gs_dir, wxT("You must call wxFindFirstFile before!") ); | |
1417 | ||
1418 | wxString result; | |
1419 | gs_dir->GetNext(&result); | |
2db300c6 | 1420 | |
8ff12342 | 1421 | if ( result.IsEmpty() ) |
e168b6ac | 1422 | { |
8ff12342 | 1423 | wxDELETE(gs_dir); |
e168b6ac VS |
1424 | return result; |
1425 | } | |
2db300c6 | 1426 | |
8ff12342 VS |
1427 | return gs_dirPath + result; |
1428 | } | |
1429 | ||
c801d85f KB |
1430 | |
1431 | // Get current working directory. | |
1432 | // If buf is NULL, allocates space using new, else | |
1433 | // copies into buf. | |
50920146 | 1434 | wxChar *wxGetWorkingDirectory(wxChar *buf, int sz) |
c801d85f | 1435 | { |
13b1f8a7 | 1436 | if ( !buf ) |
f6bcfd97 | 1437 | { |
13b1f8a7 VZ |
1438 | buf = new wxChar[sz + 1]; |
1439 | } | |
1440 | ||
dc259b79 | 1441 | bool ok = FALSE; |
13b1f8a7 VZ |
1442 | |
1443 | // for the compilers which have Unicode version of _getcwd(), call it | |
1444 | // directly, for the others call the ANSI version and do the translation | |
dbc38199 | 1445 | #if !wxUSE_UNICODE |
247f8a77 | 1446 | #define cbuf buf |
dbc38199 | 1447 | #else // wxUSE_UNICODE |
dc259b79 | 1448 | bool needsANSI = TRUE; |
dbc38199 | 1449 | |
247f8a77 | 1450 | #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU |
dc259b79 | 1451 | // This is not legal code as the compiler |
c35c94f6 RR |
1452 | // is allowed destroy the wxCharBuffer. |
1453 | // wxCharBuffer c_buffer(sz); | |
1454 | // char *cbuf = (char*)(const char*)c_buffer; | |
1455 | char cbuf[_MAXPATHLEN]; | |
247f8a77 | 1456 | #endif |
dbc38199 | 1457 | |
13b1f8a7 | 1458 | #ifdef HAVE_WGETCWD |
247f8a77 VS |
1459 | #if wxUSE_UNICODE_MSLU |
1460 | if ( wxGetOsVersion() != wxWIN95 ) | |
f5c0e657 | 1461 | #else |
dc259b79 | 1462 | char *cbuf = NULL; // never really used because needsANSI will always be FALSE |
247f8a77 VS |
1463 | #endif |
1464 | { | |
1465 | ok = _wgetcwd(buf, sz) != NULL; | |
dc259b79 | 1466 | needsANSI = FALSE; |
247f8a77 | 1467 | } |
13b1f8a7 | 1468 | #endif |
13b1f8a7 | 1469 | |
247f8a77 | 1470 | if ( needsANSI ) |
dbc38199 | 1471 | #endif // wxUSE_UNICODE |
247f8a77 | 1472 | { |
13b1f8a7 | 1473 | #ifdef _MSC_VER |
dbc38199 | 1474 | ok = _getcwd(cbuf, sz) != NULL; |
13b1f8a7 VZ |
1475 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
1476 | FSSpec cwdSpec ; | |
1477 | FCBPBRec pb; | |
1478 | OSErr error; | |
1479 | Str255 fileName ; | |
1480 | pb.ioNamePtr = (StringPtr) &fileName; | |
1481 | pb.ioVRefNum = 0; | |
1482 | pb.ioRefNum = LMGetCurApRefNum(); | |
1483 | pb.ioFCBIndx = 0; | |
1484 | error = PBGetFCBInfoSync(&pb); | |
1485 | if ( error == noErr ) | |
1486 | { | |
1487 | cwdSpec.vRefNum = pb.ioFCBVRefNum; | |
1488 | cwdSpec.parID = pb.ioFCBParID; | |
1489 | cwdSpec.name[0] = 0 ; | |
1490 | wxString res = wxMacFSSpec2MacFilename( &cwdSpec ) ; | |
1491 | ||
dbc38199 VS |
1492 | strcpy( cbuf , res ) ; |
1493 | cbuf[res.length()]=0 ; | |
13b1f8a7 | 1494 | |
dc259b79 | 1495 | ok = TRUE; |
13b1f8a7 VZ |
1496 | } |
1497 | else | |
1498 | { | |
dc259b79 | 1499 | ok = FALSE; |
13b1f8a7 VZ |
1500 | } |
1501 | #elif defined(__VISAGECPP__) || (defined (__OS2__) && defined (__WATCOMC__)) | |
1502 | APIRET rc; | |
1503 | rc = ::DosQueryCurrentDir( 0 // current drive | |
dbc38199 | 1504 | ,cbuf |
13b1f8a7 VZ |
1505 | ,(PULONG)&sz |
1506 | ); | |
1507 | ok = rc != 0; | |
1508 | #else // !Win32/VC++ !Mac !OS2 | |
dbc38199 | 1509 | ok = getcwd(cbuf, sz) != NULL; |
13b1f8a7 | 1510 | #endif // platform |
247f8a77 VS |
1511 | |
1512 | #if wxUSE_UNICODE | |
1513 | // finally convert the result to Unicode if needed | |
1514 | wxConvFile.MB2WC(buf, cbuf, sz); | |
1515 | #endif // wxUSE_UNICODE | |
1516 | } | |
13b1f8a7 VZ |
1517 | |
1518 | if ( !ok ) | |
19193a2c | 1519 | { |
13b1f8a7 | 1520 | wxLogSysError(_("Failed to get the working directory")); |
19193a2c | 1521 | |
13b1f8a7 VZ |
1522 | // VZ: the old code used to return "." on error which didn't make any |
1523 | // sense at all to me - empty string is a better error indicator | |
1524 | // (NULL might be even better but I'm afraid this could lead to | |
1525 | // problems with the old code assuming the return is never NULL) | |
1526 | buf[0] = _T('\0'); | |
19193a2c | 1527 | } |
13b1f8a7 | 1528 | else // ok, but we might need to massage the path into the right format |
19193a2c | 1529 | { |
4b00a538 | 1530 | #ifdef __DJGPP__ |
13b1f8a7 VZ |
1531 | // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths |
1532 | // with / deliminers. We don't like that. | |
1533 | for (wxChar *ch = buf; *ch; ch++) | |
1534 | { | |
1535 | if (*ch == wxT('/')) | |
1536 | *ch = wxT('\\'); | |
1537 | } | |
1538 | #endif // __DJGPP__ | |
1539 | ||
17234b26 MB |
1540 | // MBN: we hope that in the case the user is compiling a GTK+/Motif app, |
1541 | // he needs Unix as opposed to Win32 pathnames | |
1542 | #if defined( __CYGWIN__ ) && defined( __WINDOWS__ ) | |
3ffbc733 | 1543 | // another example of DOS/Unix mix (Cygwin) |
13b1f8a7 VZ |
1544 | wxString pathUnix = buf; |
1545 | cygwin_conv_to_full_win32_path(pathUnix, buf); | |
e02e8816 | 1546 | #endif // __CYGWIN__ |
13b1f8a7 | 1547 | } |
4b00a538 | 1548 | |
13b1f8a7 | 1549 | return buf; |
dbc38199 VS |
1550 | |
1551 | #if !wxUSE_UNICODE | |
247f8a77 | 1552 | #undef cbuf |
dbc38199 | 1553 | #endif |
c801d85f KB |
1554 | } |
1555 | ||
7af89395 VZ |
1556 | wxString wxGetCwd() |
1557 | { | |
4bc6e5c5 RR |
1558 | wxChar *buffer = new wxChar[_MAXPATHLEN]; |
1559 | wxGetWorkingDirectory(buffer, _MAXPATHLEN); | |
1560 | wxString str( buffer ); | |
1561 | delete [] buffer; | |
dc259b79 | 1562 | |
eac2aeb0 | 1563 | return str; |
7af89395 VZ |
1564 | } |
1565 | ||
c801d85f KB |
1566 | bool wxSetWorkingDirectory(const wxString& d) |
1567 | { | |
f48fa475 | 1568 | #if defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__) |
e90c1d2a | 1569 | return (chdir(wxFNSTRINGCAST d.fn_str()) == 0); |
f6bcfd97 BP |
1570 | #elif defined(__WXPM__) |
1571 | return (::DosSetCurrentDir((PSZ)d.c_str()) == 0); | |
34138703 | 1572 | #elif defined(__WINDOWS__) |
c801d85f KB |
1573 | |
1574 | #ifdef __WIN32__ | |
1575 | return (bool)(SetCurrentDirectory(d) != 0); | |
1576 | #else | |
1577 | // Must change drive, too. | |
1578 | bool isDriveSpec = ((strlen(d) > 1) && (d[1] == ':')); | |
1579 | if (isDriveSpec) | |
1580 | { | |
50920146 | 1581 | wxChar firstChar = d[0]; |
c801d85f KB |
1582 | |
1583 | // To upper case | |
1584 | if (firstChar > 90) | |
1585 | firstChar = firstChar - 32; | |
1586 | ||
1587 | // To a drive number | |
1588 | unsigned int driveNo = firstChar - 64; | |
1589 | if (driveNo > 0) | |
1590 | { | |
1591 | unsigned int noDrives; | |
1592 | _dos_setdrive(driveNo, &noDrives); | |
1593 | } | |
1594 | } | |
1595 | bool success = (chdir(WXSTRINGCAST d) == 0); | |
1596 | ||
1597 | return success; | |
1598 | #endif | |
1599 | ||
1600 | #endif | |
1601 | } | |
1602 | ||
631f1bfe JS |
1603 | // Get the OS directory if appropriate (such as the Windows directory). |
1604 | // On non-Windows platform, probably just return the empty string. | |
1605 | wxString wxGetOSDirectory() | |
1606 | { | |
04ef50df | 1607 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
50920146 | 1608 | wxChar buf[256]; |
631f1bfe JS |
1609 | GetWindowsDirectory(buf, 256); |
1610 | return wxString(buf); | |
1611 | #else | |
1612 | return wxEmptyString; | |
1613 | #endif | |
1614 | } | |
1615 | ||
a907da15 | 1616 | bool wxEndsWithPathSeparator(const wxChar *pszFileName) |
c801d85f | 1617 | { |
903b61cc VZ |
1618 | size_t len = wxStrlen(pszFileName); |
1619 | ||
1620 | return len && wxIsPathSeparator(pszFileName[len - 1]); | |
c801d85f KB |
1621 | } |
1622 | ||
dc259b79 | 1623 | // find a file in a list of directories, returns FALSE if not found |
50920146 | 1624 | bool wxFindFileInPath(wxString *pStr, const wxChar *pszPath, const wxChar *pszFile) |
c801d85f | 1625 | { |
a17e237f | 1626 | // we assume that it's not empty |
dc259b79 | 1627 | wxCHECK_MSG( !wxIsEmpty(pszFile), FALSE, |
f6bcfd97 | 1628 | _T("empty file name in wxFindFileInPath")); |
a17e237f VZ |
1629 | |
1630 | // skip path separator in the beginning of the file name if present | |
1631 | if ( wxIsPathSeparator(*pszFile) ) | |
1632 | pszFile++; | |
1633 | ||
1634 | // copy the path (strtok will modify it) | |
1635 | wxChar *szPath = new wxChar[wxStrlen(pszPath) + 1]; | |
1636 | wxStrcpy(szPath, pszPath); | |
1637 | ||
1638 | wxString strFile; | |
1639 | wxChar *pc, *save_ptr; | |
1640 | for ( pc = wxStrtok(szPath, wxPATH_SEP, &save_ptr); | |
1641 | pc != NULL; | |
1642 | pc = wxStrtok((wxChar *) NULL, wxPATH_SEP, &save_ptr) ) | |
1643 | { | |
1644 | // search for the file in this directory | |
1645 | strFile = pc; | |
1646 | if ( !wxEndsWithPathSeparator(pc) ) | |
1647 | strFile += wxFILE_SEP_PATH; | |
1648 | strFile += pszFile; | |
1649 | ||
2b5f62a0 | 1650 | if ( wxFileExists(strFile) ) { |
a17e237f VZ |
1651 | *pStr = strFile; |
1652 | break; | |
1653 | } | |
c801d85f | 1654 | } |
c801d85f | 1655 | |
a17e237f VZ |
1656 | // suppress warning about unused variable save_ptr when wxStrtok() is a |
1657 | // macro which throws away its third argument | |
1658 | save_ptr = pc; | |
1659 | ||
1660 | delete [] szPath; | |
c801d85f | 1661 | |
dc259b79 | 1662 | return pc != NULL; // if TRUE => we breaked from the loop |
c801d85f KB |
1663 | } |
1664 | ||
50920146 | 1665 | void WXDLLEXPORT wxSplitPath(const wxChar *pszFileName, |
3826db3e VZ |
1666 | wxString *pstrPath, |
1667 | wxString *pstrName, | |
1668 | wxString *pstrExt) | |
1669 | { | |
d37fd2fa | 1670 | // it can be empty, but it shouldn't be NULL |
223d09f6 | 1671 | wxCHECK_RET( pszFileName, wxT("NULL file name in wxSplitPath") ); |
3826db3e | 1672 | |
9e8d8607 | 1673 | wxFileName::SplitPath(pszFileName, pstrPath, pstrName, pstrExt); |
3826db3e | 1674 | } |
7f555861 | 1675 | |
a47ce4a7 VS |
1676 | time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename) |
1677 | { | |
f6bcfd97 | 1678 | wxStructStat buf; |
92980e90 | 1679 | wxStat( filename, &buf); |
dc259b79 | 1680 | |
a47ce4a7 VS |
1681 | return buf.st_mtime; |
1682 | } | |
1683 | ||
1684 | ||
7f555861 JS |
1685 | //------------------------------------------------------------------------ |
1686 | // wild character routines | |
1687 | //------------------------------------------------------------------------ | |
1688 | ||
1689 | bool wxIsWild( const wxString& pattern ) | |
1690 | { | |
2b5f62a0 VZ |
1691 | wxString tmp = pattern; |
1692 | wxChar *pat = WXSTRINGCAST(tmp); | |
dc259b79 | 1693 | while (*pat) |
2b5f62a0 | 1694 | { |
dc259b79 | 1695 | switch (*pat++) |
2b5f62a0 | 1696 | { |
223d09f6 | 1697 | case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'): |
dc259b79 | 1698 | return TRUE; |
223d09f6 | 1699 | case wxT('\\'): |
3f4a0c5b | 1700 | if (!*pat++) |
dc259b79 | 1701 | return FALSE; |
3f4a0c5b | 1702 | } |
7f555861 | 1703 | } |
dc259b79 | 1704 | return FALSE; |
dfcb1ae0 | 1705 | } |
dfcb1ae0 | 1706 | |
2b5f62a0 VZ |
1707 | /* |
1708 | * Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU> | |
1709 | * | |
1710 | * The match procedure is public domain code (from ircII's reg.c) | |
1711 | */ | |
7be1f0d9 | 1712 | |
2b5f62a0 | 1713 | bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) |
7f555861 | 1714 | { |
2b5f62a0 VZ |
1715 | if (text.empty()) |
1716 | { | |
1717 | /* Match if both are empty. */ | |
1718 | return pat.empty(); | |
1719 | } | |
dc259b79 | 1720 | |
2b5f62a0 VZ |
1721 | const wxChar *m = pat.c_str(), |
1722 | *n = text.c_str(), | |
1723 | *ma = NULL, | |
1724 | *na = NULL, | |
1725 | *mp = NULL, | |
1726 | *np = NULL; | |
1727 | int just = 0, | |
1728 | pcount = 0, | |
1729 | acount = 0, | |
1730 | count = 0; | |
1731 | ||
1732 | if (dot_special && (*n == wxT('.'))) | |
1733 | { | |
dc259b79 | 1734 | /* Never match so that hidden Unix files |
2b5f62a0 | 1735 | * are never found. */ |
dc259b79 | 1736 | return FALSE; |
2b5f62a0 | 1737 | } |
7f555861 | 1738 | |
2b5f62a0 VZ |
1739 | for (;;) |
1740 | { | |
1741 | if (*m == wxT('*')) | |
1742 | { | |
1743 | ma = ++m; | |
1744 | na = n; | |
1745 | just = 1; | |
1746 | mp = NULL; | |
1747 | acount = count; | |
3f4a0c5b | 1748 | } |
2b5f62a0 VZ |
1749 | else if (*m == wxT('?')) |
1750 | { | |
1751 | m++; | |
1752 | if (!*n++) | |
dc259b79 | 1753 | return FALSE; |
3f4a0c5b | 1754 | } |
2b5f62a0 VZ |
1755 | else |
1756 | { | |
1757 | if (*m == wxT('\\')) | |
1758 | { | |
1759 | m++; | |
1760 | /* Quoting "nothing" is a bad thing */ | |
1761 | if (!*m) | |
dc259b79 | 1762 | return FALSE; |
3f4a0c5b | 1763 | } |
2b5f62a0 VZ |
1764 | if (!*m) |
1765 | { | |
1766 | /* | |
1767 | * If we are out of both strings or we just | |
1768 | * saw a wildcard, then we can say we have a | |
1769 | * match | |
1770 | */ | |
1771 | if (!*n) | |
dc259b79 | 1772 | return TRUE; |
2b5f62a0 | 1773 | if (just) |
dc259b79 | 1774 | return TRUE; |
2b5f62a0 VZ |
1775 | just = 0; |
1776 | goto not_matched; | |
1777 | } | |
1778 | /* | |
1779 | * We could check for *n == NULL at this point, but | |
1780 | * since it's more common to have a character there, | |
1781 | * check to see if they match first (m and n) and | |
1782 | * then if they don't match, THEN we can check for | |
1783 | * the NULL of n | |
1784 | */ | |
1785 | just = 0; | |
1786 | if (*m == *n) | |
1787 | { | |
1788 | m++; | |
1789 | if (*n == wxT(' ')) | |
1790 | mp = NULL; | |
1791 | count++; | |
1792 | n++; | |
1793 | } | |
1794 | else | |
1795 | { | |
1796 | ||
1797 | not_matched: | |
1798 | ||
1799 | /* | |
1800 | * If there are no more characters in the | |
1801 | * string, but we still need to find another | |
1802 | * character (*m != NULL), then it will be | |
1803 | * impossible to match it | |
1804 | */ | |
1805 | if (!*n) | |
dc259b79 | 1806 | return FALSE; |
2b5f62a0 VZ |
1807 | if (mp) |
1808 | { | |
1809 | m = mp; | |
1810 | if (*np == wxT(' ')) | |
1811 | { | |
1812 | mp = NULL; | |
1813 | goto check_percent; | |
1814 | } | |
1815 | n = ++np; | |
1816 | count = pcount; | |
1817 | } | |
1818 | else | |
1819 | check_percent: | |
1820 | ||
1821 | if (ma) | |
1822 | { | |
1823 | m = ma; | |
1824 | n = ++na; | |
1825 | count = acount; | |
1826 | } | |
1827 | else | |
dc259b79 | 1828 | return FALSE; |
3f4a0c5b | 1829 | } |
3f4a0c5b | 1830 | } |
3f4a0c5b | 1831 | } |
2b5f62a0 | 1832 | } |
fd3f686c | 1833 | |
7f555861 | 1834 | |
3f4a0c5b | 1835 | #ifdef __VISUALC__ |
fd3f686c | 1836 | #pragma warning(default:4706) // assignment within conditional expression |
53c6e7cc | 1837 | #endif // VC++ |