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