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