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