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