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