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