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