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