]>
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 | |
55d99c7a | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
e90c1d2a VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
7af89395 | 21 | #pragma implementation "filefn.h" |
c801d85f KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | #include "wx/defs.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
7af89395 | 29 | #pragma hdrstop |
c801d85f KB |
30 | #endif |
31 | ||
c801d85f | 32 | #include "wx/utils.h" |
3096bd2f | 33 | #include "wx/intl.h" |
32f31043 | 34 | #include "wx/file.h" |
9e8d8607 | 35 | #include "wx/filename.h" |
8ff12342 | 36 | #include "wx/dir.h" |
c801d85f | 37 | |
fd3f686c | 38 | // there are just too many of those... |
3f4a0c5b | 39 | #ifdef __VISUALC__ |
fd3f686c VZ |
40 | #pragma warning(disable:4706) // assignment within conditional expression |
41 | #endif // VC++ | |
42 | ||
c801d85f KB |
43 | #include <ctype.h> |
44 | #include <stdio.h> | |
45 | #include <stdlib.h> | |
46 | #include <string.h> | |
47 | #if !defined(__WATCOMC__) | |
3f4a0c5b VZ |
48 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
49 | #include <errno.h> | |
50 | #endif | |
c801d85f | 51 | #endif |
3f4a0c5b | 52 | |
76a5e5d2 SC |
53 | #if defined(__WXMAC__) |
54 | #include "wx/mac/private.h" // includes mac headers | |
55 | #endif | |
56 | ||
1c193821 JS |
57 | #ifdef __WXWINCE__ |
58 | #include "wx/msw/wince/time.h" | |
59 | #include "wx/msw/private.h" | |
60 | #else | |
c801d85f | 61 | #include <time.h> |
1c193821 | 62 | #endif |
3f4a0c5b | 63 | |
1c193821 JS |
64 | #ifdef __WXWINCE__ |
65 | // Nothing | |
66 | #elif !defined(__MWERKS__) | |
7af89395 VZ |
67 | #include <sys/types.h> |
68 | #include <sys/stat.h> | |
31907d03 SC |
69 | #else |
70 | #ifdef __MACH__ | |
71 | #include <sys/types.h> | |
72 | #include <utime.h> | |
73 | #include <sys/stat.h> | |
74 | #include <unistd.h> | |
469e1e5c | 75 | #else |
7af89395 VZ |
76 | #include <stat.h> |
77 | #include <unistd.h> | |
5b781a67 | 78 | #include <unix.h> |
c4e41ce3 | 79 | #include <fcntl.h> |
469e1e5c | 80 | #endif |
31907d03 | 81 | #endif |
c801d85f | 82 | |
aad5220b | 83 | #ifdef __UNIX__ |
7af89395 VZ |
84 | #include <unistd.h> |
85 | #include <dirent.h> | |
92980e90 | 86 | #include <fcntl.h> |
aad5220b | 87 | #endif |
c801d85f | 88 | |
f6bcfd97 | 89 | #ifdef __WXPM__ |
c2ff79b1 | 90 | #include <process.h> |
f6bcfd97 | 91 | #include "wx/os2/private.h" |
c2ff79b1 | 92 | #endif |
b4da152e | 93 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
1c193821 | 94 | #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__) && !defined(__WXWINCE__) |
7af89395 VZ |
95 | #include <direct.h> |
96 | #include <dos.h> | |
32f31043 | 97 | #include <io.h> |
7af89395 VZ |
98 | #endif // __WINDOWS__ |
99 | #endif // native Win compiler | |
c801d85f | 100 | |
b916f809 VS |
101 | #if defined(__DOS__) |
102 | #ifdef __WATCOMC__ | |
103 | #include <direct.h> | |
104 | #include <dos.h> | |
105 | #include <io.h> | |
106 | #endif | |
107 | #ifdef __DJGPP__ | |
108 | #include <unistd.h> | |
109 | #endif | |
f48fa475 VS |
110 | #endif |
111 | ||
c801d85f KB |
112 | #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs |
113 | // this (3.1 I believe) and how to test for it. | |
114 | // If this works for Borland 4.0 as well, then no worries. | |
7af89395 | 115 | #include <dir.h> |
c801d85f KB |
116 | #endif |
117 | ||
a3ef5bf5 | 118 | #ifdef __SALFORDC__ |
7af89395 VZ |
119 | #include <dir.h> |
120 | #include <unix.h> | |
a3ef5bf5 JS |
121 | #endif |
122 | ||
f5abe911 | 123 | #include "wx/log.h" |
99cc00ed | 124 | |
7be1f0d9 JS |
125 | // No, Cygwin doesn't appear to have fnmatch.h after all. |
126 | #if defined(HAVE_FNMATCH_H) | |
7af89395 | 127 | #include "fnmatch.h" |
dfcb1ae0 KB |
128 | #endif |
129 | ||
34138703 | 130 | #ifdef __WINDOWS__ |
9ed0d735 | 131 | #include "wx/msw/wrapwin.h" |
3d5231db | 132 | #include "wx/msw/mslu.h" |
b0091f58 | 133 | |
29d0a26e MB |
134 | // for _getcwd |
135 | #ifdef __MINGW32__ | |
136 | #include <io.h> | |
137 | #endif | |
138 | ||
3ffbc733 VZ |
139 | // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path() |
140 | // | |
141 | // note that it must be included after <windows.h> | |
142 | #ifdef __GNUWIN32__ | |
e02e8816 MB |
143 | #ifdef __CYGWIN__ |
144 | #include <sys/cygwin.h> | |
145 | #endif | |
3ffbc733 VZ |
146 | #endif // __GNUWIN32__ |
147 | #endif // __WINDOWS__ | |
c801d85f | 148 | |
13b1f8a7 VZ |
149 | // TODO: Borland probably has _wgetcwd as well? |
150 | #ifdef _MSC_VER | |
151 | #define HAVE_WGETCWD | |
152 | #endif | |
153 | ||
e90c1d2a VZ |
154 | // ---------------------------------------------------------------------------- |
155 | // constants | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
13b1f8a7 VZ |
158 | #ifndef _MAXPATHLEN |
159 | #define _MAXPATHLEN 1024 | |
160 | #endif | |
c801d85f | 161 | |
da2b4b7a | 162 | #ifdef __WXMAC__ |
2d4e4f80 GD |
163 | # ifdef __DARWIN__ |
164 | # include "MoreFilesX.h" | |
165 | # else | |
da2b4b7a GD |
166 | # include "MoreFiles.h" |
167 | # include "MoreFilesExtras.h" | |
168 | # include "FullPath.h" | |
169 | # include "FSpCompat.h" | |
2d4e4f80 | 170 | # endif |
17dff81c | 171 | #endif |
c801d85f | 172 | |
e90c1d2a VZ |
173 | // ---------------------------------------------------------------------------- |
174 | // private globals | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
1f1070e2 | 177 | // MT-FIXME: get rid of this horror and all code using it |
e90c1d2a VZ |
178 | static wxChar wxFileFunctionsBuffer[4*_MAXPATHLEN]; |
179 | ||
5d33ed2c DW |
180 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
181 | // | |
32e768ae | 182 | // VisualAge C++ V4.0 cannot have any external linkage const decs |
5d33ed2c DW |
183 | // in headers included by more than one primary source |
184 | // | |
185 | const off_t wxInvalidOffset = (off_t)-1; | |
186 | #endif | |
187 | ||
a339970a VZ |
188 | // ---------------------------------------------------------------------------- |
189 | // macros | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
192 | // we need to translate Mac filenames before passing them to OS functions | |
334d2448 | 193 | #define OS_FILENAME(s) (s.fn_str()) |
a339970a | 194 | |
e90c1d2a VZ |
195 | // ============================================================================ |
196 | // implementation | |
197 | // ============================================================================ | |
198 | ||
92980e90 RR |
199 | #ifdef wxNEED_WX_UNISTD_H |
200 | ||
201 | WXDLLEXPORT int wxStat( const wxChar *file_name, wxStructStat *buf ) | |
202 | { | |
203 | return stat( wxConvFile.cWX2MB( file_name ), buf ); | |
204 | } | |
205 | ||
206 | WXDLLEXPORT int wxAccess( const wxChar *pathname, int mode ) | |
207 | { | |
208 | return access( wxConvFile.cWX2MB( pathname ), mode ); | |
209 | } | |
210 | ||
211 | WXDLLEXPORT int wxOpen( const wxChar *pathname, int flags, mode_t mode ) | |
212 | { | |
213 | return open( wxConvFile.cWX2MB( pathname ), flags, mode ); | |
214 | } | |
215 | ||
216 | #endif | |
217 | // wxNEED_WX_UNISTD_H | |
218 | ||
219 | // ---------------------------------------------------------------------------- | |
220 | // wxPathList | |
221 | // ---------------------------------------------------------------------------- | |
222 | ||
df5168c4 | 223 | // IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList) |
92980e90 | 224 | |
f526f752 MB |
225 | static inline wxChar* MYcopystring(const wxString& s) |
226 | { | |
227 | wxChar* copy = new wxChar[s.length() + 1]; | |
228 | return wxStrcpy(copy, s.c_str()); | |
229 | } | |
230 | ||
231 | static inline wxChar* MYcopystring(const wxChar* s) | |
232 | { | |
233 | wxChar* copy = new wxChar[wxStrlen(s) + 1]; | |
234 | return wxStrcpy(copy, s); | |
235 | } | |
236 | ||
c801d85f KB |
237 | void wxPathList::Add (const wxString& path) |
238 | { | |
fd82f4e6 | 239 | wxStringList::Add (WXSTRINGCAST path); |
c801d85f KB |
240 | } |
241 | ||
242 | // Add paths e.g. from the PATH environment variable | |
243 | void wxPathList::AddEnvList (const wxString& envVariable) | |
244 | { | |
1c193821 JS |
245 | // No environment variables on WinCE |
246 | #ifndef __WXWINCE__ | |
db4444f0 | 247 | static const wxChar PATH_TOKS[] = |
34138703 | 248 | #ifdef __WINDOWS__ |
db4444f0 CE |
249 | /* |
250 | The space has been removed from the tokenizers, otherwise a | |
251 | path such as "C:\Program Files" would be split into 2 paths: | |
252 | "C:\Program" and "Files" | |
253 | */ | |
254 | // wxT(" ;"); // Don't seperate with colon in DOS (used for drive) | |
255 | wxT(";"); // Don't seperate with colon in DOS (used for drive) | |
c801d85f | 256 | #else |
223d09f6 | 257 | wxT(" :;"); |
c801d85f KB |
258 | #endif |
259 | ||
db4444f0 CE |
260 | wxChar *val = wxGetenv (WXSTRINGCAST envVariable); |
261 | if (val && *val) | |
c801d85f | 262 | { |
f526f752 | 263 | wxChar *s = MYcopystring (val); |
db4444f0 CE |
264 | wxChar *save_ptr, *token = wxStrtok (s, PATH_TOKS, &save_ptr); |
265 | ||
266 | if (token) | |
267 | { | |
268 | Add(token); | |
269 | while (token) | |
270 | { | |
271 | if ( (token = wxStrtok ((wxChar *) NULL, PATH_TOKS, &save_ptr)) | |
272 | != NULL ) | |
273 | { | |
274 | Add(token); | |
275 | } | |
276 | } | |
277 | } | |
278 | ||
279 | // suppress warning about unused variable save_ptr when wxStrtok() is a | |
280 | // macro which throws away its third argument | |
281 | save_ptr = token; | |
282 | ||
283 | delete [] s; | |
c801d85f | 284 | } |
1c193821 | 285 | #endif |
c801d85f KB |
286 | } |
287 | ||
288 | // Given a full filename (with path), ensure that that file can | |
289 | // be accessed again USING FILENAME ONLY by adding the path | |
290 | // to the list if not already there. | |
291 | void wxPathList::EnsureFileAccessible (const wxString& path) | |
292 | { | |
7af89395 VZ |
293 | wxString path_only(wxPathOnly(path)); |
294 | if ( !path_only.IsEmpty() ) | |
295 | { | |
296 | if ( !Member(path_only) ) | |
297 | Add(path_only); | |
298 | } | |
c801d85f KB |
299 | } |
300 | ||
301 | bool wxPathList::Member (const wxString& path) | |
302 | { | |
df5168c4 | 303 | for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext()) |
c801d85f | 304 | { |
b1d4dd7a | 305 | wxString path2( node->GetData() ); |
c801d85f | 306 | if ( |
17dff81c | 307 | #if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__) |
c801d85f | 308 | // Case INDEPENDENT |
3f4a0c5b | 309 | path.CompareTo (path2, wxString::ignoreCase) == 0 |
c801d85f | 310 | #else |
3f4a0c5b VZ |
311 | // Case sensitive File System |
312 | path.CompareTo (path2) == 0 | |
c801d85f | 313 | #endif |
3f4a0c5b | 314 | ) |
dc259b79 | 315 | return TRUE; |
c801d85f | 316 | } |
dc259b79 | 317 | return FALSE; |
c801d85f KB |
318 | } |
319 | ||
320 | wxString wxPathList::FindValidPath (const wxString& file) | |
321 | { | |
e90c1d2a VZ |
322 | if (wxFileExists (wxExpandPath(wxFileFunctionsBuffer, file))) |
323 | return wxString(wxFileFunctionsBuffer); | |
c801d85f | 324 | |
50920146 | 325 | wxChar buf[_MAXPATHLEN]; |
e90c1d2a | 326 | wxStrcpy(buf, wxFileFunctionsBuffer); |
c801d85f | 327 | |
999836aa | 328 | wxChar *filename = wxIsAbsolutePath (buf) ? wxFileNameFromPath (buf) : (wxChar *)buf; |
c801d85f | 329 | |
df5168c4 | 330 | for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext()) |
c801d85f | 331 | { |
df5168c4 | 332 | const wxChar *path = node->GetData(); |
e90c1d2a VZ |
333 | wxStrcpy (wxFileFunctionsBuffer, path); |
334 | wxChar ch = wxFileFunctionsBuffer[wxStrlen(wxFileFunctionsBuffer)-1]; | |
223d09f6 KB |
335 | if (ch != wxT('\\') && ch != wxT('/')) |
336 | wxStrcat (wxFileFunctionsBuffer, wxT("/")); | |
e90c1d2a | 337 | wxStrcat (wxFileFunctionsBuffer, filename); |
34138703 | 338 | #ifdef __WINDOWS__ |
2b5f62a0 | 339 | wxUnix2DosFilename (wxFileFunctionsBuffer); |
c801d85f | 340 | #endif |
e90c1d2a | 341 | if (wxFileExists (wxFileFunctionsBuffer)) |
c801d85f | 342 | { |
e90c1d2a | 343 | return wxString(wxFileFunctionsBuffer); // Found! |
c801d85f | 344 | } |
3f4a0c5b | 345 | } // for() |
c801d85f | 346 | |
b1d4dd7a | 347 | return wxEmptyString; // Not found |
c801d85f KB |
348 | } |
349 | ||
350 | wxString wxPathList::FindAbsoluteValidPath (const wxString& file) | |
351 | { | |
e90c1d2a VZ |
352 | wxString f = FindValidPath(file); |
353 | if ( wxIsAbsolutePath(f) ) | |
354 | return f; | |
355 | ||
356 | wxString buf; | |
13b1f8a7 VZ |
357 | wxGetWorkingDirectory(wxStringBuffer(buf, _MAXPATHLEN), _MAXPATHLEN); |
358 | ||
e90c1d2a | 359 | if ( !wxEndsWithPathSeparator(buf) ) |
c801d85f | 360 | { |
e90c1d2a | 361 | buf += wxFILE_SEP_PATH; |
c801d85f | 362 | } |
e90c1d2a VZ |
363 | buf += f; |
364 | ||
365 | return buf; | |
c801d85f KB |
366 | } |
367 | ||
3f4a0c5b | 368 | bool |
c801d85f KB |
369 | wxFileExists (const wxString& filename) |
370 | { | |
4ea2c29f VZ |
371 | // we must use GetFileAttributes() instead of the ANSI C functions because |
372 | // it can cope with network (UNC) paths unlike them | |
2db300c6 | 373 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
2db300c6 | 374 | DWORD ret = ::GetFileAttributes(filename); |
2db300c6 | 375 | |
a28ae409 | 376 | return (ret != (DWORD)-1) && !(ret & FILE_ATTRIBUTE_DIRECTORY); |
4ea2c29f VZ |
377 | #else // !__WIN32__ |
378 | wxStructStat st; | |
379 | return wxStat(filename, &st) == 0 && (st.st_mode & S_IFREG); | |
380 | #endif // __WIN32__/!__WIN32__ | |
c801d85f KB |
381 | } |
382 | ||
3f4a0c5b | 383 | bool |
c801d85f KB |
384 | wxIsAbsolutePath (const wxString& filename) |
385 | { | |
2985ad5d RR |
386 | if (filename != wxT("")) |
387 | { | |
e8e1d09e GD |
388 | #if defined(__WXMAC__) && !defined(__DARWIN__) |
389 | // Classic or Carbon CodeWarrior like | |
390 | // Carbon with Apple DevTools is Unix like | |
2db300c6 | 391 | |
2985ad5d RR |
392 | // This seems wrong to me, but there is no fix. since |
393 | // "MacOS:MyText.txt" is absolute whereas "MyDir:MyText.txt" | |
394 | // is not. Or maybe ":MyDir:MyText.txt" has to be used? RR. | |
2985ad5d | 395 | if (filename.Find(':') != wxNOT_FOUND && filename[0] != ':') |
dc259b79 | 396 | return TRUE ; |
cd378f94 | 397 | #else |
e8e1d09e GD |
398 | // Unix like or Windows |
399 | if (filename[0] == wxT('/')) | |
dc259b79 | 400 | return TRUE; |
e8e1d09e | 401 | #endif |
c801d85f | 402 | #ifdef __VMS__ |
e8e1d09e | 403 | if ((filename[0] == wxT('[') && filename[1] != wxT('.'))) |
dc259b79 | 404 | return TRUE; |
c801d85f | 405 | #endif |
34138703 | 406 | #ifdef __WINDOWS__ |
e8e1d09e GD |
407 | // MSDOS like |
408 | if (filename[0] == wxT('\\') || (wxIsalpha (filename[0]) && filename[1] == wxT(':'))) | |
dc259b79 | 409 | return TRUE; |
c801d85f | 410 | #endif |
c801d85f | 411 | } |
dc259b79 | 412 | return FALSE ; |
c801d85f KB |
413 | } |
414 | ||
415 | /* | |
416 | * Strip off any extension (dot something) from end of file, | |
417 | * IF one exists. Inserts zero into buffer. | |
418 | * | |
419 | */ | |
3f4a0c5b | 420 | |
50920146 | 421 | void wxStripExtension(wxChar *buffer) |
c801d85f | 422 | { |
50920146 | 423 | int len = wxStrlen(buffer); |
c801d85f KB |
424 | int i = len-1; |
425 | while (i > 0) | |
426 | { | |
223d09f6 | 427 | if (buffer[i] == wxT('.')) |
c801d85f KB |
428 | { |
429 | buffer[i] = 0; | |
430 | break; | |
431 | } | |
432 | i --; | |
433 | } | |
434 | } | |
435 | ||
47fa7969 JS |
436 | void wxStripExtension(wxString& buffer) |
437 | { | |
438 | size_t len = buffer.Length(); | |
439 | size_t i = len-1; | |
440 | while (i > 0) | |
441 | { | |
223d09f6 | 442 | if (buffer.GetChar(i) == wxT('.')) |
47fa7969 JS |
443 | { |
444 | buffer = buffer.Left(i); | |
445 | break; | |
446 | } | |
447 | i --; | |
448 | } | |
449 | } | |
450 | ||
c801d85f | 451 | // Destructive removal of /./ and /../ stuff |
50920146 | 452 | wxChar *wxRealPath (wxChar *path) |
c801d85f | 453 | { |
2049ba38 | 454 | #ifdef __WXMSW__ |
223d09f6 | 455 | static const wxChar SEP = wxT('\\'); |
2b5f62a0 | 456 | wxUnix2DosFilename(path); |
c801d85f | 457 | #else |
223d09f6 | 458 | static const wxChar SEP = wxT('/'); |
c801d85f KB |
459 | #endif |
460 | if (path[0] && path[1]) { | |
461 | /* MATTHEW: special case "/./x" */ | |
50920146 | 462 | wxChar *p; |
223d09f6 | 463 | if (path[2] == SEP && path[1] == wxT('.')) |
c801d85f KB |
464 | p = &path[0]; |
465 | else | |
466 | p = &path[2]; | |
467 | for (; *p; p++) | |
468 | { | |
3f4a0c5b VZ |
469 | if (*p == SEP) |
470 | { | |
223d09f6 | 471 | if (p[1] == wxT('.') && p[2] == wxT('.') && (p[3] == SEP || p[3] == wxT('\0'))) |
3f4a0c5b | 472 | { |
50920146 | 473 | wxChar *q; |
f0e1c343 JS |
474 | for (q = p - 1; q >= path && *q != SEP; q--) |
475 | { | |
476 | // Empty | |
477 | } | |
478 | ||
223d09f6 | 479 | if (q[0] == SEP && (q[1] != wxT('.') || q[2] != wxT('.') || q[3] != SEP) |
3f4a0c5b VZ |
480 | && (q - 1 <= path || q[-1] != SEP)) |
481 | { | |
50920146 | 482 | wxStrcpy (q, p + 3); |
223d09f6 | 483 | if (path[0] == wxT('\0')) |
3f4a0c5b VZ |
484 | { |
485 | path[0] = SEP; | |
223d09f6 | 486 | path[1] = wxT('\0'); |
3f4a0c5b | 487 | } |
2049ba38 | 488 | #ifdef __WXMSW__ |
3f4a0c5b | 489 | /* Check that path[2] is NULL! */ |
223d09f6 | 490 | else if (path[1] == wxT(':') && !path[2]) |
3f4a0c5b VZ |
491 | { |
492 | path[2] = SEP; | |
223d09f6 | 493 | path[3] = wxT('\0'); |
3f4a0c5b VZ |
494 | } |
495 | #endif | |
496 | p = q - 1; | |
497 | } | |
498 | } | |
223d09f6 | 499 | else if (p[1] == wxT('.') && (p[2] == SEP || p[2] == wxT('\0'))) |
50920146 | 500 | wxStrcpy (p, p + 2); |
3f4a0c5b | 501 | } |
c801d85f KB |
502 | } |
503 | } | |
504 | return path; | |
505 | } | |
506 | ||
507 | // Must be destroyed | |
50920146 | 508 | wxChar *wxCopyAbsolutePath(const wxString& filename) |
c801d85f | 509 | { |
223d09f6 | 510 | if (filename == wxT("")) |
50920146 | 511 | return (wxChar *) NULL; |
c801d85f | 512 | |
2b5f62a0 | 513 | if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer, filename))) { |
50920146 | 514 | wxChar buf[_MAXPATHLEN]; |
223d09f6 | 515 | buf[0] = wxT('\0'); |
7af89395 | 516 | wxGetWorkingDirectory(buf, WXSIZEOF(buf)); |
50920146 | 517 | wxChar ch = buf[wxStrlen(buf) - 1]; |
2049ba38 | 518 | #ifdef __WXMSW__ |
223d09f6 KB |
519 | if (ch != wxT('\\') && ch != wxT('/')) |
520 | wxStrcat(buf, wxT("\\")); | |
c801d85f | 521 | #else |
223d09f6 KB |
522 | if (ch != wxT('/')) |
523 | wxStrcat(buf, wxT("/")); | |
c801d85f | 524 | #endif |
e90c1d2a | 525 | wxStrcat(buf, wxFileFunctionsBuffer); |
f526f752 | 526 | return MYcopystring( wxRealPath(buf) ); |
c801d85f | 527 | } |
f526f752 | 528 | return MYcopystring( wxFileFunctionsBuffer ); |
c801d85f KB |
529 | } |
530 | ||
531 | /*- | |
532 | Handles: | |
533 | ~/ => home dir | |
534 | ~user/ => user's home dir | |
535 | If the environment variable a = "foo" and b = "bar" then: | |
536 | Unix: | |
3f4a0c5b VZ |
537 | $a => foo |
538 | $a$b => foobar | |
539 | $a.c => foo.c | |
540 | xxx$a => xxxfoo | |
541 | ${a}! => foo! | |
542 | $(b)! => bar! | |
543 | \$a => \$a | |
c801d85f | 544 | MSDOS: |
3f4a0c5b VZ |
545 | $a ==> $a |
546 | $(a) ==> foo | |
547 | $(a)$b ==> foo$b | |
548 | $(a)$(b)==> foobar | |
549 | test.$$ ==> test.$$ | |
c801d85f KB |
550 | */ |
551 | ||
552 | /* input name in name, pathname output to buf. */ | |
553 | ||
50920146 | 554 | wxChar *wxExpandPath(wxChar *buf, const wxChar *name) |
c801d85f | 555 | { |
50920146 OK |
556 | register wxChar *d, *s, *nm; |
557 | wxChar lnm[_MAXPATHLEN]; | |
33ac7e6f | 558 | int q; |
c801d85f KB |
559 | |
560 | // Some compilers don't like this line. | |
223d09f6 | 561 | // const wxChar trimchars[] = wxT("\n \t"); |
c801d85f | 562 | |
50920146 | 563 | wxChar trimchars[4]; |
223d09f6 KB |
564 | trimchars[0] = wxT('\n'); |
565 | trimchars[1] = wxT(' '); | |
566 | trimchars[2] = wxT('\t'); | |
c801d85f KB |
567 | trimchars[3] = 0; |
568 | ||
2049ba38 | 569 | #ifdef __WXMSW__ |
223d09f6 | 570 | const wxChar SEP = wxT('\\'); |
c801d85f | 571 | #else |
223d09f6 | 572 | const wxChar SEP = wxT('/'); |
c801d85f | 573 | #endif |
223d09f6 KB |
574 | buf[0] = wxT('\0'); |
575 | if (name == NULL || *name == wxT('\0')) | |
3f4a0c5b | 576 | return buf; |
f526f752 | 577 | nm = MYcopystring(name); // Make a scratch copy |
50920146 | 578 | wxChar *nm_tmp = nm; |
c801d85f KB |
579 | |
580 | /* Skip leading whitespace and cr */ | |
50920146 | 581 | while (wxStrchr((wxChar *)trimchars, *nm) != NULL) |
3f4a0c5b | 582 | nm++; |
c801d85f | 583 | /* And strip off trailing whitespace and cr */ |
50920146 OK |
584 | s = nm + (q = wxStrlen(nm)) - 1; |
585 | while (q-- && wxStrchr((wxChar *)trimchars, *s) != NULL) | |
223d09f6 | 586 | *s = wxT('\0'); |
c801d85f KB |
587 | |
588 | s = nm; | |
589 | d = lnm; | |
2049ba38 | 590 | #ifdef __WXMSW__ |
dc259b79 | 591 | q = FALSE; |
c801d85f | 592 | #else |
223d09f6 | 593 | q = nm[0] == wxT('\\') && nm[1] == wxT('~'); |
c801d85f KB |
594 | #endif |
595 | ||
596 | /* Expand inline environment variables */ | |
c2ff79b1 DW |
597 | #ifdef __VISAGECPP__ |
598 | while (*d) | |
599 | { | |
600 | *d++ = *s; | |
223d09f6 | 601 | if(*s == wxT('\\')) |
c2ff79b1 DW |
602 | { |
603 | *(d - 1) = *++s; | |
604 | if (*d) | |
605 | { | |
606 | s++; | |
607 | continue; | |
608 | } | |
609 | else | |
610 | break; | |
611 | } | |
612 | else | |
613 | #else | |
22394d24 | 614 | while ((*d++ = *s) != 0) { |
c2ff79b1 | 615 | # ifndef __WXMSW__ |
223d09f6 | 616 | if (*s == wxT('\\')) { |
3f4a0c5b VZ |
617 | if ((*(d - 1) = *++s)) { |
618 | s++; | |
619 | continue; | |
620 | } else | |
621 | break; | |
622 | } else | |
c2ff79b1 | 623 | # endif |
c801d85f | 624 | #endif |
1c193821 JS |
625 | // No env variables on WinCE |
626 | #ifndef __WXWINCE__ | |
2049ba38 | 627 | #ifdef __WXMSW__ |
223d09f6 | 628 | if (*s++ == wxT('$') && (*s == wxT('{') || *s == wxT(')'))) |
c801d85f | 629 | #else |
223d09f6 | 630 | if (*s++ == wxT('$')) |
3f4a0c5b VZ |
631 | #endif |
632 | { | |
50920146 | 633 | register wxChar *start = d; |
223d09f6 | 634 | register int braces = (*s == wxT('{') || *s == wxT('(')); |
50920146 | 635 | register wxChar *value; |
22394d24 | 636 | while ((*d++ = *s) != 0) |
223d09f6 | 637 | if (braces ? (*s == wxT('}') || *s == wxT(')')) : !(wxIsalnum(*s) || *s == wxT('_')) ) |
3f4a0c5b VZ |
638 | break; |
639 | else | |
640 | s++; | |
641 | *--d = 0; | |
50920146 | 642 | value = wxGetenv(braces ? start + 1 : start); |
3f4a0c5b | 643 | if (value) { |
f0e1c343 JS |
644 | for ((d = start - 1); (*d++ = *value++) != 0;) |
645 | { | |
646 | // Empty | |
647 | } | |
648 | ||
3f4a0c5b VZ |
649 | d--; |
650 | if (braces && *s) | |
651 | s++; | |
652 | } | |
653 | } | |
1c193821 JS |
654 | #endif |
655 | // __WXWINCE__ | |
c801d85f KB |
656 | } |
657 | ||
658 | /* Expand ~ and ~user */ | |
659 | nm = lnm; | |
223d09f6 | 660 | if (nm[0] == wxT('~') && !q) |
c801d85f | 661 | { |
3f4a0c5b VZ |
662 | /* prefix ~ */ |
663 | if (nm[1] == SEP || nm[1] == 0) | |
664 | { /* ~/filename */ | |
f6bcfd97 | 665 | // FIXME: wxGetUserHome could return temporary storage in Unicode mode |
223d09f6 | 666 | if ((s = WXSTRINGCAST wxGetUserHome(wxT(""))) != NULL) { |
3f4a0c5b VZ |
667 | if (*++nm) |
668 | nm++; | |
669 | } | |
c801d85f | 670 | } else |
3f4a0c5b | 671 | { /* ~user/filename */ |
50920146 OK |
672 | register wxChar *nnm; |
673 | register wxChar *home; | |
f0e1c343 JS |
674 | for (s = nm; *s && *s != SEP; s++) |
675 | { | |
676 | // Empty | |
677 | } | |
3f4a0c5b | 678 | int was_sep; /* MATTHEW: Was there a separator, or NULL? */ |
c801d85f | 679 | was_sep = (*s == SEP); |
3f4a0c5b VZ |
680 | nnm = *s ? s + 1 : s; |
681 | *s = 0; | |
f6bcfd97 | 682 | // FIXME: wxGetUserHome could return temporary storage in Unicode mode |
0e6667e2 | 683 | if ((home = WXSTRINGCAST wxGetUserHome(wxString(nm + 1))) == NULL) { |
c801d85f | 684 | if (was_sep) /* replace only if it was there: */ |
3f4a0c5b | 685 | *s = SEP; |
295272bd | 686 | s = NULL; |
3f4a0c5b VZ |
687 | } else { |
688 | nm = nnm; | |
689 | s = home; | |
690 | } | |
691 | } | |
c801d85f KB |
692 | } |
693 | ||
694 | d = buf; | |
695 | if (s && *s) { /* MATTHEW: s could be NULL if user '~' didn't exist */ | |
3f4a0c5b | 696 | /* Copy home dir */ |
223d09f6 | 697 | while (wxT('\0') != (*d++ = *s++)) |
3f4a0c5b VZ |
698 | /* loop */; |
699 | // Handle root home | |
700 | if (d - 1 > buf && *(d - 2) != SEP) | |
701 | *(d - 1) = SEP; | |
c801d85f KB |
702 | } |
703 | s = nm; | |
f0e1c343 JS |
704 | while ((*d++ = *s++) != 0) |
705 | { | |
706 | // Empty | |
707 | } | |
c801d85f KB |
708 | delete[] nm_tmp; // clean up alloc |
709 | /* Now clean up the buffer */ | |
710 | return wxRealPath(buf); | |
711 | } | |
712 | ||
c801d85f KB |
713 | /* Contract Paths to be build upon an environment variable |
714 | component: | |
715 | ||
716 | example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib | |
717 | ||
718 | The call wxExpandPath can convert these back! | |
719 | */ | |
50920146 | 720 | wxChar * |
c801d85f KB |
721 | wxContractPath (const wxString& filename, const wxString& envname, const wxString& user) |
722 | { | |
50920146 | 723 | static wxChar dest[_MAXPATHLEN]; |
c801d85f | 724 | |
223d09f6 | 725 | if (filename == wxT("")) |
50920146 | 726 | return (wxChar *) NULL; |
c801d85f | 727 | |
50920146 | 728 | wxStrcpy (dest, WXSTRINGCAST filename); |
2049ba38 | 729 | #ifdef __WXMSW__ |
2b5f62a0 | 730 | wxUnix2DosFilename(dest); |
c801d85f KB |
731 | #endif |
732 | ||
733 | // Handle environment | |
999836aa | 734 | const wxChar *val; |
1c193821 | 735 | #ifndef __WXWINCE__ |
999836aa | 736 | wxChar *tcp; |
50920146 OK |
737 | if (envname != WXSTRINGCAST NULL && (val = wxGetenv (WXSTRINGCAST envname)) != NULL && |
738 | (tcp = wxStrstr (dest, val)) != NULL) | |
c801d85f | 739 | { |
e90c1d2a | 740 | wxStrcpy (wxFileFunctionsBuffer, tcp + wxStrlen (val)); |
223d09f6 KB |
741 | *tcp++ = wxT('$'); |
742 | *tcp++ = wxT('{'); | |
50920146 | 743 | wxStrcpy (tcp, WXSTRINGCAST envname); |
223d09f6 | 744 | wxStrcat (tcp, wxT("}")); |
e90c1d2a | 745 | wxStrcat (tcp, wxFileFunctionsBuffer); |
c801d85f | 746 | } |
1c193821 | 747 | #endif |
c801d85f KB |
748 | |
749 | // Handle User's home (ignore root homes!) | |
999836aa | 750 | size_t len; |
c801d85f | 751 | if ((val = wxGetUserHome (user)) != NULL && |
50920146 OK |
752 | (len = wxStrlen(val)) > 2 && |
753 | wxStrncmp(dest, val, len) == 0) | |
c801d85f | 754 | { |
223d09f6 KB |
755 | wxStrcpy(wxFileFunctionsBuffer, wxT("~")); |
756 | if (user != wxT("")) | |
e90c1d2a | 757 | wxStrcat(wxFileFunctionsBuffer, (const wxChar*) user); |
e90c1d2a VZ |
758 | wxStrcat(wxFileFunctionsBuffer, dest + len); |
759 | wxStrcpy (dest, wxFileFunctionsBuffer); | |
c801d85f KB |
760 | } |
761 | ||
762 | return dest; | |
763 | } | |
764 | ||
1f1070e2 | 765 | // Return just the filename, not the path (basename) |
50920146 | 766 | wxChar *wxFileNameFromPath (wxChar *path) |
c801d85f | 767 | { |
1f1070e2 VZ |
768 | wxString p = path; |
769 | wxString n = wxFileNameFromPath(p); | |
2db300c6 | 770 | |
1f1070e2 | 771 | return path + p.length() - n.length(); |
c801d85f KB |
772 | } |
773 | ||
1f1070e2 | 774 | wxString wxFileNameFromPath (const wxString& path) |
c801d85f | 775 | { |
1f1070e2 VZ |
776 | wxString name, ext; |
777 | wxFileName::SplitPath(path, NULL, &name, &ext); | |
2db300c6 | 778 | |
1f1070e2 VZ |
779 | wxString fullname = name; |
780 | if ( !ext.empty() ) | |
781 | { | |
782 | fullname << wxFILE_SEP_EXT << ext; | |
c801d85f | 783 | } |
1f1070e2 VZ |
784 | |
785 | return fullname; | |
c801d85f KB |
786 | } |
787 | ||
788 | // Return just the directory, or NULL if no directory | |
50920146 OK |
789 | wxChar * |
790 | wxPathOnly (wxChar *path) | |
c801d85f | 791 | { |
e8e1d09e | 792 | if (path && *path) |
c801d85f | 793 | { |
e8e1d09e | 794 | static wxChar buf[_MAXPATHLEN]; |
2db300c6 | 795 | |
e8e1d09e GD |
796 | // Local copy |
797 | wxStrcpy (buf, path); | |
2db300c6 | 798 | |
e8e1d09e GD |
799 | int l = wxStrlen(path); |
800 | int i = l - 1; | |
2db300c6 | 801 | |
e8e1d09e GD |
802 | // Search backward for a backward or forward slash |
803 | while (i > -1) | |
804 | { | |
805 | #if defined(__WXMAC__) && !defined(__DARWIN__) | |
806 | // Classic or Carbon CodeWarrior like | |
807 | // Carbon with Apple DevTools is Unix like | |
808 | if (path[i] == wxT(':') ) | |
809 | { | |
810 | buf[i] = 0; | |
811 | return buf; | |
812 | } | |
bedaf53e | 813 | #else |
e8e1d09e GD |
814 | // Unix like or Windows |
815 | if (path[i] == wxT('/') || path[i] == wxT('\\')) | |
816 | { | |
817 | buf[i] = 0; | |
818 | return buf; | |
819 | } | |
bedaf53e | 820 | #endif |
c801d85f | 821 | #ifdef __VMS__ |
e8e1d09e GD |
822 | if (path[i] == wxT(']')) |
823 | { | |
824 | buf[i+1] = 0; | |
825 | return buf; | |
826 | } | |
a339970a | 827 | #endif |
e8e1d09e | 828 | i --; |
c801d85f | 829 | } |
2db300c6 | 830 | |
c2ff79b1 | 831 | #if defined(__WXMSW__) || defined(__WXPM__) |
e8e1d09e GD |
832 | // Try Drive specifier |
833 | if (wxIsalpha (buf[0]) && buf[1] == wxT(':')) | |
3f4a0c5b | 834 | { |
e8e1d09e GD |
835 | // A:junk --> A:. (since A:.\junk Not A:\junk) |
836 | buf[2] = wxT('.'); | |
837 | buf[3] = wxT('\0'); | |
838 | return buf; | |
3f4a0c5b | 839 | } |
c801d85f KB |
840 | #endif |
841 | } | |
e8e1d09e | 842 | return (wxChar *) NULL; |
c801d85f KB |
843 | } |
844 | ||
845 | // Return just the directory, or NULL if no directory | |
846 | wxString wxPathOnly (const wxString& path) | |
847 | { | |
e8e1d09e | 848 | if (path != wxT("")) |
c801d85f | 849 | { |
e8e1d09e | 850 | wxChar buf[_MAXPATHLEN]; |
2db300c6 | 851 | |
e8e1d09e GD |
852 | // Local copy |
853 | wxStrcpy (buf, WXSTRINGCAST path); | |
2db300c6 | 854 | |
e8e1d09e GD |
855 | int l = path.Length(); |
856 | int i = l - 1; | |
857 | ||
858 | // Search backward for a backward or forward slash | |
859 | while (i > -1) | |
860 | { | |
861 | #if defined(__WXMAC__) && !defined(__DARWIN__) | |
862 | // Classic or Carbon CodeWarrior like | |
863 | // Carbon with Apple DevTools is Unix like | |
864 | if (path[i] == wxT(':') ) | |
865 | { | |
866 | buf[i] = 0; | |
867 | return wxString(buf); | |
868 | } | |
bedaf53e | 869 | #else |
e8e1d09e GD |
870 | // Unix like or Windows |
871 | if (path[i] == wxT('/') || path[i] == wxT('\\')) | |
872 | { | |
873 | buf[i] = 0; | |
874 | return wxString(buf); | |
875 | } | |
bedaf53e | 876 | #endif |
c801d85f | 877 | #ifdef __VMS__ |
e8e1d09e GD |
878 | if (path[i] == wxT(']')) |
879 | { | |
880 | buf[i+1] = 0; | |
881 | return wxString(buf); | |
882 | } | |
a339970a | 883 | #endif |
e8e1d09e | 884 | i --; |
c801d85f | 885 | } |
2db300c6 | 886 | |
c2ff79b1 | 887 | #if defined(__WXMSW__) || defined(__WXPM__) |
e8e1d09e GD |
888 | // Try Drive specifier |
889 | if (wxIsalpha (buf[0]) && buf[1] == wxT(':')) | |
3f4a0c5b | 890 | { |
e8e1d09e GD |
891 | // A:junk --> A:. (since A:.\junk Not A:\junk) |
892 | buf[2] = wxT('.'); | |
893 | buf[3] = wxT('\0'); | |
894 | return wxString(buf); | |
3f4a0c5b | 895 | } |
c801d85f KB |
896 | #endif |
897 | } | |
e8e1d09e | 898 | return wxString(wxT("")); |
c801d85f KB |
899 | } |
900 | ||
901 | // Utility for converting delimiters in DOS filenames to UNIX style | |
902 | // and back again - or we get nasty problems with delimiters. | |
903 | // Also, convert to lower case, since case is significant in UNIX. | |
904 | ||
da2b4b7a | 905 | #if defined(__WXMAC__) |
bedaf53e SC |
906 | wxString wxMacFSSpec2MacFilename( const FSSpec *spec ) |
907 | { | |
a1c34a78 | 908 | #ifdef __DARWIN__ |
8372d79d GD |
909 | int i; |
910 | int j; | |
911 | OSErr theErr; | |
912 | OSStatus theStatus; | |
dc259b79 | 913 | Boolean isDirectory = FALSE; |
b1d4dd7a | 914 | Str255 theParentPath = "\p"; |
8372d79d GD |
915 | FSSpec theParentSpec; |
916 | FSRef theParentRef; | |
917 | char theFileName[FILENAME_MAX]; | |
918 | char thePath[FILENAME_MAX]; | |
919 | ||
920 | strcpy(thePath, ""); | |
921 | ||
922 | // GD: Separate file name from path and make a FSRef to the parent | |
923 | // directory. This is necessary since FSRefs cannot reference files | |
924 | // that have not yet been created. | |
925 | // Based on example code from Apple Technical Note TN2022 | |
926 | // http://developer.apple.com/technotes/tn/tn2022.html | |
927 | ||
928 | // check whether we are converting a directory | |
929 | isDirectory = ((spec->name)[spec->name[0]] == ':'); | |
930 | // count length of file name | |
931 | for (i = spec->name[0] - (isDirectory ? 1 : 0); ((spec->name[i] != ':') && (i > 0)); i--); | |
932 | // copy file name | |
933 | // prepend path separator since it will later be appended to the path | |
934 | theFileName[0] = wxFILE_SEP_PATH; | |
935 | for (j = i + 1; j <= spec->name[0] - (isDirectory ? 1 : 0); j++) { | |
936 | theFileName[j - i] = spec->name[j]; | |
937 | } | |
938 | theFileName[j - i] = '\0'; | |
939 | // copy path if any | |
940 | for (j = 1; j <= i; j++) { | |
941 | theParentPath[++theParentPath[0]] = spec->name[j]; | |
942 | } | |
943 | theErr = FSMakeFSSpec(spec->vRefNum, spec->parID, theParentPath, &theParentSpec); | |
944 | if (theErr == noErr) { | |
945 | // convert the FSSpec to an FSRef | |
946 | theErr = FSpMakeFSRef(&theParentSpec, &theParentRef); | |
947 | } | |
948 | if (theErr == noErr) { | |
949 | // get the POSIX path associated with the FSRef | |
950 | theStatus = FSRefMakePath(&theParentRef, | |
951 | (UInt8 *)thePath, sizeof(thePath)); | |
952 | } | |
953 | if (theStatus == noErr) { | |
954 | // append file name to path | |
955 | // includes previously prepended path separator | |
956 | strcat(thePath, theFileName); | |
957 | } | |
b0091f58 | 958 | |
a1c34a78 | 959 | // create path string for return value |
5f3f0f17 | 960 | wxString result( thePath , wxConvLocal) ; |
a1c34a78 | 961 | #else |
bedaf53e SC |
962 | Handle myPath ; |
963 | short length ; | |
964 | ||
a1c34a78 | 965 | // get length of path and allocate handle |
bedaf53e SC |
966 | FSpGetFullPath( spec , &length , &myPath ) ; |
967 | ::SetHandleSize( myPath , length + 1 ) ; | |
968 | ::HLock( myPath ) ; | |
969 | (*myPath)[length] = 0 ; | |
a1c34a78 | 970 | if ((length > 0) && ((*myPath)[length-1] == ':')) |
bedaf53e | 971 | (*myPath)[length-1] = 0 ; |
2db300c6 | 972 | |
a1c34a78 | 973 | // create path string for return value |
939fba6c | 974 | wxString result( *myPath , wxConvLocal) ; |
a1c34a78 GD |
975 | |
976 | // free allocated handle | |
bedaf53e SC |
977 | ::HUnlock( myPath ) ; |
978 | ::DisposeHandle( myPath ) ; | |
a1c34a78 GD |
979 | #endif |
980 | ||
bedaf53e SC |
981 | return result ; |
982 | } | |
bfc2bf62 SC |
983 | #ifndef __DARWIN__ |
984 | // Mac file names are POSIX (Unix style) under Darwin | |
985 | // therefore the conversion functions below are not needed | |
986 | ||
c4e41ce3 SC |
987 | static wxChar sMacFileNameConversion[ 1000 ] ; |
988 | static char scMacFileNameConversion[ 1000 ] ; | |
e7549107 | 989 | |
bfc2bf62 | 990 | #endif |
bedaf53e SC |
991 | void wxMacFilename2FSSpec( const char *path , FSSpec *spec ) |
992 | { | |
b1d4dd7a | 993 | OSStatus err = noErr ; |
334d2448 | 994 | #ifdef __DARWIN__ |
a1c34a78 GD |
995 | FSRef theRef; |
996 | ||
997 | // get the FSRef associated with the POSIX path | |
bfc2bf62 | 998 | err = FSPathMakeRef((const UInt8 *) path, &theRef, NULL); |
a1c34a78 | 999 | // convert the FSRef to an FSSpec |
bfc2bf62 | 1000 | err = FSGetCatalogInfo(&theRef, kFSCatInfoNone, NULL, NULL, spec, NULL); |
334d2448 | 1001 | #else |
b1d4dd7a | 1002 | if ( strchr( path , ':' ) == NULL ) |
bfc2bf62 | 1003 | { |
b1d4dd7a | 1004 | // try whether it is a volume / or a mounted volume |
c4e41ce3 SC |
1005 | strncpy( scMacFileNameConversion , path , 1000 ) ; |
1006 | scMacFileNameConversion[998] = 0 ; | |
1007 | strcat( scMacFileNameConversion , ":" ) ; | |
1008 | err = FSpLocationFromFullPath( strlen(scMacFileNameConversion) , scMacFileNameConversion , spec ) ; | |
bfc2bf62 SC |
1009 | } |
1010 | else | |
1011 | { | |
b1d4dd7a | 1012 | err = FSpLocationFromFullPath( strlen(path) , path , spec ) ; |
bfc2bf62 | 1013 | } |
334d2448 | 1014 | #endif |
bedaf53e SC |
1015 | } |
1016 | ||
c4e41ce3 SC |
1017 | #if wxUSE_UNICODE |
1018 | WXDLLEXPORT void wxMacFilename2FSSpec( const wxChar *path , FSSpec *spec ) | |
1019 | { | |
939fba6c | 1020 | return wxMacFilename2FSSpec( wxConvFile.cWC2MB(path) , spec ) ; |
c4e41ce3 SC |
1021 | } |
1022 | #endif | |
1023 | ||
a1c34a78 | 1024 | #ifndef __DARWIN__ |
e7549107 | 1025 | |
c4e41ce3 | 1026 | wxString wxMac2UnixFilename (const wxChar *str) |
17dff81c | 1027 | { |
c4e41ce3 SC |
1028 | wxChar *s = sMacFileNameConversion ; |
1029 | wxStrcpy( s , str ) ; | |
a1c34a78 GD |
1030 | if (s) |
1031 | { | |
c4e41ce3 | 1032 | memmove( s+1 , s ,wxStrlen( s ) + 1 * sizeof(wxChar)) ; |
a1c34a78 | 1033 | if ( *s == ':' ) |
e7549107 | 1034 | *s = '.' ; |
a1c34a78 | 1035 | else |
e7549107 | 1036 | *s = '/' ; |
2db300c6 | 1037 | |
a1c34a78 GD |
1038 | while (*s) |
1039 | { | |
e7549107 | 1040 | if (*s == ':') |
a1c34a78 | 1041 | *s = '/'; |
e7549107 | 1042 | else |
a1c34a78 | 1043 | *s = wxTolower(*s); // Case INDEPENDENT |
e7549107 | 1044 | s++; |
a1c34a78 | 1045 | } |
e7549107 | 1046 | } |
a1c34a78 | 1047 | return wxString(sMacFileNameConversion) ; |
17dff81c SC |
1048 | } |
1049 | ||
c4e41ce3 | 1050 | wxString wxUnix2MacFilename (const wxChar *str) |
17dff81c | 1051 | { |
c4e41ce3 SC |
1052 | wxChar *s = sMacFileNameConversion ; |
1053 | wxStrcpy( s , str ) ; | |
a1c34a78 | 1054 | if (s) |
e7549107 | 1055 | { |
a1c34a78 GD |
1056 | if ( *s == '.' ) |
1057 | { | |
1058 | // relative path , since it goes on with slash which is translated to a : | |
c4e41ce3 | 1059 | memmove( s , s+1 ,wxStrlen( s ) * sizeof(wxChar)) ; |
a1c34a78 GD |
1060 | } |
1061 | else if ( *s == '/' ) | |
1062 | { | |
1063 | // absolute path -> on mac just start with the drive name | |
c4e41ce3 | 1064 | memmove( s , s+1 ,wxStrlen( s ) * sizeof(wxChar) ) ; |
a1c34a78 GD |
1065 | } |
1066 | else | |
1067 | { | |
c4e41ce3 | 1068 | wxASSERT_MSG( 1 , wxT("unkown path beginning") ) ; |
a1c34a78 GD |
1069 | } |
1070 | while (*s) | |
1071 | { | |
1072 | if (*s == '/' || *s == '\\') | |
1073 | { | |
1074 | // convert any back-directory situations | |
1075 | if ( *(s+1) == '.' && *(s+2) == '.' && ( (*(s+3) == '/' || *(s+3) == '\\') ) ) | |
1076 | { | |
1077 | *s = ':'; | |
c4e41ce3 | 1078 | memmove( s+1 , s+3 ,(wxStrlen( s+3 ) + 1)*sizeof(wxChar) ) ; |
a1c34a78 GD |
1079 | } |
1080 | else | |
1081 | *s = ':'; | |
1082 | } | |
1083 | s++ ; | |
1084 | } | |
e7549107 | 1085 | } |
c4e41ce3 | 1086 | return wxString(sMacFileNameConversion) ; |
17dff81c | 1087 | } |
e7549107 | 1088 | |
e7549107 SC |
1089 | wxString wxMacFSSpec2UnixFilename( const FSSpec *spec ) |
1090 | { | |
f6bcfd97 | 1091 | return wxMac2UnixFilename( wxMacFSSpec2MacFilename( spec) ) ; |
e7549107 SC |
1092 | } |
1093 | ||
e7549107 SC |
1094 | void wxUnixFilename2FSSpec( const char *path , FSSpec *spec ) |
1095 | { | |
f6bcfd97 BP |
1096 | wxString var = wxUnix2MacFilename( path ) ; |
1097 | wxMacFilename2FSSpec( var , spec ) ; | |
e7549107 | 1098 | } |
a1c34a78 | 1099 | #endif // ! __DARWIN__ |
12d67f8a | 1100 | |
e8e1d09e GD |
1101 | #endif // __WXMAC__ |
1102 | ||
3f4a0c5b | 1103 | void |
4603b4f9 | 1104 | wxDos2UnixFilename (wxChar *s) |
c801d85f KB |
1105 | { |
1106 | if (s) | |
1107 | while (*s) | |
1108 | { | |
4603b4f9 JS |
1109 | if (*s == _T('\\')) |
1110 | *s = _T('/'); | |
e7549107 | 1111 | #ifdef __WXMSW__ |
3f4a0c5b | 1112 | else |
e7549107 | 1113 | *s = wxTolower (*s); // Case INDEPENDENT |
c801d85f | 1114 | #endif |
3f4a0c5b | 1115 | s++; |
c801d85f KB |
1116 | } |
1117 | } | |
1118 | ||
3f4a0c5b | 1119 | void |
f28538c5 | 1120 | #if defined(__WXMSW__) || defined(__WXPM__) |
5b735202 | 1121 | wxUnix2DosFilename (wxChar *s) |
46dc76ba | 1122 | #else |
5b735202 | 1123 | wxUnix2DosFilename (wxChar *WXUNUSED(s) ) |
46dc76ba | 1124 | #endif |
c801d85f KB |
1125 | { |
1126 | // Yes, I really mean this to happen under DOS only! JACS | |
c2ff79b1 | 1127 | #if defined(__WXMSW__) || defined(__WXPM__) |
c801d85f KB |
1128 | if (s) |
1129 | while (*s) | |
1130 | { | |
223d09f6 KB |
1131 | if (*s == wxT('/')) |
1132 | *s = wxT('\\'); | |
3f4a0c5b | 1133 | s++; |
c801d85f KB |
1134 | } |
1135 | #endif | |
1136 | } | |
1137 | ||
1138 | // Concatenate two files to form third | |
3f4a0c5b | 1139 | bool |
c801d85f KB |
1140 | wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& file3) |
1141 | { | |
a339970a | 1142 | wxString outfile; |
2b5f62a0 | 1143 | if ( !wxGetTempFileName( wxT("cat"), outfile) ) |
dc259b79 | 1144 | return FALSE; |
c801d85f | 1145 | |
999836aa | 1146 | FILE *fp1 wxDUMMY_INITIALIZE(NULL); |
044bbeb0 MB |
1147 | FILE *fp2 = NULL; |
1148 | FILE *fp3 = NULL; | |
c801d85f | 1149 | // Open the inputs and outputs |
401eb3de RR |
1150 | if ((fp1 = wxFopen ( file1, wxT("rb"))) == NULL || |
1151 | (fp2 = wxFopen ( file2, wxT("rb"))) == NULL || | |
1152 | (fp3 = wxFopen ( outfile, wxT("wb"))) == NULL) | |
c801d85f KB |
1153 | { |
1154 | if (fp1) | |
3f4a0c5b | 1155 | fclose (fp1); |
c801d85f | 1156 | if (fp2) |
3f4a0c5b | 1157 | fclose (fp2); |
c801d85f | 1158 | if (fp3) |
3f4a0c5b | 1159 | fclose (fp3); |
dc259b79 | 1160 | return FALSE; |
c801d85f KB |
1161 | } |
1162 | ||
1163 | int ch; | |
1164 | while ((ch = getc (fp1)) != EOF) | |
1165 | (void) putc (ch, fp3); | |
1166 | fclose (fp1); | |
1167 | ||
1168 | while ((ch = getc (fp2)) != EOF) | |
1169 | (void) putc (ch, fp3); | |
1170 | fclose (fp2); | |
1171 | ||
1172 | fclose (fp3); | |
1173 | bool result = wxRenameFile(outfile, file3); | |
c801d85f KB |
1174 | return result; |
1175 | } | |
1176 | ||
1177 | // Copy files | |
3f4a0c5b | 1178 | bool |
4658c44e | 1179 | wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) |
c801d85f | 1180 | { |
04ef50df | 1181 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
4658c44e VZ |
1182 | // CopyFile() copies file attributes and modification time too, so use it |
1183 | // instead of our code if available | |
1184 | // | |
1185 | // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite | |
564225a1 VZ |
1186 | if ( !::CopyFile(file1, file2, !overwrite) ) |
1187 | { | |
1188 | wxLogSysError(_("Failed to copy the file '%s' to '%s'"), | |
1189 | file1.c_str(), file2.c_str()); | |
1190 | ||
dc259b79 | 1191 | return FALSE; |
564225a1 | 1192 | } |
19193a2c | 1193 | #elif defined(__WXPM__) |
564225a1 | 1194 | if ( ::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) != 0 ) |
dc259b79 | 1195 | return FALSE; |
4658c44e | 1196 | #else // !Win32 |
32f31043 | 1197 | |
b1ac3b56 | 1198 | wxStructStat fbuf; |
32f31043 | 1199 | // get permissions of file1 |
b1ac3b56 | 1200 | if ( wxStat( file1.c_str(), &fbuf) != 0 ) |
32f31043 VZ |
1201 | { |
1202 | // the file probably doesn't exist or we haven't the rights to read | |
1203 | // from it anyhow | |
1204 | wxLogSysError(_("Impossible to get permissions for file '%s'"), | |
1205 | file1.c_str()); | |
dc259b79 | 1206 | return FALSE; |
32f31043 VZ |
1207 | } |
1208 | ||
1209 | // open file1 for reading | |
1210 | wxFile fileIn(file1, wxFile::read); | |
a339970a | 1211 | if ( !fileIn.IsOpened() ) |
dc259b79 | 1212 | return FALSE; |
c801d85f | 1213 | |
32f31043 VZ |
1214 | // remove file2, if it exists. This is needed for creating |
1215 | // file2 with the correct permissions in the next step | |
4658c44e | 1216 | if ( wxFileExists(file2) && (!overwrite || !wxRemoveFile(file2))) |
32f31043 VZ |
1217 | { |
1218 | wxLogSysError(_("Impossible to overwrite the file '%s'"), | |
1219 | file2.c_str()); | |
dc259b79 | 1220 | return FALSE; |
32f31043 VZ |
1221 | } |
1222 | ||
1223 | #ifdef __UNIX__ | |
1224 | // reset the umask as we want to create the file with exactly the same | |
1225 | // permissions as the original one | |
1226 | mode_t oldUmask = umask( 0 ); | |
1227 | #endif // __UNIX__ | |
1228 | ||
1229 | // create file2 with the same permissions than file1 and open it for | |
1230 | // writing | |
dc259b79 | 1231 | |
32f31043 | 1232 | wxFile fileOut; |
4658c44e | 1233 | if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) ) |
dc259b79 | 1234 | return FALSE; |
c801d85f | 1235 | |
32f31043 VZ |
1236 | #ifdef __UNIX__ |
1237 | /// restore the old umask | |
1238 | umask(oldUmask); | |
1239 | #endif // __UNIX__ | |
1240 | ||
1241 | // copy contents of file1 to file2 | |
a339970a VZ |
1242 | char buf[4096]; |
1243 | size_t count; | |
1244 | for ( ;; ) | |
c7386783 | 1245 | { |
a339970a VZ |
1246 | count = fileIn.Read(buf, WXSIZEOF(buf)); |
1247 | if ( fileIn.Error() ) | |
dc259b79 | 1248 | return FALSE; |
a339970a VZ |
1249 | |
1250 | // end of file? | |
1251 | if ( !count ) | |
1252 | break; | |
1253 | ||
1254 | if ( fileOut.Write(buf, count) < count ) | |
dc259b79 | 1255 | return FALSE; |
c7386783 | 1256 | } |
c801d85f | 1257 | |
abb74e97 VZ |
1258 | // we can expect fileIn to be closed successfully, but we should ensure |
1259 | // that fileOut was closed as some write errors (disk full) might not be | |
1260 | // detected before doing this | |
1261 | if ( !fileIn.Close() || !fileOut.Close() ) | |
dc259b79 | 1262 | return FALSE; |
abb74e97 | 1263 | |
5fde6fcc | 1264 | #if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__) |
abb74e97 VZ |
1265 | // no chmod in VA. Should be some permission API for HPFS386 partitions |
1266 | // however | |
c3396917 | 1267 | if ( chmod(OS_FILENAME(file2), fbuf.st_mode) != 0 ) |
32f31043 VZ |
1268 | { |
1269 | wxLogSysError(_("Impossible to set permissions for the file '%s'"), | |
1270 | file2.c_str()); | |
dc259b79 | 1271 | return FALSE; |
32f31043 | 1272 | } |
4658c44e | 1273 | #endif // OS/2 || Mac |
564225a1 | 1274 | #endif // __WXMSW__ && __WIN32__ |
4658c44e | 1275 | |
dc259b79 | 1276 | return TRUE; |
c801d85f KB |
1277 | } |
1278 | ||
3f4a0c5b | 1279 | bool |
c801d85f KB |
1280 | wxRenameFile (const wxString& file1, const wxString& file2) |
1281 | { | |
1c193821 JS |
1282 | #ifndef __WXWINCE__ |
1283 | // Normal system call | |
c3396917 | 1284 | if ( wxRename (file1, file2) == 0 ) |
dc259b79 | 1285 | return TRUE; |
1c193821 | 1286 | #endif |
a339970a | 1287 | |
c801d85f KB |
1288 | // Try to copy |
1289 | if (wxCopyFile(file1, file2)) { | |
1290 | wxRemoveFile(file1); | |
dc259b79 | 1291 | return TRUE; |
c801d85f KB |
1292 | } |
1293 | // Give up | |
dc259b79 | 1294 | return FALSE; |
c801d85f KB |
1295 | } |
1296 | ||
1297 | bool wxRemoveFile(const wxString& file) | |
1298 | { | |
161f4f73 VZ |
1299 | #if defined(__VISUALC__) \ |
1300 | || defined(__BORLANDC__) \ | |
1301 | || defined(__WATCOMC__) \ | |
1302 | || defined(__GNUWIN32__) | |
a339970a | 1303 | int res = wxRemove(file); |
c4e41ce3 SC |
1304 | #elif defined(__WXMAC__) |
1305 | int res = unlink(wxFNCONV(file)); | |
c801d85f | 1306 | #else |
a339970a | 1307 | int res = unlink(OS_FILENAME(file)); |
c801d85f | 1308 | #endif |
a339970a VZ |
1309 | |
1310 | return res == 0; | |
c801d85f KB |
1311 | } |
1312 | ||
1a33c3ba | 1313 | bool wxMkdir(const wxString& dir, int perm) |
c801d85f | 1314 | { |
5fde6fcc | 1315 | #if defined(__WXMAC__) && !defined(__UNIX__) |
c4e41ce3 | 1316 | return (mkdir( wxFNCONV(dir) , 0 ) == 0); |
7708abe9 | 1317 | #else // !Mac |
50920146 | 1318 | const wxChar *dirname = dir.c_str(); |
1a33c3ba | 1319 | |
c2ff79b1 | 1320 | // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too |
7708abe9 | 1321 | // for the GNU compiler |
b4da152e | 1322 | #if (!(defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__) |
2e38557f | 1323 | #ifndef MSVCRT |
50920146 | 1324 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) |
2e38557f JS |
1325 | #else |
1326 | if ( mkdir(wxFNCONV(dirname)) != 0 ) | |
1327 | #endif | |
f6bcfd97 BP |
1328 | #elif defined(__WXPM__) |
1329 | if (::DosCreateDir((PSZ)dirname, NULL) != 0) // enhance for EAB's?? | |
b916f809 VS |
1330 | #elif defined(__DOS__) |
1331 | #if defined(__WATCOMC__) | |
1332 | (void)perm; | |
1333 | if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) | |
1334 | #elif defined(__DJGPP__) | |
1335 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) | |
1336 | #else | |
1337 | #error "Unsupported DOS compiler!" | |
1338 | #endif | |
1339 | #else // !MSW, !DOS and !OS/2 VAC++ | |
19193a2c | 1340 | (void)perm; |
1c193821 JS |
1341 | #ifdef __WXWINCE__ |
1342 | if ( !CreateDirectory(dirname, NULL) ) | |
1343 | #else | |
f6bcfd97 | 1344 | if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) |
1c193821 | 1345 | #endif |
7708abe9 | 1346 | #endif // !MSW/MSW |
1a33c3ba VZ |
1347 | { |
1348 | wxLogSysError(_("Directory '%s' couldn't be created"), dirname); | |
1349 | ||
dc259b79 | 1350 | return FALSE; |
1a33c3ba VZ |
1351 | } |
1352 | ||
dc259b79 | 1353 | return TRUE; |
e7549107 | 1354 | #endif // Mac/!Mac |
c801d85f KB |
1355 | } |
1356 | ||
1357 | bool wxRmdir(const wxString& dir, int WXUNUSED(flags)) | |
1358 | { | |
1359 | #ifdef __VMS__ | |
1c193821 | 1360 | return FALSE; //to be changed since rmdir exists in VMS7.x |
f6bcfd97 | 1361 | #elif defined(__WXPM__) |
1c193821 | 1362 | return (::DosDeleteDir((PSZ)dir.c_str()) == 0); |
c801d85f | 1363 | #else |
a3ef5bf5 | 1364 | |
1c193821 JS |
1365 | #ifdef __WXWINCE__ |
1366 | return (CreateDirectory(dir, NULL) != 0); | |
a3ef5bf5 | 1367 | #else |
1c193821 | 1368 | return (wxRmDir(OS_FILENAME(dir)) == 0); |
c801d85f KB |
1369 | #endif |
1370 | ||
1371 | #endif | |
1372 | } | |
1373 | ||
c801d85f | 1374 | // does the path exists? (may have or not '/' or '\\' at the end) |
50920146 | 1375 | bool wxPathExists(const wxChar *pszPathName) |
c801d85f | 1376 | { |
f6bcfd97 | 1377 | wxString strPath(pszPathName); |
e32d659d | 1378 | |
f6bcfd97 BP |
1379 | #ifdef __WINDOWS__ |
1380 | // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists, | |
1381 | // so remove all trailing backslashes from the path - but don't do this for | |
1382 | // the pathes "d:\" (which are different from "d:") nor for just "\" | |
1383 | while ( wxEndsWithPathSeparator(strPath) ) | |
1384 | { | |
1385 | size_t len = strPath.length(); | |
cc4a1ce1 | 1386 | if ( len == 1 || (len == 3 && strPath[len - 2] == _T(':')) ) |
f6bcfd97 | 1387 | break; |
c801d85f | 1388 | |
f6bcfd97 BP |
1389 | strPath.Truncate(len - 1); |
1390 | } | |
1391 | #endif // __WINDOWS__ | |
1392 | ||
2db300c6 | 1393 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
e32d659d VZ |
1394 | // stat() can't cope with network paths |
1395 | DWORD ret = ::GetFileAttributes(strPath); | |
2db300c6 | 1396 | |
a28ae409 | 1397 | return (ret != (DWORD)-1) && (ret & FILE_ATTRIBUTE_DIRECTORY); |
e32d659d | 1398 | #else // !__WIN32__ |
4c97e024 | 1399 | |
f6bcfd97 | 1400 | wxStructStat st; |
71c97a89 | 1401 | #ifndef __VISAGECPP__ |
92980e90 | 1402 | return wxStat(pszPathName, &st) == 0 && ((st.st_mode & S_IFMT) == S_IFDIR); |
71c97a89 DW |
1403 | #else |
1404 | // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only | |
92980e90 | 1405 | return wxStat(pszPathName, &st) == 0 && (st.st_mode == S_IFDIR); |
71c97a89 DW |
1406 | #endif |
1407 | ||
e32d659d | 1408 | #endif // __WIN32__/!__WIN32__ |
c801d85f KB |
1409 | } |
1410 | ||
1411 | // Get a temporary filename, opening and closing the file. | |
50920146 | 1412 | wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf) |
c801d85f | 1413 | { |
ade35f11 VZ |
1414 | wxString filename = wxFileName::CreateTempFileName(prefix); |
1415 | if ( filename.empty() ) | |
1416 | return NULL; | |
c801d85f | 1417 | |
ade35f11 VZ |
1418 | if ( buf ) |
1419 | wxStrcpy(buf, filename); | |
1420 | else | |
f526f752 | 1421 | buf = MYcopystring(filename); |
c801d85f | 1422 | |
ade35f11 | 1423 | return buf; |
c801d85f KB |
1424 | } |
1425 | ||
c0ab6adf JS |
1426 | bool wxGetTempFileName(const wxString& prefix, wxString& buf) |
1427 | { | |
ade35f11 VZ |
1428 | buf = wxFileName::CreateTempFileName(prefix); |
1429 | ||
1430 | return !buf.empty(); | |
c0ab6adf JS |
1431 | } |
1432 | ||
c801d85f KB |
1433 | // Get first file name matching given wild card. |
1434 | ||
8ff12342 VS |
1435 | static wxDir *gs_dir = NULL; |
1436 | static wxString gs_dirPath; | |
1437 | ||
1438 | wxString wxFindFirstFile(const wxChar *spec, int flags) | |
1439 | { | |
9a5ead1e | 1440 | wxSplitPath(spec, &gs_dirPath, NULL, NULL); |
8ff12342 VS |
1441 | if ( gs_dirPath.IsEmpty() ) |
1442 | gs_dirPath = wxT("."); | |
083f7497 | 1443 | if ( !wxEndsWithPathSeparator(gs_dirPath ) ) |
8ff12342 VS |
1444 | gs_dirPath << wxFILE_SEP_PATH; |
1445 | ||
1446 | if (gs_dir) | |
1447 | delete gs_dir; | |
1448 | gs_dir = new wxDir(gs_dirPath); | |
2db300c6 | 1449 | |
8ff12342 VS |
1450 | if ( !gs_dir->IsOpened() ) |
1451 | { | |
1452 | wxLogSysError(_("Can not enumerate files '%s'"), spec); | |
1453 | return wxEmptyString; | |
1454 | } | |
2db300c6 | 1455 | |
999836aa | 1456 | int dirFlags; |
8ff12342 VS |
1457 | switch (flags) |
1458 | { | |
1459 | case wxDIR: dirFlags = wxDIR_DIRS; break; | |
1460 | case wxFILE: dirFlags = wxDIR_FILES; break; | |
1461 | default: dirFlags = wxDIR_DIRS | wxDIR_FILES; break; | |
1462 | } | |
2db300c6 | 1463 | |
8ff12342 | 1464 | wxString result; |
1f1070e2 | 1465 | gs_dir->GetFirst(&result, wxFileNameFromPath(wxString(spec)), dirFlags); |
8ff12342 | 1466 | if ( result.IsEmpty() ) |
e168b6ac | 1467 | { |
8ff12342 | 1468 | wxDELETE(gs_dir); |
e168b6ac VS |
1469 | return result; |
1470 | } | |
8ff12342 VS |
1471 | |
1472 | return gs_dirPath + result; | |
1473 | } | |
1474 | ||
1475 | wxString wxFindNextFile() | |
1476 | { | |
1477 | wxASSERT_MSG( gs_dir, wxT("You must call wxFindFirstFile before!") ); | |
1478 | ||
1479 | wxString result; | |
1480 | gs_dir->GetNext(&result); | |
2db300c6 | 1481 | |
8ff12342 | 1482 | if ( result.IsEmpty() ) |
e168b6ac | 1483 | { |
8ff12342 | 1484 | wxDELETE(gs_dir); |
e168b6ac VS |
1485 | return result; |
1486 | } | |
2db300c6 | 1487 | |
8ff12342 VS |
1488 | return gs_dirPath + result; |
1489 | } | |
1490 | ||
c801d85f KB |
1491 | |
1492 | // Get current working directory. | |
1493 | // If buf is NULL, allocates space using new, else | |
1494 | // copies into buf. | |
50920146 | 1495 | wxChar *wxGetWorkingDirectory(wxChar *buf, int sz) |
c801d85f | 1496 | { |
1c193821 JS |
1497 | #ifdef __WXWINCE__ |
1498 | return NULL; | |
1499 | #else | |
13b1f8a7 | 1500 | if ( !buf ) |
f6bcfd97 | 1501 | { |
13b1f8a7 VZ |
1502 | buf = new wxChar[sz + 1]; |
1503 | } | |
1504 | ||
999836aa | 1505 | bool ok wxDUMMY_INITIALIZE(FALSE); |
13b1f8a7 VZ |
1506 | |
1507 | // for the compilers which have Unicode version of _getcwd(), call it | |
1508 | // directly, for the others call the ANSI version and do the translation | |
dbc38199 | 1509 | #if !wxUSE_UNICODE |
247f8a77 | 1510 | #define cbuf buf |
dbc38199 | 1511 | #else // wxUSE_UNICODE |
dc259b79 | 1512 | bool needsANSI = TRUE; |
dbc38199 | 1513 | |
247f8a77 | 1514 | #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU |
dc259b79 | 1515 | // This is not legal code as the compiler |
c35c94f6 RR |
1516 | // is allowed destroy the wxCharBuffer. |
1517 | // wxCharBuffer c_buffer(sz); | |
1518 | // char *cbuf = (char*)(const char*)c_buffer; | |
1519 | char cbuf[_MAXPATHLEN]; | |
247f8a77 | 1520 | #endif |
dbc38199 | 1521 | |
13b1f8a7 | 1522 | #ifdef HAVE_WGETCWD |
247f8a77 VS |
1523 | #if wxUSE_UNICODE_MSLU |
1524 | if ( wxGetOsVersion() != wxWIN95 ) | |
f5c0e657 | 1525 | #else |
dc259b79 | 1526 | char *cbuf = NULL; // never really used because needsANSI will always be FALSE |
247f8a77 VS |
1527 | #endif |
1528 | { | |
1529 | ok = _wgetcwd(buf, sz) != NULL; | |
dc259b79 | 1530 | needsANSI = FALSE; |
247f8a77 | 1531 | } |
13b1f8a7 | 1532 | #endif |
13b1f8a7 | 1533 | |
247f8a77 | 1534 | if ( needsANSI ) |
dbc38199 | 1535 | #endif // wxUSE_UNICODE |
247f8a77 | 1536 | { |
29d0a26e | 1537 | #if defined(_MSC_VER) || defined(__MINGW32__) |
dbc38199 | 1538 | ok = _getcwd(cbuf, sz) != NULL; |
13b1f8a7 VZ |
1539 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
1540 | FSSpec cwdSpec ; | |
1541 | FCBPBRec pb; | |
1542 | OSErr error; | |
1543 | Str255 fileName ; | |
1544 | pb.ioNamePtr = (StringPtr) &fileName; | |
1545 | pb.ioVRefNum = 0; | |
1546 | pb.ioRefNum = LMGetCurApRefNum(); | |
1547 | pb.ioFCBIndx = 0; | |
1548 | error = PBGetFCBInfoSync(&pb); | |
1549 | if ( error == noErr ) | |
1550 | { | |
1551 | cwdSpec.vRefNum = pb.ioFCBVRefNum; | |
1552 | cwdSpec.parID = pb.ioFCBParID; | |
1553 | cwdSpec.name[0] = 0 ; | |
1554 | wxString res = wxMacFSSpec2MacFilename( &cwdSpec ) ; | |
c4e41ce3 | 1555 | wxStrcpy( buf , res ) ; |
dc259b79 | 1556 | ok = TRUE; |
13b1f8a7 VZ |
1557 | } |
1558 | else | |
1559 | { | |
dc259b79 | 1560 | ok = FALSE; |
13b1f8a7 VZ |
1561 | } |
1562 | #elif defined(__VISAGECPP__) || (defined (__OS2__) && defined (__WATCOMC__)) | |
1563 | APIRET rc; | |
1564 | rc = ::DosQueryCurrentDir( 0 // current drive | |
dbc38199 | 1565 | ,cbuf |
13b1f8a7 VZ |
1566 | ,(PULONG)&sz |
1567 | ); | |
1568 | ok = rc != 0; | |
1569 | #else // !Win32/VC++ !Mac !OS2 | |
dbc38199 | 1570 | ok = getcwd(cbuf, sz) != NULL; |
13b1f8a7 | 1571 | #endif // platform |
247f8a77 | 1572 | |
c4e41ce3 | 1573 | #if wxUSE_UNICODE && !defined(__WXMAC__) |
247f8a77 VS |
1574 | // finally convert the result to Unicode if needed |
1575 | wxConvFile.MB2WC(buf, cbuf, sz); | |
1576 | #endif // wxUSE_UNICODE | |
1577 | } | |
13b1f8a7 VZ |
1578 | |
1579 | if ( !ok ) | |
19193a2c | 1580 | { |
13b1f8a7 | 1581 | wxLogSysError(_("Failed to get the working directory")); |
19193a2c | 1582 | |
13b1f8a7 VZ |
1583 | // VZ: the old code used to return "." on error which didn't make any |
1584 | // sense at all to me - empty string is a better error indicator | |
1585 | // (NULL might be even better but I'm afraid this could lead to | |
1586 | // problems with the old code assuming the return is never NULL) | |
1587 | buf[0] = _T('\0'); | |
19193a2c | 1588 | } |
13b1f8a7 | 1589 | else // ok, but we might need to massage the path into the right format |
19193a2c | 1590 | { |
4b00a538 | 1591 | #ifdef __DJGPP__ |
13b1f8a7 VZ |
1592 | // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths |
1593 | // with / deliminers. We don't like that. | |
1594 | for (wxChar *ch = buf; *ch; ch++) | |
1595 | { | |
1596 | if (*ch == wxT('/')) | |
1597 | *ch = wxT('\\'); | |
1598 | } | |
1599 | #endif // __DJGPP__ | |
1600 | ||
17234b26 MB |
1601 | // MBN: we hope that in the case the user is compiling a GTK+/Motif app, |
1602 | // he needs Unix as opposed to Win32 pathnames | |
1603 | #if defined( __CYGWIN__ ) && defined( __WINDOWS__ ) | |
3ffbc733 | 1604 | // another example of DOS/Unix mix (Cygwin) |
13b1f8a7 VZ |
1605 | wxString pathUnix = buf; |
1606 | cygwin_conv_to_full_win32_path(pathUnix, buf); | |
e02e8816 | 1607 | #endif // __CYGWIN__ |
13b1f8a7 | 1608 | } |
4b00a538 | 1609 | |
13b1f8a7 | 1610 | return buf; |
dbc38199 VS |
1611 | |
1612 | #if !wxUSE_UNICODE | |
247f8a77 | 1613 | #undef cbuf |
dbc38199 | 1614 | #endif |
1c193821 JS |
1615 | |
1616 | #endif | |
1617 | // __WXWINCE__ | |
c801d85f KB |
1618 | } |
1619 | ||
7af89395 VZ |
1620 | wxString wxGetCwd() |
1621 | { | |
4bc6e5c5 RR |
1622 | wxChar *buffer = new wxChar[_MAXPATHLEN]; |
1623 | wxGetWorkingDirectory(buffer, _MAXPATHLEN); | |
1624 | wxString str( buffer ); | |
1625 | delete [] buffer; | |
dc259b79 | 1626 | |
eac2aeb0 | 1627 | return str; |
7af89395 VZ |
1628 | } |
1629 | ||
c801d85f KB |
1630 | bool wxSetWorkingDirectory(const wxString& d) |
1631 | { | |
f48fa475 | 1632 | #if defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__) |
1c193821 | 1633 | return (chdir(wxFNSTRINGCAST d.fn_str()) == 0); |
f6bcfd97 | 1634 | #elif defined(__WXPM__) |
1c193821 | 1635 | return (::DosSetCurrentDir((PSZ)d.c_str()) == 0); |
34138703 | 1636 | #elif defined(__WINDOWS__) |
1c193821 | 1637 | |
c801d85f | 1638 | #ifdef __WIN32__ |
1c193821 JS |
1639 | #ifdef __WXWINCE__ |
1640 | // No equivalent in WinCE | |
1641 | return FALSE; | |
c801d85f | 1642 | #else |
1c193821 JS |
1643 | return (bool)(SetCurrentDirectory(d) != 0); |
1644 | #endif | |
1645 | #else | |
1646 | // Must change drive, too. | |
1647 | bool isDriveSpec = ((strlen(d) > 1) && (d[1] == ':')); | |
1648 | if (isDriveSpec) | |
c801d85f | 1649 | { |
1c193821 JS |
1650 | wxChar firstChar = d[0]; |
1651 | ||
1652 | // To upper case | |
1653 | if (firstChar > 90) | |
1654 | firstChar = firstChar - 32; | |
1655 | ||
1656 | // To a drive number | |
1657 | unsigned int driveNo = firstChar - 64; | |
1658 | if (driveNo > 0) | |
1659 | { | |
1660 | unsigned int noDrives; | |
1661 | _dos_setdrive(driveNo, &noDrives); | |
1662 | } | |
c801d85f | 1663 | } |
1c193821 JS |
1664 | bool success = (chdir(WXSTRINGCAST d) == 0); |
1665 | ||
1666 | return success; | |
c801d85f | 1667 | #endif |
1c193821 | 1668 | |
c801d85f KB |
1669 | #endif |
1670 | } | |
1671 | ||
631f1bfe JS |
1672 | // Get the OS directory if appropriate (such as the Windows directory). |
1673 | // On non-Windows platform, probably just return the empty string. | |
1674 | wxString wxGetOSDirectory() | |
1675 | { | |
1c193821 JS |
1676 | #ifdef __WXWINCE__ |
1677 | return wxString(wxT("\\Windows")); | |
1678 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
50920146 | 1679 | wxChar buf[256]; |
631f1bfe JS |
1680 | GetWindowsDirectory(buf, 256); |
1681 | return wxString(buf); | |
2d3441d7 GD |
1682 | #elif defined(__WXMAC__) |
1683 | return wxMacFindFolder(kOnSystemDisk, 'macs', false); | |
631f1bfe JS |
1684 | #else |
1685 | return wxEmptyString; | |
1686 | #endif | |
1687 | } | |
1688 | ||
a907da15 | 1689 | bool wxEndsWithPathSeparator(const wxChar *pszFileName) |
c801d85f | 1690 | { |
903b61cc VZ |
1691 | size_t len = wxStrlen(pszFileName); |
1692 | ||
1693 | return len && wxIsPathSeparator(pszFileName[len - 1]); | |
c801d85f KB |
1694 | } |
1695 | ||
dc259b79 | 1696 | // find a file in a list of directories, returns FALSE if not found |
50920146 | 1697 | bool wxFindFileInPath(wxString *pStr, const wxChar *pszPath, const wxChar *pszFile) |
c801d85f | 1698 | { |
a17e237f | 1699 | // we assume that it's not empty |
dc259b79 | 1700 | wxCHECK_MSG( !wxIsEmpty(pszFile), FALSE, |
f6bcfd97 | 1701 | _T("empty file name in wxFindFileInPath")); |
a17e237f VZ |
1702 | |
1703 | // skip path separator in the beginning of the file name if present | |
1704 | if ( wxIsPathSeparator(*pszFile) ) | |
1705 | pszFile++; | |
1706 | ||
1707 | // copy the path (strtok will modify it) | |
1708 | wxChar *szPath = new wxChar[wxStrlen(pszPath) + 1]; | |
1709 | wxStrcpy(szPath, pszPath); | |
1710 | ||
1711 | wxString strFile; | |
1712 | wxChar *pc, *save_ptr; | |
1713 | for ( pc = wxStrtok(szPath, wxPATH_SEP, &save_ptr); | |
1714 | pc != NULL; | |
1715 | pc = wxStrtok((wxChar *) NULL, wxPATH_SEP, &save_ptr) ) | |
1716 | { | |
1717 | // search for the file in this directory | |
1718 | strFile = pc; | |
1719 | if ( !wxEndsWithPathSeparator(pc) ) | |
1720 | strFile += wxFILE_SEP_PATH; | |
1721 | strFile += pszFile; | |
1722 | ||
2b5f62a0 | 1723 | if ( wxFileExists(strFile) ) { |
a17e237f VZ |
1724 | *pStr = strFile; |
1725 | break; | |
1726 | } | |
c801d85f | 1727 | } |
c801d85f | 1728 | |
a17e237f VZ |
1729 | // suppress warning about unused variable save_ptr when wxStrtok() is a |
1730 | // macro which throws away its third argument | |
1731 | save_ptr = pc; | |
1732 | ||
1733 | delete [] szPath; | |
c801d85f | 1734 | |
dc259b79 | 1735 | return pc != NULL; // if TRUE => we breaked from the loop |
c801d85f KB |
1736 | } |
1737 | ||
50920146 | 1738 | void WXDLLEXPORT wxSplitPath(const wxChar *pszFileName, |
3826db3e VZ |
1739 | wxString *pstrPath, |
1740 | wxString *pstrName, | |
1741 | wxString *pstrExt) | |
1742 | { | |
d37fd2fa | 1743 | // it can be empty, but it shouldn't be NULL |
223d09f6 | 1744 | wxCHECK_RET( pszFileName, wxT("NULL file name in wxSplitPath") ); |
3826db3e | 1745 | |
9e8d8607 | 1746 | wxFileName::SplitPath(pszFileName, pstrPath, pstrName, pstrExt); |
3826db3e | 1747 | } |
7f555861 | 1748 | |
a47ce4a7 VS |
1749 | time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename) |
1750 | { | |
1c193821 JS |
1751 | #ifdef __WXWINCE__ |
1752 | FILETIME creationTime, lastAccessTime, lastWriteTime; | |
1753 | HANDLE fileHandle = ::CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
1754 | 0, FILE_ATTRIBUTE_NORMAL, 0); | |
1755 | if (fileHandle == INVALID_HANDLE_VALUE) | |
1756 | return 0; | |
1757 | else | |
1758 | { | |
1759 | if (GetFileTime(fileHandle, & creationTime, & lastAccessTime, & lastWriteTime)) | |
1760 | { | |
1761 | CloseHandle(fileHandle); | |
1762 | ||
1763 | wxDateTime dateTime; | |
1764 | FILETIME ftLocal; | |
1765 | if ( !::FileTimeToLocalFileTime(&lastWriteTime, &ftLocal) ) | |
1766 | { | |
1767 | wxLogLastError(_T("FileTimeToLocalFileTime")); | |
1768 | } | |
1769 | ||
1770 | SYSTEMTIME st; | |
1771 | if ( !::FileTimeToSystemTime(&ftLocal, &st) ) | |
1772 | { | |
1773 | wxLogLastError(_T("FileTimeToSystemTime")); | |
1774 | } | |
1775 | ||
1776 | dateTime.Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear, | |
1777 | st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); | |
1778 | return dateTime.GetTicks(); | |
1779 | } | |
1780 | else | |
1781 | return 0; | |
1782 | } | |
1783 | #else | |
f6bcfd97 | 1784 | wxStructStat buf; |
92980e90 | 1785 | wxStat( filename, &buf); |
dc259b79 | 1786 | |
a47ce4a7 | 1787 | return buf.st_mtime; |
1c193821 | 1788 | #endif |
a47ce4a7 VS |
1789 | } |
1790 | ||
1791 | ||
7f555861 JS |
1792 | //------------------------------------------------------------------------ |
1793 | // wild character routines | |
1794 | //------------------------------------------------------------------------ | |
1795 | ||
1796 | bool wxIsWild( const wxString& pattern ) | |
1797 | { | |
2b5f62a0 VZ |
1798 | wxString tmp = pattern; |
1799 | wxChar *pat = WXSTRINGCAST(tmp); | |
dc259b79 | 1800 | while (*pat) |
2b5f62a0 | 1801 | { |
dc259b79 | 1802 | switch (*pat++) |
2b5f62a0 | 1803 | { |
223d09f6 | 1804 | case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'): |
dc259b79 | 1805 | return TRUE; |
223d09f6 | 1806 | case wxT('\\'): |
3f4a0c5b | 1807 | if (!*pat++) |
dc259b79 | 1808 | return FALSE; |
3f4a0c5b | 1809 | } |
7f555861 | 1810 | } |
dc259b79 | 1811 | return FALSE; |
dfcb1ae0 | 1812 | } |
dfcb1ae0 | 1813 | |
2b5f62a0 VZ |
1814 | /* |
1815 | * Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU> | |
1816 | * | |
1817 | * The match procedure is public domain code (from ircII's reg.c) | |
1818 | */ | |
7be1f0d9 | 1819 | |
2b5f62a0 | 1820 | bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) |
7f555861 | 1821 | { |
2b5f62a0 VZ |
1822 | if (text.empty()) |
1823 | { | |
1824 | /* Match if both are empty. */ | |
1825 | return pat.empty(); | |
1826 | } | |
dc259b79 | 1827 | |
2b5f62a0 VZ |
1828 | const wxChar *m = pat.c_str(), |
1829 | *n = text.c_str(), | |
1830 | *ma = NULL, | |
1831 | *na = NULL, | |
1832 | *mp = NULL, | |
1833 | *np = NULL; | |
1834 | int just = 0, | |
1835 | pcount = 0, | |
1836 | acount = 0, | |
1837 | count = 0; | |
1838 | ||
1839 | if (dot_special && (*n == wxT('.'))) | |
1840 | { | |
dc259b79 | 1841 | /* Never match so that hidden Unix files |
2b5f62a0 | 1842 | * are never found. */ |
dc259b79 | 1843 | return FALSE; |
2b5f62a0 | 1844 | } |
7f555861 | 1845 | |
2b5f62a0 VZ |
1846 | for (;;) |
1847 | { | |
1848 | if (*m == wxT('*')) | |
1849 | { | |
1850 | ma = ++m; | |
1851 | na = n; | |
1852 | just = 1; | |
1853 | mp = NULL; | |
1854 | acount = count; | |
3f4a0c5b | 1855 | } |
2b5f62a0 VZ |
1856 | else if (*m == wxT('?')) |
1857 | { | |
1858 | m++; | |
1859 | if (!*n++) | |
dc259b79 | 1860 | return FALSE; |
3f4a0c5b | 1861 | } |
2b5f62a0 VZ |
1862 | else |
1863 | { | |
1864 | if (*m == wxT('\\')) | |
1865 | { | |
1866 | m++; | |
1867 | /* Quoting "nothing" is a bad thing */ | |
1868 | if (!*m) | |
dc259b79 | 1869 | return FALSE; |
3f4a0c5b | 1870 | } |
2b5f62a0 VZ |
1871 | if (!*m) |
1872 | { | |
1873 | /* | |
1874 | * If we are out of both strings or we just | |
1875 | * saw a wildcard, then we can say we have a | |
1876 | * match | |
1877 | */ | |
1878 | if (!*n) | |
dc259b79 | 1879 | return TRUE; |
2b5f62a0 | 1880 | if (just) |
dc259b79 | 1881 | return TRUE; |
2b5f62a0 VZ |
1882 | just = 0; |
1883 | goto not_matched; | |
1884 | } | |
1885 | /* | |
1886 | * We could check for *n == NULL at this point, but | |
1887 | * since it's more common to have a character there, | |
1888 | * check to see if they match first (m and n) and | |
1889 | * then if they don't match, THEN we can check for | |
1890 | * the NULL of n | |
1891 | */ | |
1892 | just = 0; | |
1893 | if (*m == *n) | |
1894 | { | |
1895 | m++; | |
1896 | if (*n == wxT(' ')) | |
1897 | mp = NULL; | |
1898 | count++; | |
1899 | n++; | |
1900 | } | |
1901 | else | |
1902 | { | |
1903 | ||
1904 | not_matched: | |
1905 | ||
1906 | /* | |
1907 | * If there are no more characters in the | |
1908 | * string, but we still need to find another | |
1909 | * character (*m != NULL), then it will be | |
1910 | * impossible to match it | |
1911 | */ | |
1912 | if (!*n) | |
dc259b79 | 1913 | return FALSE; |
2b5f62a0 VZ |
1914 | if (mp) |
1915 | { | |
1916 | m = mp; | |
1917 | if (*np == wxT(' ')) | |
1918 | { | |
1919 | mp = NULL; | |
1920 | goto check_percent; | |
1921 | } | |
1922 | n = ++np; | |
1923 | count = pcount; | |
1924 | } | |
1925 | else | |
1926 | check_percent: | |
1927 | ||
1928 | if (ma) | |
1929 | { | |
1930 | m = ma; | |
1931 | n = ++na; | |
1932 | count = acount; | |
1933 | } | |
1934 | else | |
dc259b79 | 1935 | return FALSE; |
3f4a0c5b | 1936 | } |
3f4a0c5b | 1937 | } |
3f4a0c5b | 1938 | } |
2b5f62a0 | 1939 | } |
fd3f686c | 1940 | |
7f555861 | 1941 | |
3f4a0c5b | 1942 | #ifdef __VISUALC__ |
fd3f686c | 1943 | #pragma warning(default:4706) // assignment within conditional expression |
53c6e7cc | 1944 | #endif // VC++ |