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