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