]>
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 | #if defined(__WXMAC__) && !defined(__DARWIN__) | |
541 | dir = wxMacFindFolder( (short) kOnSystemDisk, pTemporaryFolder, kCreateFolder ) ; | |
542 | #else // !Mac | |
543 | dir = wxGetenv(_T("TMP")); | |
544 | if ( dir.empty() ) | |
545 | { | |
546 | dir = wxGetenv(_T("TEMP")); | |
547 | } | |
548 | ||
549 | if ( dir.empty() ) | |
550 | { | |
551 | // default | |
552 | #ifdef __DOS__ | |
553 | dir = _T("."); | |
554 | #else | |
555 | dir = _T("/tmp"); | |
556 | #endif | |
557 | } | |
558 | #endif // Mac/!Mac | |
559 | } | |
560 | ||
561 | path = dir; | |
562 | ||
563 | if ( !wxEndsWithPathSeparator(dir) && | |
564 | (name.empty() || !wxIsPathSeparator(name[0u])) ) | |
565 | { | |
566 | path += wxFILE_SEP_PATH; | |
567 | } | |
568 | ||
569 | path += name; | |
570 | ||
571 | #if defined(HAVE_MKSTEMP) | |
572 | // scratch space for mkstemp() | |
573 | path += _T("XXXXXX"); | |
574 | ||
575 | // can use the cast here because the length doesn't change and the string | |
576 | // is not shared | |
577 | int fdTemp = mkstemp((char *)path.mb_str()); | |
578 | if ( fdTemp == -1 ) | |
579 | { | |
580 | // this might be not necessary as mkstemp() on most systems should have | |
581 | // already done it but it doesn't hurt neither... | |
582 | path.clear(); | |
583 | } | |
584 | else // mkstemp() succeeded | |
585 | { | |
586 | // avoid leaking the fd | |
587 | if ( fileTemp ) | |
588 | { | |
589 | fileTemp->Attach(fdTemp); | |
590 | } | |
591 | else | |
592 | { | |
593 | close(fdTemp); | |
594 | } | |
595 | } | |
596 | #else // !HAVE_MKSTEMP | |
597 | ||
598 | #ifdef HAVE_MKTEMP | |
599 | // same as above | |
600 | path += _T("XXXXXX"); | |
601 | ||
602 | if ( !mktemp((char *)path.mb_str()) ) | |
603 | { | |
604 | path.clear(); | |
605 | } | |
606 | #else // !HAVE_MKTEMP (includes __DOS__) | |
607 | // generate the unique file name ourselves | |
608 | #ifndef __DOS__ | |
609 | path << (unsigned int)getpid(); | |
610 | #endif | |
611 | ||
612 | wxString pathTry; | |
613 | ||
614 | static const size_t numTries = 1000; | |
615 | for ( size_t n = 0; n < numTries; n++ ) | |
616 | { | |
617 | // 3 hex digits is enough for numTries == 1000 < 4096 | |
618 | pathTry = path + wxString::Format(_T("%.03x"), n); | |
619 | if ( !wxFile::Exists(pathTry) ) | |
620 | { | |
621 | break; | |
622 | } | |
623 | ||
624 | pathTry.clear(); | |
625 | } | |
626 | ||
627 | path = pathTry; | |
628 | #endif // HAVE_MKTEMP/!HAVE_MKTEMP | |
629 | ||
630 | if ( !path.empty() ) | |
631 | { | |
632 | } | |
633 | #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP | |
634 | ||
635 | #endif // Windows/!Windows | |
636 | ||
637 | if ( path.empty() ) | |
638 | { | |
639 | wxLogSysError(_("Failed to create a temporary file name")); | |
640 | } | |
641 | else if ( fileTemp && !fileTemp->IsOpened() ) | |
642 | { | |
643 | // open the file - of course, there is a race condition here, this is | |
644 | // why we always prefer using mkstemp()... | |
645 | // | |
646 | // NB: GetTempFileName() under Windows creates the file, so using | |
647 | // write_excl there would fail | |
648 | if ( !fileTemp->Open(path, | |
649 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
650 | wxFile::write, | |
651 | #else | |
652 | wxFile::write_excl, | |
653 | #endif | |
654 | wxS_IRUSR | wxS_IWUSR) ) | |
655 | { | |
656 | // FIXME: If !ok here should we loop and try again with another | |
657 | // file name? That is the standard recourse if open(O_EXCL) | |
658 | // fails, though of course it should be protected against | |
659 | // possible infinite looping too. | |
660 | ||
661 | wxLogError(_("Failed to open temporary file.")); | |
662 | ||
663 | path.clear(); | |
664 | } | |
665 | } | |
666 | ||
667 | return path; | |
668 | } | |
669 | ||
670 | // ---------------------------------------------------------------------------- | |
671 | // directory operations | |
672 | // ---------------------------------------------------------------------------- | |
673 | ||
674 | bool wxFileName::Mkdir( int perm, bool full ) | |
675 | { | |
676 | return wxFileName::Mkdir( GetFullPath(), perm, full ); | |
677 | } | |
678 | ||
679 | bool wxFileName::Mkdir( const wxString &dir, int perm, bool full ) | |
680 | { | |
681 | if (full) | |
682 | { | |
683 | wxFileName filename(dir); | |
684 | wxArrayString dirs = filename.GetDirs(); | |
685 | dirs.Add(filename.GetName()); | |
686 | ||
687 | size_t count = dirs.GetCount(); | |
688 | size_t i; | |
689 | wxString currPath; | |
690 | int noErrors = 0; | |
691 | for ( i = 0; i < count; i++ ) | |
692 | { | |
693 | currPath += dirs[i]; | |
694 | ||
695 | if (currPath.Last() == wxT(':')) | |
696 | { | |
697 | // Can't create a root directory so continue to next dir | |
698 | currPath += wxFILE_SEP_PATH; | |
699 | continue; | |
700 | } | |
701 | ||
702 | if (!DirExists(currPath)) | |
703 | if (!wxMkdir(currPath, perm)) | |
704 | noErrors ++; | |
705 | ||
706 | if ( (i < (count-1)) ) | |
707 | currPath += wxFILE_SEP_PATH; | |
708 | } | |
709 | ||
710 | return (noErrors == 0); | |
711 | ||
712 | } | |
713 | else | |
714 | return ::wxMkdir( dir, perm ); | |
715 | } | |
716 | ||
717 | bool wxFileName::Rmdir() | |
718 | { | |
719 | return wxFileName::Rmdir( GetFullPath() ); | |
720 | } | |
721 | ||
722 | bool wxFileName::Rmdir( const wxString &dir ) | |
723 | { | |
724 | return ::wxRmdir( dir ); | |
725 | } | |
726 | ||
727 | // ---------------------------------------------------------------------------- | |
728 | // path normalization | |
729 | // ---------------------------------------------------------------------------- | |
730 | ||
731 | bool wxFileName::Normalize(wxPathNormalize flags, | |
732 | const wxString& cwd, | |
733 | wxPathFormat format) | |
734 | { | |
735 | // the existing path components | |
736 | wxArrayString dirs = GetDirs(); | |
737 | ||
738 | // the path to prepend in front to make the path absolute | |
739 | wxFileName curDir; | |
740 | ||
741 | format = GetFormat(format); | |
742 | ||
743 | // make the path absolute | |
744 | if ( (flags & wxPATH_NORM_ABSOLUTE) && m_relative ) | |
745 | { | |
746 | if ( cwd.empty() ) | |
747 | { | |
748 | curDir.AssignCwd(GetVolume()); | |
749 | } | |
750 | else // cwd provided | |
751 | { | |
752 | curDir.AssignDir(cwd); | |
753 | } | |
754 | ||
755 | #if 0 | |
756 | // the path may be not absolute because it doesn't have the volume name | |
757 | // but in this case we shouldn't modify the directory components of it | |
758 | // but just set the current volume | |
759 | if ( !HasVolume() && curDir.HasVolume() ) | |
760 | { | |
761 | SetVolume(curDir.GetVolume()); | |
762 | ||
763 | if ( IsAbsolute() ) | |
764 | { | |
765 | // yes, it was the case - we don't need curDir then | |
766 | curDir.Clear(); | |
767 | } | |
768 | } | |
769 | #endif | |
770 | m_relative = FALSE; | |
771 | } | |
772 | ||
773 | // handle ~ stuff under Unix only | |
774 | if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) ) | |
775 | { | |
776 | if ( !dirs.IsEmpty() ) | |
777 | { | |
778 | wxString dir = dirs[0u]; | |
779 | if ( !dir.empty() && dir[0u] == _T('~') ) | |
780 | { | |
781 | curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); | |
782 | ||
783 | dirs.RemoveAt(0u); | |
784 | } | |
785 | } | |
786 | } | |
787 | ||
788 | // transform relative path into abs one | |
789 | if ( curDir.IsOk() ) | |
790 | { | |
791 | wxArrayString dirsNew = curDir.GetDirs(); | |
792 | size_t count = dirs.GetCount(); | |
793 | for ( size_t n = 0; n < count; n++ ) | |
794 | { | |
795 | dirsNew.Add(dirs[n]); | |
796 | } | |
797 | ||
798 | dirs = dirsNew; | |
799 | } | |
800 | ||
801 | // now deal with ".", ".." and the rest | |
802 | m_dirs.Empty(); | |
803 | size_t count = dirs.GetCount(); | |
804 | for ( size_t n = 0; n < count; n++ ) | |
805 | { | |
806 | wxString dir = dirs[n]; | |
807 | ||
808 | if ( flags & wxPATH_NORM_DOTS ) | |
809 | { | |
810 | if ( dir == wxT(".") ) | |
811 | { | |
812 | // just ignore | |
813 | continue; | |
814 | } | |
815 | ||
816 | if ( dir == wxT("..") ) | |
817 | { | |
818 | if ( m_dirs.IsEmpty() ) | |
819 | { | |
820 | wxLogError(_("The path '%s' contains too many \"..\"!"), | |
821 | GetFullPath().c_str()); | |
822 | return FALSE; | |
823 | } | |
824 | ||
825 | m_dirs.RemoveAt(m_dirs.GetCount() - 1); | |
826 | continue; | |
827 | } | |
828 | } | |
829 | ||
830 | if ( flags & wxPATH_NORM_ENV_VARS ) | |
831 | { | |
832 | dir = wxExpandEnvVars(dir); | |
833 | } | |
834 | ||
835 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
836 | { | |
837 | dir.MakeLower(); | |
838 | } | |
839 | ||
840 | m_dirs.Add(dir); | |
841 | } | |
842 | ||
843 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
844 | { | |
845 | // VZ: expand env vars here too? | |
846 | ||
847 | m_name.MakeLower(); | |
848 | m_ext.MakeLower(); | |
849 | } | |
850 | ||
851 | #if defined(__WIN32__) | |
852 | if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) ) | |
853 | { | |
854 | Assign(GetLongPath()); | |
855 | } | |
856 | #endif // Win32 | |
857 | ||
858 | return TRUE; | |
859 | } | |
860 | ||
861 | bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) | |
862 | { | |
863 | wxFileName fnBase(pathBase, format); | |
864 | ||
865 | // get cwd only once - small time saving | |
866 | wxString cwd = wxGetCwd(); | |
867 | Normalize(wxPATH_NORM_ALL, cwd, format); | |
868 | fnBase.Normalize(wxPATH_NORM_ALL, cwd, format); | |
869 | ||
870 | bool withCase = IsCaseSensitive(format); | |
871 | ||
872 | // we can't do anything if the files live on different volumes | |
873 | if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) ) | |
874 | { | |
875 | // nothing done | |
876 | return FALSE; | |
877 | } | |
878 | ||
879 | // same drive, so we don't need our volume | |
880 | m_volume.clear(); | |
881 | ||
882 | // remove common directories starting at the top | |
883 | while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() && | |
884 | m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) ) | |
885 | { | |
886 | m_dirs.RemoveAt(0); | |
887 | fnBase.m_dirs.RemoveAt(0); | |
888 | } | |
889 | ||
890 | // add as many ".." as needed | |
891 | size_t count = fnBase.m_dirs.GetCount(); | |
892 | for ( size_t i = 0; i < count; i++ ) | |
893 | { | |
894 | m_dirs.Insert(wxT(".."), 0u); | |
895 | } | |
896 | ||
897 | m_relative = TRUE; | |
898 | ||
899 | // we were modified | |
900 | return TRUE; | |
901 | } | |
902 | ||
903 | // ---------------------------------------------------------------------------- | |
904 | // filename kind tests | |
905 | // ---------------------------------------------------------------------------- | |
906 | ||
907 | bool wxFileName::SameAs(const wxFileName &filepath, wxPathFormat format) | |
908 | { | |
909 | wxFileName fn1 = *this, | |
910 | fn2 = filepath; | |
911 | ||
912 | // get cwd only once - small time saving | |
913 | wxString cwd = wxGetCwd(); | |
914 | fn1.Normalize(wxPATH_NORM_ALL, cwd, format); | |
915 | fn2.Normalize(wxPATH_NORM_ALL, cwd, format); | |
916 | ||
917 | if ( fn1.GetFullPath() == fn2.GetFullPath() ) | |
918 | return TRUE; | |
919 | ||
920 | // TODO: compare inodes for Unix, this works even when filenames are | |
921 | // different but files are the same (symlinks) (VZ) | |
922 | ||
923 | return FALSE; | |
924 | } | |
925 | ||
926 | /* static */ | |
927 | bool wxFileName::IsCaseSensitive( wxPathFormat format ) | |
928 | { | |
929 | // only Unix filenames are truely case-sensitive | |
930 | return GetFormat(format) == wxPATH_UNIX; | |
931 | } | |
932 | ||
933 | /* static */ | |
934 | wxString wxFileName::GetVolumeSeparator(wxPathFormat format) | |
935 | { | |
936 | wxString sepVol; | |
937 | ||
938 | if ( (GetFormat(format) == wxPATH_DOS) || | |
939 | (GetFormat(format) == wxPATH_VMS) ) | |
940 | { | |
941 | sepVol = wxFILE_SEP_DSK; | |
942 | } | |
943 | //else: leave empty | |
944 | ||
945 | return sepVol; | |
946 | } | |
947 | ||
948 | /* static */ | |
949 | wxString wxFileName::GetPathSeparators(wxPathFormat format) | |
950 | { | |
951 | wxString seps; | |
952 | switch ( GetFormat(format) ) | |
953 | { | |
954 | case wxPATH_DOS: | |
955 | // accept both as native APIs do but put the native one first as | |
956 | // this is the one we use in GetFullPath() | |
957 | seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX; | |
958 | break; | |
959 | ||
960 | default: | |
961 | wxFAIL_MSG( _T("unknown wxPATH_XXX style") ); | |
962 | // fall through | |
963 | ||
964 | case wxPATH_UNIX: | |
965 | seps = wxFILE_SEP_PATH_UNIX; | |
966 | break; | |
967 | ||
968 | case wxPATH_MAC: | |
969 | seps = wxFILE_SEP_PATH_MAC; | |
970 | break; | |
971 | ||
972 | case wxPATH_VMS: | |
973 | seps = wxFILE_SEP_PATH_VMS; | |
974 | break; | |
975 | } | |
976 | ||
977 | return seps; | |
978 | } | |
979 | ||
980 | /* static */ | |
981 | bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) | |
982 | { | |
983 | // wxString::Find() doesn't work as expected with NUL - it will always find | |
984 | // it, so it is almost surely a bug if this function is called with NUL arg | |
985 | wxASSERT_MSG( ch != _T('\0'), _T("shouldn't be called with NUL") ); | |
986 | ||
987 | return GetPathSeparators(format).Find(ch) != wxNOT_FOUND; | |
988 | } | |
989 | ||
990 | bool wxFileName::IsWild( wxPathFormat format ) | |
991 | { | |
992 | // FIXME: this is probably false for Mac and this is surely wrong for most | |
993 | // of Unix shells (think about "[...]") | |
994 | (void)format; | |
995 | return m_name.find_first_of(_T("*?")) != wxString::npos; | |
996 | } | |
997 | ||
998 | // ---------------------------------------------------------------------------- | |
999 | // path components manipulation | |
1000 | // ---------------------------------------------------------------------------- | |
1001 | ||
1002 | void wxFileName::AppendDir( const wxString &dir ) | |
1003 | { | |
1004 | m_dirs.Add( dir ); | |
1005 | } | |
1006 | ||
1007 | void wxFileName::PrependDir( const wxString &dir ) | |
1008 | { | |
1009 | m_dirs.Insert( dir, 0 ); | |
1010 | } | |
1011 | ||
1012 | void wxFileName::InsertDir( int before, const wxString &dir ) | |
1013 | { | |
1014 | m_dirs.Insert( dir, before ); | |
1015 | } | |
1016 | ||
1017 | void wxFileName::RemoveDir( int pos ) | |
1018 | { | |
1019 | m_dirs.Remove( (size_t)pos ); | |
1020 | } | |
1021 | ||
1022 | // ---------------------------------------------------------------------------- | |
1023 | // accessors | |
1024 | // ---------------------------------------------------------------------------- | |
1025 | ||
1026 | void wxFileName::SetFullName(const wxString& fullname) | |
1027 | { | |
1028 | SplitPath(fullname, NULL /* no path */, &m_name, &m_ext); | |
1029 | } | |
1030 | ||
1031 | wxString wxFileName::GetFullName() const | |
1032 | { | |
1033 | wxString fullname = m_name; | |
1034 | if ( !m_ext.empty() ) | |
1035 | { | |
1036 | fullname << wxFILE_SEP_EXT << m_ext; | |
1037 | } | |
1038 | ||
1039 | return fullname; | |
1040 | } | |
1041 | ||
1042 | wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const | |
1043 | { | |
1044 | format = GetFormat( format ); | |
1045 | ||
1046 | wxString fullpath; | |
1047 | ||
1048 | // the leading character | |
1049 | if ( format == wxPATH_MAC && m_relative ) | |
1050 | { | |
1051 | fullpath += wxFILE_SEP_PATH_MAC; | |
1052 | } | |
1053 | else if ( format == wxPATH_DOS ) | |
1054 | { | |
1055 | if (!m_relative) | |
1056 | fullpath += wxFILE_SEP_PATH_DOS; | |
1057 | } | |
1058 | else if ( format == wxPATH_UNIX ) | |
1059 | { | |
1060 | if (!m_relative) | |
1061 | fullpath += wxFILE_SEP_PATH_UNIX; | |
1062 | } | |
1063 | ||
1064 | // then concatenate all the path components using the path separator | |
1065 | size_t dirCount = m_dirs.GetCount(); | |
1066 | if ( dirCount ) | |
1067 | { | |
1068 | if ( format == wxPATH_VMS ) | |
1069 | { | |
1070 | fullpath += wxT('['); | |
1071 | } | |
1072 | ||
1073 | ||
1074 | for ( size_t i = 0; i < dirCount; i++ ) | |
1075 | { | |
1076 | // TODO: What to do with ".." under VMS | |
1077 | ||
1078 | switch (format) | |
1079 | { | |
1080 | case wxPATH_MAC: | |
1081 | { | |
1082 | if (m_dirs[i] == wxT(".")) | |
1083 | break; | |
1084 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1085 | fullpath += m_dirs[i]; | |
1086 | fullpath += wxT(':'); | |
1087 | break; | |
1088 | } | |
1089 | case wxPATH_DOS: | |
1090 | { | |
1091 | fullpath += m_dirs[i]; | |
1092 | fullpath += wxT('\\'); | |
1093 | break; | |
1094 | } | |
1095 | case wxPATH_UNIX: | |
1096 | { | |
1097 | fullpath += m_dirs[i]; | |
1098 | fullpath += wxT('/'); | |
1099 | break; | |
1100 | } | |
1101 | case wxPATH_VMS: | |
1102 | { | |
1103 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1104 | fullpath += m_dirs[i]; | |
1105 | if (i == dirCount-1) | |
1106 | fullpath += wxT(']'); | |
1107 | else | |
1108 | fullpath += wxT('.'); | |
1109 | break; | |
1110 | } | |
1111 | default: | |
1112 | { | |
1113 | wxFAIL_MSG( wxT("error") ); | |
1114 | } | |
1115 | } | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | ||
1120 | ||
1121 | return fullpath; | |
1122 | } | |
1123 | ||
1124 | wxString wxFileName::GetFullPath( wxPathFormat format ) const | |
1125 | { | |
1126 | format = GetFormat(format); | |
1127 | ||
1128 | wxString fullpath; | |
1129 | ||
1130 | // first put the volume | |
1131 | if ( !m_volume.empty() ) | |
1132 | { | |
1133 | { | |
1134 | // Special Windows UNC paths hack, part 2: undo what we did in | |
1135 | // SplitPath() and make an UNC path if we have a drive which is not a | |
1136 | // single letter (hopefully the network shares can't be one letter only | |
1137 | // although I didn't find any authoritative docs on this) | |
1138 | if ( format == wxPATH_DOS && m_volume.length() > 1 ) | |
1139 | { | |
1140 | fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume; | |
1141 | } | |
1142 | else if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
1143 | { | |
1144 | fullpath << m_volume << GetVolumeSeparator(format); | |
1145 | } | |
1146 | // else ignore | |
1147 | } | |
1148 | } | |
1149 | ||
1150 | // the leading character | |
1151 | if ( format == wxPATH_MAC && m_relative ) | |
1152 | { | |
1153 | fullpath += wxFILE_SEP_PATH_MAC; | |
1154 | } | |
1155 | else if ( format == wxPATH_DOS ) | |
1156 | { | |
1157 | if (!m_relative) | |
1158 | fullpath += wxFILE_SEP_PATH_DOS; | |
1159 | } | |
1160 | else if ( format == wxPATH_UNIX ) | |
1161 | { | |
1162 | if (!m_relative) | |
1163 | fullpath += wxFILE_SEP_PATH_UNIX; | |
1164 | } | |
1165 | ||
1166 | // then concatenate all the path components using the path separator | |
1167 | size_t dirCount = m_dirs.GetCount(); | |
1168 | if ( dirCount ) | |
1169 | { | |
1170 | if ( format == wxPATH_VMS ) | |
1171 | { | |
1172 | fullpath += wxT('['); | |
1173 | } | |
1174 | ||
1175 | ||
1176 | for ( size_t i = 0; i < dirCount; i++ ) | |
1177 | { | |
1178 | // TODO: What to do with ".." under VMS | |
1179 | ||
1180 | switch (format) | |
1181 | { | |
1182 | case wxPATH_MAC: | |
1183 | { | |
1184 | if (m_dirs[i] == wxT(".")) | |
1185 | break; | |
1186 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1187 | fullpath += m_dirs[i]; | |
1188 | fullpath += wxT(':'); | |
1189 | break; | |
1190 | } | |
1191 | case wxPATH_DOS: | |
1192 | { | |
1193 | fullpath += m_dirs[i]; | |
1194 | fullpath += wxT('\\'); | |
1195 | break; | |
1196 | } | |
1197 | case wxPATH_UNIX: | |
1198 | { | |
1199 | fullpath += m_dirs[i]; | |
1200 | fullpath += wxT('/'); | |
1201 | break; | |
1202 | } | |
1203 | case wxPATH_VMS: | |
1204 | { | |
1205 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1206 | fullpath += m_dirs[i]; | |
1207 | if (i == dirCount-1) | |
1208 | fullpath += wxT(']'); | |
1209 | else | |
1210 | fullpath += wxT('.'); | |
1211 | break; | |
1212 | } | |
1213 | default: | |
1214 | { | |
1215 | wxFAIL_MSG( wxT("error") ); | |
1216 | } | |
1217 | } | |
1218 | } | |
1219 | } | |
1220 | ||
1221 | // finally add the file name and extension | |
1222 | fullpath += GetFullName(); | |
1223 | ||
1224 | return fullpath; | |
1225 | } | |
1226 | ||
1227 | // Return the short form of the path (returns identity on non-Windows platforms) | |
1228 | wxString wxFileName::GetShortPath() const | |
1229 | { | |
1230 | #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) | |
1231 | wxString path(GetFullPath()); | |
1232 | wxString pathOut; | |
1233 | DWORD sz = ::GetShortPathName(path, NULL, 0); | |
1234 | bool ok = sz != 0; | |
1235 | if ( ok ) | |
1236 | { | |
1237 | ok = ::GetShortPathName | |
1238 | ( | |
1239 | path, | |
1240 | pathOut.GetWriteBuf(sz), | |
1241 | sz | |
1242 | ) != 0; | |
1243 | pathOut.UngetWriteBuf(); | |
1244 | } | |
1245 | if (ok) | |
1246 | return pathOut; | |
1247 | ||
1248 | return path; | |
1249 | #else | |
1250 | return GetFullPath(); | |
1251 | #endif | |
1252 | } | |
1253 | ||
1254 | // Return the long form of the path (returns identity on non-Windows platforms) | |
1255 | wxString wxFileName::GetLongPath() const | |
1256 | { | |
1257 | wxString pathOut, | |
1258 | path = GetFullPath(); | |
1259 | ||
1260 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
1261 | bool success = FALSE; | |
1262 | ||
1263 | // VZ: this code was disabled, why? | |
1264 | #if 0 // wxUSE_DYNAMIC_LOADER | |
1265 | typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); | |
1266 | ||
1267 | static bool s_triedToLoad = FALSE; | |
1268 | ||
1269 | if ( !s_triedToLoad ) | |
1270 | { | |
1271 | s_triedToLoad = TRUE; | |
1272 | wxDynamicLibrary dllKernel(_T("kernel32")); | |
1273 | if ( dllKernel.IsLoaded() ) | |
1274 | { | |
1275 | // may succeed or fail depending on the Windows version | |
1276 | static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; | |
1277 | #ifdef _UNICODE | |
1278 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameW")); | |
1279 | #else | |
1280 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameA")); | |
1281 | #endif | |
1282 | ||
1283 | if ( s_pfnGetLongPathName ) | |
1284 | { | |
1285 | DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0); | |
1286 | bool ok = dwSize > 0; | |
1287 | ||
1288 | if ( ok ) | |
1289 | { | |
1290 | DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0); | |
1291 | ok = sz != 0; | |
1292 | if ( ok ) | |
1293 | { | |
1294 | ok = (*s_pfnGetLongPathName) | |
1295 | ( | |
1296 | path, | |
1297 | pathOut.GetWriteBuf(sz), | |
1298 | sz | |
1299 | ) != 0; | |
1300 | pathOut.UngetWriteBuf(); | |
1301 | ||
1302 | success = TRUE; | |
1303 | } | |
1304 | } | |
1305 | } | |
1306 | } | |
1307 | } | |
1308 | if (success) | |
1309 | return pathOut; | |
1310 | #endif // wxUSE_DYNAMIC_LOADER | |
1311 | ||
1312 | if (!success) | |
1313 | { | |
1314 | // The OS didn't support GetLongPathName, or some other error. | |
1315 | // We need to call FindFirstFile on each component in turn. | |
1316 | ||
1317 | WIN32_FIND_DATA findFileData; | |
1318 | HANDLE hFind; | |
1319 | pathOut = wxEmptyString; | |
1320 | ||
1321 | wxArrayString dirs = GetDirs(); | |
1322 | dirs.Add(GetFullName()); | |
1323 | ||
1324 | wxString tmpPath; | |
1325 | ||
1326 | size_t count = dirs.GetCount(); | |
1327 | for ( size_t i = 0; i < count; i++ ) | |
1328 | { | |
1329 | // We're using pathOut to collect the long-name path, but using a | |
1330 | // temporary for appending the last path component which may be | |
1331 | // short-name | |
1332 | tmpPath = pathOut + dirs[i]; | |
1333 | ||
1334 | if ( tmpPath.empty() ) | |
1335 | continue; | |
1336 | ||
1337 | if ( tmpPath.Last() == wxT(':') ) | |
1338 | { | |
1339 | // Can't pass a drive and root dir to FindFirstFile, | |
1340 | // so continue to next dir | |
1341 | tmpPath += wxFILE_SEP_PATH; | |
1342 | pathOut = tmpPath; | |
1343 | continue; | |
1344 | } | |
1345 | ||
1346 | hFind = ::FindFirstFile(tmpPath, &findFileData); | |
1347 | if (hFind == INVALID_HANDLE_VALUE) | |
1348 | { | |
1349 | // Error: return immediately with the original path | |
1350 | return path; | |
1351 | } | |
1352 | ||
1353 | pathOut += findFileData.cFileName; | |
1354 | if ( (i < (count-1)) ) | |
1355 | pathOut += wxFILE_SEP_PATH; | |
1356 | ||
1357 | ::FindClose(hFind); | |
1358 | } | |
1359 | } | |
1360 | #else // !Win32 | |
1361 | pathOut = path; | |
1362 | #endif // Win32/!Win32 | |
1363 | ||
1364 | return pathOut; | |
1365 | } | |
1366 | ||
1367 | wxPathFormat wxFileName::GetFormat( wxPathFormat format ) | |
1368 | { | |
1369 | if (format == wxPATH_NATIVE) | |
1370 | { | |
1371 | #if defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__) | |
1372 | format = wxPATH_DOS; | |
1373 | #elif defined(__WXMAC__) && !defined(__DARWIN__) | |
1374 | format = wxPATH_MAC; | |
1375 | #elif defined(__VMS) | |
1376 | format = wxPATH_VMS; | |
1377 | #else | |
1378 | format = wxPATH_UNIX; | |
1379 | #endif | |
1380 | } | |
1381 | return format; | |
1382 | } | |
1383 | ||
1384 | // ---------------------------------------------------------------------------- | |
1385 | // path splitting function | |
1386 | // ---------------------------------------------------------------------------- | |
1387 | ||
1388 | /* static */ | |
1389 | void wxFileName::SplitPath(const wxString& fullpathWithVolume, | |
1390 | wxString *pstrVolume, | |
1391 | wxString *pstrPath, | |
1392 | wxString *pstrName, | |
1393 | wxString *pstrExt, | |
1394 | wxPathFormat format) | |
1395 | { | |
1396 | format = GetFormat(format); | |
1397 | ||
1398 | wxString fullpath = fullpathWithVolume; | |
1399 | ||
1400 | // under VMS the end of the path is ']', not the path separator used to | |
1401 | // separate the components | |
1402 | wxString sepPath = format == wxPATH_VMS ? wxString(_T(']')) | |
1403 | : GetPathSeparators(format); | |
1404 | ||
1405 | // special Windows UNC paths hack: transform \\share\path into share:path | |
1406 | if ( format == wxPATH_DOS ) | |
1407 | { | |
1408 | if ( fullpath.length() >= 4 && | |
1409 | fullpath[0u] == wxFILE_SEP_PATH_DOS && | |
1410 | fullpath[1u] == wxFILE_SEP_PATH_DOS ) | |
1411 | { | |
1412 | fullpath.erase(0, 2); | |
1413 | ||
1414 | size_t posFirstSlash = fullpath.find_first_of(sepPath); | |
1415 | if ( posFirstSlash != wxString::npos ) | |
1416 | { | |
1417 | fullpath[posFirstSlash] = wxFILE_SEP_DSK; | |
1418 | ||
1419 | // UNC paths are always absolute, right? (FIXME) | |
1420 | fullpath.insert(posFirstSlash + 1, wxFILE_SEP_PATH_DOS); | |
1421 | } | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | // We separate the volume here | |
1426 | if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
1427 | { | |
1428 | wxString sepVol = GetVolumeSeparator(format); | |
1429 | ||
1430 | size_t posFirstColon = fullpath.find_first_of(sepVol); | |
1431 | if ( posFirstColon != wxString::npos ) | |
1432 | { | |
1433 | if ( pstrVolume ) | |
1434 | { | |
1435 | *pstrVolume = fullpath.Left(posFirstColon); | |
1436 | } | |
1437 | ||
1438 | // remove the volume name and the separator from the full path | |
1439 | fullpath.erase(0, posFirstColon + sepVol.length()); | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | // find the positions of the last dot and last path separator in the path | |
1444 | size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); | |
1445 | size_t posLastSlash = fullpath.find_last_of(sepPath); | |
1446 | ||
1447 | if ( (posLastDot != wxString::npos) && | |
1448 | ((format == wxPATH_UNIX) || (format == wxPATH_VMS)) ) | |
1449 | { | |
1450 | if ( (posLastDot == 0) || | |
1451 | (fullpath[posLastDot - 1] == sepPath[0u] ) ) | |
1452 | { | |
1453 | // under Unix and VMS, dot may be (and commonly is) the first | |
1454 | // character of the filename, don't treat the entire filename as | |
1455 | // extension in this case | |
1456 | posLastDot = wxString::npos; | |
1457 | } | |
1458 | } | |
1459 | ||
1460 | // if we do have a dot and a slash, check that the dot is in the name part | |
1461 | if ( (posLastDot != wxString::npos) && | |
1462 | (posLastSlash != wxString::npos) && | |
1463 | (posLastDot < posLastSlash) ) | |
1464 | { | |
1465 | // the dot is part of the path, not the start of the extension | |
1466 | posLastDot = wxString::npos; | |
1467 | } | |
1468 | ||
1469 | // now fill in the variables provided by user | |
1470 | if ( pstrPath ) | |
1471 | { | |
1472 | if ( posLastSlash == wxString::npos ) | |
1473 | { | |
1474 | // no path at all | |
1475 | pstrPath->Empty(); | |
1476 | } | |
1477 | else | |
1478 | { | |
1479 | // take everything up to the path separator but take care to make | |
1480 | // the path equal to something like '/', not empty, for the files | |
1481 | // immediately under root directory | |
1482 | size_t len = posLastSlash; | |
1483 | if ( !len ) | |
1484 | len++; | |
1485 | ||
1486 | *pstrPath = fullpath.Left(len); | |
1487 | ||
1488 | // special VMS hack: remove the initial bracket | |
1489 | if ( format == wxPATH_VMS ) | |
1490 | { | |
1491 | if ( (*pstrPath)[0u] == _T('[') ) | |
1492 | pstrPath->erase(0, 1); | |
1493 | } | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | if ( pstrName ) | |
1498 | { | |
1499 | // take all characters starting from the one after the last slash and | |
1500 | // up to, but excluding, the last dot | |
1501 | size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; | |
1502 | size_t count; | |
1503 | if ( posLastDot == wxString::npos ) | |
1504 | { | |
1505 | // take all until the end | |
1506 | count = wxString::npos; | |
1507 | } | |
1508 | else if ( posLastSlash == wxString::npos ) | |
1509 | { | |
1510 | count = posLastDot; | |
1511 | } | |
1512 | else // have both dot and slash | |
1513 | { | |
1514 | count = posLastDot - posLastSlash - 1; | |
1515 | } | |
1516 | ||
1517 | *pstrName = fullpath.Mid(nStart, count); | |
1518 | } | |
1519 | ||
1520 | if ( pstrExt ) | |
1521 | { | |
1522 | if ( posLastDot == wxString::npos ) | |
1523 | { | |
1524 | // no extension | |
1525 | pstrExt->Empty(); | |
1526 | } | |
1527 | else | |
1528 | { | |
1529 | // take everything after the dot | |
1530 | *pstrExt = fullpath.Mid(posLastDot + 1); | |
1531 | } | |
1532 | } | |
1533 | } | |
1534 | ||
1535 | /* static */ | |
1536 | void wxFileName::SplitPath(const wxString& fullpath, | |
1537 | wxString *path, | |
1538 | wxString *name, | |
1539 | wxString *ext, | |
1540 | wxPathFormat format) | |
1541 | { | |
1542 | wxString volume; | |
1543 | SplitPath(fullpath, &volume, path, name, ext, format); | |
1544 | ||
1545 | if ( path && !volume.empty() ) | |
1546 | { | |
1547 | path->Prepend(volume + GetVolumeSeparator(format)); | |
1548 | } | |
1549 | } | |
1550 | ||
1551 | // ---------------------------------------------------------------------------- | |
1552 | // time functions | |
1553 | // ---------------------------------------------------------------------------- | |
1554 | ||
1555 | bool wxFileName::SetTimes(const wxDateTime *dtCreate, | |
1556 | const wxDateTime *dtAccess, | |
1557 | const wxDateTime *dtMod) | |
1558 | { | |
1559 | #if defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) | |
1560 | if ( !dtAccess && !dtMod ) | |
1561 | { | |
1562 | // can't modify the creation time anyhow, don't try | |
1563 | return TRUE; | |
1564 | } | |
1565 | ||
1566 | // if dtAccess or dtMod is not specified, use the other one (which must be | |
1567 | // non NULL because of the test above) for both times | |
1568 | utimbuf utm; | |
1569 | utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks(); | |
1570 | utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); | |
1571 | if ( utime(GetFullPath(), &utm) == 0 ) | |
1572 | { | |
1573 | return TRUE; | |
1574 | } | |
1575 | #elif defined(__WIN32__) | |
1576 | wxFileHandle fh(GetFullPath()); | |
1577 | if ( fh.IsOk() ) | |
1578 | { | |
1579 | FILETIME ftAccess, ftCreate, ftWrite; | |
1580 | ||
1581 | if ( dtCreate ) | |
1582 | ConvertWxToFileTime(&ftCreate, *dtCreate); | |
1583 | if ( dtAccess ) | |
1584 | ConvertWxToFileTime(&ftAccess, *dtAccess); | |
1585 | if ( dtMod ) | |
1586 | ConvertWxToFileTime(&ftWrite, *dtMod); | |
1587 | ||
1588 | if ( ::SetFileTime(fh, | |
1589 | dtCreate ? &ftCreate : NULL, | |
1590 | dtAccess ? &ftAccess : NULL, | |
1591 | dtMod ? &ftWrite : NULL) ) | |
1592 | { | |
1593 | return TRUE; | |
1594 | } | |
1595 | } | |
1596 | #else // other platform | |
1597 | #endif // platforms | |
1598 | ||
1599 | wxLogSysError(_("Failed to modify file times for '%s'"), | |
1600 | GetFullPath().c_str()); | |
1601 | ||
1602 | return FALSE; | |
1603 | } | |
1604 | ||
1605 | bool wxFileName::Touch() | |
1606 | { | |
1607 | #if defined(__UNIX_LIKE__) | |
1608 | // under Unix touching file is simple: just pass NULL to utime() | |
1609 | if ( utime(GetFullPath(), NULL) == 0 ) | |
1610 | { | |
1611 | return TRUE; | |
1612 | } | |
1613 | ||
1614 | wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str()); | |
1615 | ||
1616 | return FALSE; | |
1617 | #else // other platform | |
1618 | wxDateTime dtNow = wxDateTime::Now(); | |
1619 | ||
1620 | return SetTimes(NULL /* don't change create time */, &dtNow, &dtNow); | |
1621 | #endif // platforms | |
1622 | } | |
1623 | ||
1624 | bool wxFileName::GetTimes(wxDateTime *dtAccess, | |
1625 | wxDateTime *dtMod, | |
1626 | wxDateTime *dtChange) const | |
1627 | { | |
1628 | #if defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__)) | |
1629 | wxStructStat stBuf; | |
1630 | if ( wxStat(GetFullPath(), &stBuf) == 0 ) | |
1631 | { | |
1632 | if ( dtAccess ) | |
1633 | dtAccess->Set(stBuf.st_atime); | |
1634 | if ( dtMod ) | |
1635 | dtMod->Set(stBuf.st_mtime); | |
1636 | if ( dtChange ) | |
1637 | dtChange->Set(stBuf.st_ctime); | |
1638 | ||
1639 | return TRUE; | |
1640 | } | |
1641 | #elif defined(__WIN32__) | |
1642 | wxFileHandle fh(GetFullPath()); | |
1643 | if ( fh.IsOk() ) | |
1644 | { | |
1645 | FILETIME ftAccess, ftCreate, ftWrite; | |
1646 | ||
1647 | if ( ::GetFileTime(fh, | |
1648 | dtMod ? &ftCreate : NULL, | |
1649 | dtAccess ? &ftAccess : NULL, | |
1650 | dtChange ? &ftWrite : NULL) ) | |
1651 | { | |
1652 | if ( dtMod ) | |
1653 | ConvertFileTimeToWx(dtMod, ftCreate); | |
1654 | if ( dtAccess ) | |
1655 | ConvertFileTimeToWx(dtAccess, ftAccess); | |
1656 | if ( dtChange ) | |
1657 | ConvertFileTimeToWx(dtChange, ftWrite); | |
1658 | ||
1659 | return TRUE; | |
1660 | } | |
1661 | } | |
1662 | #else // other platform | |
1663 | #endif // platforms | |
1664 | ||
1665 | wxLogSysError(_("Failed to retrieve file times for '%s'"), | |
1666 | GetFullPath().c_str()); | |
1667 | ||
1668 | return FALSE; | |
1669 | } | |
1670 |