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