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