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