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