]>
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 license | |
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 specyfication | |
51 | . separator between directory and subdirectory | |
52 | ] between directory and file | |
53 | */ | |
54 | ||
55 | // ============================================================================ | |
56 | // declarations | |
57 | // ============================================================================ | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // headers | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | #ifdef __GNUG__ | |
64 | #pragma implementation "filename.h" | |
65 | #endif | |
66 | ||
67 | // For compilers that support precompilation, includes "wx.h". | |
68 | #include "wx/wxprec.h" | |
69 | ||
70 | #ifdef __BORLANDC__ | |
71 | #pragma hdrstop | |
72 | #endif | |
73 | ||
74 | #ifndef WX_PRECOMP | |
75 | #include "wx/intl.h" | |
76 | #include "wx/log.h" | |
77 | #endif | |
78 | ||
79 | #include "wx/filename.h" | |
80 | #include "wx/tokenzr.h" | |
81 | #include "wx/config.h" // for wxExpandEnvVars | |
82 | #include "wx/utils.h" | |
83 | ||
84 | #if wxUSE_DYNLIB_CLASS | |
85 | #include "wx/dynlib.h" | |
86 | #endif | |
87 | ||
88 | // For GetShort/LongPathName | |
89 | #ifdef __WIN32__ | |
90 | #include <windows.h> | |
91 | ||
92 | #include "wx/msw/winundef.h" | |
93 | #endif | |
94 | ||
95 | // utime() is POSIX so should normally be available on all Unices | |
96 | #ifdef __UNIX_LIKE__ | |
97 | #include <sys/types.h> | |
98 | #include <utime.h> | |
99 | #include <sys/stat.h> | |
100 | #include <unistd.h> | |
101 | #endif | |
102 | ||
103 | #ifdef __MWERKS__ | |
104 | #include <stat.h> | |
105 | #include <unistd.h> | |
106 | #include <unix.h> | |
107 | #endif | |
108 | ||
109 | #ifdef __WATCOMC__ | |
110 | #include <io.h> | |
111 | #include <sys/utime.h> | |
112 | #include <sys/stat.h> | |
113 | #endif | |
114 | ||
115 | #ifdef __VISAGECPP__ | |
116 | #ifndef MAX_PATH | |
117 | #define MAX_PATH 256 | |
118 | #endif | |
119 | #endif | |
120 | ||
121 | // ---------------------------------------------------------------------------- | |
122 | // private classes | |
123 | // ---------------------------------------------------------------------------- | |
124 | ||
125 | // small helper class which opens and closes the file - we use it just to get | |
126 | // a file handle for the given file name to pass it to some Win32 API function | |
127 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
128 | ||
129 | class wxFileHandle | |
130 | { | |
131 | public: | |
132 | wxFileHandle(const wxString& filename) | |
133 | { | |
134 | m_hFile = ::CreateFile | |
135 | ( | |
136 | filename, // name | |
137 | GENERIC_READ, // access mask | |
138 | 0, // no sharing | |
139 | NULL, // no secutity attr | |
140 | OPEN_EXISTING, // creation disposition | |
141 | 0, // no flags | |
142 | NULL // no template file | |
143 | ); | |
144 | ||
145 | if ( m_hFile == INVALID_HANDLE_VALUE ) | |
146 | { | |
147 | wxLogSysError(_("Failed to open '%s' for reading"), | |
148 | filename.c_str()); | |
149 | } | |
150 | } | |
151 | ||
152 | ~wxFileHandle() | |
153 | { | |
154 | if ( m_hFile != INVALID_HANDLE_VALUE ) | |
155 | { | |
156 | if ( !::CloseHandle(m_hFile) ) | |
157 | { | |
158 | wxLogSysError(_("Failed to close file handle")); | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | // return TRUE only if the file could be opened successfully | |
164 | bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; } | |
165 | ||
166 | // get the handle | |
167 | operator HANDLE() const { return m_hFile; } | |
168 | ||
169 | private: | |
170 | HANDLE m_hFile; | |
171 | }; | |
172 | ||
173 | #endif // __WIN32__ | |
174 | ||
175 | // ---------------------------------------------------------------------------- | |
176 | // private functions | |
177 | // ---------------------------------------------------------------------------- | |
178 | ||
179 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
180 | ||
181 | // convert between wxDateTime and FILETIME which is a 64-bit value representing | |
182 | // the number of 100-nanosecond intervals since January 1, 1601. | |
183 | ||
184 | // the number of milliseconds between the Unix Epoch (January 1, 1970) and the | |
185 | // FILETIME reference point (January 1, 1601) | |
186 | static const wxLongLong FILETIME_EPOCH_OFFSET = wxLongLong(0xa97, 0x30b66800); | |
187 | ||
188 | static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft) | |
189 | { | |
190 | wxLongLong ll(ft.dwHighDateTime, ft.dwLowDateTime); | |
191 | ||
192 | // convert 100ns to ms | |
193 | ll /= 10000; | |
194 | ||
195 | // move it to our Epoch | |
196 | ll -= FILETIME_EPOCH_OFFSET; | |
197 | ||
198 | *dt = wxDateTime(ll); | |
199 | } | |
200 | ||
201 | static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) | |
202 | { | |
203 | // do the reverse of ConvertFileTimeToWx() | |
204 | wxLongLong ll = dt.GetValue(); | |
205 | ll *= 10000; | |
206 | ll += FILETIME_EPOCH_OFFSET; | |
207 | ||
208 | ft->dwHighDateTime = ll.GetHi(); | |
209 | ft->dwLowDateTime = ll.GetLo(); | |
210 | } | |
211 | ||
212 | #endif // __WIN32__ | |
213 | ||
214 | // ============================================================================ | |
215 | // implementation | |
216 | // ============================================================================ | |
217 | ||
218 | // ---------------------------------------------------------------------------- | |
219 | // wxFileName construction | |
220 | // ---------------------------------------------------------------------------- | |
221 | ||
222 | void wxFileName::Assign( const wxFileName &filepath ) | |
223 | { | |
224 | m_volume = filepath.GetVolume(); | |
225 | m_dirs = filepath.GetDirs(); | |
226 | m_name = filepath.GetName(); | |
227 | m_ext = filepath.GetExt(); | |
228 | m_relative = filepath.IsRelative(); | |
229 | } | |
230 | ||
231 | void wxFileName::Assign(const wxString& volume, | |
232 | const wxString& path, | |
233 | const wxString& name, | |
234 | const wxString& ext, | |
235 | wxPathFormat format ) | |
236 | { | |
237 | wxPathFormat my_format = GetFormat( format ); | |
238 | wxString my_path = path; | |
239 | ||
240 | m_dirs.Clear(); | |
241 | ||
242 | if (!my_path.empty()) | |
243 | { | |
244 | // 1) Determine if the path is relative or absolute. | |
245 | ||
246 | switch (my_format) | |
247 | { | |
248 | case wxPATH_MAC: | |
249 | m_relative = ( my_path[0u] == wxT(':') ); | |
250 | // We then remove a leading ":". The reason is in our | |
251 | // storage form for relative paths: | |
252 | // ":dir:file.txt" actually means "./dir/file.txt" in | |
253 | // DOS notation and should get stored as | |
254 | // (relative) (dir) (file.txt) | |
255 | // "::dir:file.txt" actually means "../dir/file.txt" | |
256 | // stored as (relative) (..) (dir) (file.txt) | |
257 | // This is important only for the Mac as an empty dir | |
258 | // actually means <UP>, whereas under DOS, double | |
259 | // slashes can be ignored: "\\\\" is the same as "\\". | |
260 | if (m_relative) | |
261 | my_path.Remove( 0, 1 ); | |
262 | break; | |
263 | case wxPATH_VMS: | |
264 | // TODO: what is the relative path format here? | |
265 | m_relative = FALSE; | |
266 | break; | |
267 | case wxPATH_UNIX: | |
268 | m_relative = ( my_path[0u] != wxT('/') ); | |
269 | break; | |
270 | case wxPATH_DOS: | |
271 | m_relative = ( (my_path[0u] != wxT('/')) && (my_path[0u] != wxT('\\')) ); | |
272 | break; | |
273 | default: | |
274 | wxFAIL_MSG( "error" ); | |
275 | break; | |
276 | } | |
277 | ||
278 | // 2) Break up the path into its members. If the original path | |
279 | // was just "/" or "\\", m_dirs will be empty. We know from | |
280 | // the m_relative field, if this means "nothing" or "root dir". | |
281 | ||
282 | wxStringTokenizer tn( my_path, GetPathSeparators(my_format) ); | |
283 | ||
284 | while ( tn.HasMoreTokens() ) | |
285 | { | |
286 | wxString token = tn.GetNextToken(); | |
287 | ||
288 | // Remove empty token under DOS and Unix, interpret them | |
289 | // as .. under Mac. | |
290 | if (token.empty()) | |
291 | { | |
292 | if (my_format == wxPATH_MAC) | |
293 | m_dirs.Add( wxT("..") ); | |
294 | // else ignore | |
295 | } | |
296 | else | |
297 | { | |
298 | m_dirs.Add( token ); | |
299 | } | |
300 | } | |
301 | } | |
302 | ||
303 | m_volume = volume; | |
304 | m_ext = ext; | |
305 | m_name = name; | |
306 | } | |
307 | ||
308 | void wxFileName::Assign(const wxString& fullpath, | |
309 | wxPathFormat format) | |
310 | { | |
311 | wxString volume, path, name, ext; | |
312 | SplitPath(fullpath, &volume, &path, &name, &ext, format); | |
313 | ||
314 | Assign(volume, path, name, ext, format); | |
315 | } | |
316 | ||
317 | void wxFileName::Assign(const wxString& fullpathOrig, | |
318 | const wxString& fullname, | |
319 | wxPathFormat format) | |
320 | { | |
321 | // always recognize fullpath as directory, even if it doesn't end with a | |
322 | // slash | |
323 | wxString fullpath = fullpathOrig; | |
324 | if ( !wxEndsWithPathSeparator(fullpath) ) | |
325 | { | |
326 | fullpath += GetPathSeparators(format)[0u]; | |
327 | } | |
328 | ||
329 | wxString volume, path, name, ext; | |
330 | ||
331 | // do some consistency checks in debug mode: the name should be really just | |
332 | // the filename and the path should be really just a path | |
333 | #ifdef __WXDEBUG__ | |
334 | wxString pathDummy, nameDummy, extDummy; | |
335 | ||
336 | SplitPath(fullname, &pathDummy, &name, &ext, format); | |
337 | ||
338 | wxASSERT_MSG( pathDummy.empty(), | |
339 | _T("the file name shouldn't contain the path") ); | |
340 | ||
341 | SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format); | |
342 | ||
343 | wxASSERT_MSG( nameDummy.empty() && extDummy.empty(), | |
344 | _T("the path shouldn't contain file name nor extension") ); | |
345 | ||
346 | #else // !__WXDEBUG__ | |
347 | SplitPath(fullname, NULL /* no path */, &name, &ext, format); | |
348 | SplitPath(fullpath, &volume, &path, NULL, NULL, format); | |
349 | #endif // __WXDEBUG__/!__WXDEBUG__ | |
350 | ||
351 | Assign(volume, path, name, ext, format); | |
352 | } | |
353 | ||
354 | void wxFileName::AssignDir(const wxString& dir, wxPathFormat format) | |
355 | { | |
356 | Assign(dir, _T(""), format); | |
357 | } | |
358 | ||
359 | void wxFileName::Clear() | |
360 | { | |
361 | m_dirs.Clear(); | |
362 | ||
363 | m_volume = | |
364 | m_name = | |
365 | m_ext = wxEmptyString; | |
366 | } | |
367 | ||
368 | /* static */ | |
369 | wxFileName wxFileName::FileName(const wxString& file) | |
370 | { | |
371 | return wxFileName(file); | |
372 | } | |
373 | ||
374 | /* static */ | |
375 | wxFileName wxFileName::DirName(const wxString& dir) | |
376 | { | |
377 | wxFileName fn; | |
378 | fn.AssignDir(dir); | |
379 | return fn; | |
380 | } | |
381 | ||
382 | // ---------------------------------------------------------------------------- | |
383 | // existence tests | |
384 | // ---------------------------------------------------------------------------- | |
385 | ||
386 | bool wxFileName::FileExists() | |
387 | { | |
388 | return wxFileName::FileExists( GetFullPath() ); | |
389 | } | |
390 | ||
391 | bool wxFileName::FileExists( const wxString &file ) | |
392 | { | |
393 | return ::wxFileExists( file ); | |
394 | } | |
395 | ||
396 | bool wxFileName::DirExists() | |
397 | { | |
398 | return wxFileName::DirExists( GetFullPath() ); | |
399 | } | |
400 | ||
401 | bool wxFileName::DirExists( const wxString &dir ) | |
402 | { | |
403 | return ::wxDirExists( dir ); | |
404 | } | |
405 | ||
406 | // ---------------------------------------------------------------------------- | |
407 | // CWD and HOME stuff | |
408 | // ---------------------------------------------------------------------------- | |
409 | ||
410 | void wxFileName::AssignCwd(const wxString& volume) | |
411 | { | |
412 | AssignDir(wxFileName::GetCwd(volume)); | |
413 | } | |
414 | ||
415 | /* static */ | |
416 | wxString wxFileName::GetCwd(const wxString& volume) | |
417 | { | |
418 | // if we have the volume, we must get the current directory on this drive | |
419 | // and to do this we have to chdir to this volume - at least under Windows, | |
420 | // I don't know how to get the current drive on another volume elsewhere | |
421 | // (TODO) | |
422 | wxString cwdOld; | |
423 | if ( !volume.empty() ) | |
424 | { | |
425 | cwdOld = wxGetCwd(); | |
426 | SetCwd(volume + GetVolumeSeparator()); | |
427 | } | |
428 | ||
429 | wxString cwd = ::wxGetCwd(); | |
430 | ||
431 | if ( !volume.empty() ) | |
432 | { | |
433 | SetCwd(cwdOld); | |
434 | } | |
435 | ||
436 | return cwd; | |
437 | } | |
438 | ||
439 | bool wxFileName::SetCwd() | |
440 | { | |
441 | return wxFileName::SetCwd( GetFullPath() ); | |
442 | } | |
443 | ||
444 | bool wxFileName::SetCwd( const wxString &cwd ) | |
445 | { | |
446 | return ::wxSetWorkingDirectory( cwd ); | |
447 | } | |
448 | ||
449 | void wxFileName::AssignHomeDir() | |
450 | { | |
451 | AssignDir(wxFileName::GetHomeDir()); | |
452 | } | |
453 | ||
454 | wxString wxFileName::GetHomeDir() | |
455 | { | |
456 | return ::wxGetHomeDir(); | |
457 | } | |
458 | ||
459 | void wxFileName::AssignTempFileName( const wxString& prefix ) | |
460 | { | |
461 | wxString tempname = CreateTempFileName(prefix); | |
462 | if ( tempname.empty() ) | |
463 | { | |
464 | // error, failed to get temp file name | |
465 | Clear(); | |
466 | } | |
467 | else // ok | |
468 | { | |
469 | Assign(tempname); | |
470 | } | |
471 | } | |
472 | ||
473 | /* static */ | |
474 | wxString wxFileName::CreateTempFileName(const wxString& prefix) | |
475 | { | |
476 | wxString path, dir, name; | |
477 | ||
478 | // use the directory specified by the prefix | |
479 | SplitPath(prefix, &dir, &name, NULL /* extension */); | |
480 | ||
481 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
482 | ||
483 | #ifdef __WIN32__ | |
484 | if ( dir.empty() ) | |
485 | { | |
486 | if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) ) | |
487 | { | |
488 | wxLogLastError(_T("GetTempPath")); | |
489 | } | |
490 | ||
491 | if ( dir.empty() ) | |
492 | { | |
493 | // GetTempFileName() fails if we pass it an empty string | |
494 | dir = _T('.'); | |
495 | } | |
496 | } | |
497 | ||
498 | if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) ) | |
499 | { | |
500 | wxLogLastError(_T("GetTempFileName")); | |
501 | ||
502 | path.clear(); | |
503 | } | |
504 | #else // Win16 | |
505 | if ( !::GetTempFileName(NULL, prefix, 0, wxStringBuffer(path, 1025)) ) | |
506 | { | |
507 | path.clear(); | |
508 | } | |
509 | #endif // Win32/16 | |
510 | ||
511 | #elif defined(__WXPM__) | |
512 | // for now just create a file | |
513 | // | |
514 | // future enhancements can be to set some extended attributes for file | |
515 | // systems OS/2 supports that have them (HPFS, FAT32) and security | |
516 | // (HPFS386) | |
517 | static const wxChar *szMktempSuffix = wxT("XXX"); | |
518 | path << dir << _T('/') << name << szMktempSuffix; | |
519 | ||
520 | // Temporarily remove - MN | |
521 | #ifndef __WATCOMC__ | |
522 | ::DosCreateDir(wxStringBuffer(path, MAX_PATH), NULL); | |
523 | #endif | |
524 | ||
525 | #else // !Windows, !OS/2 | |
526 | if ( dir.empty() ) | |
527 | { | |
528 | dir = wxGetenv(_T("TMP")); | |
529 | if ( path.empty() ) | |
530 | { | |
531 | dir = wxGetenv(_T("TEMP")); | |
532 | } | |
533 | ||
534 | if ( dir.empty() ) | |
535 | { | |
536 | // default | |
537 | #ifdef __DOS__ | |
538 | dir = _T("."); | |
539 | #else | |
540 | dir = _T("/tmp"); | |
541 | #endif | |
542 | } | |
543 | } | |
544 | ||
545 | path = dir; | |
546 | ||
547 | if ( !wxEndsWithPathSeparator(dir) && | |
548 | (name.empty() || !wxIsPathSeparator(name[0u])) ) | |
549 | { | |
550 | path += wxFILE_SEP_PATH; | |
551 | } | |
552 | ||
553 | path += name; | |
554 | ||
555 | #if defined(HAVE_MKSTEMP) | |
556 | // scratch space for mkstemp() | |
557 | path += _T("XXXXXX"); | |
558 | ||
559 | // can use the cast here because the length doesn't change and the string | |
560 | // is not shared | |
561 | if ( mkstemp((char *)path.mb_str()) == -1 ) | |
562 | { | |
563 | // this might be not necessary as mkstemp() on most systems should have | |
564 | // already done it but it doesn't hurt neither... | |
565 | path.clear(); | |
566 | } | |
567 | //else: file already created | |
568 | #else // !HAVE_MKSTEMP | |
569 | ||
570 | #ifdef HAVE_MKTEMP | |
571 | // same as above | |
572 | path += _T("XXXXXX"); | |
573 | ||
574 | if ( !mktemp((char *)path.mb_str()) ) | |
575 | { | |
576 | path.clear(); | |
577 | } | |
578 | #else // !HAVE_MKTEMP (includes __DOS__) | |
579 | // generate the unique file name ourselves | |
580 | #ifndef __DOS__ | |
581 | path << (unsigned int)getpid(); | |
582 | #endif | |
583 | ||
584 | wxString pathTry; | |
585 | ||
586 | static const size_t numTries = 1000; | |
587 | for ( size_t n = 0; n < numTries; n++ ) | |
588 | { | |
589 | // 3 hex digits is enough for numTries == 1000 < 4096 | |
590 | pathTry = path + wxString::Format(_T("%.03x"), n); | |
591 | if ( !wxFile::Exists(pathTry) ) | |
592 | { | |
593 | break; | |
594 | } | |
595 | ||
596 | pathTry.clear(); | |
597 | } | |
598 | ||
599 | path = pathTry; | |
600 | #endif // HAVE_MKTEMP/!HAVE_MKTEMP | |
601 | ||
602 | if ( !path.empty() ) | |
603 | { | |
604 | // create the file - of course, there is a race condition here, this is | |
605 | // why we always prefer using mkstemp()... | |
606 | wxFile file; | |
607 | if ( !file.Open(path, wxFile::write_excl, wxS_IRUSR | wxS_IWUSR) ) | |
608 | { | |
609 | // FIXME: If !ok here should we loop and try again with another | |
610 | // file name? That is the standard recourse if open(O_EXCL) | |
611 | // fails, though of course it should be protected against | |
612 | // possible infinite looping too. | |
613 | ||
614 | wxLogError(_("Failed to open temporary file.")); | |
615 | ||
616 | path.clear(); | |
617 | } | |
618 | } | |
619 | #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP | |
620 | ||
621 | #endif // Windows/!Windows | |
622 | ||
623 | if ( path.empty() ) | |
624 | { | |
625 | wxLogSysError(_("Failed to create a temporary file name")); | |
626 | } | |
627 | ||
628 | return path; | |
629 | } | |
630 | ||
631 | // ---------------------------------------------------------------------------- | |
632 | // directory operations | |
633 | // ---------------------------------------------------------------------------- | |
634 | ||
635 | bool wxFileName::Mkdir( int perm, bool full ) | |
636 | { | |
637 | return wxFileName::Mkdir( GetFullPath(), perm, full ); | |
638 | } | |
639 | ||
640 | bool wxFileName::Mkdir( const wxString &dir, int perm, bool full ) | |
641 | { | |
642 | if (full) | |
643 | { | |
644 | wxFileName filename(dir); | |
645 | wxArrayString dirs = filename.GetDirs(); | |
646 | dirs.Add(filename.GetName()); | |
647 | ||
648 | size_t count = dirs.GetCount(); | |
649 | size_t i; | |
650 | wxString currPath; | |
651 | int noErrors = 0; | |
652 | for ( i = 0; i < count; i++ ) | |
653 | { | |
654 | currPath += dirs[i]; | |
655 | ||
656 | if (currPath.Last() == wxT(':')) | |
657 | { | |
658 | // Can't create a root directory so continue to next dir | |
659 | currPath += wxFILE_SEP_PATH; | |
660 | continue; | |
661 | } | |
662 | ||
663 | if (!DirExists(currPath)) | |
664 | if (!wxMkdir(currPath, perm)) | |
665 | noErrors ++; | |
666 | ||
667 | if ( (i < (count-1)) ) | |
668 | currPath += wxFILE_SEP_PATH; | |
669 | } | |
670 | ||
671 | return (noErrors == 0); | |
672 | ||
673 | } | |
674 | else | |
675 | return ::wxMkdir( dir, perm ); | |
676 | } | |
677 | ||
678 | bool wxFileName::Rmdir() | |
679 | { | |
680 | return wxFileName::Rmdir( GetFullPath() ); | |
681 | } | |
682 | ||
683 | bool wxFileName::Rmdir( const wxString &dir ) | |
684 | { | |
685 | return ::wxRmdir( dir ); | |
686 | } | |
687 | ||
688 | // ---------------------------------------------------------------------------- | |
689 | // path normalization | |
690 | // ---------------------------------------------------------------------------- | |
691 | ||
692 | bool wxFileName::Normalize(wxPathNormalize flags, | |
693 | const wxString& cwd, | |
694 | wxPathFormat format) | |
695 | { | |
696 | // the existing path components | |
697 | wxArrayString dirs = GetDirs(); | |
698 | ||
699 | // the path to prepend in front to make the path absolute | |
700 | wxFileName curDir; | |
701 | ||
702 | format = GetFormat(format); | |
703 | ||
704 | // make the path absolute | |
705 | if ( (flags & wxPATH_NORM_ABSOLUTE) && m_relative ) | |
706 | { | |
707 | if ( cwd.empty() ) | |
708 | { | |
709 | curDir.AssignCwd(GetVolume()); | |
710 | } | |
711 | else // cwd provided | |
712 | { | |
713 | curDir.AssignDir(cwd); | |
714 | } | |
715 | ||
716 | #if 0 | |
717 | // the path may be not absolute because it doesn't have the volume name | |
718 | // but in this case we shouldn't modify the directory components of it | |
719 | // but just set the current volume | |
720 | if ( !HasVolume() && curDir.HasVolume() ) | |
721 | { | |
722 | SetVolume(curDir.GetVolume()); | |
723 | ||
724 | if ( IsAbsolute() ) | |
725 | { | |
726 | // yes, it was the case - we don't need curDir then | |
727 | curDir.Clear(); | |
728 | } | |
729 | } | |
730 | #endif | |
731 | } | |
732 | ||
733 | // handle ~ stuff under Unix only | |
734 | if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) ) | |
735 | { | |
736 | if ( !dirs.IsEmpty() ) | |
737 | { | |
738 | wxString dir = dirs[0u]; | |
739 | if ( !dir.empty() && dir[0u] == _T('~') ) | |
740 | { | |
741 | curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); | |
742 | ||
743 | dirs.RemoveAt(0u); | |
744 | } | |
745 | } | |
746 | } | |
747 | ||
748 | // transform relative path into abs one | |
749 | if ( curDir.IsOk() ) | |
750 | { | |
751 | wxArrayString dirsNew = curDir.GetDirs(); | |
752 | size_t count = dirs.GetCount(); | |
753 | for ( size_t n = 0; n < count; n++ ) | |
754 | { | |
755 | dirsNew.Add(dirs[n]); | |
756 | } | |
757 | ||
758 | dirs = dirsNew; | |
759 | } | |
760 | ||
761 | // now deal with ".", ".." and the rest | |
762 | m_dirs.Empty(); | |
763 | size_t count = dirs.GetCount(); | |
764 | for ( size_t n = 0; n < count; n++ ) | |
765 | { | |
766 | wxString dir = dirs[n]; | |
767 | ||
768 | if ( flags & wxPATH_NORM_DOTS ) | |
769 | { | |
770 | if ( dir == wxT(".") ) | |
771 | { | |
772 | // just ignore | |
773 | continue; | |
774 | } | |
775 | ||
776 | if ( dir == wxT("..") ) | |
777 | { | |
778 | if ( m_dirs.IsEmpty() ) | |
779 | { | |
780 | wxLogError(_("The path '%s' contains too many \"..\"!"), | |
781 | GetFullPath().c_str()); | |
782 | return FALSE; | |
783 | } | |
784 | ||
785 | m_dirs.RemoveAt(m_dirs.GetCount() - 1); | |
786 | continue; | |
787 | } | |
788 | } | |
789 | ||
790 | if ( flags & wxPATH_NORM_ENV_VARS ) | |
791 | { | |
792 | dir = wxExpandEnvVars(dir); | |
793 | } | |
794 | ||
795 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
796 | { | |
797 | dir.MakeLower(); | |
798 | } | |
799 | ||
800 | m_dirs.Add(dir); | |
801 | } | |
802 | ||
803 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
804 | { | |
805 | // VZ: expand env vars here too? | |
806 | ||
807 | m_name.MakeLower(); | |
808 | m_ext.MakeLower(); | |
809 | } | |
810 | ||
811 | #if defined(__WIN32__) | |
812 | if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) ) | |
813 | { | |
814 | Assign(GetLongPath()); | |
815 | } | |
816 | #endif // Win32 | |
817 | ||
818 | return TRUE; | |
819 | } | |
820 | ||
821 | bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) | |
822 | { | |
823 | wxFileName fnBase(pathBase, format); | |
824 | ||
825 | // get cwd only once - small time saving | |
826 | wxString cwd = wxGetCwd(); | |
827 | Normalize(wxPATH_NORM_ALL, cwd, format); | |
828 | fnBase.Normalize(wxPATH_NORM_ALL, cwd, format); | |
829 | ||
830 | bool withCase = IsCaseSensitive(format); | |
831 | ||
832 | // we can't do anything if the files live on different volumes | |
833 | if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) ) | |
834 | { | |
835 | // nothing done | |
836 | return FALSE; | |
837 | } | |
838 | ||
839 | // same drive, so we don't need our volume | |
840 | m_volume.clear(); | |
841 | ||
842 | // remove common directories starting at the top | |
843 | while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() && | |
844 | m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) ) | |
845 | { | |
846 | m_dirs.RemoveAt(0); | |
847 | fnBase.m_dirs.RemoveAt(0); | |
848 | } | |
849 | ||
850 | // add as many ".." as needed | |
851 | size_t count = fnBase.m_dirs.GetCount(); | |
852 | for ( size_t i = 0; i < count; i++ ) | |
853 | { | |
854 | m_dirs.Insert(wxT(".."), 0u); | |
855 | } | |
856 | ||
857 | m_relative = TRUE; | |
858 | ||
859 | // we were modified | |
860 | return TRUE; | |
861 | } | |
862 | ||
863 | // ---------------------------------------------------------------------------- | |
864 | // filename kind tests | |
865 | // ---------------------------------------------------------------------------- | |
866 | ||
867 | bool wxFileName::SameAs(const wxFileName &filepath, wxPathFormat format) | |
868 | { | |
869 | wxFileName fn1 = *this, | |
870 | fn2 = filepath; | |
871 | ||
872 | // get cwd only once - small time saving | |
873 | wxString cwd = wxGetCwd(); | |
874 | fn1.Normalize(wxPATH_NORM_ALL, cwd, format); | |
875 | fn2.Normalize(wxPATH_NORM_ALL, cwd, format); | |
876 | ||
877 | if ( fn1.GetFullPath() == fn2.GetFullPath() ) | |
878 | return TRUE; | |
879 | ||
880 | // TODO: compare inodes for Unix, this works even when filenames are | |
881 | // different but files are the same (symlinks) (VZ) | |
882 | ||
883 | return FALSE; | |
884 | } | |
885 | ||
886 | /* static */ | |
887 | bool wxFileName::IsCaseSensitive( wxPathFormat format ) | |
888 | { | |
889 | // only Unix filenames are truely case-sensitive | |
890 | return GetFormat(format) == wxPATH_UNIX; | |
891 | } | |
892 | ||
893 | /* static */ | |
894 | wxString wxFileName::GetVolumeSeparator(wxPathFormat format) | |
895 | { | |
896 | wxString sepVol; | |
897 | ||
898 | if ( (GetFormat(format) == wxPATH_DOS) || | |
899 | (GetFormat(format) == wxPATH_VMS) ) | |
900 | { | |
901 | sepVol = wxFILE_SEP_DSK; | |
902 | } | |
903 | //else: leave empty | |
904 | ||
905 | return sepVol; | |
906 | } | |
907 | ||
908 | /* static */ | |
909 | wxString wxFileName::GetPathSeparators(wxPathFormat format) | |
910 | { | |
911 | wxString seps; | |
912 | switch ( GetFormat(format) ) | |
913 | { | |
914 | case wxPATH_DOS: | |
915 | // accept both as native APIs do but put the native one first as | |
916 | // this is the one we use in GetFullPath() | |
917 | seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX; | |
918 | break; | |
919 | ||
920 | default: | |
921 | wxFAIL_MSG( _T("unknown wxPATH_XXX style") ); | |
922 | // fall through | |
923 | ||
924 | case wxPATH_UNIX: | |
925 | seps = wxFILE_SEP_PATH_UNIX; | |
926 | break; | |
927 | ||
928 | case wxPATH_MAC: | |
929 | seps = wxFILE_SEP_PATH_MAC; | |
930 | break; | |
931 | ||
932 | case wxPATH_VMS: | |
933 | seps = wxFILE_SEP_PATH_VMS; | |
934 | break; | |
935 | } | |
936 | ||
937 | return seps; | |
938 | } | |
939 | ||
940 | /* static */ | |
941 | bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) | |
942 | { | |
943 | // wxString::Find() doesn't work as expected with NUL - it will always find | |
944 | // it, so it is almost surely a bug if this function is called with NUL arg | |
945 | wxASSERT_MSG( ch != _T('\0'), _T("shouldn't be called with NUL") ); | |
946 | ||
947 | return GetPathSeparators(format).Find(ch) != wxNOT_FOUND; | |
948 | } | |
949 | ||
950 | bool wxFileName::IsWild( wxPathFormat format ) | |
951 | { | |
952 | // FIXME: this is probably false for Mac and this is surely wrong for most | |
953 | // of Unix shells (think about "[...]") | |
954 | (void)format; | |
955 | return m_name.find_first_of(_T("*?")) != wxString::npos; | |
956 | } | |
957 | ||
958 | // ---------------------------------------------------------------------------- | |
959 | // path components manipulation | |
960 | // ---------------------------------------------------------------------------- | |
961 | ||
962 | void wxFileName::AppendDir( const wxString &dir ) | |
963 | { | |
964 | m_dirs.Add( dir ); | |
965 | } | |
966 | ||
967 | void wxFileName::PrependDir( const wxString &dir ) | |
968 | { | |
969 | m_dirs.Insert( dir, 0 ); | |
970 | } | |
971 | ||
972 | void wxFileName::InsertDir( int before, const wxString &dir ) | |
973 | { | |
974 | m_dirs.Insert( dir, before ); | |
975 | } | |
976 | ||
977 | void wxFileName::RemoveDir( int pos ) | |
978 | { | |
979 | m_dirs.Remove( (size_t)pos ); | |
980 | } | |
981 | ||
982 | // ---------------------------------------------------------------------------- | |
983 | // accessors | |
984 | // ---------------------------------------------------------------------------- | |
985 | ||
986 | void wxFileName::SetFullName(const wxString& fullname) | |
987 | { | |
988 | SplitPath(fullname, NULL /* no path */, &m_name, &m_ext); | |
989 | } | |
990 | ||
991 | wxString wxFileName::GetFullName() const | |
992 | { | |
993 | wxString fullname = m_name; | |
994 | if ( !m_ext.empty() ) | |
995 | { | |
996 | fullname << wxFILE_SEP_EXT << m_ext; | |
997 | } | |
998 | ||
999 | return fullname; | |
1000 | } | |
1001 | ||
1002 | wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const | |
1003 | { | |
1004 | format = GetFormat( format ); | |
1005 | ||
1006 | wxString fullpath; | |
1007 | ||
1008 | // the leading character | |
1009 | if ( format == wxPATH_MAC && m_relative ) | |
1010 | { | |
1011 | fullpath += wxFILE_SEP_PATH_MAC; | |
1012 | } | |
1013 | else if ( format == wxPATH_DOS ) | |
1014 | { | |
1015 | if (!m_relative) | |
1016 | fullpath += wxFILE_SEP_PATH_DOS; | |
1017 | } | |
1018 | else if ( format == wxPATH_UNIX ) | |
1019 | { | |
1020 | if (!m_relative) | |
1021 | fullpath += wxFILE_SEP_PATH_UNIX; | |
1022 | } | |
1023 | ||
1024 | // then concatenate all the path components using the path separator | |
1025 | size_t dirCount = m_dirs.GetCount(); | |
1026 | if ( dirCount ) | |
1027 | { | |
1028 | if ( format == wxPATH_VMS ) | |
1029 | { | |
1030 | fullpath += wxT('['); | |
1031 | } | |
1032 | ||
1033 | ||
1034 | for ( size_t i = 0; i < dirCount; i++ ) | |
1035 | { | |
1036 | // TODO: What to do with ".." under VMS | |
1037 | ||
1038 | switch (format) | |
1039 | { | |
1040 | case wxPATH_MAC: | |
1041 | { | |
1042 | if (m_dirs[i] == wxT(".")) | |
1043 | break; | |
1044 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1045 | fullpath += m_dirs[i]; | |
1046 | fullpath += wxT(':'); | |
1047 | break; | |
1048 | } | |
1049 | case wxPATH_DOS: | |
1050 | { | |
1051 | fullpath += m_dirs[i]; | |
1052 | fullpath += wxT('\\'); | |
1053 | break; | |
1054 | } | |
1055 | case wxPATH_UNIX: | |
1056 | { | |
1057 | fullpath += m_dirs[i]; | |
1058 | fullpath += wxT('/'); | |
1059 | break; | |
1060 | } | |
1061 | case wxPATH_VMS: | |
1062 | { | |
1063 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1064 | fullpath += m_dirs[i]; | |
1065 | if (i == dirCount-1) | |
1066 | fullpath += wxT(']'); | |
1067 | else | |
1068 | fullpath += wxT('.'); | |
1069 | break; | |
1070 | } | |
1071 | default: | |
1072 | { | |
1073 | wxFAIL_MSG( "error" ); | |
1074 | } | |
1075 | } | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | ||
1080 | ||
1081 | return fullpath; | |
1082 | } | |
1083 | ||
1084 | wxString wxFileName::GetFullPath( wxPathFormat format ) const | |
1085 | { | |
1086 | format = GetFormat(format); | |
1087 | ||
1088 | wxString fullpath; | |
1089 | ||
1090 | // first put the volume | |
1091 | if ( !m_volume.empty() ) | |
1092 | { | |
1093 | { | |
1094 | // Special Windows UNC paths hack, part 2: undo what we did in | |
1095 | // SplitPath() and make an UNC path if we have a drive which is not a | |
1096 | // single letter (hopefully the network shares can't be one letter only | |
1097 | // although I didn't find any authoritative docs on this) | |
1098 | if ( format == wxPATH_DOS && m_volume.length() > 1 ) | |
1099 | { | |
1100 | fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume; | |
1101 | } | |
1102 | else if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
1103 | { | |
1104 | fullpath << m_volume << GetVolumeSeparator(format); | |
1105 | } | |
1106 | // else ignore | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | // the leading character | |
1111 | if ( format == wxPATH_MAC && m_relative ) | |
1112 | { | |
1113 | fullpath += wxFILE_SEP_PATH_MAC; | |
1114 | } | |
1115 | else if ( format == wxPATH_DOS ) | |
1116 | { | |
1117 | if (!m_relative) | |
1118 | fullpath += wxFILE_SEP_PATH_DOS; | |
1119 | } | |
1120 | else if ( format == wxPATH_UNIX ) | |
1121 | { | |
1122 | if (!m_relative) | |
1123 | fullpath += wxFILE_SEP_PATH_UNIX; | |
1124 | } | |
1125 | ||
1126 | // then concatenate all the path components using the path separator | |
1127 | size_t dirCount = m_dirs.GetCount(); | |
1128 | if ( dirCount ) | |
1129 | { | |
1130 | if ( format == wxPATH_VMS ) | |
1131 | { | |
1132 | fullpath += wxT('['); | |
1133 | } | |
1134 | ||
1135 | ||
1136 | for ( size_t i = 0; i < dirCount; i++ ) | |
1137 | { | |
1138 | // TODO: What to do with ".." under VMS | |
1139 | ||
1140 | switch (format) | |
1141 | { | |
1142 | case wxPATH_MAC: | |
1143 | { | |
1144 | if (m_dirs[i] == wxT(".")) | |
1145 | break; | |
1146 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1147 | fullpath += m_dirs[i]; | |
1148 | fullpath += wxT(':'); | |
1149 | break; | |
1150 | } | |
1151 | case wxPATH_DOS: | |
1152 | { | |
1153 | fullpath += m_dirs[i]; | |
1154 | fullpath += wxT('\\'); | |
1155 | break; | |
1156 | } | |
1157 | case wxPATH_UNIX: | |
1158 | { | |
1159 | fullpath += m_dirs[i]; | |
1160 | fullpath += wxT('/'); | |
1161 | break; | |
1162 | } | |
1163 | case wxPATH_VMS: | |
1164 | { | |
1165 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1166 | fullpath += m_dirs[i]; | |
1167 | if (i == dirCount-1) | |
1168 | fullpath += wxT(']'); | |
1169 | else | |
1170 | fullpath += wxT('.'); | |
1171 | break; | |
1172 | } | |
1173 | default: | |
1174 | { | |
1175 | wxFAIL_MSG( "error" ); | |
1176 | } | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | // finally add the file name and extension | |
1182 | fullpath += GetFullName(); | |
1183 | ||
1184 | return fullpath; | |
1185 | } | |
1186 | ||
1187 | // Return the short form of the path (returns identity on non-Windows platforms) | |
1188 | wxString wxFileName::GetShortPath() const | |
1189 | { | |
1190 | #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) | |
1191 | wxString path(GetFullPath()); | |
1192 | wxString pathOut; | |
1193 | DWORD sz = ::GetShortPathName(path, NULL, 0); | |
1194 | bool ok = sz != 0; | |
1195 | if ( ok ) | |
1196 | { | |
1197 | ok = ::GetShortPathName | |
1198 | ( | |
1199 | path, | |
1200 | pathOut.GetWriteBuf(sz), | |
1201 | sz | |
1202 | ) != 0; | |
1203 | pathOut.UngetWriteBuf(); | |
1204 | } | |
1205 | if (ok) | |
1206 | return pathOut; | |
1207 | ||
1208 | return path; | |
1209 | #else | |
1210 | return GetFullPath(); | |
1211 | #endif | |
1212 | } | |
1213 | ||
1214 | // Return the long form of the path (returns identity on non-Windows platforms) | |
1215 | wxString wxFileName::GetLongPath() const | |
1216 | { | |
1217 | wxString pathOut, | |
1218 | path = GetFullPath(); | |
1219 | ||
1220 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
1221 | bool success = FALSE; | |
1222 | ||
1223 | // VZ: this code was disabled, why? | |
1224 | #if 0 // wxUSE_DYNLIB_CLASS | |
1225 | typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); | |
1226 | ||
1227 | static bool s_triedToLoad = FALSE; | |
1228 | ||
1229 | if ( !s_triedToLoad ) | |
1230 | { | |
1231 | s_triedToLoad = TRUE; | |
1232 | wxDllType dllKernel = wxDllLoader::LoadLibrary(_T("kernel32")); | |
1233 | if ( dllKernel ) | |
1234 | { | |
1235 | // may succeed or fail depending on the Windows version | |
1236 | static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; | |
1237 | #ifdef _UNICODE | |
1238 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameW")); | |
1239 | #else | |
1240 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameA")); | |
1241 | #endif | |
1242 | ||
1243 | wxDllLoader::UnloadLibrary(dllKernel); | |
1244 | ||
1245 | if ( s_pfnGetLongPathName ) | |
1246 | { | |
1247 | DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0); | |
1248 | bool ok = dwSize > 0; | |
1249 | ||
1250 | if ( ok ) | |
1251 | { | |
1252 | DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0); | |
1253 | ok = sz != 0; | |
1254 | if ( ok ) | |
1255 | { | |
1256 | ok = (*s_pfnGetLongPathName) | |
1257 | ( | |
1258 | path, | |
1259 | pathOut.GetWriteBuf(sz), | |
1260 | sz | |
1261 | ) != 0; | |
1262 | pathOut.UngetWriteBuf(); | |
1263 | ||
1264 | success = TRUE; | |
1265 | } | |
1266 | } | |
1267 | } | |
1268 | } | |
1269 | } | |
1270 | if (success) | |
1271 | return pathOut; | |
1272 | #endif // wxUSE_DYNLIB_CLASS | |
1273 | ||
1274 | if (!success) | |
1275 | { | |
1276 | // The OS didn't support GetLongPathName, or some other error. | |
1277 | // We need to call FindFirstFile on each component in turn. | |
1278 | ||
1279 | WIN32_FIND_DATA findFileData; | |
1280 | HANDLE hFind; | |
1281 | pathOut = wxEmptyString; | |
1282 | ||
1283 | wxArrayString dirs = GetDirs(); | |
1284 | dirs.Add(GetFullName()); | |
1285 | ||
1286 | wxString tmpPath; | |
1287 | ||
1288 | size_t count = dirs.GetCount(); | |
1289 | for ( size_t i = 0; i < count; i++ ) | |
1290 | { | |
1291 | // We're using pathOut to collect the long-name path, but using a | |
1292 | // temporary for appending the last path component which may be | |
1293 | // short-name | |
1294 | tmpPath = pathOut + dirs[i]; | |
1295 | ||
1296 | if ( tmpPath.empty() ) | |
1297 | continue; | |
1298 | ||
1299 | if ( tmpPath.Last() == wxT(':') ) | |
1300 | { | |
1301 | // Can't pass a drive and root dir to FindFirstFile, | |
1302 | // so continue to next dir | |
1303 | tmpPath += wxFILE_SEP_PATH; | |
1304 | pathOut = tmpPath; | |
1305 | continue; | |
1306 | } | |
1307 | ||
1308 | hFind = ::FindFirstFile(tmpPath, &findFileData); | |
1309 | if (hFind == INVALID_HANDLE_VALUE) | |
1310 | { | |
1311 | // Error: return immediately with the original path | |
1312 | return path; | |
1313 | } | |
1314 | ||
1315 | pathOut += findFileData.cFileName; | |
1316 | if ( (i < (count-1)) ) | |
1317 | pathOut += wxFILE_SEP_PATH; | |
1318 | ||
1319 | ::FindClose(hFind); | |
1320 | } | |
1321 | } | |
1322 | #else // !Win32 | |
1323 | pathOut = path; | |
1324 | #endif // Win32/!Win32 | |
1325 | ||
1326 | return pathOut; | |
1327 | } | |
1328 | ||
1329 | wxPathFormat wxFileName::GetFormat( wxPathFormat format ) | |
1330 | { | |
1331 | if (format == wxPATH_NATIVE) | |
1332 | { | |
1333 | #if defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__) | |
1334 | format = wxPATH_DOS; | |
1335 | #elif defined(__WXMAC__) && !defined(__DARWIN__) | |
1336 | format = wxPATH_MAC; | |
1337 | #elif defined(__VMS) | |
1338 | format = wxPATH_VMS; | |
1339 | #else | |
1340 | format = wxPATH_UNIX; | |
1341 | #endif | |
1342 | } | |
1343 | return format; | |
1344 | } | |
1345 | ||
1346 | // ---------------------------------------------------------------------------- | |
1347 | // path splitting function | |
1348 | // ---------------------------------------------------------------------------- | |
1349 | ||
1350 | /* static */ | |
1351 | void wxFileName::SplitPath(const wxString& fullpathWithVolume, | |
1352 | wxString *pstrVolume, | |
1353 | wxString *pstrPath, | |
1354 | wxString *pstrName, | |
1355 | wxString *pstrExt, | |
1356 | wxPathFormat format) | |
1357 | { | |
1358 | format = GetFormat(format); | |
1359 | ||
1360 | wxString fullpath = fullpathWithVolume; | |
1361 | ||
1362 | // under VMS the end of the path is ']', not the path separator used to | |
1363 | // separate the components | |
1364 | wxString sepPath = format == wxPATH_VMS ? wxString(_T(']')) | |
1365 | : GetPathSeparators(format); | |
1366 | ||
1367 | // special Windows UNC paths hack: transform \\share\path into share:path | |
1368 | if ( format == wxPATH_DOS ) | |
1369 | { | |
1370 | if ( fullpath.length() >= 4 && | |
1371 | fullpath[0u] == wxFILE_SEP_PATH_DOS && | |
1372 | fullpath[1u] == wxFILE_SEP_PATH_DOS ) | |
1373 | { | |
1374 | fullpath.erase(0, 2); | |
1375 | ||
1376 | size_t posFirstSlash = fullpath.find_first_of(sepPath); | |
1377 | if ( posFirstSlash != wxString::npos ) | |
1378 | { | |
1379 | fullpath[posFirstSlash] = wxFILE_SEP_DSK; | |
1380 | ||
1381 | // UNC paths are always absolute, right? (FIXME) | |
1382 | fullpath.insert(posFirstSlash + 1, wxFILE_SEP_PATH_DOS); | |
1383 | } | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | // We separate the volume here | |
1388 | if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
1389 | { | |
1390 | wxString sepVol = GetVolumeSeparator(format); | |
1391 | ||
1392 | size_t posFirstColon = fullpath.find_first_of(sepVol); | |
1393 | if ( posFirstColon != wxString::npos ) | |
1394 | { | |
1395 | if ( pstrVolume ) | |
1396 | { | |
1397 | *pstrVolume = fullpath.Left(posFirstColon); | |
1398 | } | |
1399 | ||
1400 | // remove the volume name and the separator from the full path | |
1401 | fullpath.erase(0, posFirstColon + sepVol.length()); | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | // find the positions of the last dot and last path separator in the path | |
1406 | size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); | |
1407 | size_t posLastSlash = fullpath.find_last_of(sepPath); | |
1408 | ||
1409 | if ( (posLastDot != wxString::npos) && | |
1410 | ((format == wxPATH_UNIX) || (format == wxPATH_VMS)) ) | |
1411 | { | |
1412 | if ( (posLastDot == 0) || | |
1413 | (fullpath[posLastDot - 1] == sepPath[0u] ) ) | |
1414 | { | |
1415 | // under Unix and VMS, dot may be (and commonly is) the first | |
1416 | // character of the filename, don't treat the entire filename as | |
1417 | // extension in this case | |
1418 | posLastDot = wxString::npos; | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | // if we do have a dot and a slash, check that the dot is in the name part | |
1423 | if ( (posLastDot != wxString::npos) && | |
1424 | (posLastSlash != wxString::npos) && | |
1425 | (posLastDot < posLastSlash) ) | |
1426 | { | |
1427 | // the dot is part of the path, not the start of the extension | |
1428 | posLastDot = wxString::npos; | |
1429 | } | |
1430 | ||
1431 | // now fill in the variables provided by user | |
1432 | if ( pstrPath ) | |
1433 | { | |
1434 | if ( posLastSlash == wxString::npos ) | |
1435 | { | |
1436 | // no path at all | |
1437 | pstrPath->Empty(); | |
1438 | } | |
1439 | else | |
1440 | { | |
1441 | // take everything up to the path separator but take care to make | |
1442 | // the path equal to something like '/', not empty, for the files | |
1443 | // immediately under root directory | |
1444 | size_t len = posLastSlash; | |
1445 | if ( !len ) | |
1446 | len++; | |
1447 | ||
1448 | *pstrPath = fullpath.Left(len); | |
1449 | ||
1450 | // special VMS hack: remove the initial bracket | |
1451 | if ( format == wxPATH_VMS ) | |
1452 | { | |
1453 | if ( (*pstrPath)[0u] == _T('[') ) | |
1454 | pstrPath->erase(0, 1); | |
1455 | } | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | if ( pstrName ) | |
1460 | { | |
1461 | // take all characters starting from the one after the last slash and | |
1462 | // up to, but excluding, the last dot | |
1463 | size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; | |
1464 | size_t count; | |
1465 | if ( posLastDot == wxString::npos ) | |
1466 | { | |
1467 | // take all until the end | |
1468 | count = wxString::npos; | |
1469 | } | |
1470 | else if ( posLastSlash == wxString::npos ) | |
1471 | { | |
1472 | count = posLastDot; | |
1473 | } | |
1474 | else // have both dot and slash | |
1475 | { | |
1476 | count = posLastDot - posLastSlash - 1; | |
1477 | } | |
1478 | ||
1479 | *pstrName = fullpath.Mid(nStart, count); | |
1480 | } | |
1481 | ||
1482 | if ( pstrExt ) | |
1483 | { | |
1484 | if ( posLastDot == wxString::npos ) | |
1485 | { | |
1486 | // no extension | |
1487 | pstrExt->Empty(); | |
1488 | } | |
1489 | else | |
1490 | { | |
1491 | // take everything after the dot | |
1492 | *pstrExt = fullpath.Mid(posLastDot + 1); | |
1493 | } | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | /* static */ | |
1498 | void wxFileName::SplitPath(const wxString& fullpath, | |
1499 | wxString *path, | |
1500 | wxString *name, | |
1501 | wxString *ext, | |
1502 | wxPathFormat format) | |
1503 | { | |
1504 | wxString volume; | |
1505 | SplitPath(fullpath, &volume, path, name, ext, format); | |
1506 | ||
1507 | if ( path && !volume.empty() ) | |
1508 | { | |
1509 | path->Prepend(volume + GetVolumeSeparator(format)); | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | // ---------------------------------------------------------------------------- | |
1514 | // time functions | |
1515 | // ---------------------------------------------------------------------------- | |
1516 | ||
1517 | bool wxFileName::SetTimes(const wxDateTime *dtCreate, | |
1518 | const wxDateTime *dtAccess, | |
1519 | const wxDateTime *dtMod) | |
1520 | { | |
1521 | #if defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) | |
1522 | if ( !dtAccess && !dtMod ) | |
1523 | { | |
1524 | // can't modify the creation time anyhow, don't try | |
1525 | return TRUE; | |
1526 | } | |
1527 | ||
1528 | // if dtAccess or dtMod is not specified, use the other one (which must be | |
1529 | // non NULL because of the test above) for both times | |
1530 | utimbuf utm; | |
1531 | utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks(); | |
1532 | utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); | |
1533 | if ( utime(GetFullPath(), &utm) == 0 ) | |
1534 | { | |
1535 | return TRUE; | |
1536 | } | |
1537 | #elif defined(__WIN32__) | |
1538 | wxFileHandle fh(GetFullPath()); | |
1539 | if ( fh.IsOk() ) | |
1540 | { | |
1541 | FILETIME ftAccess, ftCreate, ftWrite; | |
1542 | ||
1543 | if ( dtCreate ) | |
1544 | ConvertWxToFileTime(&ftCreate, *dtCreate); | |
1545 | if ( dtAccess ) | |
1546 | ConvertWxToFileTime(&ftAccess, *dtAccess); | |
1547 | if ( dtMod ) | |
1548 | ConvertWxToFileTime(&ftWrite, *dtMod); | |
1549 | ||
1550 | if ( ::SetFileTime(fh, | |
1551 | dtCreate ? &ftCreate : NULL, | |
1552 | dtAccess ? &ftAccess : NULL, | |
1553 | dtMod ? &ftWrite : NULL) ) | |
1554 | { | |
1555 | return TRUE; | |
1556 | } | |
1557 | } | |
1558 | #else // other platform | |
1559 | #endif // platforms | |
1560 | ||
1561 | wxLogSysError(_("Failed to modify file times for '%s'"), | |
1562 | GetFullPath().c_str()); | |
1563 | ||
1564 | return FALSE; | |
1565 | } | |
1566 | ||
1567 | bool wxFileName::Touch() | |
1568 | { | |
1569 | #if defined(__UNIX_LIKE__) | |
1570 | // under Unix touching file is simple: just pass NULL to utime() | |
1571 | if ( utime(GetFullPath(), NULL) == 0 ) | |
1572 | { | |
1573 | return TRUE; | |
1574 | } | |
1575 | ||
1576 | wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str()); | |
1577 | ||
1578 | return FALSE; | |
1579 | #else // other platform | |
1580 | wxDateTime dtNow = wxDateTime::Now(); | |
1581 | ||
1582 | return SetTimes(NULL /* don't change create time */, &dtNow, &dtNow); | |
1583 | #endif // platforms | |
1584 | } | |
1585 | ||
1586 | bool wxFileName::GetTimes(wxDateTime *dtAccess, | |
1587 | wxDateTime *dtMod, | |
1588 | wxDateTime *dtChange) const | |
1589 | { | |
1590 | #if defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__)) | |
1591 | wxStructStat stBuf; | |
1592 | if ( wxStat(GetFullPath(), &stBuf) == 0 ) | |
1593 | { | |
1594 | if ( dtAccess ) | |
1595 | dtAccess->Set(stBuf.st_atime); | |
1596 | if ( dtMod ) | |
1597 | dtMod->Set(stBuf.st_mtime); | |
1598 | if ( dtChange ) | |
1599 | dtChange->Set(stBuf.st_ctime); | |
1600 | ||
1601 | return TRUE; | |
1602 | } | |
1603 | #elif defined(__WIN32__) | |
1604 | wxFileHandle fh(GetFullPath()); | |
1605 | if ( fh.IsOk() ) | |
1606 | { | |
1607 | FILETIME ftAccess, ftCreate, ftWrite; | |
1608 | ||
1609 | if ( ::GetFileTime(fh, | |
1610 | dtMod ? &ftCreate : NULL, | |
1611 | dtAccess ? &ftAccess : NULL, | |
1612 | dtChange ? &ftWrite : NULL) ) | |
1613 | { | |
1614 | if ( dtMod ) | |
1615 | ConvertFileTimeToWx(dtMod, ftCreate); | |
1616 | if ( dtAccess ) | |
1617 | ConvertFileTimeToWx(dtAccess, ftAccess); | |
1618 | if ( dtChange ) | |
1619 | ConvertFileTimeToWx(dtChange, ftWrite); | |
1620 | ||
1621 | return TRUE; | |
1622 | } | |
1623 | } | |
1624 | #else // other platform | |
1625 | #endif // platforms | |
1626 | ||
1627 | wxLogSysError(_("Failed to retrieve file times for '%s'"), | |
1628 | GetFullPath().c_str()); | |
1629 | ||
1630 | return FALSE; | |
1631 | } | |
1632 |