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