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