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