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