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