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