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