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