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