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