]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/filename.cpp | |
3 | // Purpose: wxFileName - encapsulates a file path | |
4 | // Author: Robert Roebling, Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 28.12.2000 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 Robert Roebling | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | /* | |
13 | Here are brief descriptions of the filename formats supported by this class: | |
14 | ||
15 | wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file | |
16 | names have the form: | |
17 | /dir1/dir2/.../dirN/filename, "." and ".." stand for the | |
18 | current and parent directory respectively, "~" is parsed as the | |
19 | user HOME and "~username" as the HOME of that user | |
20 | ||
21 | wxPATH_DOS: DOS/Windows format, absolute file names have the form: | |
22 | drive:\dir1\dir2\...\dirN\filename.ext where drive is a single | |
23 | letter. "." and ".." as for Unix but no "~". | |
24 | ||
25 | There are also UNC names of the form \\share\fullpath | |
26 | ||
27 | wxPATH_MAC: Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file | |
28 | names have the form | |
29 | volume:dir1:...:dirN:filename | |
30 | and the relative file names are either | |
31 | :dir1:...:dirN:filename | |
32 | or just | |
33 | filename | |
34 | (although :filename works as well). | |
35 | Since the volume is just part of the file path, it is not | |
36 | treated like a separate entity as it is done under DOS and | |
37 | VMS, it is just treated as another dir. | |
38 | ||
39 | wxPATH_VMS: VMS native format, absolute file names have the form | |
40 | <device>:[dir1.dir2.dir3]file.txt | |
41 | or | |
42 | <device>:[000000.dir1.dir2.dir3]file.txt | |
43 | ||
44 | the <device> is the physical device (i.e. disk). 000000 is the | |
45 | root directory on the device which can be omitted. | |
46 | ||
47 | Note that VMS uses different separators unlike Unix: | |
48 | : always after the device. If the path does not contain : than | |
49 | the default (the device of the current directory) is assumed. | |
50 | [ start of directory specification | |
51 | . separator between directory and subdirectory | |
52 | ] between directory and file | |
53 | */ | |
54 | ||
55 | // ============================================================================ | |
56 | // declarations | |
57 | // ============================================================================ | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // headers | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | // For compilers that support precompilation, includes "wx.h". | |
64 | #include "wx/wxprec.h" | |
65 | ||
66 | #ifdef __BORLANDC__ | |
67 | #pragma hdrstop | |
68 | #endif | |
69 | ||
70 | #ifndef WX_PRECOMP | |
71 | #ifdef __WXMSW__ | |
72 | #include "wx/msw/wrapwin.h" // For GetShort/LongPathName | |
73 | #endif | |
74 | #include "wx/dynarray.h" | |
75 | #include "wx/intl.h" | |
76 | #include "wx/log.h" | |
77 | #include "wx/utils.h" | |
78 | #include "wx/crt.h" | |
79 | #endif | |
80 | ||
81 | #include "wx/filename.h" | |
82 | #include "wx/private/filename.h" | |
83 | #include "wx/tokenzr.h" | |
84 | #include "wx/config.h" // for wxExpandEnvVars | |
85 | #include "wx/dynlib.h" | |
86 | #include "wx/dir.h" | |
87 | ||
88 | #if defined(__WIN32__) && defined(__MINGW32__) | |
89 | #include "wx/msw/gccpriv.h" | |
90 | #endif | |
91 | ||
92 | #ifdef __WXWINCE__ | |
93 | #include "wx/msw/private.h" | |
94 | #endif | |
95 | ||
96 | #if defined(__WXMAC__) | |
97 | #include "wx/osx/private.h" // includes mac headers | |
98 | #endif | |
99 | ||
100 | // utime() is POSIX so should normally be available on all Unices | |
101 | #ifdef __UNIX_LIKE__ | |
102 | #include <sys/types.h> | |
103 | #include <utime.h> | |
104 | #include <sys/stat.h> | |
105 | #include <unistd.h> | |
106 | #endif | |
107 | ||
108 | #ifdef __DJGPP__ | |
109 | #include <unistd.h> | |
110 | #endif | |
111 | ||
112 | #ifdef __MWERKS__ | |
113 | #ifdef __MACH__ | |
114 | #include <sys/types.h> | |
115 | #include <utime.h> | |
116 | #include <sys/stat.h> | |
117 | #include <unistd.h> | |
118 | #else | |
119 | #include <stat.h> | |
120 | #include <unistd.h> | |
121 | #include <unix.h> | |
122 | #endif | |
123 | #endif | |
124 | ||
125 | #ifdef __WATCOMC__ | |
126 | #include <io.h> | |
127 | #include <sys/utime.h> | |
128 | #include <sys/stat.h> | |
129 | #endif | |
130 | ||
131 | #ifdef __VISAGECPP__ | |
132 | #ifndef MAX_PATH | |
133 | #define MAX_PATH 256 | |
134 | #endif | |
135 | #endif | |
136 | ||
137 | #ifdef __EMX__ | |
138 | #include <os2.h> | |
139 | #define MAX_PATH _MAX_PATH | |
140 | #endif | |
141 | ||
142 | ||
143 | #if wxUSE_LONGLONG | |
144 | extern const wxULongLong wxInvalidSize = (unsigned)-1; | |
145 | #endif // wxUSE_LONGLONG | |
146 | ||
147 | #ifdef __WIN32__ | |
148 | // this define is missing from VC6 headers | |
149 | #ifndef INVALID_FILE_ATTRIBUTES | |
150 | #define INVALID_FILE_ATTRIBUTES ((DWORD)-1) | |
151 | #endif | |
152 | #endif // __WIN32__ | |
153 | ||
154 | namespace | |
155 | { | |
156 | ||
157 | // ---------------------------------------------------------------------------- | |
158 | // private classes | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | // small helper class which opens and closes the file - we use it just to get | |
162 | // a file handle for the given file name to pass it to some Win32 API function | |
163 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
164 | ||
165 | class wxFileHandle | |
166 | { | |
167 | public: | |
168 | enum OpenMode | |
169 | { | |
170 | ReadAttr, | |
171 | WriteAttr | |
172 | }; | |
173 | ||
174 | wxFileHandle(const wxString& filename, OpenMode mode, int flags = 0) | |
175 | { | |
176 | // be careful and use FILE_{READ,WRITE}_ATTRIBUTES here instead of the | |
177 | // usual GENERIC_{READ,WRITE} as we don't want the file access time to | |
178 | // be changed when we open it because this class is used for setting | |
179 | // access time (see #10567) | |
180 | m_hFile = ::CreateFile | |
181 | ( | |
182 | filename.fn_str(), // name | |
183 | mode == ReadAttr ? FILE_READ_ATTRIBUTES // access mask | |
184 | : FILE_WRITE_ATTRIBUTES, | |
185 | FILE_SHARE_READ | // sharing mode | |
186 | FILE_SHARE_WRITE, // (allow everything) | |
187 | NULL, // no secutity attr | |
188 | OPEN_EXISTING, // creation disposition | |
189 | flags, // flags | |
190 | NULL // no template file | |
191 | ); | |
192 | ||
193 | if ( m_hFile == INVALID_HANDLE_VALUE ) | |
194 | { | |
195 | if ( mode == ReadAttr ) | |
196 | { | |
197 | wxLogSysError(_("Failed to open '%s' for reading"), | |
198 | filename.c_str()); | |
199 | } | |
200 | else | |
201 | { | |
202 | wxLogSysError(_("Failed to open '%s' for writing"), | |
203 | filename.c_str()); | |
204 | } | |
205 | } | |
206 | } | |
207 | ||
208 | ~wxFileHandle() | |
209 | { | |
210 | if ( m_hFile != INVALID_HANDLE_VALUE ) | |
211 | { | |
212 | if ( !::CloseHandle(m_hFile) ) | |
213 | { | |
214 | wxLogSysError(_("Failed to close file handle")); | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
219 | // return true only if the file could be opened successfully | |
220 | bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; } | |
221 | ||
222 | // get the handle | |
223 | operator HANDLE() const { return m_hFile; } | |
224 | ||
225 | private: | |
226 | HANDLE m_hFile; | |
227 | }; | |
228 | ||
229 | #endif // __WIN32__ | |
230 | ||
231 | // ---------------------------------------------------------------------------- | |
232 | // private functions | |
233 | // ---------------------------------------------------------------------------- | |
234 | ||
235 | #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__) | |
236 | ||
237 | // convert between wxDateTime and FILETIME which is a 64-bit value representing | |
238 | // the number of 100-nanosecond intervals since January 1, 1601. | |
239 | ||
240 | static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft) | |
241 | { | |
242 | FILETIME ftcopy = ft; | |
243 | FILETIME ftLocal; | |
244 | if ( !::FileTimeToLocalFileTime(&ftcopy, &ftLocal) ) | |
245 | { | |
246 | wxLogLastError(wxT("FileTimeToLocalFileTime")); | |
247 | } | |
248 | ||
249 | SYSTEMTIME st; | |
250 | if ( !::FileTimeToSystemTime(&ftLocal, &st) ) | |
251 | { | |
252 | wxLogLastError(wxT("FileTimeToSystemTime")); | |
253 | } | |
254 | ||
255 | dt->Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear, | |
256 | st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); | |
257 | } | |
258 | ||
259 | static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) | |
260 | { | |
261 | SYSTEMTIME st; | |
262 | st.wDay = dt.GetDay(); | |
263 | st.wMonth = (WORD)(dt.GetMonth() + 1); | |
264 | st.wYear = (WORD)dt.GetYear(); | |
265 | st.wHour = dt.GetHour(); | |
266 | st.wMinute = dt.GetMinute(); | |
267 | st.wSecond = dt.GetSecond(); | |
268 | st.wMilliseconds = dt.GetMillisecond(); | |
269 | ||
270 | FILETIME ftLocal; | |
271 | if ( !::SystemTimeToFileTime(&st, &ftLocal) ) | |
272 | { | |
273 | wxLogLastError(wxT("SystemTimeToFileTime")); | |
274 | } | |
275 | ||
276 | if ( !::LocalFileTimeToFileTime(&ftLocal, ft) ) | |
277 | { | |
278 | wxLogLastError(wxT("LocalFileTimeToFileTime")); | |
279 | } | |
280 | } | |
281 | ||
282 | #endif // wxUSE_DATETIME && __WIN32__ | |
283 | ||
284 | // return a string with the volume par | |
285 | static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format) | |
286 | { | |
287 | wxString path; | |
288 | ||
289 | if ( !volume.empty() ) | |
290 | { | |
291 | format = wxFileName::GetFormat(format); | |
292 | ||
293 | // Special Windows UNC paths hack, part 2: undo what we did in | |
294 | // SplitPath() and make an UNC path if we have a drive which is not a | |
295 | // single letter (hopefully the network shares can't be one letter only | |
296 | // although I didn't find any authoritative docs on this) | |
297 | if ( format == wxPATH_DOS && volume.length() > 1 ) | |
298 | { | |
299 | path << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << volume; | |
300 | } | |
301 | else if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
302 | { | |
303 | path << volume << wxFileName::GetVolumeSeparator(format); | |
304 | } | |
305 | // else ignore | |
306 | } | |
307 | ||
308 | return path; | |
309 | } | |
310 | ||
311 | // return true if the character is a DOS path separator i.e. either a slash or | |
312 | // a backslash | |
313 | inline bool IsDOSPathSep(wxUniChar ch) | |
314 | { | |
315 | return ch == wxFILE_SEP_PATH_DOS || ch == wxFILE_SEP_PATH_UNIX; | |
316 | } | |
317 | ||
318 | // return true if the format used is the DOS/Windows one and the string looks | |
319 | // like a UNC path | |
320 | static bool IsUNCPath(const wxString& path, wxPathFormat format) | |
321 | { | |
322 | return format == wxPATH_DOS && | |
323 | path.length() >= 4 && // "\\a" can't be a UNC path | |
324 | IsDOSPathSep(path[0u]) && | |
325 | IsDOSPathSep(path[1u]) && | |
326 | !IsDOSPathSep(path[2u]); | |
327 | } | |
328 | ||
329 | } // anonymous namespace | |
330 | ||
331 | // ============================================================================ | |
332 | // implementation | |
333 | // ============================================================================ | |
334 | ||
335 | // ---------------------------------------------------------------------------- | |
336 | // wxFileName construction | |
337 | // ---------------------------------------------------------------------------- | |
338 | ||
339 | void wxFileName::Assign( const wxFileName &filepath ) | |
340 | { | |
341 | m_volume = filepath.GetVolume(); | |
342 | m_dirs = filepath.GetDirs(); | |
343 | m_name = filepath.GetName(); | |
344 | m_ext = filepath.GetExt(); | |
345 | m_relative = filepath.m_relative; | |
346 | m_hasExt = filepath.m_hasExt; | |
347 | } | |
348 | ||
349 | void wxFileName::Assign(const wxString& volume, | |
350 | const wxString& path, | |
351 | const wxString& name, | |
352 | const wxString& ext, | |
353 | bool hasExt, | |
354 | wxPathFormat format) | |
355 | { | |
356 | // we should ignore paths which look like UNC shares because we already | |
357 | // have the volume here and the UNC notation (\\server\path) is only valid | |
358 | // for paths which don't start with a volume, so prevent SetPath() from | |
359 | // recognizing "\\foo\bar" in "c:\\foo\bar" as an UNC path | |
360 | // | |
361 | // note also that this is a rather ugly way to do what we want (passing | |
362 | // some kind of flag telling to ignore UNC paths to SetPath() would be | |
363 | // better) but this is the safest thing to do to avoid breaking backwards | |
364 | // compatibility in 2.8 | |
365 | if ( IsUNCPath(path, format) ) | |
366 | { | |
367 | // remove one of the 2 leading backslashes to ensure that it's not | |
368 | // recognized as an UNC path by SetPath() | |
369 | wxString pathNonUNC(path, 1, wxString::npos); | |
370 | SetPath(pathNonUNC, format); | |
371 | } | |
372 | else // no UNC complications | |
373 | { | |
374 | SetPath(path, format); | |
375 | } | |
376 | ||
377 | m_volume = volume; | |
378 | m_ext = ext; | |
379 | m_name = name; | |
380 | ||
381 | m_hasExt = hasExt; | |
382 | } | |
383 | ||
384 | void wxFileName::SetPath( const wxString& pathOrig, wxPathFormat format ) | |
385 | { | |
386 | m_dirs.Clear(); | |
387 | ||
388 | if ( pathOrig.empty() ) | |
389 | { | |
390 | // no path at all | |
391 | m_relative = true; | |
392 | ||
393 | return; | |
394 | } | |
395 | ||
396 | format = GetFormat( format ); | |
397 | ||
398 | // 0) deal with possible volume part first | |
399 | wxString volume, | |
400 | path; | |
401 | SplitVolume(pathOrig, &volume, &path, format); | |
402 | if ( !volume.empty() ) | |
403 | { | |
404 | m_relative = false; | |
405 | ||
406 | SetVolume(volume); | |
407 | } | |
408 | ||
409 | // 1) Determine if the path is relative or absolute. | |
410 | ||
411 | if ( path.empty() ) | |
412 | { | |
413 | // we had only the volume | |
414 | return; | |
415 | } | |
416 | ||
417 | wxChar leadingChar = path[0u]; | |
418 | ||
419 | switch (format) | |
420 | { | |
421 | case wxPATH_MAC: | |
422 | m_relative = leadingChar == wxT(':'); | |
423 | ||
424 | // We then remove a leading ":". The reason is in our | |
425 | // storage form for relative paths: | |
426 | // ":dir:file.txt" actually means "./dir/file.txt" in | |
427 | // DOS notation and should get stored as | |
428 | // (relative) (dir) (file.txt) | |
429 | // "::dir:file.txt" actually means "../dir/file.txt" | |
430 | // stored as (relative) (..) (dir) (file.txt) | |
431 | // This is important only for the Mac as an empty dir | |
432 | // actually means <UP>, whereas under DOS, double | |
433 | // slashes can be ignored: "\\\\" is the same as "\\". | |
434 | if (m_relative) | |
435 | path.erase( 0, 1 ); | |
436 | break; | |
437 | ||
438 | case wxPATH_VMS: | |
439 | // TODO: what is the relative path format here? | |
440 | m_relative = false; | |
441 | break; | |
442 | ||
443 | default: | |
444 | wxFAIL_MSG( wxT("Unknown path format") ); | |
445 | // !! Fall through !! | |
446 | ||
447 | case wxPATH_UNIX: | |
448 | m_relative = leadingChar != wxT('/'); | |
449 | break; | |
450 | ||
451 | case wxPATH_DOS: | |
452 | m_relative = !IsPathSeparator(leadingChar, format); | |
453 | break; | |
454 | ||
455 | } | |
456 | ||
457 | // 2) Break up the path into its members. If the original path | |
458 | // was just "/" or "\\", m_dirs will be empty. We know from | |
459 | // the m_relative field, if this means "nothing" or "root dir". | |
460 | ||
461 | wxStringTokenizer tn( path, GetPathSeparators(format) ); | |
462 | ||
463 | while ( tn.HasMoreTokens() ) | |
464 | { | |
465 | wxString token = tn.GetNextToken(); | |
466 | ||
467 | // Remove empty token under DOS and Unix, interpret them | |
468 | // as .. under Mac. | |
469 | if (token.empty()) | |
470 | { | |
471 | if (format == wxPATH_MAC) | |
472 | m_dirs.Add( wxT("..") ); | |
473 | // else ignore | |
474 | } | |
475 | else | |
476 | { | |
477 | m_dirs.Add( token ); | |
478 | } | |
479 | } | |
480 | } | |
481 | ||
482 | void wxFileName::Assign(const wxString& fullpath, | |
483 | wxPathFormat format) | |
484 | { | |
485 | wxString volume, path, name, ext; | |
486 | bool hasExt; | |
487 | SplitPath(fullpath, &volume, &path, &name, &ext, &hasExt, format); | |
488 | ||
489 | Assign(volume, path, name, ext, hasExt, format); | |
490 | } | |
491 | ||
492 | void wxFileName::Assign(const wxString& fullpathOrig, | |
493 | const wxString& fullname, | |
494 | wxPathFormat format) | |
495 | { | |
496 | // always recognize fullpath as directory, even if it doesn't end with a | |
497 | // slash | |
498 | wxString fullpath = fullpathOrig; | |
499 | if ( !fullpath.empty() && !wxEndsWithPathSeparator(fullpath) ) | |
500 | { | |
501 | fullpath += GetPathSeparator(format); | |
502 | } | |
503 | ||
504 | wxString volume, path, name, ext; | |
505 | bool hasExt; | |
506 | ||
507 | // do some consistency checks: the name should be really just the filename | |
508 | // and the path should be really just a path | |
509 | wxString volDummy, pathDummy, nameDummy, extDummy; | |
510 | ||
511 | SplitPath(fullname, &volDummy, &pathDummy, &name, &ext, &hasExt, format); | |
512 | ||
513 | wxASSERT_MSG( volDummy.empty() && pathDummy.empty(), | |
514 | wxT("the file name shouldn't contain the path") ); | |
515 | ||
516 | SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format); | |
517 | ||
518 | #ifndef __VMS | |
519 | // This test makes no sense on an OpenVMS system. | |
520 | wxASSERT_MSG( nameDummy.empty() && extDummy.empty(), | |
521 | wxT("the path shouldn't contain file name nor extension") ); | |
522 | #endif | |
523 | Assign(volume, path, name, ext, hasExt, format); | |
524 | } | |
525 | ||
526 | void wxFileName::Assign(const wxString& pathOrig, | |
527 | const wxString& name, | |
528 | const wxString& ext, | |
529 | wxPathFormat format) | |
530 | { | |
531 | wxString volume, | |
532 | path; | |
533 | SplitVolume(pathOrig, &volume, &path, format); | |
534 | ||
535 | Assign(volume, path, name, ext, format); | |
536 | } | |
537 | ||
538 | void wxFileName::AssignDir(const wxString& dir, wxPathFormat format) | |
539 | { | |
540 | Assign(dir, wxEmptyString, format); | |
541 | } | |
542 | ||
543 | void wxFileName::Clear() | |
544 | { | |
545 | m_dirs.Clear(); | |
546 | ||
547 | m_volume = | |
548 | m_name = | |
549 | m_ext = wxEmptyString; | |
550 | ||
551 | // we don't have any absolute path for now | |
552 | m_relative = true; | |
553 | ||
554 | // nor any extension | |
555 | m_hasExt = false; | |
556 | } | |
557 | ||
558 | /* static */ | |
559 | wxFileName wxFileName::FileName(const wxString& file, wxPathFormat format) | |
560 | { | |
561 | return wxFileName(file, format); | |
562 | } | |
563 | ||
564 | /* static */ | |
565 | wxFileName wxFileName::DirName(const wxString& dir, wxPathFormat format) | |
566 | { | |
567 | wxFileName fn; | |
568 | fn.AssignDir(dir, format); | |
569 | return fn; | |
570 | } | |
571 | ||
572 | // ---------------------------------------------------------------------------- | |
573 | // existence tests | |
574 | // ---------------------------------------------------------------------------- | |
575 | ||
576 | bool wxFileName::FileExists() const | |
577 | { | |
578 | return wxFileName::FileExists( GetFullPath() ); | |
579 | } | |
580 | ||
581 | /* static */ | |
582 | bool wxFileName::FileExists( const wxString &filePath ) | |
583 | { | |
584 | #if defined(__WXPALMOS__) | |
585 | return false; | |
586 | #elif defined(__WIN32__) && !defined(__WXMICROWIN__) | |
587 | // we must use GetFileAttributes() instead of the ANSI C functions because | |
588 | // it can cope with network (UNC) paths unlike them | |
589 | DWORD ret = ::GetFileAttributes(filePath.fn_str()); | |
590 | ||
591 | return (ret != INVALID_FILE_ATTRIBUTES) && !(ret & FILE_ATTRIBUTE_DIRECTORY); | |
592 | #else // !__WIN32__ | |
593 | #ifndef S_ISREG | |
594 | #define S_ISREG(mode) ((mode) & S_IFREG) | |
595 | #endif | |
596 | wxStructStat st; | |
597 | #ifndef wxNEED_WX_UNISTD_H | |
598 | return (wxStat( filePath.fn_str() , &st) == 0 && S_ISREG(st.st_mode)) | |
599 | #ifdef __OS2__ | |
600 | || (errno == EACCES) // if access is denied something with that name | |
601 | // exists and is opened in exclusive mode. | |
602 | #endif | |
603 | ; | |
604 | #else | |
605 | return wxStat( filePath , &st) == 0 && S_ISREG(st.st_mode); | |
606 | #endif | |
607 | #endif // __WIN32__/!__WIN32__ | |
608 | } | |
609 | ||
610 | bool wxFileName::DirExists() const | |
611 | { | |
612 | return wxFileName::DirExists( GetPath() ); | |
613 | } | |
614 | ||
615 | /* static */ | |
616 | bool wxFileName::DirExists( const wxString &dirPath ) | |
617 | { | |
618 | wxString strPath(dirPath); | |
619 | ||
620 | #if defined(__WINDOWS__) || defined(__OS2__) | |
621 | // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists, | |
622 | // so remove all trailing backslashes from the path - but don't do this for | |
623 | // the paths "d:\" (which are different from "d:") nor for just "\" | |
624 | while ( wxEndsWithPathSeparator(strPath) ) | |
625 | { | |
626 | size_t len = strPath.length(); | |
627 | if ( len == 1 || (len == 3 && strPath[len - 2] == wxT(':')) ) | |
628 | break; | |
629 | ||
630 | strPath.Truncate(len - 1); | |
631 | } | |
632 | #endif // __WINDOWS__ | |
633 | ||
634 | #ifdef __OS2__ | |
635 | // OS/2 can't handle "d:", it wants either "d:\" or "d:." | |
636 | if (strPath.length() == 2 && strPath[1u] == wxT(':')) | |
637 | strPath << wxT('.'); | |
638 | #endif | |
639 | ||
640 | #if defined(__WXPALMOS__) | |
641 | return false; | |
642 | #elif defined(__WIN32__) && !defined(__WXMICROWIN__) | |
643 | // stat() can't cope with network paths | |
644 | DWORD ret = ::GetFileAttributes(strPath.fn_str()); | |
645 | ||
646 | return (ret != INVALID_FILE_ATTRIBUTES) && (ret & FILE_ATTRIBUTE_DIRECTORY); | |
647 | #elif defined(__OS2__) | |
648 | FILESTATUS3 Info = {{0}}; | |
649 | APIRET rc = ::DosQueryPathInfo((PSZ)(WXSTRINGCAST strPath), FIL_STANDARD, | |
650 | (void*) &Info, sizeof(FILESTATUS3)); | |
651 | ||
652 | return ((rc == NO_ERROR) && (Info.attrFile & FILE_DIRECTORY)) || | |
653 | (rc == ERROR_SHARING_VIOLATION); | |
654 | // If we got a sharing violation, there must be something with this name. | |
655 | #else // !__WIN32__ | |
656 | ||
657 | wxStructStat st; | |
658 | #ifndef __VISAGECPP__ | |
659 | return wxStat(strPath.c_str(), &st) == 0 && ((st.st_mode & S_IFMT) == S_IFDIR); | |
660 | #else | |
661 | // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only | |
662 | return wxStat(strPath.c_str(), &st) == 0 && (st.st_mode == S_IFDIR); | |
663 | #endif | |
664 | ||
665 | #endif // __WIN32__/!__WIN32__ | |
666 | } | |
667 | ||
668 | // ---------------------------------------------------------------------------- | |
669 | // CWD and HOME stuff | |
670 | // ---------------------------------------------------------------------------- | |
671 | ||
672 | void wxFileName::AssignCwd(const wxString& volume) | |
673 | { | |
674 | AssignDir(wxFileName::GetCwd(volume)); | |
675 | } | |
676 | ||
677 | /* static */ | |
678 | wxString wxFileName::GetCwd(const wxString& volume) | |
679 | { | |
680 | // if we have the volume, we must get the current directory on this drive | |
681 | // and to do this we have to chdir to this volume - at least under Windows, | |
682 | // I don't know how to get the current drive on another volume elsewhere | |
683 | // (TODO) | |
684 | wxString cwdOld; | |
685 | if ( !volume.empty() ) | |
686 | { | |
687 | cwdOld = wxGetCwd(); | |
688 | SetCwd(volume + GetVolumeSeparator()); | |
689 | } | |
690 | ||
691 | wxString cwd = ::wxGetCwd(); | |
692 | ||
693 | if ( !volume.empty() ) | |
694 | { | |
695 | SetCwd(cwdOld); | |
696 | } | |
697 | ||
698 | return cwd; | |
699 | } | |
700 | ||
701 | bool wxFileName::SetCwd() const | |
702 | { | |
703 | return wxFileName::SetCwd( GetPath() ); | |
704 | } | |
705 | ||
706 | bool wxFileName::SetCwd( const wxString &cwd ) | |
707 | { | |
708 | return ::wxSetWorkingDirectory( cwd ); | |
709 | } | |
710 | ||
711 | void wxFileName::AssignHomeDir() | |
712 | { | |
713 | AssignDir(wxFileName::GetHomeDir()); | |
714 | } | |
715 | ||
716 | wxString wxFileName::GetHomeDir() | |
717 | { | |
718 | return ::wxGetHomeDir(); | |
719 | } | |
720 | ||
721 | ||
722 | // ---------------------------------------------------------------------------- | |
723 | // CreateTempFileName | |
724 | // ---------------------------------------------------------------------------- | |
725 | ||
726 | #if wxUSE_FILE || wxUSE_FFILE | |
727 | ||
728 | ||
729 | #if !defined wx_fdopen && defined HAVE_FDOPEN | |
730 | #define wx_fdopen fdopen | |
731 | #endif | |
732 | ||
733 | // NB: GetTempFileName() under Windows creates the file, so using | |
734 | // O_EXCL there would fail | |
735 | #ifdef __WINDOWS__ | |
736 | #define wxOPEN_EXCL 0 | |
737 | #else | |
738 | #define wxOPEN_EXCL O_EXCL | |
739 | #endif | |
740 | ||
741 | ||
742 | #ifdef wxOpenOSFHandle | |
743 | #define WX_HAVE_DELETE_ON_CLOSE | |
744 | // On Windows create a file with the FILE_FLAGS_DELETE_ON_CLOSE flags. | |
745 | // | |
746 | static int wxOpenWithDeleteOnClose(const wxString& filename) | |
747 | { | |
748 | DWORD access = GENERIC_READ | GENERIC_WRITE; | |
749 | ||
750 | DWORD disposition = OPEN_ALWAYS; | |
751 | ||
752 | DWORD attributes = FILE_ATTRIBUTE_TEMPORARY | | |
753 | FILE_FLAG_DELETE_ON_CLOSE; | |
754 | ||
755 | HANDLE h = ::CreateFile(filename.fn_str(), access, 0, NULL, | |
756 | disposition, attributes, NULL); | |
757 | ||
758 | return wxOpenOSFHandle(h, wxO_BINARY); | |
759 | } | |
760 | #endif // wxOpenOSFHandle | |
761 | ||
762 | ||
763 | // Helper to open the file | |
764 | // | |
765 | static int wxTempOpen(const wxString& path, bool *deleteOnClose) | |
766 | { | |
767 | #ifdef WX_HAVE_DELETE_ON_CLOSE | |
768 | if (*deleteOnClose) | |
769 | return wxOpenWithDeleteOnClose(path); | |
770 | #endif | |
771 | ||
772 | *deleteOnClose = false; | |
773 | ||
774 | return wxOpen(path, wxO_BINARY | O_RDWR | O_CREAT | wxOPEN_EXCL, 0600); | |
775 | } | |
776 | ||
777 | ||
778 | #if wxUSE_FFILE | |
779 | // Helper to open the file and attach it to the wxFFile | |
780 | // | |
781 | static bool wxTempOpen(wxFFile *file, const wxString& path, bool *deleteOnClose) | |
782 | { | |
783 | #ifndef wx_fdopen | |
784 | *deleteOnClose = false; | |
785 | return file->Open(path, wxT("w+b")); | |
786 | #else // wx_fdopen | |
787 | int fd = wxTempOpen(path, deleteOnClose); | |
788 | if (fd == -1) | |
789 | return false; | |
790 | file->Attach(wx_fdopen(fd, "w+b")); | |
791 | return file->IsOpened(); | |
792 | #endif // wx_fdopen | |
793 | } | |
794 | #endif // wxUSE_FFILE | |
795 | ||
796 | ||
797 | #if !wxUSE_FILE | |
798 | #define WXFILEARGS(x, y) y | |
799 | #elif !wxUSE_FFILE | |
800 | #define WXFILEARGS(x, y) x | |
801 | #else | |
802 | #define WXFILEARGS(x, y) x, y | |
803 | #endif | |
804 | ||
805 | ||
806 | // Implementation of wxFileName::CreateTempFileName(). | |
807 | // | |
808 | static wxString wxCreateTempImpl( | |
809 | const wxString& prefix, | |
810 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp), | |
811 | bool *deleteOnClose = NULL) | |
812 | { | |
813 | #if wxUSE_FILE && wxUSE_FFILE | |
814 | wxASSERT(fileTemp == NULL || ffileTemp == NULL); | |
815 | #endif | |
816 | wxString path, dir, name; | |
817 | bool wantDeleteOnClose = false; | |
818 | ||
819 | if (deleteOnClose) | |
820 | { | |
821 | // set the result to false initially | |
822 | wantDeleteOnClose = *deleteOnClose; | |
823 | *deleteOnClose = false; | |
824 | } | |
825 | else | |
826 | { | |
827 | // easier if it alwasys points to something | |
828 | deleteOnClose = &wantDeleteOnClose; | |
829 | } | |
830 | ||
831 | // use the directory specified by the prefix | |
832 | wxFileName::SplitPath(prefix, &dir, &name, NULL /* extension */); | |
833 | ||
834 | if (dir.empty()) | |
835 | { | |
836 | dir = wxFileName::GetTempDir(); | |
837 | } | |
838 | ||
839 | #if defined(__WXWINCE__) | |
840 | path = dir + wxT("\\") + name; | |
841 | int i = 1; | |
842 | while (wxFileName::FileExists(path)) | |
843 | { | |
844 | path = dir + wxT("\\") + name ; | |
845 | path << i; | |
846 | i ++; | |
847 | } | |
848 | ||
849 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
850 | if ( !::GetTempFileName(dir.fn_str(), name.fn_str(), 0, | |
851 | wxStringBuffer(path, MAX_PATH + 1)) ) | |
852 | { | |
853 | wxLogLastError(wxT("GetTempFileName")); | |
854 | ||
855 | path.clear(); | |
856 | } | |
857 | ||
858 | #else // !Windows | |
859 | path = dir; | |
860 | ||
861 | if ( !wxEndsWithPathSeparator(dir) && | |
862 | (name.empty() || !wxIsPathSeparator(name[0u])) ) | |
863 | { | |
864 | path += wxFILE_SEP_PATH; | |
865 | } | |
866 | ||
867 | path += name; | |
868 | ||
869 | #if defined(HAVE_MKSTEMP) | |
870 | // scratch space for mkstemp() | |
871 | path += wxT("XXXXXX"); | |
872 | ||
873 | // we need to copy the path to the buffer in which mkstemp() can modify it | |
874 | wxCharBuffer buf(path.fn_str()); | |
875 | ||
876 | // cast is safe because the string length doesn't change | |
877 | int fdTemp = mkstemp( (char*)(const char*) buf ); | |
878 | if ( fdTemp == -1 ) | |
879 | { | |
880 | // this might be not necessary as mkstemp() on most systems should have | |
881 | // already done it but it doesn't hurt neither... | |
882 | path.clear(); | |
883 | } | |
884 | else // mkstemp() succeeded | |
885 | { | |
886 | path = wxConvFile.cMB2WX( (const char*) buf ); | |
887 | ||
888 | #if wxUSE_FILE | |
889 | // avoid leaking the fd | |
890 | if ( fileTemp ) | |
891 | { | |
892 | fileTemp->Attach(fdTemp); | |
893 | } | |
894 | else | |
895 | #endif | |
896 | ||
897 | #if wxUSE_FFILE | |
898 | if ( ffileTemp ) | |
899 | { | |
900 | #ifdef wx_fdopen | |
901 | ffileTemp->Attach(wx_fdopen(fdTemp, "r+b")); | |
902 | #else | |
903 | ffileTemp->Open(path, wxT("r+b")); | |
904 | close(fdTemp); | |
905 | #endif | |
906 | } | |
907 | else | |
908 | #endif | |
909 | ||
910 | { | |
911 | close(fdTemp); | |
912 | } | |
913 | } | |
914 | #else // !HAVE_MKSTEMP | |
915 | ||
916 | #ifdef HAVE_MKTEMP | |
917 | // same as above | |
918 | path += wxT("XXXXXX"); | |
919 | ||
920 | wxCharBuffer buf = wxConvFile.cWX2MB( path ); | |
921 | if ( !mktemp( (char*)(const char*) buf ) ) | |
922 | { | |
923 | path.clear(); | |
924 | } | |
925 | else | |
926 | { | |
927 | path = wxConvFile.cMB2WX( (const char*) buf ); | |
928 | } | |
929 | #else // !HAVE_MKTEMP (includes __DOS__) | |
930 | // generate the unique file name ourselves | |
931 | #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) ) | |
932 | path << (unsigned int)getpid(); | |
933 | #endif | |
934 | ||
935 | wxString pathTry; | |
936 | ||
937 | static const size_t numTries = 1000; | |
938 | for ( size_t n = 0; n < numTries; n++ ) | |
939 | { | |
940 | // 3 hex digits is enough for numTries == 1000 < 4096 | |
941 | pathTry = path + wxString::Format(wxT("%.03x"), (unsigned int) n); | |
942 | if ( !wxFileName::FileExists(pathTry) ) | |
943 | { | |
944 | break; | |
945 | } | |
946 | ||
947 | pathTry.clear(); | |
948 | } | |
949 | ||
950 | path = pathTry; | |
951 | #endif // HAVE_MKTEMP/!HAVE_MKTEMP | |
952 | ||
953 | #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP | |
954 | ||
955 | #endif // Windows/!Windows | |
956 | ||
957 | if ( path.empty() ) | |
958 | { | |
959 | wxLogSysError(_("Failed to create a temporary file name")); | |
960 | } | |
961 | else | |
962 | { | |
963 | bool ok = true; | |
964 | ||
965 | // open the file - of course, there is a race condition here, this is | |
966 | // why we always prefer using mkstemp()... | |
967 | #if wxUSE_FILE | |
968 | if ( fileTemp && !fileTemp->IsOpened() ) | |
969 | { | |
970 | *deleteOnClose = wantDeleteOnClose; | |
971 | int fd = wxTempOpen(path, deleteOnClose); | |
972 | if (fd != -1) | |
973 | fileTemp->Attach(fd); | |
974 | else | |
975 | ok = false; | |
976 | } | |
977 | #endif | |
978 | ||
979 | #if wxUSE_FFILE | |
980 | if ( ffileTemp && !ffileTemp->IsOpened() ) | |
981 | { | |
982 | *deleteOnClose = wantDeleteOnClose; | |
983 | ok = wxTempOpen(ffileTemp, path, deleteOnClose); | |
984 | } | |
985 | #endif | |
986 | ||
987 | if ( !ok ) | |
988 | { | |
989 | // FIXME: If !ok here should we loop and try again with another | |
990 | // file name? That is the standard recourse if open(O_EXCL) | |
991 | // fails, though of course it should be protected against | |
992 | // possible infinite looping too. | |
993 | ||
994 | wxLogError(_("Failed to open temporary file.")); | |
995 | ||
996 | path.clear(); | |
997 | } | |
998 | } | |
999 | ||
1000 | return path; | |
1001 | } | |
1002 | ||
1003 | ||
1004 | static bool wxCreateTempImpl( | |
1005 | const wxString& prefix, | |
1006 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp), | |
1007 | wxString *name) | |
1008 | { | |
1009 | bool deleteOnClose = true; | |
1010 | ||
1011 | *name = wxCreateTempImpl(prefix, | |
1012 | WXFILEARGS(fileTemp, ffileTemp), | |
1013 | &deleteOnClose); | |
1014 | ||
1015 | bool ok = !name->empty(); | |
1016 | ||
1017 | if (deleteOnClose) | |
1018 | name->clear(); | |
1019 | #ifdef __UNIX__ | |
1020 | else if (ok && wxRemoveFile(*name)) | |
1021 | name->clear(); | |
1022 | #endif | |
1023 | ||
1024 | return ok; | |
1025 | } | |
1026 | ||
1027 | ||
1028 | static void wxAssignTempImpl( | |
1029 | wxFileName *fn, | |
1030 | const wxString& prefix, | |
1031 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp)) | |
1032 | { | |
1033 | wxString tempname; | |
1034 | tempname = wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, ffileTemp)); | |
1035 | ||
1036 | if ( tempname.empty() ) | |
1037 | { | |
1038 | // error, failed to get temp file name | |
1039 | fn->Clear(); | |
1040 | } | |
1041 | else // ok | |
1042 | { | |
1043 | fn->Assign(tempname); | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | ||
1048 | void wxFileName::AssignTempFileName(const wxString& prefix) | |
1049 | { | |
1050 | wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, NULL)); | |
1051 | } | |
1052 | ||
1053 | /* static */ | |
1054 | wxString wxFileName::CreateTempFileName(const wxString& prefix) | |
1055 | { | |
1056 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, NULL)); | |
1057 | } | |
1058 | ||
1059 | #endif // wxUSE_FILE || wxUSE_FFILE | |
1060 | ||
1061 | ||
1062 | #if wxUSE_FILE | |
1063 | ||
1064 | wxString wxCreateTempFileName(const wxString& prefix, | |
1065 | wxFile *fileTemp, | |
1066 | bool *deleteOnClose) | |
1067 | { | |
1068 | return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), deleteOnClose); | |
1069 | } | |
1070 | ||
1071 | bool wxCreateTempFile(const wxString& prefix, | |
1072 | wxFile *fileTemp, | |
1073 | wxString *name) | |
1074 | { | |
1075 | return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), name); | |
1076 | } | |
1077 | ||
1078 | void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp) | |
1079 | { | |
1080 | wxAssignTempImpl(this, prefix, WXFILEARGS(fileTemp, NULL)); | |
1081 | } | |
1082 | ||
1083 | /* static */ | |
1084 | wxString | |
1085 | wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) | |
1086 | { | |
1087 | return wxCreateTempFileName(prefix, fileTemp); | |
1088 | } | |
1089 | ||
1090 | #endif // wxUSE_FILE | |
1091 | ||
1092 | ||
1093 | #if wxUSE_FFILE | |
1094 | ||
1095 | wxString wxCreateTempFileName(const wxString& prefix, | |
1096 | wxFFile *fileTemp, | |
1097 | bool *deleteOnClose) | |
1098 | { | |
1099 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), deleteOnClose); | |
1100 | } | |
1101 | ||
1102 | bool wxCreateTempFile(const wxString& prefix, | |
1103 | wxFFile *fileTemp, | |
1104 | wxString *name) | |
1105 | { | |
1106 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), name); | |
1107 | ||
1108 | } | |
1109 | ||
1110 | void wxFileName::AssignTempFileName(const wxString& prefix, wxFFile *fileTemp) | |
1111 | { | |
1112 | wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, fileTemp)); | |
1113 | } | |
1114 | ||
1115 | /* static */ | |
1116 | wxString | |
1117 | wxFileName::CreateTempFileName(const wxString& prefix, wxFFile *fileTemp) | |
1118 | { | |
1119 | return wxCreateTempFileName(prefix, fileTemp); | |
1120 | } | |
1121 | ||
1122 | #endif // wxUSE_FFILE | |
1123 | ||
1124 | ||
1125 | // ---------------------------------------------------------------------------- | |
1126 | // directory operations | |
1127 | // ---------------------------------------------------------------------------- | |
1128 | ||
1129 | // helper of GetTempDir(): check if the given directory exists and return it if | |
1130 | // it does or an empty string otherwise | |
1131 | namespace | |
1132 | { | |
1133 | ||
1134 | wxString CheckIfDirExists(const wxString& dir) | |
1135 | { | |
1136 | return wxFileName::DirExists(dir) ? dir : wxString(); | |
1137 | } | |
1138 | ||
1139 | } // anonymous namespace | |
1140 | ||
1141 | wxString wxFileName::GetTempDir() | |
1142 | { | |
1143 | // first try getting it from environment: this allows overriding the values | |
1144 | // used by default if the user wants to create temporary files in another | |
1145 | // directory | |
1146 | wxString dir = CheckIfDirExists(wxGetenv("TMPDIR")); | |
1147 | if ( dir.empty() ) | |
1148 | { | |
1149 | dir = CheckIfDirExists(wxGetenv("TMP")); | |
1150 | if ( dir.empty() ) | |
1151 | dir = CheckIfDirExists(wxGetenv("TEMP")); | |
1152 | } | |
1153 | ||
1154 | // if no environment variables are set, use the system default | |
1155 | if ( dir.empty() ) | |
1156 | { | |
1157 | #if defined(__WXWINCE__) | |
1158 | dir = CheckIfDirExists(wxT("\\temp")); | |
1159 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
1160 | if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) ) | |
1161 | { | |
1162 | wxLogLastError(wxT("GetTempPath")); | |
1163 | } | |
1164 | #elif defined(__WXMAC__) && wxOSX_USE_CARBON | |
1165 | dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder); | |
1166 | #endif // systems with native way | |
1167 | } | |
1168 | ||
1169 | // fall back to hard coded value | |
1170 | if ( dir.empty() ) | |
1171 | { | |
1172 | #ifdef __UNIX_LIKE__ | |
1173 | dir = CheckIfDirExists("/tmp"); | |
1174 | if ( dir.empty() ) | |
1175 | #endif // __UNIX_LIKE__ | |
1176 | dir = "."; | |
1177 | } | |
1178 | ||
1179 | return dir; | |
1180 | } | |
1181 | ||
1182 | bool wxFileName::Mkdir( int perm, int flags ) const | |
1183 | { | |
1184 | return wxFileName::Mkdir(GetPath(), perm, flags); | |
1185 | } | |
1186 | ||
1187 | bool wxFileName::Mkdir( const wxString& dir, int perm, int flags ) | |
1188 | { | |
1189 | if ( flags & wxPATH_MKDIR_FULL ) | |
1190 | { | |
1191 | // split the path in components | |
1192 | wxFileName filename; | |
1193 | filename.AssignDir(dir); | |
1194 | ||
1195 | wxString currPath; | |
1196 | if ( filename.HasVolume()) | |
1197 | { | |
1198 | currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE); | |
1199 | } | |
1200 | ||
1201 | wxArrayString dirs = filename.GetDirs(); | |
1202 | size_t count = dirs.GetCount(); | |
1203 | for ( size_t i = 0; i < count; i++ ) | |
1204 | { | |
1205 | if ( i > 0 || filename.IsAbsolute() ) | |
1206 | currPath += wxFILE_SEP_PATH; | |
1207 | currPath += dirs[i]; | |
1208 | ||
1209 | if (!DirExists(currPath)) | |
1210 | { | |
1211 | if (!wxMkdir(currPath, perm)) | |
1212 | { | |
1213 | // no need to try creating further directories | |
1214 | return false; | |
1215 | } | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | return true; | |
1220 | ||
1221 | } | |
1222 | ||
1223 | return ::wxMkdir( dir, perm ); | |
1224 | } | |
1225 | ||
1226 | bool wxFileName::Rmdir(int flags) const | |
1227 | { | |
1228 | return wxFileName::Rmdir( GetPath(), flags ); | |
1229 | } | |
1230 | ||
1231 | bool wxFileName::Rmdir(const wxString& dir, int flags) | |
1232 | { | |
1233 | #ifdef __WXMSW__ | |
1234 | if ( flags & wxPATH_RMDIR_RECURSIVE ) | |
1235 | { | |
1236 | // SHFileOperation needs double null termination string | |
1237 | // but without separator at the end of the path | |
1238 | wxString path(dir); | |
1239 | if ( path.Last() == wxFILE_SEP_PATH ) | |
1240 | path.RemoveLast(); | |
1241 | path += wxT('\0'); | |
1242 | ||
1243 | SHFILEOPSTRUCT fileop; | |
1244 | wxZeroMemory(fileop); | |
1245 | fileop.wFunc = FO_DELETE; | |
1246 | fileop.pFrom = path.fn_str(); | |
1247 | fileop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; | |
1248 | #ifndef __WXWINCE__ | |
1249 | // FOF_NOERRORUI is not defined in WinCE | |
1250 | fileop.fFlags |= FOF_NOERRORUI; | |
1251 | #endif | |
1252 | ||
1253 | int ret = SHFileOperation(&fileop); | |
1254 | if ( ret != 0 ) | |
1255 | { | |
1256 | // SHFileOperation may return non-Win32 error codes, so the error | |
1257 | // message can be incorrect | |
1258 | wxLogApiError(wxT("SHFileOperation"), ret); | |
1259 | return false; | |
1260 | } | |
1261 | ||
1262 | return true; | |
1263 | } | |
1264 | else if ( flags & wxPATH_RMDIR_FULL ) | |
1265 | #else // !__WXMSW__ | |
1266 | if ( flags != 0 ) // wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE | |
1267 | #endif // !__WXMSW__ | |
1268 | { | |
1269 | wxString path(dir); | |
1270 | if ( path.Last() != wxFILE_SEP_PATH ) | |
1271 | path += wxFILE_SEP_PATH; | |
1272 | ||
1273 | wxDir d(path); | |
1274 | ||
1275 | if ( !d.IsOpened() ) | |
1276 | return false; | |
1277 | ||
1278 | wxString filename; | |
1279 | ||
1280 | // first delete all subdirectories | |
1281 | bool cont = d.GetFirst(&filename, "", wxDIR_DIRS | wxDIR_HIDDEN); | |
1282 | while ( cont ) | |
1283 | { | |
1284 | wxFileName::Rmdir(path + filename, flags); | |
1285 | cont = d.GetNext(&filename); | |
1286 | } | |
1287 | ||
1288 | #ifndef __WXMSW__ | |
1289 | if ( flags & wxPATH_RMDIR_RECURSIVE ) | |
1290 | { | |
1291 | // delete all files too | |
1292 | cont = d.GetFirst(&filename, "", wxDIR_FILES | wxDIR_HIDDEN); | |
1293 | while ( cont ) | |
1294 | { | |
1295 | ::wxRemoveFile(path + filename); | |
1296 | cont = d.GetNext(&filename); | |
1297 | } | |
1298 | } | |
1299 | #endif // !__WXMSW__ | |
1300 | } | |
1301 | ||
1302 | return ::wxRmdir(dir); | |
1303 | } | |
1304 | ||
1305 | // ---------------------------------------------------------------------------- | |
1306 | // path normalization | |
1307 | // ---------------------------------------------------------------------------- | |
1308 | ||
1309 | bool wxFileName::Normalize(int flags, | |
1310 | const wxString& cwd, | |
1311 | wxPathFormat format) | |
1312 | { | |
1313 | // deal with env vars renaming first as this may seriously change the path | |
1314 | if ( flags & wxPATH_NORM_ENV_VARS ) | |
1315 | { | |
1316 | wxString pathOrig = GetFullPath(format); | |
1317 | wxString path = wxExpandEnvVars(pathOrig); | |
1318 | if ( path != pathOrig ) | |
1319 | { | |
1320 | Assign(path); | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | // the existing path components | |
1325 | wxArrayString dirs = GetDirs(); | |
1326 | ||
1327 | // the path to prepend in front to make the path absolute | |
1328 | wxFileName curDir; | |
1329 | ||
1330 | format = GetFormat(format); | |
1331 | ||
1332 | // set up the directory to use for making the path absolute later | |
1333 | if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) ) | |
1334 | { | |
1335 | if ( cwd.empty() ) | |
1336 | { | |
1337 | curDir.AssignCwd(GetVolume()); | |
1338 | } | |
1339 | else // cwd provided | |
1340 | { | |
1341 | curDir.AssignDir(cwd); | |
1342 | } | |
1343 | } | |
1344 | ||
1345 | // handle ~ stuff under Unix only | |
1346 | if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) && m_relative ) | |
1347 | { | |
1348 | if ( !dirs.IsEmpty() ) | |
1349 | { | |
1350 | wxString dir = dirs[0u]; | |
1351 | if ( !dir.empty() && dir[0u] == wxT('~') ) | |
1352 | { | |
1353 | // to make the path absolute use the home directory | |
1354 | curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); | |
1355 | dirs.RemoveAt(0u); | |
1356 | } | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | // transform relative path into abs one | |
1361 | if ( curDir.IsOk() ) | |
1362 | { | |
1363 | // this path may be relative because it doesn't have the volume name | |
1364 | // and still have m_relative=true; in this case we shouldn't modify | |
1365 | // our directory components but just set the current volume | |
1366 | if ( !HasVolume() && curDir.HasVolume() ) | |
1367 | { | |
1368 | SetVolume(curDir.GetVolume()); | |
1369 | ||
1370 | if ( !m_relative ) | |
1371 | { | |
1372 | // yes, it was the case - we don't need curDir then | |
1373 | curDir.Clear(); | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | // finally, prepend curDir to the dirs array | |
1378 | wxArrayString dirsNew = curDir.GetDirs(); | |
1379 | WX_PREPEND_ARRAY(dirs, dirsNew); | |
1380 | ||
1381 | // if we used e.g. tilde expansion previously and wxGetUserHome didn't | |
1382 | // return for some reason an absolute path, then curDir maybe not be absolute! | |
1383 | if ( !curDir.m_relative ) | |
1384 | { | |
1385 | // we have prepended an absolute path and thus we are now an absolute | |
1386 | // file name too | |
1387 | m_relative = false; | |
1388 | } | |
1389 | // else if (flags & wxPATH_NORM_ABSOLUTE): | |
1390 | // should we warn the user that we didn't manage to make the path absolute? | |
1391 | } | |
1392 | ||
1393 | // now deal with ".", ".." and the rest | |
1394 | m_dirs.Empty(); | |
1395 | size_t count = dirs.GetCount(); | |
1396 | for ( size_t n = 0; n < count; n++ ) | |
1397 | { | |
1398 | wxString dir = dirs[n]; | |
1399 | ||
1400 | if ( flags & wxPATH_NORM_DOTS ) | |
1401 | { | |
1402 | if ( dir == wxT(".") ) | |
1403 | { | |
1404 | // just ignore | |
1405 | continue; | |
1406 | } | |
1407 | ||
1408 | if ( dir == wxT("..") ) | |
1409 | { | |
1410 | if ( m_dirs.IsEmpty() ) | |
1411 | { | |
1412 | wxLogError(_("The path '%s' contains too many \"..\"!"), | |
1413 | GetFullPath().c_str()); | |
1414 | return false; | |
1415 | } | |
1416 | ||
1417 | m_dirs.RemoveAt(m_dirs.GetCount() - 1); | |
1418 | continue; | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | m_dirs.Add(dir); | |
1423 | } | |
1424 | ||
1425 | #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE | |
1426 | if ( (flags & wxPATH_NORM_SHORTCUT) ) | |
1427 | { | |
1428 | wxString filename; | |
1429 | if (GetShortcutTarget(GetFullPath(format), filename)) | |
1430 | { | |
1431 | m_relative = false; | |
1432 | Assign(filename); | |
1433 | } | |
1434 | } | |
1435 | #endif | |
1436 | ||
1437 | #if defined(__WIN32__) | |
1438 | if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) ) | |
1439 | { | |
1440 | Assign(GetLongPath()); | |
1441 | } | |
1442 | #endif // Win32 | |
1443 | ||
1444 | // Change case (this should be kept at the end of the function, to ensure | |
1445 | // that the path doesn't change any more after we normalize its case) | |
1446 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
1447 | { | |
1448 | m_volume.MakeLower(); | |
1449 | m_name.MakeLower(); | |
1450 | m_ext.MakeLower(); | |
1451 | ||
1452 | // directory entries must be made lower case as well | |
1453 | count = m_dirs.GetCount(); | |
1454 | for ( size_t i = 0; i < count; i++ ) | |
1455 | { | |
1456 | m_dirs[i].MakeLower(); | |
1457 | } | |
1458 | } | |
1459 | ||
1460 | return true; | |
1461 | } | |
1462 | ||
1463 | #ifndef __WXWINCE__ | |
1464 | bool wxFileName::ReplaceEnvVariable(const wxString& envname, | |
1465 | const wxString& replacementFmtString, | |
1466 | wxPathFormat format) | |
1467 | { | |
1468 | // look into stringForm for the contents of the given environment variable | |
1469 | wxString val; | |
1470 | if (envname.empty() || | |
1471 | !wxGetEnv(envname, &val)) | |
1472 | return false; | |
1473 | if (val.empty()) | |
1474 | return false; | |
1475 | ||
1476 | wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); | |
1477 | // do not touch the file name and the extension | |
1478 | ||
1479 | wxString replacement = wxString::Format(replacementFmtString, envname); | |
1480 | stringForm.Replace(val, replacement); | |
1481 | ||
1482 | // Now assign ourselves the modified path: | |
1483 | Assign(stringForm, GetFullName(), format); | |
1484 | ||
1485 | return true; | |
1486 | } | |
1487 | #endif | |
1488 | ||
1489 | bool wxFileName::ReplaceHomeDir(wxPathFormat format) | |
1490 | { | |
1491 | wxString homedir = wxGetHomeDir(); | |
1492 | if (homedir.empty()) | |
1493 | return false; | |
1494 | ||
1495 | wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); | |
1496 | // do not touch the file name and the extension | |
1497 | ||
1498 | stringForm.Replace(homedir, "~"); | |
1499 | ||
1500 | // Now assign ourselves the modified path: | |
1501 | Assign(stringForm, GetFullName(), format); | |
1502 | ||
1503 | return true; | |
1504 | } | |
1505 | ||
1506 | // ---------------------------------------------------------------------------- | |
1507 | // get the shortcut target | |
1508 | // ---------------------------------------------------------------------------- | |
1509 | ||
1510 | // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions. | |
1511 | // The .lnk file is a plain text file so it should be easy to | |
1512 | // make it work. Hint from Google Groups: | |
1513 | // "If you open up a lnk file, you'll see a | |
1514 | // number, followed by a pound sign (#), followed by more text. The | |
1515 | // number is the number of characters that follows the pound sign. The | |
1516 | // characters after the pound sign are the command line (which _can_ | |
1517 | // include arguments) to be executed. Any path (e.g. \windows\program | |
1518 | // files\myapp.exe) that includes spaces needs to be enclosed in | |
1519 | // quotation marks." | |
1520 | ||
1521 | #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE | |
1522 | // The following lines are necessary under WinCE | |
1523 | // #include "wx/msw/private.h" | |
1524 | // #include <ole2.h> | |
1525 | #include <shlobj.h> | |
1526 | #if defined(__WXWINCE__) | |
1527 | #include <shlguid.h> | |
1528 | #endif | |
1529 | ||
1530 | bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, | |
1531 | wxString& targetFilename, | |
1532 | wxString* arguments) const | |
1533 | { | |
1534 | wxString path, file, ext; | |
1535 | wxFileName::SplitPath(shortcutPath, & path, & file, & ext); | |
1536 | ||
1537 | HRESULT hres; | |
1538 | IShellLink* psl; | |
1539 | bool success = false; | |
1540 | ||
1541 | // Assume it's not a shortcut if it doesn't end with lnk | |
1542 | if (ext.CmpNoCase(wxT("lnk"))!=0) | |
1543 | return false; | |
1544 | ||
1545 | // create a ShellLink object | |
1546 | hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, | |
1547 | IID_IShellLink, (LPVOID*) &psl); | |
1548 | ||
1549 | if (SUCCEEDED(hres)) | |
1550 | { | |
1551 | IPersistFile* ppf; | |
1552 | hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf); | |
1553 | if (SUCCEEDED(hres)) | |
1554 | { | |
1555 | WCHAR wsz[MAX_PATH]; | |
1556 | ||
1557 | MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz, | |
1558 | MAX_PATH); | |
1559 | ||
1560 | hres = ppf->Load(wsz, 0); | |
1561 | ppf->Release(); | |
1562 | ||
1563 | if (SUCCEEDED(hres)) | |
1564 | { | |
1565 | wxChar buf[2048]; | |
1566 | // Wrong prototype in early versions | |
1567 | #if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2) | |
1568 | psl->GetPath((CHAR*) buf, 2048, NULL, SLGP_UNCPRIORITY); | |
1569 | #else | |
1570 | psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY); | |
1571 | #endif | |
1572 | targetFilename = wxString(buf); | |
1573 | success = (shortcutPath != targetFilename); | |
1574 | ||
1575 | psl->GetArguments(buf, 2048); | |
1576 | wxString args(buf); | |
1577 | if (!args.empty() && arguments) | |
1578 | { | |
1579 | *arguments = args; | |
1580 | } | |
1581 | } | |
1582 | } | |
1583 | ||
1584 | psl->Release(); | |
1585 | } | |
1586 | return success; | |
1587 | } | |
1588 | ||
1589 | #endif // __WIN32__ && !__WXWINCE__ | |
1590 | ||
1591 | ||
1592 | // ---------------------------------------------------------------------------- | |
1593 | // absolute/relative paths | |
1594 | // ---------------------------------------------------------------------------- | |
1595 | ||
1596 | bool wxFileName::IsAbsolute(wxPathFormat format) const | |
1597 | { | |
1598 | // unix paths beginning with ~ are reported as being absolute | |
1599 | if ( format == wxPATH_UNIX ) | |
1600 | { | |
1601 | if ( !m_dirs.IsEmpty() ) | |
1602 | { | |
1603 | wxString dir = m_dirs[0u]; | |
1604 | ||
1605 | if (!dir.empty() && dir[0u] == wxT('~')) | |
1606 | return true; | |
1607 | } | |
1608 | } | |
1609 | ||
1610 | // if our path doesn't start with a path separator, it's not an absolute | |
1611 | // path | |
1612 | if ( m_relative ) | |
1613 | return false; | |
1614 | ||
1615 | if ( !GetVolumeSeparator(format).empty() ) | |
1616 | { | |
1617 | // this format has volumes and an absolute path must have one, it's not | |
1618 | // enough to have the full path to be an absolute file under Windows | |
1619 | if ( GetVolume().empty() ) | |
1620 | return false; | |
1621 | } | |
1622 | ||
1623 | return true; | |
1624 | } | |
1625 | ||
1626 | bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) | |
1627 | { | |
1628 | wxFileName fnBase = wxFileName::DirName(pathBase, format); | |
1629 | ||
1630 | // get cwd only once - small time saving | |
1631 | wxString cwd = wxGetCwd(); | |
1632 | Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); | |
1633 | fnBase.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); | |
1634 | ||
1635 | bool withCase = IsCaseSensitive(format); | |
1636 | ||
1637 | // we can't do anything if the files live on different volumes | |
1638 | if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) ) | |
1639 | { | |
1640 | // nothing done | |
1641 | return false; | |
1642 | } | |
1643 | ||
1644 | // same drive, so we don't need our volume | |
1645 | m_volume.clear(); | |
1646 | ||
1647 | // remove common directories starting at the top | |
1648 | while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() && | |
1649 | m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) ) | |
1650 | { | |
1651 | m_dirs.RemoveAt(0); | |
1652 | fnBase.m_dirs.RemoveAt(0); | |
1653 | } | |
1654 | ||
1655 | // add as many ".." as needed | |
1656 | size_t count = fnBase.m_dirs.GetCount(); | |
1657 | for ( size_t i = 0; i < count; i++ ) | |
1658 | { | |
1659 | m_dirs.Insert(wxT(".."), 0u); | |
1660 | } | |
1661 | ||
1662 | if ( format == wxPATH_UNIX || format == wxPATH_DOS ) | |
1663 | { | |
1664 | // a directory made relative with respect to itself is '.' under Unix | |
1665 | // and DOS, by definition (but we don't have to insert "./" for the | |
1666 | // files) | |
1667 | if ( m_dirs.IsEmpty() && IsDir() ) | |
1668 | { | |
1669 | m_dirs.Add(wxT('.')); | |
1670 | } | |
1671 | } | |
1672 | ||
1673 | m_relative = true; | |
1674 | ||
1675 | // we were modified | |
1676 | return true; | |
1677 | } | |
1678 | ||
1679 | // ---------------------------------------------------------------------------- | |
1680 | // filename kind tests | |
1681 | // ---------------------------------------------------------------------------- | |
1682 | ||
1683 | bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const | |
1684 | { | |
1685 | wxFileName fn1 = *this, | |
1686 | fn2 = filepath; | |
1687 | ||
1688 | // get cwd only once - small time saving | |
1689 | wxString cwd = wxGetCwd(); | |
1690 | fn1.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format); | |
1691 | fn2.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format); | |
1692 | ||
1693 | if ( fn1.GetFullPath() == fn2.GetFullPath() ) | |
1694 | return true; | |
1695 | ||
1696 | // TODO: compare inodes for Unix, this works even when filenames are | |
1697 | // different but files are the same (symlinks) (VZ) | |
1698 | ||
1699 | return false; | |
1700 | } | |
1701 | ||
1702 | /* static */ | |
1703 | bool wxFileName::IsCaseSensitive( wxPathFormat format ) | |
1704 | { | |
1705 | // only Unix filenames are truely case-sensitive | |
1706 | return GetFormat(format) == wxPATH_UNIX; | |
1707 | } | |
1708 | ||
1709 | /* static */ | |
1710 | wxString wxFileName::GetForbiddenChars(wxPathFormat format) | |
1711 | { | |
1712 | // Inits to forbidden characters that are common to (almost) all platforms. | |
1713 | wxString strForbiddenChars = wxT("*?"); | |
1714 | ||
1715 | // If asserts, wxPathFormat has been changed. In case of a new path format | |
1716 | // addition, the following code might have to be updated. | |
1717 | wxCOMPILE_TIME_ASSERT(wxPATH_MAX == 5, wxPathFormatChanged); | |
1718 | switch ( GetFormat(format) ) | |
1719 | { | |
1720 | default : | |
1721 | wxFAIL_MSG( wxT("Unknown path format") ); | |
1722 | // !! Fall through !! | |
1723 | ||
1724 | case wxPATH_UNIX: | |
1725 | break; | |
1726 | ||
1727 | case wxPATH_MAC: | |
1728 | // On a Mac even names with * and ? are allowed (Tested with OS | |
1729 | // 9.2.1 and OS X 10.2.5) | |
1730 | strForbiddenChars = wxEmptyString; | |
1731 | break; | |
1732 | ||
1733 | case wxPATH_DOS: | |
1734 | strForbiddenChars += wxT("\\/:\"<>|"); | |
1735 | break; | |
1736 | ||
1737 | case wxPATH_VMS: | |
1738 | break; | |
1739 | } | |
1740 | ||
1741 | return strForbiddenChars; | |
1742 | } | |
1743 | ||
1744 | /* static */ | |
1745 | wxString wxFileName::GetVolumeSeparator(wxPathFormat WXUNUSED_IN_WINCE(format)) | |
1746 | { | |
1747 | #ifdef __WXWINCE__ | |
1748 | return wxEmptyString; | |
1749 | #else | |
1750 | wxString sepVol; | |
1751 | ||
1752 | if ( (GetFormat(format) == wxPATH_DOS) || | |
1753 | (GetFormat(format) == wxPATH_VMS) ) | |
1754 | { | |
1755 | sepVol = wxFILE_SEP_DSK; | |
1756 | } | |
1757 | //else: leave empty | |
1758 | ||
1759 | return sepVol; | |
1760 | #endif | |
1761 | } | |
1762 | ||
1763 | /* static */ | |
1764 | wxString wxFileName::GetPathSeparators(wxPathFormat format) | |
1765 | { | |
1766 | wxString seps; | |
1767 | switch ( GetFormat(format) ) | |
1768 | { | |
1769 | case wxPATH_DOS: | |
1770 | // accept both as native APIs do but put the native one first as | |
1771 | // this is the one we use in GetFullPath() | |
1772 | seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX; | |
1773 | break; | |
1774 | ||
1775 | default: | |
1776 | wxFAIL_MSG( wxT("Unknown wxPATH_XXX style") ); | |
1777 | // fall through | |
1778 | ||
1779 | case wxPATH_UNIX: | |
1780 | seps = wxFILE_SEP_PATH_UNIX; | |
1781 | break; | |
1782 | ||
1783 | case wxPATH_MAC: | |
1784 | seps = wxFILE_SEP_PATH_MAC; | |
1785 | break; | |
1786 | ||
1787 | case wxPATH_VMS: | |
1788 | seps = wxFILE_SEP_PATH_VMS; | |
1789 | break; | |
1790 | } | |
1791 | ||
1792 | return seps; | |
1793 | } | |
1794 | ||
1795 | /* static */ | |
1796 | wxString wxFileName::GetPathTerminators(wxPathFormat format) | |
1797 | { | |
1798 | format = GetFormat(format); | |
1799 | ||
1800 | // under VMS the end of the path is ']', not the path separator used to | |
1801 | // separate the components | |
1802 | return format == wxPATH_VMS ? wxString(wxT(']')) : GetPathSeparators(format); | |
1803 | } | |
1804 | ||
1805 | /* static */ | |
1806 | bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) | |
1807 | { | |
1808 | // wxString::Find() doesn't work as expected with NUL - it will always find | |
1809 | // it, so test for it separately | |
1810 | return ch != wxT('\0') && GetPathSeparators(format).Find(ch) != wxNOT_FOUND; | |
1811 | } | |
1812 | ||
1813 | // ---------------------------------------------------------------------------- | |
1814 | // path components manipulation | |
1815 | // ---------------------------------------------------------------------------- | |
1816 | ||
1817 | /* static */ bool wxFileName::IsValidDirComponent(const wxString& dir) | |
1818 | { | |
1819 | if ( dir.empty() ) | |
1820 | { | |
1821 | wxFAIL_MSG( wxT("empty directory passed to wxFileName::InsertDir()") ); | |
1822 | ||
1823 | return false; | |
1824 | } | |
1825 | ||
1826 | const size_t len = dir.length(); | |
1827 | for ( size_t n = 0; n < len; n++ ) | |
1828 | { | |
1829 | if ( dir[n] == GetVolumeSeparator() || IsPathSeparator(dir[n]) ) | |
1830 | { | |
1831 | wxFAIL_MSG( wxT("invalid directory component in wxFileName") ); | |
1832 | ||
1833 | return false; | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | return true; | |
1838 | } | |
1839 | ||
1840 | void wxFileName::AppendDir( const wxString& dir ) | |
1841 | { | |
1842 | if ( IsValidDirComponent(dir) ) | |
1843 | m_dirs.Add( dir ); | |
1844 | } | |
1845 | ||
1846 | void wxFileName::PrependDir( const wxString& dir ) | |
1847 | { | |
1848 | InsertDir(0, dir); | |
1849 | } | |
1850 | ||
1851 | void wxFileName::InsertDir(size_t before, const wxString& dir) | |
1852 | { | |
1853 | if ( IsValidDirComponent(dir) ) | |
1854 | m_dirs.Insert(dir, before); | |
1855 | } | |
1856 | ||
1857 | void wxFileName::RemoveDir(size_t pos) | |
1858 | { | |
1859 | m_dirs.RemoveAt(pos); | |
1860 | } | |
1861 | ||
1862 | // ---------------------------------------------------------------------------- | |
1863 | // accessors | |
1864 | // ---------------------------------------------------------------------------- | |
1865 | ||
1866 | void wxFileName::SetFullName(const wxString& fullname) | |
1867 | { | |
1868 | SplitPath(fullname, NULL /* no volume */, NULL /* no path */, | |
1869 | &m_name, &m_ext, &m_hasExt); | |
1870 | } | |
1871 | ||
1872 | wxString wxFileName::GetFullName() const | |
1873 | { | |
1874 | wxString fullname = m_name; | |
1875 | if ( m_hasExt ) | |
1876 | { | |
1877 | fullname << wxFILE_SEP_EXT << m_ext; | |
1878 | } | |
1879 | ||
1880 | return fullname; | |
1881 | } | |
1882 | ||
1883 | wxString wxFileName::GetPath( int flags, wxPathFormat format ) const | |
1884 | { | |
1885 | format = GetFormat( format ); | |
1886 | ||
1887 | wxString fullpath; | |
1888 | ||
1889 | // return the volume with the path as well if requested | |
1890 | if ( flags & wxPATH_GET_VOLUME ) | |
1891 | { | |
1892 | fullpath += wxGetVolumeString(GetVolume(), format); | |
1893 | } | |
1894 | ||
1895 | // the leading character | |
1896 | switch ( format ) | |
1897 | { | |
1898 | case wxPATH_MAC: | |
1899 | if ( m_relative ) | |
1900 | fullpath += wxFILE_SEP_PATH_MAC; | |
1901 | break; | |
1902 | ||
1903 | case wxPATH_DOS: | |
1904 | if ( !m_relative ) | |
1905 | fullpath += wxFILE_SEP_PATH_DOS; | |
1906 | break; | |
1907 | ||
1908 | default: | |
1909 | wxFAIL_MSG( wxT("Unknown path format") ); | |
1910 | // fall through | |
1911 | ||
1912 | case wxPATH_UNIX: | |
1913 | if ( !m_relative ) | |
1914 | { | |
1915 | fullpath += wxFILE_SEP_PATH_UNIX; | |
1916 | } | |
1917 | break; | |
1918 | ||
1919 | case wxPATH_VMS: | |
1920 | // no leading character here but use this place to unset | |
1921 | // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense | |
1922 | // as, if I understand correctly, there should never be a dot | |
1923 | // before the closing bracket | |
1924 | flags &= ~wxPATH_GET_SEPARATOR; | |
1925 | } | |
1926 | ||
1927 | if ( m_dirs.empty() ) | |
1928 | { | |
1929 | // there is nothing more | |
1930 | return fullpath; | |
1931 | } | |
1932 | ||
1933 | // then concatenate all the path components using the path separator | |
1934 | if ( format == wxPATH_VMS ) | |
1935 | { | |
1936 | fullpath += wxT('['); | |
1937 | } | |
1938 | ||
1939 | const size_t dirCount = m_dirs.GetCount(); | |
1940 | for ( size_t i = 0; i < dirCount; i++ ) | |
1941 | { | |
1942 | switch (format) | |
1943 | { | |
1944 | case wxPATH_MAC: | |
1945 | if ( m_dirs[i] == wxT(".") ) | |
1946 | { | |
1947 | // skip appending ':', this shouldn't be done in this | |
1948 | // case as "::" is interpreted as ".." under Unix | |
1949 | continue; | |
1950 | } | |
1951 | ||
1952 | // convert back from ".." to nothing | |
1953 | if ( !m_dirs[i].IsSameAs(wxT("..")) ) | |
1954 | fullpath += m_dirs[i]; | |
1955 | break; | |
1956 | ||
1957 | default: | |
1958 | wxFAIL_MSG( wxT("Unexpected path format") ); | |
1959 | // still fall through | |
1960 | ||
1961 | case wxPATH_DOS: | |
1962 | case wxPATH_UNIX: | |
1963 | fullpath += m_dirs[i]; | |
1964 | break; | |
1965 | ||
1966 | case wxPATH_VMS: | |
1967 | // TODO: What to do with ".." under VMS | |
1968 | ||
1969 | // convert back from ".." to nothing | |
1970 | if ( !m_dirs[i].IsSameAs(wxT("..")) ) | |
1971 | fullpath += m_dirs[i]; | |
1972 | break; | |
1973 | } | |
1974 | ||
1975 | if ( (flags & wxPATH_GET_SEPARATOR) || (i != dirCount - 1) ) | |
1976 | fullpath += GetPathSeparator(format); | |
1977 | } | |
1978 | ||
1979 | if ( format == wxPATH_VMS ) | |
1980 | { | |
1981 | fullpath += wxT(']'); | |
1982 | } | |
1983 | ||
1984 | return fullpath; | |
1985 | } | |
1986 | ||
1987 | wxString wxFileName::GetFullPath( wxPathFormat format ) const | |
1988 | { | |
1989 | // we already have a function to get the path | |
1990 | wxString fullpath = GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR, | |
1991 | format); | |
1992 | ||
1993 | // now just add the file name and extension to it | |
1994 | fullpath += GetFullName(); | |
1995 | ||
1996 | return fullpath; | |
1997 | } | |
1998 | ||
1999 | // Return the short form of the path (returns identity on non-Windows platforms) | |
2000 | wxString wxFileName::GetShortPath() const | |
2001 | { | |
2002 | wxString path(GetFullPath()); | |
2003 | ||
2004 | #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) | |
2005 | DWORD sz = ::GetShortPathName(path.fn_str(), NULL, 0); | |
2006 | if ( sz != 0 ) | |
2007 | { | |
2008 | wxString pathOut; | |
2009 | if ( ::GetShortPathName | |
2010 | ( | |
2011 | path.fn_str(), | |
2012 | wxStringBuffer(pathOut, sz), | |
2013 | sz | |
2014 | ) != 0 ) | |
2015 | { | |
2016 | return pathOut; | |
2017 | } | |
2018 | } | |
2019 | #endif // Windows | |
2020 | ||
2021 | return path; | |
2022 | } | |
2023 | ||
2024 | // Return the long form of the path (returns identity on non-Windows platforms) | |
2025 | wxString wxFileName::GetLongPath() const | |
2026 | { | |
2027 | wxString pathOut, | |
2028 | path = GetFullPath(); | |
2029 | ||
2030 | #if defined(__WIN32__) && !defined(__WXWINCE__) && !defined(__WXMICROWIN__) | |
2031 | ||
2032 | #if wxUSE_DYNLIB_CLASS | |
2033 | typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); | |
2034 | ||
2035 | // this is MT-safe as in the worst case we're going to resolve the function | |
2036 | // twice -- but as the result is the same in both threads, it's ok | |
2037 | static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; | |
2038 | if ( !s_pfnGetLongPathName ) | |
2039 | { | |
2040 | static bool s_triedToLoad = false; | |
2041 | ||
2042 | if ( !s_triedToLoad ) | |
2043 | { | |
2044 | s_triedToLoad = true; | |
2045 | ||
2046 | wxDynamicLibrary dllKernel(wxT("kernel32")); | |
2047 | ||
2048 | const wxChar* GetLongPathName = wxT("GetLongPathName") | |
2049 | #if wxUSE_UNICODE | |
2050 | wxT("W"); | |
2051 | #else // ANSI | |
2052 | wxT("A"); | |
2053 | #endif // Unicode/ANSI | |
2054 | ||
2055 | if ( dllKernel.HasSymbol(GetLongPathName) ) | |
2056 | { | |
2057 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) | |
2058 | dllKernel.GetSymbol(GetLongPathName); | |
2059 | } | |
2060 | ||
2061 | // note that kernel32.dll can be unloaded, it stays in memory | |
2062 | // anyhow as all Win32 programs link to it and so it's safe to call | |
2063 | // GetLongPathName() even after unloading it | |
2064 | } | |
2065 | } | |
2066 | ||
2067 | if ( s_pfnGetLongPathName ) | |
2068 | { | |
2069 | DWORD dwSize = (*s_pfnGetLongPathName)(path.fn_str(), NULL, 0); | |
2070 | if ( dwSize > 0 ) | |
2071 | { | |
2072 | if ( (*s_pfnGetLongPathName) | |
2073 | ( | |
2074 | path.fn_str(), | |
2075 | wxStringBuffer(pathOut, dwSize), | |
2076 | dwSize | |
2077 | ) != 0 ) | |
2078 | { | |
2079 | return pathOut; | |
2080 | } | |
2081 | } | |
2082 | } | |
2083 | #endif // wxUSE_DYNLIB_CLASS | |
2084 | ||
2085 | // The OS didn't support GetLongPathName, or some other error. | |
2086 | // We need to call FindFirstFile on each component in turn. | |
2087 | ||
2088 | WIN32_FIND_DATA findFileData; | |
2089 | HANDLE hFind; | |
2090 | ||
2091 | if ( HasVolume() ) | |
2092 | pathOut = GetVolume() + | |
2093 | GetVolumeSeparator(wxPATH_DOS) + | |
2094 | GetPathSeparator(wxPATH_DOS); | |
2095 | else | |
2096 | pathOut = wxEmptyString; | |
2097 | ||
2098 | wxArrayString dirs = GetDirs(); | |
2099 | dirs.Add(GetFullName()); | |
2100 | ||
2101 | wxString tmpPath; | |
2102 | ||
2103 | size_t count = dirs.GetCount(); | |
2104 | for ( size_t i = 0; i < count; i++ ) | |
2105 | { | |
2106 | const wxString& dir = dirs[i]; | |
2107 | ||
2108 | // We're using pathOut to collect the long-name path, but using a | |
2109 | // temporary for appending the last path component which may be | |
2110 | // short-name | |
2111 | tmpPath = pathOut + dir; | |
2112 | ||
2113 | // We must not process "." or ".." here as they would be (unexpectedly) | |
2114 | // replaced by the corresponding directory names so just leave them | |
2115 | // alone | |
2116 | // | |
2117 | // And we can't pass a drive and root dir to FindFirstFile (VZ: why?) | |
2118 | if ( tmpPath.empty() || dir == '.' || dir == ".." || | |
2119 | tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) ) | |
2120 | { | |
2121 | tmpPath += wxFILE_SEP_PATH; | |
2122 | pathOut = tmpPath; | |
2123 | continue; | |
2124 | } | |
2125 | ||
2126 | hFind = ::FindFirstFile(tmpPath.fn_str(), &findFileData); | |
2127 | if (hFind == INVALID_HANDLE_VALUE) | |
2128 | { | |
2129 | // Error: most likely reason is that path doesn't exist, so | |
2130 | // append any unprocessed parts and return | |
2131 | for ( i += 1; i < count; i++ ) | |
2132 | tmpPath += wxFILE_SEP_PATH + dirs[i]; | |
2133 | ||
2134 | return tmpPath; | |
2135 | } | |
2136 | ||
2137 | pathOut += findFileData.cFileName; | |
2138 | if ( (i < (count-1)) ) | |
2139 | pathOut += wxFILE_SEP_PATH; | |
2140 | ||
2141 | ::FindClose(hFind); | |
2142 | } | |
2143 | #else // !Win32 | |
2144 | pathOut = path; | |
2145 | #endif // Win32/!Win32 | |
2146 | ||
2147 | return pathOut; | |
2148 | } | |
2149 | ||
2150 | wxPathFormat wxFileName::GetFormat( wxPathFormat format ) | |
2151 | { | |
2152 | if (format == wxPATH_NATIVE) | |
2153 | { | |
2154 | #if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__) | |
2155 | format = wxPATH_DOS; | |
2156 | #elif defined(__VMS) | |
2157 | format = wxPATH_VMS; | |
2158 | #else | |
2159 | format = wxPATH_UNIX; | |
2160 | #endif | |
2161 | } | |
2162 | return format; | |
2163 | } | |
2164 | ||
2165 | #ifdef wxHAS_FILESYSTEM_VOLUMES | |
2166 | ||
2167 | /* static */ | |
2168 | wxString wxFileName::GetVolumeString(char drive, int flags) | |
2169 | { | |
2170 | wxASSERT_MSG( !(flags & ~wxPATH_GET_SEPARATOR), "invalid flag specified" ); | |
2171 | ||
2172 | wxString vol(drive); | |
2173 | vol += wxFILE_SEP_DSK; | |
2174 | if ( flags & wxPATH_GET_SEPARATOR ) | |
2175 | vol += wxFILE_SEP_PATH; | |
2176 | ||
2177 | return vol; | |
2178 | } | |
2179 | ||
2180 | #endif // wxHAS_FILESYSTEM_VOLUMES | |
2181 | ||
2182 | // ---------------------------------------------------------------------------- | |
2183 | // path splitting function | |
2184 | // ---------------------------------------------------------------------------- | |
2185 | ||
2186 | /* static */ | |
2187 | void | |
2188 | wxFileName::SplitVolume(const wxString& fullpathWithVolume, | |
2189 | wxString *pstrVolume, | |
2190 | wxString *pstrPath, | |
2191 | wxPathFormat format) | |
2192 | { | |
2193 | format = GetFormat(format); | |
2194 | ||
2195 | wxString fullpath = fullpathWithVolume; | |
2196 | ||
2197 | // special Windows UNC paths hack: transform \\share\path into share:path | |
2198 | if ( IsUNCPath(fullpath, format) ) | |
2199 | { | |
2200 | fullpath.erase(0, 2); | |
2201 | ||
2202 | size_t posFirstSlash = | |
2203 | fullpath.find_first_of(GetPathTerminators(format)); | |
2204 | if ( posFirstSlash != wxString::npos ) | |
2205 | { | |
2206 | fullpath[posFirstSlash] = wxFILE_SEP_DSK; | |
2207 | ||
2208 | // UNC paths are always absolute, right? (FIXME) | |
2209 | fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS); | |
2210 | } | |
2211 | } | |
2212 | ||
2213 | // We separate the volume here | |
2214 | if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
2215 | { | |
2216 | wxString sepVol = GetVolumeSeparator(format); | |
2217 | ||
2218 | // we have to exclude the case of a colon in the very beginning of the | |
2219 | // string as it can't be a volume separator (nor can this be a valid | |
2220 | // DOS file name at all but we'll leave dealing with this to our caller) | |
2221 | size_t posFirstColon = fullpath.find_first_of(sepVol); | |
2222 | if ( posFirstColon && posFirstColon != wxString::npos ) | |
2223 | { | |
2224 | if ( pstrVolume ) | |
2225 | { | |
2226 | *pstrVolume = fullpath.Left(posFirstColon); | |
2227 | } | |
2228 | ||
2229 | // remove the volume name and the separator from the full path | |
2230 | fullpath.erase(0, posFirstColon + sepVol.length()); | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | if ( pstrPath ) | |
2235 | *pstrPath = fullpath; | |
2236 | } | |
2237 | ||
2238 | /* static */ | |
2239 | void wxFileName::SplitPath(const wxString& fullpathWithVolume, | |
2240 | wxString *pstrVolume, | |
2241 | wxString *pstrPath, | |
2242 | wxString *pstrName, | |
2243 | wxString *pstrExt, | |
2244 | bool *hasExt, | |
2245 | wxPathFormat format) | |
2246 | { | |
2247 | format = GetFormat(format); | |
2248 | ||
2249 | wxString fullpath; | |
2250 | SplitVolume(fullpathWithVolume, pstrVolume, &fullpath, format); | |
2251 | ||
2252 | // find the positions of the last dot and last path separator in the path | |
2253 | size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); | |
2254 | size_t posLastSlash = fullpath.find_last_of(GetPathTerminators(format)); | |
2255 | ||
2256 | // check whether this dot occurs at the very beginning of a path component | |
2257 | if ( (posLastDot != wxString::npos) && | |
2258 | (posLastDot == 0 || | |
2259 | IsPathSeparator(fullpath[posLastDot - 1]) || | |
2260 | (format == wxPATH_VMS && fullpath[posLastDot - 1] == wxT(']'))) ) | |
2261 | { | |
2262 | // dot may be (and commonly -- at least under Unix -- is) the first | |
2263 | // character of the filename, don't treat the entire filename as | |
2264 | // extension in this case | |
2265 | posLastDot = wxString::npos; | |
2266 | } | |
2267 | ||
2268 | // if we do have a dot and a slash, check that the dot is in the name part | |
2269 | if ( (posLastDot != wxString::npos) && | |
2270 | (posLastSlash != wxString::npos) && | |
2271 | (posLastDot < posLastSlash) ) | |
2272 | { | |
2273 | // the dot is part of the path, not the start of the extension | |
2274 | posLastDot = wxString::npos; | |
2275 | } | |
2276 | ||
2277 | // now fill in the variables provided by user | |
2278 | if ( pstrPath ) | |
2279 | { | |
2280 | if ( posLastSlash == wxString::npos ) | |
2281 | { | |
2282 | // no path at all | |
2283 | pstrPath->Empty(); | |
2284 | } | |
2285 | else | |
2286 | { | |
2287 | // take everything up to the path separator but take care to make | |
2288 | // the path equal to something like '/', not empty, for the files | |
2289 | // immediately under root directory | |
2290 | size_t len = posLastSlash; | |
2291 | ||
2292 | // this rule does not apply to mac since we do not start with colons (sep) | |
2293 | // except for relative paths | |
2294 | if ( !len && format != wxPATH_MAC) | |
2295 | len++; | |
2296 | ||
2297 | *pstrPath = fullpath.Left(len); | |
2298 | ||
2299 | // special VMS hack: remove the initial bracket | |
2300 | if ( format == wxPATH_VMS ) | |
2301 | { | |
2302 | if ( (*pstrPath)[0u] == wxT('[') ) | |
2303 | pstrPath->erase(0, 1); | |
2304 | } | |
2305 | } | |
2306 | } | |
2307 | ||
2308 | if ( pstrName ) | |
2309 | { | |
2310 | // take all characters starting from the one after the last slash and | |
2311 | // up to, but excluding, the last dot | |
2312 | size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; | |
2313 | size_t count; | |
2314 | if ( posLastDot == wxString::npos ) | |
2315 | { | |
2316 | // take all until the end | |
2317 | count = wxString::npos; | |
2318 | } | |
2319 | else if ( posLastSlash == wxString::npos ) | |
2320 | { | |
2321 | count = posLastDot; | |
2322 | } | |
2323 | else // have both dot and slash | |
2324 | { | |
2325 | count = posLastDot - posLastSlash - 1; | |
2326 | } | |
2327 | ||
2328 | *pstrName = fullpath.Mid(nStart, count); | |
2329 | } | |
2330 | ||
2331 | // finally deal with the extension here: we have an added complication that | |
2332 | // extension may be empty (but present) as in "foo." where trailing dot | |
2333 | // indicates the empty extension at the end -- and hence we must remember | |
2334 | // that we have it independently of pstrExt | |
2335 | if ( posLastDot == wxString::npos ) | |
2336 | { | |
2337 | // no extension | |
2338 | if ( pstrExt ) | |
2339 | pstrExt->clear(); | |
2340 | if ( hasExt ) | |
2341 | *hasExt = false; | |
2342 | } | |
2343 | else | |
2344 | { | |
2345 | // take everything after the dot | |
2346 | if ( pstrExt ) | |
2347 | *pstrExt = fullpath.Mid(posLastDot + 1); | |
2348 | if ( hasExt ) | |
2349 | *hasExt = true; | |
2350 | } | |
2351 | } | |
2352 | ||
2353 | /* static */ | |
2354 | void wxFileName::SplitPath(const wxString& fullpath, | |
2355 | wxString *path, | |
2356 | wxString *name, | |
2357 | wxString *ext, | |
2358 | wxPathFormat format) | |
2359 | { | |
2360 | wxString volume; | |
2361 | SplitPath(fullpath, &volume, path, name, ext, format); | |
2362 | ||
2363 | if ( path ) | |
2364 | { | |
2365 | path->Prepend(wxGetVolumeString(volume, format)); | |
2366 | } | |
2367 | } | |
2368 | ||
2369 | /* static */ | |
2370 | wxString wxFileName::StripExtension(const wxString& fullpath) | |
2371 | { | |
2372 | wxFileName fn(fullpath); | |
2373 | fn.SetExt(""); | |
2374 | return fn.GetFullPath(); | |
2375 | } | |
2376 | ||
2377 | // ---------------------------------------------------------------------------- | |
2378 | // time functions | |
2379 | // ---------------------------------------------------------------------------- | |
2380 | ||
2381 | #if wxUSE_DATETIME | |
2382 | ||
2383 | bool wxFileName::SetTimes(const wxDateTime *dtAccess, | |
2384 | const wxDateTime *dtMod, | |
2385 | const wxDateTime *dtCreate) const | |
2386 | { | |
2387 | #if defined(__WIN32__) | |
2388 | FILETIME ftAccess, ftCreate, ftWrite; | |
2389 | ||
2390 | if ( dtCreate ) | |
2391 | ConvertWxToFileTime(&ftCreate, *dtCreate); | |
2392 | if ( dtAccess ) | |
2393 | ConvertWxToFileTime(&ftAccess, *dtAccess); | |
2394 | if ( dtMod ) | |
2395 | ConvertWxToFileTime(&ftWrite, *dtMod); | |
2396 | ||
2397 | wxString path; | |
2398 | int flags; | |
2399 | if ( IsDir() ) | |
2400 | { | |
2401 | if ( wxGetOsVersion() == wxOS_WINDOWS_9X ) | |
2402 | { | |
2403 | wxLogError(_("Setting directory access times is not supported " | |
2404 | "under this OS version")); | |
2405 | return false; | |
2406 | } | |
2407 | ||
2408 | path = GetPath(); | |
2409 | flags = FILE_FLAG_BACKUP_SEMANTICS; | |
2410 | } | |
2411 | else // file | |
2412 | { | |
2413 | path = GetFullPath(); | |
2414 | flags = 0; | |
2415 | } | |
2416 | ||
2417 | wxFileHandle fh(path, wxFileHandle::WriteAttr, flags); | |
2418 | if ( fh.IsOk() ) | |
2419 | { | |
2420 | if ( ::SetFileTime(fh, | |
2421 | dtCreate ? &ftCreate : NULL, | |
2422 | dtAccess ? &ftAccess : NULL, | |
2423 | dtMod ? &ftWrite : NULL) ) | |
2424 | { | |
2425 | return true; | |
2426 | } | |
2427 | } | |
2428 | #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) | |
2429 | wxUnusedVar(dtCreate); | |
2430 | ||
2431 | if ( !dtAccess && !dtMod ) | |
2432 | { | |
2433 | // can't modify the creation time anyhow, don't try | |
2434 | return true; | |
2435 | } | |
2436 | ||
2437 | // if dtAccess or dtMod is not specified, use the other one (which must be | |
2438 | // non NULL because of the test above) for both times | |
2439 | utimbuf utm; | |
2440 | utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks(); | |
2441 | utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); | |
2442 | if ( utime(GetFullPath().fn_str(), &utm) == 0 ) | |
2443 | { | |
2444 | return true; | |
2445 | } | |
2446 | #else // other platform | |
2447 | wxUnusedVar(dtAccess); | |
2448 | wxUnusedVar(dtMod); | |
2449 | wxUnusedVar(dtCreate); | |
2450 | #endif // platforms | |
2451 | ||
2452 | wxLogSysError(_("Failed to modify file times for '%s'"), | |
2453 | GetFullPath().c_str()); | |
2454 | ||
2455 | return false; | |
2456 | } | |
2457 | ||
2458 | bool wxFileName::Touch() const | |
2459 | { | |
2460 | #if defined(__UNIX_LIKE__) | |
2461 | // under Unix touching file is simple: just pass NULL to utime() | |
2462 | if ( utime(GetFullPath().fn_str(), NULL) == 0 ) | |
2463 | { | |
2464 | return true; | |
2465 | } | |
2466 | ||
2467 | wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str()); | |
2468 | ||
2469 | return false; | |
2470 | #else // other platform | |
2471 | wxDateTime dtNow = wxDateTime::Now(); | |
2472 | ||
2473 | return SetTimes(&dtNow, &dtNow, NULL /* don't change create time */); | |
2474 | #endif // platforms | |
2475 | } | |
2476 | ||
2477 | bool wxFileName::GetTimes(wxDateTime *dtAccess, | |
2478 | wxDateTime *dtMod, | |
2479 | wxDateTime *dtCreate) const | |
2480 | { | |
2481 | #if defined(__WIN32__) | |
2482 | // we must use different methods for the files and directories under | |
2483 | // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and | |
2484 | // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and | |
2485 | // not 9x | |
2486 | bool ok; | |
2487 | FILETIME ftAccess, ftCreate, ftWrite; | |
2488 | if ( IsDir() ) | |
2489 | { | |
2490 | // implemented in msw/dir.cpp | |
2491 | extern bool wxGetDirectoryTimes(const wxString& dirname, | |
2492 | FILETIME *, FILETIME *, FILETIME *); | |
2493 | ||
2494 | // we should pass the path without the trailing separator to | |
2495 | // wxGetDirectoryTimes() | |
2496 | ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME), | |
2497 | &ftAccess, &ftCreate, &ftWrite); | |
2498 | } | |
2499 | else // file | |
2500 | { | |
2501 | wxFileHandle fh(GetFullPath(), wxFileHandle::ReadAttr); | |
2502 | if ( fh.IsOk() ) | |
2503 | { | |
2504 | ok = ::GetFileTime(fh, | |
2505 | dtCreate ? &ftCreate : NULL, | |
2506 | dtAccess ? &ftAccess : NULL, | |
2507 | dtMod ? &ftWrite : NULL) != 0; | |
2508 | } | |
2509 | else | |
2510 | { | |
2511 | ok = false; | |
2512 | } | |
2513 | } | |
2514 | ||
2515 | if ( ok ) | |
2516 | { | |
2517 | if ( dtCreate ) | |
2518 | ConvertFileTimeToWx(dtCreate, ftCreate); | |
2519 | if ( dtAccess ) | |
2520 | ConvertFileTimeToWx(dtAccess, ftAccess); | |
2521 | if ( dtMod ) | |
2522 | ConvertFileTimeToWx(dtMod, ftWrite); | |
2523 | ||
2524 | return true; | |
2525 | } | |
2526 | #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__)) | |
2527 | // no need to test for IsDir() here | |
2528 | wxStructStat stBuf; | |
2529 | if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 ) | |
2530 | { | |
2531 | if ( dtAccess ) | |
2532 | dtAccess->Set(stBuf.st_atime); | |
2533 | if ( dtMod ) | |
2534 | dtMod->Set(stBuf.st_mtime); | |
2535 | if ( dtCreate ) | |
2536 | dtCreate->Set(stBuf.st_ctime); | |
2537 | ||
2538 | return true; | |
2539 | } | |
2540 | #else // other platform | |
2541 | wxUnusedVar(dtAccess); | |
2542 | wxUnusedVar(dtMod); | |
2543 | wxUnusedVar(dtCreate); | |
2544 | #endif // platforms | |
2545 | ||
2546 | wxLogSysError(_("Failed to retrieve file times for '%s'"), | |
2547 | GetFullPath().c_str()); | |
2548 | ||
2549 | return false; | |
2550 | } | |
2551 | ||
2552 | #endif // wxUSE_DATETIME | |
2553 | ||
2554 | ||
2555 | // ---------------------------------------------------------------------------- | |
2556 | // file size functions | |
2557 | // ---------------------------------------------------------------------------- | |
2558 | ||
2559 | #if wxUSE_LONGLONG | |
2560 | ||
2561 | /* static */ | |
2562 | wxULongLong wxFileName::GetSize(const wxString &filename) | |
2563 | { | |
2564 | if (!wxFileExists(filename)) | |
2565 | return wxInvalidSize; | |
2566 | ||
2567 | #if defined(__WXPALMOS__) | |
2568 | // TODO | |
2569 | return wxInvalidSize; | |
2570 | #elif defined(__WIN32__) | |
2571 | wxFileHandle f(filename, wxFileHandle::ReadAttr); | |
2572 | if (!f.IsOk()) | |
2573 | return wxInvalidSize; | |
2574 | ||
2575 | DWORD lpFileSizeHigh; | |
2576 | DWORD ret = GetFileSize(f, &lpFileSizeHigh); | |
2577 | if ( ret == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR ) | |
2578 | return wxInvalidSize; | |
2579 | ||
2580 | return wxULongLong(lpFileSizeHigh, ret); | |
2581 | #else // ! __WIN32__ | |
2582 | wxStructStat st; | |
2583 | #ifndef wxNEED_WX_UNISTD_H | |
2584 | if (wxStat( filename.fn_str() , &st) != 0) | |
2585 | #else | |
2586 | if (wxStat( filename, &st) != 0) | |
2587 | #endif | |
2588 | return wxInvalidSize; | |
2589 | return wxULongLong(st.st_size); | |
2590 | #endif | |
2591 | } | |
2592 | ||
2593 | /* static */ | |
2594 | wxString wxFileName::GetHumanReadableSize(const wxULongLong &bs, | |
2595 | const wxString &nullsize, | |
2596 | int precision) | |
2597 | { | |
2598 | static const double KILOBYTESIZE = 1024.0; | |
2599 | static const double MEGABYTESIZE = 1024.0*KILOBYTESIZE; | |
2600 | static const double GIGABYTESIZE = 1024.0*MEGABYTESIZE; | |
2601 | static const double TERABYTESIZE = 1024.0*GIGABYTESIZE; | |
2602 | ||
2603 | if (bs == 0 || bs == wxInvalidSize) | |
2604 | return nullsize; | |
2605 | ||
2606 | double bytesize = bs.ToDouble(); | |
2607 | if (bytesize < KILOBYTESIZE) | |
2608 | return wxString::Format(_("%s B"), bs.ToString().c_str()); | |
2609 | if (bytesize < MEGABYTESIZE) | |
2610 | return wxString::Format(_("%.*f kB"), precision, bytesize/KILOBYTESIZE); | |
2611 | if (bytesize < GIGABYTESIZE) | |
2612 | return wxString::Format(_("%.*f MB"), precision, bytesize/MEGABYTESIZE); | |
2613 | if (bytesize < TERABYTESIZE) | |
2614 | return wxString::Format(_("%.*f GB"), precision, bytesize/GIGABYTESIZE); | |
2615 | ||
2616 | return wxString::Format(_("%.*f TB"), precision, bytesize/TERABYTESIZE); | |
2617 | } | |
2618 | ||
2619 | wxULongLong wxFileName::GetSize() const | |
2620 | { | |
2621 | return GetSize(GetFullPath()); | |
2622 | } | |
2623 | ||
2624 | wxString wxFileName::GetHumanReadableSize(const wxString &failmsg, int precision) const | |
2625 | { | |
2626 | return GetHumanReadableSize(GetSize(), failmsg, precision); | |
2627 | } | |
2628 | ||
2629 | #endif // wxUSE_LONGLONG | |
2630 | ||
2631 | // ---------------------------------------------------------------------------- | |
2632 | // Mac-specific functions | |
2633 | // ---------------------------------------------------------------------------- | |
2634 | ||
2635 | #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON | |
2636 | ||
2637 | namespace | |
2638 | { | |
2639 | ||
2640 | class MacDefaultExtensionRecord | |
2641 | { | |
2642 | public: | |
2643 | MacDefaultExtensionRecord() | |
2644 | { | |
2645 | m_type = | |
2646 | m_creator = 0 ; | |
2647 | } | |
2648 | ||
2649 | // default copy ctor, assignment operator and dtor are ok | |
2650 | ||
2651 | MacDefaultExtensionRecord(const wxString& ext, OSType type, OSType creator) | |
2652 | : m_ext(ext) | |
2653 | { | |
2654 | m_type = type; | |
2655 | m_creator = creator; | |
2656 | } | |
2657 | ||
2658 | wxString m_ext; | |
2659 | OSType m_type; | |
2660 | OSType m_creator; | |
2661 | }; | |
2662 | ||
2663 | WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray); | |
2664 | ||
2665 | bool gMacDefaultExtensionsInited = false; | |
2666 | ||
2667 | #include "wx/arrimpl.cpp" | |
2668 | ||
2669 | WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray); | |
2670 | ||
2671 | MacDefaultExtensionArray gMacDefaultExtensions; | |
2672 | ||
2673 | // load the default extensions | |
2674 | const MacDefaultExtensionRecord gDefaults[] = | |
2675 | { | |
2676 | MacDefaultExtensionRecord( "txt", 'TEXT', 'ttxt' ), | |
2677 | MacDefaultExtensionRecord( "tif", 'TIFF', '****' ), | |
2678 | MacDefaultExtensionRecord( "jpg", 'JPEG', '****' ), | |
2679 | }; | |
2680 | ||
2681 | void MacEnsureDefaultExtensionsLoaded() | |
2682 | { | |
2683 | if ( !gMacDefaultExtensionsInited ) | |
2684 | { | |
2685 | // we could load the pc exchange prefs here too | |
2686 | for ( size_t i = 0 ; i < WXSIZEOF( gDefaults ) ; ++i ) | |
2687 | { | |
2688 | gMacDefaultExtensions.Add( gDefaults[i] ) ; | |
2689 | } | |
2690 | gMacDefaultExtensionsInited = true; | |
2691 | } | |
2692 | } | |
2693 | ||
2694 | } // anonymous namespace | |
2695 | ||
2696 | bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) | |
2697 | { | |
2698 | FSRef fsRef ; | |
2699 | FSCatalogInfo catInfo; | |
2700 | FileInfo *finfo ; | |
2701 | ||
2702 | if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) | |
2703 | { | |
2704 | if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) | |
2705 | { | |
2706 | finfo = (FileInfo*)&catInfo.finderInfo; | |
2707 | finfo->fileType = type ; | |
2708 | finfo->fileCreator = creator ; | |
2709 | FSSetCatalogInfo( &fsRef, kFSCatInfoFinderInfo, &catInfo ) ; | |
2710 | return true ; | |
2711 | } | |
2712 | } | |
2713 | return false ; | |
2714 | } | |
2715 | ||
2716 | bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) const | |
2717 | { | |
2718 | FSRef fsRef ; | |
2719 | FSCatalogInfo catInfo; | |
2720 | FileInfo *finfo ; | |
2721 | ||
2722 | if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) | |
2723 | { | |
2724 | if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) | |
2725 | { | |
2726 | finfo = (FileInfo*)&catInfo.finderInfo; | |
2727 | *type = finfo->fileType ; | |
2728 | *creator = finfo->fileCreator ; | |
2729 | return true ; | |
2730 | } | |
2731 | } | |
2732 | return false ; | |
2733 | } | |
2734 | ||
2735 | bool wxFileName::MacSetDefaultTypeAndCreator() | |
2736 | { | |
2737 | wxUint32 type , creator ; | |
2738 | if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type , | |
2739 | &creator ) ) | |
2740 | { | |
2741 | return MacSetTypeAndCreator( type , creator ) ; | |
2742 | } | |
2743 | return false; | |
2744 | } | |
2745 | ||
2746 | bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator ) | |
2747 | { | |
2748 | MacEnsureDefaultExtensionsLoaded() ; | |
2749 | wxString extl = ext.Lower() ; | |
2750 | for( int i = gMacDefaultExtensions.Count() - 1 ; i >= 0 ; --i ) | |
2751 | { | |
2752 | if ( gMacDefaultExtensions.Item(i).m_ext == extl ) | |
2753 | { | |
2754 | *type = gMacDefaultExtensions.Item(i).m_type ; | |
2755 | *creator = gMacDefaultExtensions.Item(i).m_creator ; | |
2756 | return true ; | |
2757 | } | |
2758 | } | |
2759 | return false ; | |
2760 | } | |
2761 | ||
2762 | void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator ) | |
2763 | { | |
2764 | MacEnsureDefaultExtensionsLoaded(); | |
2765 | MacDefaultExtensionRecord rec(ext.Lower(), type, creator); | |
2766 | gMacDefaultExtensions.Add( rec ); | |
2767 | } | |
2768 | ||
2769 | #endif // defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON |