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