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