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