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