]>
Commit | Line | Data |
---|---|---|
df5ddbca | 1 | ///////////////////////////////////////////////////////////////////////////// |
097ead30 VZ |
2 | // Name: src/common/filename.cpp |
3 | // Purpose: wxFileName - encapsulates a file path | |
844f90fb | 4 | // Author: Robert Roebling, Vadim Zeitlin |
df5ddbca RR |
5 | // Modified by: |
6 | // Created: 28.12.2000 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 Robert Roebling | |
65571936 | 9 | // Licence: wxWindows licence |
df5ddbca RR |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
04c943b1 VZ |
12 | /* |
13 | Here are brief descriptions of the filename formats supported by this class: | |
14 | ||
2bc60417 RR |
15 | wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file |
16 | names have the form: | |
04c943b1 VZ |
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 | ||
2bc60417 | 21 | wxPATH_DOS: DOS/Windows format, absolute file names have the form: |
04c943b1 VZ |
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 | ||
ae4d7004 | 27 | wxPATH_MAC: Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file |
2bc60417 | 28 | names have the form |
04c943b1 VZ |
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). | |
a385b5df | 35 | Since the volume is just part of the file path, it is not |
353f41cb | 36 | treated like a separate entity as it is done under DOS and |
90a68369 | 37 | VMS, it is just treated as another dir. |
04c943b1 VZ |
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. | |
ba623ba2 | 50 | [ start of directory specification |
04c943b1 VZ |
51 | . separator between directory and subdirectory |
52 | ] between directory and file | |
53 | */ | |
54 | ||
097ead30 VZ |
55 | // ============================================================================ |
56 | // declarations | |
57 | // ============================================================================ | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // headers | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
df5ddbca RR |
63 | // For compilers that support precompilation, includes "wx.h". |
64 | #include "wx/wxprec.h" | |
65 | ||
66 | #ifdef __BORLANDC__ | |
0b9ab0bd | 67 | #pragma hdrstop |
df5ddbca RR |
68 | #endif |
69 | ||
70 | #ifndef WX_PRECOMP | |
57bd4c60 WS |
71 | #ifdef __WXMSW__ |
72 | #include "wx/msw/wrapwin.h" // For GetShort/LongPathName | |
73 | #endif | |
ad9835c9 WS |
74 | #include "wx/dynarray.h" |
75 | #include "wx/intl.h" | |
76 | #include "wx/log.h" | |
de6185e2 | 77 | #include "wx/utils.h" |
0bf751e7 | 78 | #include "wx/crt.h" |
df5ddbca RR |
79 | #endif |
80 | ||
81 | #include "wx/filename.h" | |
b70a2866 | 82 | #include "wx/private/filename.h" |
df5ddbca | 83 | #include "wx/tokenzr.h" |
844f90fb | 84 | #include "wx/config.h" // for wxExpandEnvVars |
35a6691a | 85 | #include "wx/dynlib.h" |
110c5094 | 86 | #include "wx/dir.h" |
df5ddbca | 87 | |
57bd4c60 WS |
88 | #if defined(__WIN32__) && defined(__MINGW32__) |
89 | #include "wx/msw/gccpriv.h" | |
951cd180 VZ |
90 | #endif |
91 | ||
1c193821 JS |
92 | #ifdef __WXWINCE__ |
93 | #include "wx/msw/private.h" | |
94 | #endif | |
95 | ||
76a5e5d2 | 96 | #if defined(__WXMAC__) |
c933e267 | 97 | #include "wx/osx/private.h" // includes mac headers |
76a5e5d2 SC |
98 | #endif |
99 | ||
951cd180 VZ |
100 | // utime() is POSIX so should normally be available on all Unices |
101 | #ifdef __UNIX_LIKE__ | |
0b9ab0bd RL |
102 | #include <sys/types.h> |
103 | #include <utime.h> | |
104 | #include <sys/stat.h> | |
105 | #include <unistd.h> | |
9e9b65c1 JS |
106 | #endif |
107 | ||
444d61ba VS |
108 | #ifdef __DJGPP__ |
109 | #include <unistd.h> | |
110 | #endif | |
111 | ||
01d981ec | 112 | #ifdef __MWERKS__ |
31907d03 SC |
113 | #ifdef __MACH__ |
114 | #include <sys/types.h> | |
115 | #include <utime.h> | |
116 | #include <sys/stat.h> | |
117 | #include <unistd.h> | |
118 | #else | |
0b9ab0bd RL |
119 | #include <stat.h> |
120 | #include <unistd.h> | |
121 | #include <unix.h> | |
01d981ec | 122 | #endif |
31907d03 | 123 | #endif |
01d981ec | 124 | |
d9f54bb0 | 125 | #ifdef __WATCOMC__ |
0b9ab0bd RL |
126 | #include <io.h> |
127 | #include <sys/utime.h> | |
128 | #include <sys/stat.h> | |
d9f54bb0 VS |
129 | #endif |
130 | ||
24eb81cb DW |
131 | #ifdef __VISAGECPP__ |
132 | #ifndef MAX_PATH | |
133 | #define MAX_PATH 256 | |
134 | #endif | |
135 | #endif | |
136 | ||
01c9a906 | 137 | #ifdef __EMX__ |
5d66debf | 138 | #include <os2.h> |
01c9a906 SN |
139 | #define MAX_PATH _MAX_PATH |
140 | #endif | |
141 | ||
23b8a262 | 142 | |
bd08f2f7 | 143 | #if wxUSE_LONGLONG |
060668a1 | 144 | extern const wxULongLong wxInvalidSize = (unsigned)-1; |
bd08f2f7 | 145 | #endif // wxUSE_LONGLONG |
23b8a262 | 146 | |
7b611a3a VZ |
147 | namespace |
148 | { | |
23b8a262 | 149 | |
951cd180 VZ |
150 | // ---------------------------------------------------------------------------- |
151 | // private classes | |
152 | // ---------------------------------------------------------------------------- | |
153 | ||
154 | // small helper class which opens and closes the file - we use it just to get | |
155 | // a file handle for the given file name to pass it to some Win32 API function | |
c67d6888 | 156 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
951cd180 VZ |
157 | |
158 | class wxFileHandle | |
159 | { | |
160 | public: | |
6dbb903b VZ |
161 | enum OpenMode |
162 | { | |
f3c74c8d VZ |
163 | ReadAttr, |
164 | WriteAttr | |
6dbb903b VZ |
165 | }; |
166 | ||
6bc176b4 | 167 | wxFileHandle(const wxString& filename, OpenMode mode, int flags = 0) |
951cd180 | 168 | { |
f3c74c8d VZ |
169 | // be careful and use FILE_{READ,WRITE}_ATTRIBUTES here instead of the |
170 | // usual GENERIC_{READ,WRITE} as we don't want the file access time to | |
171 | // be changed when we open it because this class is used for setting | |
172 | // access time (see #10567) | |
951cd180 VZ |
173 | m_hFile = ::CreateFile |
174 | ( | |
e0a050e3 | 175 | filename.fn_str(), // name |
f3c74c8d VZ |
176 | mode == ReadAttr ? FILE_READ_ATTRIBUTES // access mask |
177 | : FILE_WRITE_ATTRIBUTES, | |
54bcff35 VZ |
178 | FILE_SHARE_READ | // sharing mode |
179 | FILE_SHARE_WRITE, // (allow everything) | |
6dbb903b VZ |
180 | NULL, // no secutity attr |
181 | OPEN_EXISTING, // creation disposition | |
6bc176b4 | 182 | flags, // flags |
6dbb903b | 183 | NULL // no template file |
951cd180 VZ |
184 | ); |
185 | ||
186 | if ( m_hFile == INVALID_HANDLE_VALUE ) | |
187 | { | |
f3c74c8d | 188 | if ( mode == ReadAttr ) |
43b2d5e7 | 189 | { |
49a70977 VS |
190 | wxLogSysError(_("Failed to open '%s' for reading"), |
191 | filename.c_str()); | |
43b2d5e7 | 192 | } |
49a70977 | 193 | else |
43b2d5e7 | 194 | { |
49a70977 VS |
195 | wxLogSysError(_("Failed to open '%s' for writing"), |
196 | filename.c_str()); | |
43b2d5e7 | 197 | } |
951cd180 VZ |
198 | } |
199 | } | |
200 | ||
201 | ~wxFileHandle() | |
202 | { | |
203 | if ( m_hFile != INVALID_HANDLE_VALUE ) | |
204 | { | |
205 | if ( !::CloseHandle(m_hFile) ) | |
206 | { | |
207 | wxLogSysError(_("Failed to close file handle")); | |
208 | } | |
209 | } | |
210 | } | |
211 | ||
f363e05c | 212 | // return true only if the file could be opened successfully |
951cd180 VZ |
213 | bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; } |
214 | ||
215 | // get the handle | |
216 | operator HANDLE() const { return m_hFile; } | |
217 | ||
218 | private: | |
219 | HANDLE m_hFile; | |
220 | }; | |
221 | ||
222 | #endif // __WIN32__ | |
223 | ||
224 | // ---------------------------------------------------------------------------- | |
225 | // private functions | |
226 | // ---------------------------------------------------------------------------- | |
227 | ||
e2b87f38 | 228 | #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__) |
951cd180 VZ |
229 | |
230 | // convert between wxDateTime and FILETIME which is a 64-bit value representing | |
231 | // the number of 100-nanosecond intervals since January 1, 1601. | |
232 | ||
d56e2b97 | 233 | static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft) |
951cd180 | 234 | { |
752f10a8 | 235 | FILETIME ftcopy = ft; |
6dbb903b | 236 | FILETIME ftLocal; |
752f10a8 | 237 | if ( !::FileTimeToLocalFileTime(&ftcopy, &ftLocal) ) |
6dbb903b | 238 | { |
9a83f860 | 239 | wxLogLastError(wxT("FileTimeToLocalFileTime")); |
6dbb903b | 240 | } |
d56e2b97 | 241 | |
6dbb903b VZ |
242 | SYSTEMTIME st; |
243 | if ( !::FileTimeToSystemTime(&ftLocal, &st) ) | |
244 | { | |
9a83f860 | 245 | wxLogLastError(wxT("FileTimeToSystemTime")); |
6dbb903b | 246 | } |
d56e2b97 | 247 | |
6dbb903b VZ |
248 | dt->Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear, |
249 | st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); | |
951cd180 VZ |
250 | } |
251 | ||
d56e2b97 | 252 | static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) |
951cd180 | 253 | { |
6dbb903b VZ |
254 | SYSTEMTIME st; |
255 | st.wDay = dt.GetDay(); | |
ad816476 WS |
256 | st.wMonth = (WORD)(dt.GetMonth() + 1); |
257 | st.wYear = (WORD)dt.GetYear(); | |
6dbb903b VZ |
258 | st.wHour = dt.GetHour(); |
259 | st.wMinute = dt.GetMinute(); | |
260 | st.wSecond = dt.GetSecond(); | |
261 | st.wMilliseconds = dt.GetMillisecond(); | |
262 | ||
263 | FILETIME ftLocal; | |
264 | if ( !::SystemTimeToFileTime(&st, &ftLocal) ) | |
265 | { | |
9a83f860 | 266 | wxLogLastError(wxT("SystemTimeToFileTime")); |
6dbb903b | 267 | } |
d56e2b97 | 268 | |
6dbb903b VZ |
269 | if ( !::LocalFileTimeToFileTime(&ftLocal, ft) ) |
270 | { | |
9a83f860 | 271 | wxLogLastError(wxT("LocalFileTimeToFileTime")); |
6dbb903b | 272 | } |
951cd180 VZ |
273 | } |
274 | ||
e2b87f38 | 275 | #endif // wxUSE_DATETIME && __WIN32__ |
951cd180 | 276 | |
67c34f64 VZ |
277 | // return a string with the volume par |
278 | static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format) | |
279 | { | |
280 | wxString path; | |
281 | ||
282 | if ( !volume.empty() ) | |
283 | { | |
6ce27aa2 VZ |
284 | format = wxFileName::GetFormat(format); |
285 | ||
67c34f64 VZ |
286 | // Special Windows UNC paths hack, part 2: undo what we did in |
287 | // SplitPath() and make an UNC path if we have a drive which is not a | |
288 | // single letter (hopefully the network shares can't be one letter only | |
289 | // although I didn't find any authoritative docs on this) | |
290 | if ( format == wxPATH_DOS && volume.length() > 1 ) | |
291 | { | |
292 | path << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << volume; | |
293 | } | |
294 | else if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
295 | { | |
296 | path << volume << wxFileName::GetVolumeSeparator(format); | |
297 | } | |
298 | // else ignore | |
299 | } | |
300 | ||
301 | return path; | |
302 | } | |
303 | ||
7b611a3a VZ |
304 | // return true if the character is a DOS path separator i.e. either a slash or |
305 | // a backslash | |
306 | inline bool IsDOSPathSep(wxUniChar ch) | |
307 | { | |
308 | return ch == wxFILE_SEP_PATH_DOS || ch == wxFILE_SEP_PATH_UNIX; | |
309 | } | |
310 | ||
9e1c7236 VZ |
311 | // return true if the format used is the DOS/Windows one and the string looks |
312 | // like a UNC path | |
313 | static bool IsUNCPath(const wxString& path, wxPathFormat format) | |
314 | { | |
315 | return format == wxPATH_DOS && | |
316 | path.length() >= 4 && // "\\a" can't be a UNC path | |
7b611a3a VZ |
317 | IsDOSPathSep(path[0u]) && |
318 | IsDOSPathSep(path[1u]) && | |
319 | !IsDOSPathSep(path[2u]); | |
9e1c7236 VZ |
320 | } |
321 | ||
7b611a3a VZ |
322 | } // anonymous namespace |
323 | ||
097ead30 VZ |
324 | // ============================================================================ |
325 | // implementation | |
326 | // ============================================================================ | |
327 | ||
328 | // ---------------------------------------------------------------------------- | |
844f90fb | 329 | // wxFileName construction |
097ead30 | 330 | // ---------------------------------------------------------------------------- |
df5ddbca | 331 | |
a35b27b1 RR |
332 | void wxFileName::Assign( const wxFileName &filepath ) |
333 | { | |
04c943b1 | 334 | m_volume = filepath.GetVolume(); |
844f90fb | 335 | m_dirs = filepath.GetDirs(); |
04c943b1 VZ |
336 | m_name = filepath.GetName(); |
337 | m_ext = filepath.GetExt(); | |
a2fa5040 | 338 | m_relative = filepath.m_relative; |
dfecbee5 | 339 | m_hasExt = filepath.m_hasExt; |
df5ddbca RR |
340 | } |
341 | ||
04c943b1 VZ |
342 | void wxFileName::Assign(const wxString& volume, |
343 | const wxString& path, | |
344 | const wxString& name, | |
345 | const wxString& ext, | |
dfecbee5 | 346 | bool hasExt, |
9e1c7236 | 347 | wxPathFormat format) |
a7b51bc8 | 348 | { |
9e1c7236 VZ |
349 | // we should ignore paths which look like UNC shares because we already |
350 | // have the volume here and the UNC notation (\\server\path) is only valid | |
351 | // for paths which don't start with a volume, so prevent SetPath() from | |
352 | // recognizing "\\foo\bar" in "c:\\foo\bar" as an UNC path | |
353 | // | |
354 | // note also that this is a rather ugly way to do what we want (passing | |
355 | // some kind of flag telling to ignore UNC paths to SetPath() would be | |
356 | // better) but this is the safest thing to do to avoid breaking backwards | |
357 | // compatibility in 2.8 | |
358 | if ( IsUNCPath(path, format) ) | |
359 | { | |
360 | // remove one of the 2 leading backslashes to ensure that it's not | |
361 | // recognized as an UNC path by SetPath() | |
362 | wxString pathNonUNC(path, 1, wxString::npos); | |
363 | SetPath(pathNonUNC, format); | |
364 | } | |
365 | else // no UNC complications | |
366 | { | |
367 | SetPath(path, format); | |
368 | } | |
a7b51bc8 RR |
369 | |
370 | m_volume = volume; | |
371 | m_ext = ext; | |
372 | m_name = name; | |
dfecbee5 VZ |
373 | |
374 | m_hasExt = hasExt; | |
a7b51bc8 RR |
375 | } |
376 | ||
f1e77933 | 377 | void wxFileName::SetPath( const wxString& pathOrig, wxPathFormat format ) |
df5ddbca | 378 | { |
844f90fb | 379 | m_dirs.Clear(); |
90a68369 | 380 | |
f1e77933 | 381 | if ( pathOrig.empty() ) |
df5ddbca | 382 | { |
f1e77933 VZ |
383 | // no path at all |
384 | m_relative = true; | |
a2fa5040 | 385 | |
f1e77933 VZ |
386 | return; |
387 | } | |
90a68369 | 388 | |
f1e77933 | 389 | format = GetFormat( format ); |
a2fa5040 | 390 | |
f1e77933 VZ |
391 | // 0) deal with possible volume part first |
392 | wxString volume, | |
393 | path; | |
394 | SplitVolume(pathOrig, &volume, &path, format); | |
395 | if ( !volume.empty() ) | |
396 | { | |
397 | m_relative = false; | |
a2fa5040 | 398 | |
f1e77933 VZ |
399 | SetVolume(volume); |
400 | } | |
f363e05c | 401 | |
f1e77933 | 402 | // 1) Determine if the path is relative or absolute. |
0f506ded VZ |
403 | |
404 | if ( path.empty() ) | |
405 | { | |
406 | // we had only the volume | |
407 | return; | |
408 | } | |
409 | ||
f1e77933 | 410 | wxChar leadingChar = path[0u]; |
a2fa5040 | 411 | |
f1e77933 VZ |
412 | switch (format) |
413 | { | |
414 | case wxPATH_MAC: | |
415 | m_relative = leadingChar == wxT(':'); | |
416 | ||
417 | // We then remove a leading ":". The reason is in our | |
418 | // storage form for relative paths: | |
419 | // ":dir:file.txt" actually means "./dir/file.txt" in | |
420 | // DOS notation and should get stored as | |
421 | // (relative) (dir) (file.txt) | |
422 | // "::dir:file.txt" actually means "../dir/file.txt" | |
423 | // stored as (relative) (..) (dir) (file.txt) | |
424 | // This is important only for the Mac as an empty dir | |
425 | // actually means <UP>, whereas under DOS, double | |
426 | // slashes can be ignored: "\\\\" is the same as "\\". | |
427 | if (m_relative) | |
428 | path.erase( 0, 1 ); | |
429 | break; | |
a2fa5040 | 430 | |
f1e77933 VZ |
431 | case wxPATH_VMS: |
432 | // TODO: what is the relative path format here? | |
433 | m_relative = false; | |
434 | break; | |
90a68369 | 435 | |
f1e77933 | 436 | default: |
9a83f860 | 437 | wxFAIL_MSG( wxT("Unknown path format") ); |
f1e77933 | 438 | // !! Fall through !! |
90a68369 | 439 | |
f1e77933 | 440 | case wxPATH_UNIX: |
be5be16a | 441 | m_relative = leadingChar != wxT('/'); |
f1e77933 | 442 | break; |
353f41cb | 443 | |
f1e77933 VZ |
444 | case wxPATH_DOS: |
445 | m_relative = !IsPathSeparator(leadingChar, format); | |
446 | break; | |
844f90fb | 447 | |
df5ddbca | 448 | } |
f1e77933 VZ |
449 | |
450 | // 2) Break up the path into its members. If the original path | |
451 | // was just "/" or "\\", m_dirs will be empty. We know from | |
452 | // the m_relative field, if this means "nothing" or "root dir". | |
453 | ||
454 | wxStringTokenizer tn( path, GetPathSeparators(format) ); | |
455 | ||
456 | while ( tn.HasMoreTokens() ) | |
a7b51bc8 | 457 | { |
f1e77933 VZ |
458 | wxString token = tn.GetNextToken(); |
459 | ||
460 | // Remove empty token under DOS and Unix, interpret them | |
461 | // as .. under Mac. | |
462 | if (token.empty()) | |
463 | { | |
464 | if (format == wxPATH_MAC) | |
465 | m_dirs.Add( wxT("..") ); | |
466 | // else ignore | |
467 | } | |
468 | else | |
469 | { | |
470 | m_dirs.Add( token ); | |
471 | } | |
a7b51bc8 | 472 | } |
844f90fb VZ |
473 | } |
474 | ||
475 | void wxFileName::Assign(const wxString& fullpath, | |
476 | wxPathFormat format) | |
477 | { | |
04c943b1 | 478 | wxString volume, path, name, ext; |
dfecbee5 VZ |
479 | bool hasExt; |
480 | SplitPath(fullpath, &volume, &path, &name, &ext, &hasExt, format); | |
844f90fb | 481 | |
dfecbee5 | 482 | Assign(volume, path, name, ext, hasExt, format); |
844f90fb VZ |
483 | } |
484 | ||
81f25632 | 485 | void wxFileName::Assign(const wxString& fullpathOrig, |
844f90fb VZ |
486 | const wxString& fullname, |
487 | wxPathFormat format) | |
488 | { | |
81f25632 VZ |
489 | // always recognize fullpath as directory, even if it doesn't end with a |
490 | // slash | |
491 | wxString fullpath = fullpathOrig; | |
69858116 | 492 | if ( !fullpath.empty() && !wxEndsWithPathSeparator(fullpath) ) |
81f25632 | 493 | { |
33b97389 | 494 | fullpath += GetPathSeparator(format); |
81f25632 VZ |
495 | } |
496 | ||
52dbd056 | 497 | wxString volume, path, name, ext; |
dfecbee5 | 498 | bool hasExt; |
81f25632 | 499 | |
4b6a582b VZ |
500 | // do some consistency checks: the name should be really just the filename |
501 | // and the path should be really just a path | |
dfecbee5 | 502 | wxString volDummy, pathDummy, nameDummy, extDummy; |
81f25632 | 503 | |
dfecbee5 | 504 | SplitPath(fullname, &volDummy, &pathDummy, &name, &ext, &hasExt, format); |
81f25632 | 505 | |
dfecbee5 | 506 | wxASSERT_MSG( volDummy.empty() && pathDummy.empty(), |
9a83f860 | 507 | wxT("the file name shouldn't contain the path") ); |
81f25632 VZ |
508 | |
509 | SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format); | |
510 | ||
4c914788 JJ |
511 | #ifndef __VMS |
512 | // This test makes no sense on an OpenVMS system. | |
513 | wxASSERT_MSG( nameDummy.empty() && extDummy.empty(), | |
9a83f860 | 514 | wxT("the path shouldn't contain file name nor extension") ); |
4c914788 | 515 | #endif |
dfecbee5 | 516 | Assign(volume, path, name, ext, hasExt, format); |
52dbd056 VZ |
517 | } |
518 | ||
4c2deb19 VZ |
519 | void wxFileName::Assign(const wxString& pathOrig, |
520 | const wxString& name, | |
521 | const wxString& ext, | |
522 | wxPathFormat format) | |
523 | { | |
524 | wxString volume, | |
525 | path; | |
526 | SplitVolume(pathOrig, &volume, &path, format); | |
527 | ||
528 | Assign(volume, path, name, ext, format); | |
529 | } | |
530 | ||
52dbd056 VZ |
531 | void wxFileName::AssignDir(const wxString& dir, wxPathFormat format) |
532 | { | |
b494c48b | 533 | Assign(dir, wxEmptyString, format); |
844f90fb VZ |
534 | } |
535 | ||
536 | void wxFileName::Clear() | |
537 | { | |
538 | m_dirs.Clear(); | |
04c943b1 VZ |
539 | |
540 | m_volume = | |
844f90fb VZ |
541 | m_name = |
542 | m_ext = wxEmptyString; | |
fb969475 VZ |
543 | |
544 | // we don't have any absolute path for now | |
f363e05c | 545 | m_relative = true; |
dfecbee5 VZ |
546 | |
547 | // nor any extension | |
548 | m_hasExt = false; | |
844f90fb VZ |
549 | } |
550 | ||
551 | /* static */ | |
520200fd | 552 | wxFileName wxFileName::FileName(const wxString& file, wxPathFormat format) |
844f90fb | 553 | { |
520200fd | 554 | return wxFileName(file, format); |
844f90fb VZ |
555 | } |
556 | ||
557 | /* static */ | |
520200fd | 558 | wxFileName wxFileName::DirName(const wxString& dir, wxPathFormat format) |
844f90fb VZ |
559 | { |
560 | wxFileName fn; | |
520200fd | 561 | fn.AssignDir(dir, format); |
844f90fb | 562 | return fn; |
df5ddbca RR |
563 | } |
564 | ||
844f90fb VZ |
565 | // ---------------------------------------------------------------------------- |
566 | // existence tests | |
567 | // ---------------------------------------------------------------------------- | |
568 | ||
8e41796c | 569 | bool wxFileName::FileExists() const |
df5ddbca | 570 | { |
a35b27b1 RR |
571 | return wxFileName::FileExists( GetFullPath() ); |
572 | } | |
573 | ||
574 | bool wxFileName::FileExists( const wxString &file ) | |
575 | { | |
576 | return ::wxFileExists( file ); | |
df5ddbca RR |
577 | } |
578 | ||
8e41796c | 579 | bool wxFileName::DirExists() const |
df5ddbca | 580 | { |
db759dde | 581 | return wxFileName::DirExists( GetPath() ); |
a35b27b1 RR |
582 | } |
583 | ||
584 | bool wxFileName::DirExists( const wxString &dir ) | |
585 | { | |
da865fdd | 586 | return ::wxDirExists( dir ); |
df5ddbca RR |
587 | } |
588 | ||
844f90fb VZ |
589 | // ---------------------------------------------------------------------------- |
590 | // CWD and HOME stuff | |
591 | // ---------------------------------------------------------------------------- | |
592 | ||
6f91bc33 | 593 | void wxFileName::AssignCwd(const wxString& volume) |
df5ddbca | 594 | { |
6f91bc33 | 595 | AssignDir(wxFileName::GetCwd(volume)); |
a35b27b1 RR |
596 | } |
597 | ||
844f90fb | 598 | /* static */ |
6f91bc33 | 599 | wxString wxFileName::GetCwd(const wxString& volume) |
a35b27b1 | 600 | { |
6f91bc33 VZ |
601 | // if we have the volume, we must get the current directory on this drive |
602 | // and to do this we have to chdir to this volume - at least under Windows, | |
603 | // I don't know how to get the current drive on another volume elsewhere | |
604 | // (TODO) | |
605 | wxString cwdOld; | |
606 | if ( !volume.empty() ) | |
607 | { | |
608 | cwdOld = wxGetCwd(); | |
609 | SetCwd(volume + GetVolumeSeparator()); | |
610 | } | |
611 | ||
612 | wxString cwd = ::wxGetCwd(); | |
613 | ||
614 | if ( !volume.empty() ) | |
615 | { | |
616 | SetCwd(cwdOld); | |
617 | } | |
ae4d7004 | 618 | |
6f91bc33 | 619 | return cwd; |
a35b27b1 RR |
620 | } |
621 | ||
d9e80dce | 622 | bool wxFileName::SetCwd() const |
a35b27b1 | 623 | { |
db759dde | 624 | return wxFileName::SetCwd( GetPath() ); |
df5ddbca RR |
625 | } |
626 | ||
a35b27b1 | 627 | bool wxFileName::SetCwd( const wxString &cwd ) |
df5ddbca | 628 | { |
a35b27b1 | 629 | return ::wxSetWorkingDirectory( cwd ); |
df5ddbca RR |
630 | } |
631 | ||
a35b27b1 RR |
632 | void wxFileName::AssignHomeDir() |
633 | { | |
844f90fb | 634 | AssignDir(wxFileName::GetHomeDir()); |
a35b27b1 | 635 | } |
844f90fb | 636 | |
a35b27b1 RR |
637 | wxString wxFileName::GetHomeDir() |
638 | { | |
639 | return ::wxGetHomeDir(); | |
640 | } | |
844f90fb | 641 | |
68351053 | 642 | |
b70a2866 MW |
643 | // ---------------------------------------------------------------------------- |
644 | // CreateTempFileName | |
645 | // ---------------------------------------------------------------------------- | |
646 | ||
647 | #if wxUSE_FILE || wxUSE_FFILE | |
648 | ||
649 | ||
650 | #if !defined wx_fdopen && defined HAVE_FDOPEN | |
651 | #define wx_fdopen fdopen | |
652 | #endif | |
653 | ||
654 | // NB: GetTempFileName() under Windows creates the file, so using | |
655 | // O_EXCL there would fail | |
656 | #ifdef __WINDOWS__ | |
657 | #define wxOPEN_EXCL 0 | |
658 | #else | |
659 | #define wxOPEN_EXCL O_EXCL | |
660 | #endif | |
661 | ||
662 | ||
663 | #ifdef wxOpenOSFHandle | |
664 | #define WX_HAVE_DELETE_ON_CLOSE | |
665 | // On Windows create a file with the FILE_FLAGS_DELETE_ON_CLOSE flags. | |
666 | // | |
667 | static int wxOpenWithDeleteOnClose(const wxString& filename) | |
df5ddbca | 668 | { |
b70a2866 MW |
669 | DWORD access = GENERIC_READ | GENERIC_WRITE; |
670 | ||
671 | DWORD disposition = OPEN_ALWAYS; | |
672 | ||
673 | DWORD attributes = FILE_ATTRIBUTE_TEMPORARY | | |
674 | FILE_FLAG_DELETE_ON_CLOSE; | |
675 | ||
e0a050e3 | 676 | HANDLE h = ::CreateFile(filename.fn_str(), access, 0, NULL, |
b70a2866 MW |
677 | disposition, attributes, NULL); |
678 | ||
693bfcaf | 679 | return wxOpenOSFHandle(h, wxO_BINARY); |
ade35f11 | 680 | } |
b70a2866 | 681 | #endif // wxOpenOSFHandle |
ade35f11 | 682 | |
b70a2866 MW |
683 | |
684 | // Helper to open the file | |
685 | // | |
686 | static int wxTempOpen(const wxString& path, bool *deleteOnClose) | |
687 | { | |
688 | #ifdef WX_HAVE_DELETE_ON_CLOSE | |
689 | if (*deleteOnClose) | |
690 | return wxOpenWithDeleteOnClose(path); | |
691 | #endif | |
692 | ||
693 | *deleteOnClose = false; | |
694 | ||
695 | return wxOpen(path, wxO_BINARY | O_RDWR | O_CREAT | wxOPEN_EXCL, 0600); | |
696 | } | |
697 | ||
698 | ||
699 | #if wxUSE_FFILE | |
700 | // Helper to open the file and attach it to the wxFFile | |
701 | // | |
702 | static bool wxTempOpen(wxFFile *file, const wxString& path, bool *deleteOnClose) | |
ade35f11 | 703 | { |
b70a2866 MW |
704 | #ifndef wx_fdopen |
705 | *deleteOnClose = false; | |
9a83f860 | 706 | return file->Open(path, wxT("w+b")); |
b70a2866 MW |
707 | #else // wx_fdopen |
708 | int fd = wxTempOpen(path, deleteOnClose); | |
693bfcaf | 709 | if (fd == -1) |
b70a2866 MW |
710 | return false; |
711 | file->Attach(wx_fdopen(fd, "w+b")); | |
712 | return file->IsOpened(); | |
713 | #endif // wx_fdopen | |
714 | } | |
715 | #endif // wxUSE_FFILE | |
716 | ||
717 | ||
718 | #if !wxUSE_FILE | |
719 | #define WXFILEARGS(x, y) y | |
720 | #elif !wxUSE_FFILE | |
721 | #define WXFILEARGS(x, y) x | |
722 | #else | |
723 | #define WXFILEARGS(x, y) x, y | |
724 | #endif | |
725 | ||
726 | ||
727 | // Implementation of wxFileName::CreateTempFileName(). | |
728 | // | |
729 | static wxString wxCreateTempImpl( | |
730 | const wxString& prefix, | |
731 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp), | |
732 | bool *deleteOnClose = NULL) | |
733 | { | |
734 | #if wxUSE_FILE && wxUSE_FFILE | |
735 | wxASSERT(fileTemp == NULL || ffileTemp == NULL); | |
736 | #endif | |
ade35f11 | 737 | wxString path, dir, name; |
b70a2866 MW |
738 | bool wantDeleteOnClose = false; |
739 | ||
740 | if (deleteOnClose) | |
741 | { | |
742 | // set the result to false initially | |
743 | wantDeleteOnClose = *deleteOnClose; | |
744 | *deleteOnClose = false; | |
745 | } | |
746 | else | |
747 | { | |
748 | // easier if it alwasys points to something | |
749 | deleteOnClose = &wantDeleteOnClose; | |
750 | } | |
ade35f11 VZ |
751 | |
752 | // use the directory specified by the prefix | |
b70a2866 | 753 | wxFileName::SplitPath(prefix, &dir, &name, NULL /* extension */); |
ade35f11 | 754 | |
6091caf0 JS |
755 | if (dir.empty()) |
756 | { | |
8d7d6dea | 757 | dir = wxFileName::GetTempDir(); |
6091caf0 JS |
758 | } |
759 | ||
1c193821 | 760 | #if defined(__WXWINCE__) |
f05ba7ff | 761 | path = dir + wxT("\\") + name; |
1c193821 | 762 | int i = 1; |
b70a2866 | 763 | while (wxFileName::FileExists(path)) |
1c193821 | 764 | { |
f05ba7ff | 765 | path = dir + wxT("\\") + name ; |
1c193821 JS |
766 | path << i; |
767 | i ++; | |
768 | } | |
ade35f11 | 769 | |
1c193821 | 770 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
e0a050e3 VS |
771 | if ( !::GetTempFileName(dir.fn_str(), name.fn_str(), 0, |
772 | wxStringBuffer(path, MAX_PATH + 1)) ) | |
ade35f11 | 773 | { |
9a83f860 | 774 | wxLogLastError(wxT("GetTempFileName")); |
ade35f11 VZ |
775 | |
776 | path.clear(); | |
777 | } | |
ade35f11 | 778 | |
5a3912f2 | 779 | #else // !Windows |
ade35f11 VZ |
780 | path = dir; |
781 | ||
782 | if ( !wxEndsWithPathSeparator(dir) && | |
783 | (name.empty() || !wxIsPathSeparator(name[0u])) ) | |
784 | { | |
d9f54bb0 | 785 | path += wxFILE_SEP_PATH; |
ade35f11 VZ |
786 | } |
787 | ||
788 | path += name; | |
789 | ||
7070f55b | 790 | #if defined(HAVE_MKSTEMP) |
ade35f11 | 791 | // scratch space for mkstemp() |
9a83f860 | 792 | path += wxT("XXXXXX"); |
ade35f11 | 793 | |
74cf9763 | 794 | // we need to copy the path to the buffer in which mkstemp() can modify it |
86501081 | 795 | wxCharBuffer buf(path.fn_str()); |
74cf9763 VZ |
796 | |
797 | // cast is safe because the string length doesn't change | |
ca11abde | 798 | int fdTemp = mkstemp( (char*)(const char*) buf ); |
df22f860 | 799 | if ( fdTemp == -1 ) |
ade35f11 VZ |
800 | { |
801 | // this might be not necessary as mkstemp() on most systems should have | |
802 | // already done it but it doesn't hurt neither... | |
803 | path.clear(); | |
804 | } | |
df22f860 VZ |
805 | else // mkstemp() succeeded |
806 | { | |
ca11abde | 807 | path = wxConvFile.cMB2WX( (const char*) buf ); |
a62848fd | 808 | |
b70a2866 | 809 | #if wxUSE_FILE |
df22f860 VZ |
810 | // avoid leaking the fd |
811 | if ( fileTemp ) | |
812 | { | |
813 | fileTemp->Attach(fdTemp); | |
814 | } | |
815 | else | |
b70a2866 MW |
816 | #endif |
817 | ||
818 | #if wxUSE_FFILE | |
819 | if ( ffileTemp ) | |
820 | { | |
821 | #ifdef wx_fdopen | |
822 | ffileTemp->Attach(wx_fdopen(fdTemp, "r+b")); | |
823 | #else | |
9a83f860 | 824 | ffileTemp->Open(path, wxT("r+b")); |
b70a2866 MW |
825 | close(fdTemp); |
826 | #endif | |
827 | } | |
828 | else | |
829 | #endif | |
830 | ||
df22f860 VZ |
831 | { |
832 | close(fdTemp); | |
833 | } | |
834 | } | |
ade35f11 VZ |
835 | #else // !HAVE_MKSTEMP |
836 | ||
837 | #ifdef HAVE_MKTEMP | |
838 | // same as above | |
9a83f860 | 839 | path += wxT("XXXXXX"); |
ade35f11 | 840 | |
ca11abde | 841 | wxCharBuffer buf = wxConvFile.cWX2MB( path ); |
b70a2866 | 842 | if ( !mktemp( (char*)(const char*) buf ) ) |
ade35f11 VZ |
843 | { |
844 | path.clear(); | |
845 | } | |
aed08d79 RR |
846 | else |
847 | { | |
ca11abde | 848 | path = wxConvFile.cMB2WX( (const char*) buf ); |
aed08d79 | 849 | } |
7070f55b | 850 | #else // !HAVE_MKTEMP (includes __DOS__) |
ade35f11 | 851 | // generate the unique file name ourselves |
68351053 | 852 | #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) ) |
ade35f11 | 853 | path << (unsigned int)getpid(); |
7070f55b | 854 | #endif |
ade35f11 VZ |
855 | |
856 | wxString pathTry; | |
857 | ||
858 | static const size_t numTries = 1000; | |
859 | for ( size_t n = 0; n < numTries; n++ ) | |
860 | { | |
861 | // 3 hex digits is enough for numTries == 1000 < 4096 | |
9a83f860 | 862 | pathTry = path + wxString::Format(wxT("%.03x"), (unsigned int) n); |
b70a2866 | 863 | if ( !wxFileName::FileExists(pathTry) ) |
ade35f11 VZ |
864 | { |
865 | break; | |
866 | } | |
867 | ||
868 | pathTry.clear(); | |
869 | } | |
870 | ||
871 | path = pathTry; | |
872 | #endif // HAVE_MKTEMP/!HAVE_MKTEMP | |
873 | ||
df22f860 VZ |
874 | #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP |
875 | ||
876 | #endif // Windows/!Windows | |
877 | ||
878 | if ( path.empty() ) | |
879 | { | |
880 | wxLogSysError(_("Failed to create a temporary file name")); | |
881 | } | |
b70a2866 | 882 | else |
df22f860 | 883 | { |
b70a2866 MW |
884 | bool ok = true; |
885 | ||
df22f860 | 886 | // open the file - of course, there is a race condition here, this is |
ade35f11 | 887 | // why we always prefer using mkstemp()... |
b70a2866 MW |
888 | #if wxUSE_FILE |
889 | if ( fileTemp && !fileTemp->IsOpened() ) | |
890 | { | |
891 | *deleteOnClose = wantDeleteOnClose; | |
892 | int fd = wxTempOpen(path, deleteOnClose); | |
893 | if (fd != -1) | |
894 | fileTemp->Attach(fd); | |
895 | else | |
896 | ok = false; | |
897 | } | |
898 | #endif | |
899 | ||
900 | #if wxUSE_FFILE | |
901 | if ( ffileTemp && !ffileTemp->IsOpened() ) | |
902 | { | |
903 | *deleteOnClose = wantDeleteOnClose; | |
904 | ok = wxTempOpen(ffileTemp, path, deleteOnClose); | |
905 | } | |
906 | #endif | |
907 | ||
908 | if ( !ok ) | |
ade35f11 VZ |
909 | { |
910 | // FIXME: If !ok here should we loop and try again with another | |
911 | // file name? That is the standard recourse if open(O_EXCL) | |
912 | // fails, though of course it should be protected against | |
913 | // possible infinite looping too. | |
914 | ||
915 | wxLogError(_("Failed to open temporary file.")); | |
916 | ||
917 | path.clear(); | |
918 | } | |
919 | } | |
ade35f11 VZ |
920 | |
921 | return path; | |
df5ddbca RR |
922 | } |
923 | ||
b70a2866 MW |
924 | |
925 | static bool wxCreateTempImpl( | |
926 | const wxString& prefix, | |
927 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp), | |
928 | wxString *name) | |
929 | { | |
930 | bool deleteOnClose = true; | |
931 | ||
932 | *name = wxCreateTempImpl(prefix, | |
933 | WXFILEARGS(fileTemp, ffileTemp), | |
934 | &deleteOnClose); | |
935 | ||
936 | bool ok = !name->empty(); | |
937 | ||
938 | if (deleteOnClose) | |
939 | name->clear(); | |
940 | #ifdef __UNIX__ | |
941 | else if (ok && wxRemoveFile(*name)) | |
942 | name->clear(); | |
943 | #endif | |
944 | ||
945 | return ok; | |
946 | } | |
947 | ||
948 | ||
949 | static void wxAssignTempImpl( | |
950 | wxFileName *fn, | |
951 | const wxString& prefix, | |
952 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp)) | |
953 | { | |
954 | wxString tempname; | |
955 | tempname = wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, ffileTemp)); | |
956 | ||
957 | if ( tempname.empty() ) | |
958 | { | |
959 | // error, failed to get temp file name | |
960 | fn->Clear(); | |
961 | } | |
962 | else // ok | |
963 | { | |
964 | fn->Assign(tempname); | |
965 | } | |
966 | } | |
967 | ||
968 | ||
969 | void wxFileName::AssignTempFileName(const wxString& prefix) | |
970 | { | |
971 | wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, NULL)); | |
972 | } | |
973 | ||
974 | /* static */ | |
975 | wxString wxFileName::CreateTempFileName(const wxString& prefix) | |
976 | { | |
977 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, NULL)); | |
978 | } | |
979 | ||
980 | #endif // wxUSE_FILE || wxUSE_FFILE | |
981 | ||
982 | ||
983 | #if wxUSE_FILE | |
984 | ||
985 | wxString wxCreateTempFileName(const wxString& prefix, | |
986 | wxFile *fileTemp, | |
987 | bool *deleteOnClose) | |
988 | { | |
989 | return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), deleteOnClose); | |
990 | } | |
991 | ||
992 | bool wxCreateTempFile(const wxString& prefix, | |
993 | wxFile *fileTemp, | |
994 | wxString *name) | |
995 | { | |
996 | return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), name); | |
997 | } | |
998 | ||
999 | void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp) | |
1000 | { | |
1001 | wxAssignTempImpl(this, prefix, WXFILEARGS(fileTemp, NULL)); | |
1002 | } | |
1003 | ||
1004 | /* static */ | |
1005 | wxString | |
1006 | wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) | |
1007 | { | |
1008 | return wxCreateTempFileName(prefix, fileTemp); | |
1009 | } | |
1010 | ||
68351053 WS |
1011 | #endif // wxUSE_FILE |
1012 | ||
b70a2866 MW |
1013 | |
1014 | #if wxUSE_FFILE | |
1015 | ||
1016 | wxString wxCreateTempFileName(const wxString& prefix, | |
1017 | wxFFile *fileTemp, | |
1018 | bool *deleteOnClose) | |
1019 | { | |
1020 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), deleteOnClose); | |
1021 | } | |
1022 | ||
1023 | bool wxCreateTempFile(const wxString& prefix, | |
1024 | wxFFile *fileTemp, | |
1025 | wxString *name) | |
1026 | { | |
1027 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), name); | |
1028 | ||
1029 | } | |
1030 | ||
1031 | void wxFileName::AssignTempFileName(const wxString& prefix, wxFFile *fileTemp) | |
1032 | { | |
1033 | wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, fileTemp)); | |
1034 | } | |
1035 | ||
1036 | /* static */ | |
1037 | wxString | |
1038 | wxFileName::CreateTempFileName(const wxString& prefix, wxFFile *fileTemp) | |
1039 | { | |
1040 | return wxCreateTempFileName(prefix, fileTemp); | |
1041 | } | |
1042 | ||
1043 | #endif // wxUSE_FFILE | |
1044 | ||
1045 | ||
844f90fb VZ |
1046 | // ---------------------------------------------------------------------------- |
1047 | // directory operations | |
1048 | // ---------------------------------------------------------------------------- | |
1049 | ||
8758875e VZ |
1050 | // helper of GetTempDir(): check if the given directory exists and return it if |
1051 | // it does or an empty string otherwise | |
1052 | namespace | |
8d7d6dea | 1053 | { |
8d7d6dea | 1054 | |
8758875e VZ |
1055 | wxString CheckIfDirExists(const wxString& dir) |
1056 | { | |
1057 | return wxFileName::DirExists(dir) ? dir : wxString(); | |
1058 | } | |
1059 | ||
1060 | } // anonymous namespace | |
1061 | ||
1062 | wxString wxFileName::GetTempDir() | |
1063 | { | |
1064 | // first try getting it from environment: this allows overriding the values | |
1065 | // used by default if the user wants to create temporary files in another | |
1066 | // directory | |
1067 | wxString dir = CheckIfDirExists(wxGetenv("TMPDIR")); | |
1068 | if ( dir.empty() ) | |
8d7d6dea | 1069 | { |
8758875e VZ |
1070 | dir = CheckIfDirExists(wxGetenv("TMP")); |
1071 | if ( dir.empty() ) | |
1072 | dir = CheckIfDirExists(wxGetenv("TEMP")); | |
8d7d6dea | 1073 | } |
8d7d6dea | 1074 | |
8758875e | 1075 | // if no environment variables are set, use the system default |
8d7d6dea JS |
1076 | if ( dir.empty() ) |
1077 | { | |
8758875e VZ |
1078 | #if defined(__WXWINCE__) |
1079 | dir = CheckIfDirExists(wxT("\\temp")); | |
1080 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
8d7d6dea JS |
1081 | if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) ) |
1082 | { | |
9a83f860 | 1083 | wxLogLastError(wxT("GetTempPath")); |
8d7d6dea | 1084 | } |
8758875e VZ |
1085 | #elif defined(__WXMAC__) && wxOSX_USE_CARBON |
1086 | dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder); | |
1087 | #endif // systems with native way | |
8d7d6dea | 1088 | } |
8d7d6dea | 1089 | |
8758875e | 1090 | // fall back to hard coded value |
8d7d6dea JS |
1091 | if ( dir.empty() ) |
1092 | { | |
8758875e VZ |
1093 | #ifdef __UNIX_LIKE__ |
1094 | dir = CheckIfDirExists("/tmp"); | |
1095 | if ( dir.empty() ) | |
1096 | #endif // __UNIX_LIKE__ | |
1097 | dir = "."; | |
8d7d6dea | 1098 | } |
8d7d6dea JS |
1099 | |
1100 | return dir; | |
1101 | } | |
1102 | ||
89391a4e | 1103 | bool wxFileName::Mkdir( int perm, int flags ) const |
a35b27b1 | 1104 | { |
db759dde | 1105 | return wxFileName::Mkdir(GetPath(), perm, flags); |
a35b27b1 RR |
1106 | } |
1107 | ||
1527281e | 1108 | bool wxFileName::Mkdir( const wxString& dir, int perm, int flags ) |
df5ddbca | 1109 | { |
1527281e | 1110 | if ( flags & wxPATH_MKDIR_FULL ) |
f0ce3409 | 1111 | { |
1527281e VZ |
1112 | // split the path in components |
1113 | wxFileName filename; | |
1114 | filename.AssignDir(dir); | |
f0ce3409 | 1115 | |
f0ce3409 | 1116 | wxString currPath; |
0ea621cc VZ |
1117 | if ( filename.HasVolume()) |
1118 | { | |
1527281e | 1119 | currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE); |
0ea621cc | 1120 | } |
f0ce3409 | 1121 | |
1527281e VZ |
1122 | wxArrayString dirs = filename.GetDirs(); |
1123 | size_t count = dirs.GetCount(); | |
1124 | for ( size_t i = 0; i < count; i++ ) | |
1125 | { | |
e0d81eac | 1126 | if ( i > 0 || filename.IsAbsolute() ) |
f0ce3409 | 1127 | currPath += wxFILE_SEP_PATH; |
1527281e | 1128 | currPath += dirs[i]; |
f0ce3409 JS |
1129 | |
1130 | if (!DirExists(currPath)) | |
1527281e | 1131 | { |
f0ce3409 | 1132 | if (!wxMkdir(currPath, perm)) |
1527281e VZ |
1133 | { |
1134 | // no need to try creating further directories | |
f363e05c | 1135 | return false; |
1527281e VZ |
1136 | } |
1137 | } | |
f0ce3409 JS |
1138 | } |
1139 | ||
f363e05c | 1140 | return true; |
f0ce3409 JS |
1141 | |
1142 | } | |
1527281e VZ |
1143 | |
1144 | return ::wxMkdir( dir, perm ); | |
df5ddbca RR |
1145 | } |
1146 | ||
89391a4e | 1147 | bool wxFileName::Rmdir(int flags) const |
df5ddbca | 1148 | { |
110c5094 | 1149 | return wxFileName::Rmdir( GetPath(), flags ); |
df5ddbca RR |
1150 | } |
1151 | ||
110c5094 | 1152 | bool wxFileName::Rmdir(const wxString& dir, int flags) |
df5ddbca | 1153 | { |
110c5094 VZ |
1154 | #ifdef __WXMSW__ |
1155 | if ( flags & wxPATH_RMDIR_RECURSIVE ) | |
1156 | { | |
1157 | // SHFileOperation needs double null termination string | |
1158 | // but without separator at the end of the path | |
1159 | wxString path(dir); | |
1160 | if ( path.Last() == wxFILE_SEP_PATH ) | |
1161 | path.RemoveLast(); | |
9a83f860 | 1162 | path += wxT('\0'); |
110c5094 VZ |
1163 | |
1164 | SHFILEOPSTRUCT fileop; | |
1165 | wxZeroMemory(fileop); | |
1166 | fileop.wFunc = FO_DELETE; | |
1167 | fileop.pFrom = path.fn_str(); | |
1168 | fileop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; | |
1169 | #ifndef __WXWINCE__ | |
1170 | // FOF_NOERRORUI is not defined in WinCE | |
1171 | fileop.fFlags |= FOF_NOERRORUI; | |
1172 | #endif | |
1173 | ||
1174 | int ret = SHFileOperation(&fileop); | |
1175 | if ( ret != 0 ) | |
1176 | { | |
1177 | // SHFileOperation may return non-Win32 error codes, so the error | |
1178 | // message can be incorrect | |
9a83f860 | 1179 | wxLogApiError(wxT("SHFileOperation"), ret); |
110c5094 VZ |
1180 | return false; |
1181 | } | |
1182 | ||
1183 | return true; | |
1184 | } | |
1185 | else if ( flags & wxPATH_RMDIR_FULL ) | |
1186 | #else // !__WXMSW__ | |
1187 | if ( flags != 0 ) // wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE | |
1188 | #endif // !__WXMSW__ | |
1189 | { | |
1190 | wxString path(dir); | |
1191 | if ( path.Last() != wxFILE_SEP_PATH ) | |
1192 | path += wxFILE_SEP_PATH; | |
1193 | ||
1194 | wxDir d(path); | |
1195 | ||
1196 | if ( !d.IsOpened() ) | |
1197 | return false; | |
1198 | ||
1199 | wxString filename; | |
1200 | ||
1201 | // first delete all subdirectories | |
1202 | bool cont = d.GetFirst(&filename, "", wxDIR_DIRS | wxDIR_HIDDEN); | |
1203 | while ( cont ) | |
1204 | { | |
1205 | wxFileName::Rmdir(path + filename, flags); | |
1206 | cont = d.GetNext(&filename); | |
1207 | } | |
1208 | ||
1209 | #ifndef __WXMSW__ | |
1210 | if ( flags & wxPATH_RMDIR_RECURSIVE ) | |
1211 | { | |
1212 | // delete all files too | |
1213 | cont = d.GetFirst(&filename, "", wxDIR_FILES | wxDIR_HIDDEN); | |
1214 | while ( cont ) | |
1215 | { | |
1216 | ::wxRemoveFile(path + filename); | |
1217 | cont = d.GetNext(&filename); | |
1218 | } | |
1219 | } | |
1220 | #endif // !__WXMSW__ | |
1221 | } | |
1222 | ||
1223 | return ::wxRmdir(dir); | |
df5ddbca RR |
1224 | } |
1225 | ||
844f90fb VZ |
1226 | // ---------------------------------------------------------------------------- |
1227 | // path normalization | |
1228 | // ---------------------------------------------------------------------------- | |
1229 | ||
32a0d013 | 1230 | bool wxFileName::Normalize(int flags, |
844f90fb VZ |
1231 | const wxString& cwd, |
1232 | wxPathFormat format) | |
a35b27b1 | 1233 | { |
05e1201c VZ |
1234 | // deal with env vars renaming first as this may seriously change the path |
1235 | if ( flags & wxPATH_NORM_ENV_VARS ) | |
1236 | { | |
1237 | wxString pathOrig = GetFullPath(format); | |
1238 | wxString path = wxExpandEnvVars(pathOrig); | |
1239 | if ( path != pathOrig ) | |
1240 | { | |
1241 | Assign(path); | |
1242 | } | |
1243 | } | |
1244 | ||
844f90fb VZ |
1245 | // the existing path components |
1246 | wxArrayString dirs = GetDirs(); | |
1247 | ||
1248 | // the path to prepend in front to make the path absolute | |
1249 | wxFileName curDir; | |
1250 | ||
1251 | format = GetFormat(format); | |
ae4d7004 | 1252 | |
bf7f7793 | 1253 | // set up the directory to use for making the path absolute later |
a2fa5040 | 1254 | if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) ) |
a35b27b1 | 1255 | { |
844f90fb | 1256 | if ( cwd.empty() ) |
6f91bc33 VZ |
1257 | { |
1258 | curDir.AssignCwd(GetVolume()); | |
1259 | } | |
1260 | else // cwd provided | |
1261 | { | |
844f90fb | 1262 | curDir.AssignDir(cwd); |
6f91bc33 | 1263 | } |
a35b27b1 | 1264 | } |
ae4d7004 | 1265 | |
844f90fb | 1266 | // handle ~ stuff under Unix only |
be5be16a | 1267 | if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) && m_relative ) |
a35b27b1 | 1268 | { |
844f90fb VZ |
1269 | if ( !dirs.IsEmpty() ) |
1270 | { | |
1271 | wxString dir = dirs[0u]; | |
9a83f860 | 1272 | if ( !dir.empty() && dir[0u] == wxT('~') ) |
844f90fb | 1273 | { |
bf7f7793 | 1274 | // to make the path absolute use the home directory |
844f90fb | 1275 | curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); |
da2fd5ac | 1276 | dirs.RemoveAt(0u); |
844f90fb VZ |
1277 | } |
1278 | } | |
a35b27b1 | 1279 | } |
844f90fb | 1280 | |
52dbd056 | 1281 | // transform relative path into abs one |
844f90fb | 1282 | if ( curDir.IsOk() ) |
a35b27b1 | 1283 | { |
bf7f7793 RR |
1284 | // this path may be relative because it doesn't have the volume name |
1285 | // and still have m_relative=true; in this case we shouldn't modify | |
1286 | // our directory components but just set the current volume | |
1287 | if ( !HasVolume() && curDir.HasVolume() ) | |
844f90fb | 1288 | { |
bf7f7793 RR |
1289 | SetVolume(curDir.GetVolume()); |
1290 | ||
1291 | if ( !m_relative ) | |
1292 | { | |
1293 | // yes, it was the case - we don't need curDir then | |
1294 | curDir.Clear(); | |
1295 | } | |
844f90fb VZ |
1296 | } |
1297 | ||
bf7f7793 RR |
1298 | // finally, prepend curDir to the dirs array |
1299 | wxArrayString dirsNew = curDir.GetDirs(); | |
1300 | WX_PREPEND_ARRAY(dirs, dirsNew); | |
1301 | ||
1302 | // if we used e.g. tilde expansion previously and wxGetUserHome didn't | |
1303 | // return for some reason an absolute path, then curDir maybe not be absolute! | |
be5be16a | 1304 | if ( !curDir.m_relative ) |
bf7f7793 RR |
1305 | { |
1306 | // we have prepended an absolute path and thus we are now an absolute | |
1307 | // file name too | |
1308 | m_relative = false; | |
1309 | } | |
1310 | // else if (flags & wxPATH_NORM_ABSOLUTE): | |
1311 | // should we warn the user that we didn't manage to make the path absolute? | |
a35b27b1 | 1312 | } |
844f90fb VZ |
1313 | |
1314 | // now deal with ".", ".." and the rest | |
1315 | m_dirs.Empty(); | |
1316 | size_t count = dirs.GetCount(); | |
1317 | for ( size_t n = 0; n < count; n++ ) | |
a35b27b1 | 1318 | { |
844f90fb VZ |
1319 | wxString dir = dirs[n]; |
1320 | ||
2bc60417 | 1321 | if ( flags & wxPATH_NORM_DOTS ) |
844f90fb VZ |
1322 | { |
1323 | if ( dir == wxT(".") ) | |
1324 | { | |
1325 | // just ignore | |
1326 | continue; | |
1327 | } | |
1328 | ||
1329 | if ( dir == wxT("..") ) | |
1330 | { | |
1331 | if ( m_dirs.IsEmpty() ) | |
1332 | { | |
1333 | wxLogError(_("The path '%s' contains too many \"..\"!"), | |
1334 | GetFullPath().c_str()); | |
f363e05c | 1335 | return false; |
844f90fb VZ |
1336 | } |
1337 | ||
ae4d7004 | 1338 | m_dirs.RemoveAt(m_dirs.GetCount() - 1); |
844f90fb VZ |
1339 | continue; |
1340 | } | |
1341 | } | |
1342 | ||
844f90fb | 1343 | m_dirs.Add(dir); |
a35b27b1 | 1344 | } |
a62848fd | 1345 | |
6bda7391 | 1346 | #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE |
21f60945 JS |
1347 | if ( (flags & wxPATH_NORM_SHORTCUT) ) |
1348 | { | |
1349 | wxString filename; | |
1350 | if (GetShortcutTarget(GetFullPath(format), filename)) | |
1351 | { | |
21f60945 JS |
1352 | m_relative = false; |
1353 | Assign(filename); | |
1354 | } | |
1355 | } | |
1356 | #endif | |
844f90fb | 1357 | |
9a0c5c01 VZ |
1358 | #if defined(__WIN32__) |
1359 | if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) ) | |
844f90fb | 1360 | { |
9a0c5c01 VZ |
1361 | Assign(GetLongPath()); |
1362 | } | |
1363 | #endif // Win32 | |
844f90fb | 1364 | |
9a0c5c01 VZ |
1365 | // Change case (this should be kept at the end of the function, to ensure |
1366 | // that the path doesn't change any more after we normalize its case) | |
1367 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
1368 | { | |
55268a3a | 1369 | m_volume.MakeLower(); |
844f90fb VZ |
1370 | m_name.MakeLower(); |
1371 | m_ext.MakeLower(); | |
844f90fb | 1372 | |
9a0c5c01 VZ |
1373 | // directory entries must be made lower case as well |
1374 | count = m_dirs.GetCount(); | |
1375 | for ( size_t i = 0; i < count; i++ ) | |
1376 | { | |
1377 | m_dirs[i].MakeLower(); | |
1378 | } | |
9e9b65c1 | 1379 | } |
9e9b65c1 | 1380 | |
f363e05c | 1381 | return true; |
a2fa5040 VZ |
1382 | } |
1383 | ||
395f3aa8 FM |
1384 | #ifndef __WXWINCE__ |
1385 | bool wxFileName::ReplaceEnvVariable(const wxString& envname, | |
1386 | const wxString& replacementFmtString, | |
1387 | wxPathFormat format) | |
1388 | { | |
1389 | // look into stringForm for the contents of the given environment variable | |
1390 | wxString val; | |
1391 | if (envname.empty() || | |
1392 | !wxGetEnv(envname, &val)) | |
1393 | return false; | |
1394 | if (val.empty()) | |
1395 | return false; | |
1396 | ||
1397 | wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); | |
1398 | // do not touch the file name and the extension | |
1399 | ||
1400 | wxString replacement = wxString::Format(replacementFmtString, envname); | |
1401 | stringForm.Replace(val, replacement); | |
1402 | ||
1403 | // Now assign ourselves the modified path: | |
1404 | Assign(stringForm, GetFullName(), format); | |
1405 | ||
1406 | return true; | |
1407 | } | |
1408 | #endif | |
1409 | ||
1410 | bool wxFileName::ReplaceHomeDir(wxPathFormat format) | |
1411 | { | |
1412 | wxString homedir = wxGetHomeDir(); | |
1413 | if (homedir.empty()) | |
1414 | return false; | |
1415 | ||
1416 | wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); | |
1417 | // do not touch the file name and the extension | |
1418 | ||
1419 | stringForm.Replace(homedir, "~"); | |
1420 | ||
1421 | // Now assign ourselves the modified path: | |
1422 | Assign(stringForm, GetFullName(), format); | |
1423 | ||
1424 | return true; | |
1425 | } | |
1426 | ||
21f60945 JS |
1427 | // ---------------------------------------------------------------------------- |
1428 | // get the shortcut target | |
1429 | // ---------------------------------------------------------------------------- | |
1430 | ||
5671b3b6 JS |
1431 | // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions. |
1432 | // The .lnk file is a plain text file so it should be easy to | |
1433 | // make it work. Hint from Google Groups: | |
1434 | // "If you open up a lnk file, you'll see a | |
1435 | // number, followed by a pound sign (#), followed by more text. The | |
1436 | // number is the number of characters that follows the pound sign. The | |
1437 | // characters after the pound sign are the command line (which _can_ | |
1438 | // include arguments) to be executed. Any path (e.g. \windows\program | |
1439 | // files\myapp.exe) that includes spaces needs to be enclosed in | |
1440 | // quotation marks." | |
1441 | ||
6bda7391 | 1442 | #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE |
5671b3b6 JS |
1443 | // The following lines are necessary under WinCE |
1444 | // #include "wx/msw/private.h" | |
1445 | // #include <ole2.h> | |
21f60945 | 1446 | #include <shlobj.h> |
5671b3b6 JS |
1447 | #if defined(__WXWINCE__) |
1448 | #include <shlguid.h> | |
1449 | #endif | |
21f60945 | 1450 | |
cea0869c VZ |
1451 | bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, |
1452 | wxString& targetFilename, | |
d9e80dce | 1453 | wxString* arguments) const |
21f60945 | 1454 | { |
21f60945 | 1455 | wxString path, file, ext; |
bd365871 | 1456 | wxFileName::SplitPath(shortcutPath, & path, & file, & ext); |
a62848fd WS |
1457 | |
1458 | HRESULT hres; | |
1459 | IShellLink* psl; | |
1460 | bool success = false; | |
21f60945 JS |
1461 | |
1462 | // Assume it's not a shortcut if it doesn't end with lnk | |
489f6cf7 | 1463 | if (ext.CmpNoCase(wxT("lnk"))!=0) |
a62848fd WS |
1464 | return false; |
1465 | ||
1466 | // create a ShellLink object | |
1467 | hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, | |
1468 | IID_IShellLink, (LPVOID*) &psl); | |
1469 | ||
1470 | if (SUCCEEDED(hres)) | |
1471 | { | |
1472 | IPersistFile* ppf; | |
1473 | hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf); | |
1474 | if (SUCCEEDED(hres)) | |
1475 | { | |
1476 | WCHAR wsz[MAX_PATH]; | |
1477 | ||
1478 | MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz, | |
1479 | MAX_PATH); | |
1480 | ||
1481 | hres = ppf->Load(wsz, 0); | |
cea0869c VZ |
1482 | ppf->Release(); |
1483 | ||
a62848fd WS |
1484 | if (SUCCEEDED(hres)) |
1485 | { | |
21f60945 | 1486 | wxChar buf[2048]; |
59ba321b JS |
1487 | // Wrong prototype in early versions |
1488 | #if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2) | |
1489 | psl->GetPath((CHAR*) buf, 2048, NULL, SLGP_UNCPRIORITY); | |
1490 | #else | |
a62848fd | 1491 | psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY); |
59ba321b | 1492 | #endif |
a62848fd | 1493 | targetFilename = wxString(buf); |
21f60945 JS |
1494 | success = (shortcutPath != targetFilename); |
1495 | ||
a62848fd | 1496 | psl->GetArguments(buf, 2048); |
21f60945 | 1497 | wxString args(buf); |
b494c48b | 1498 | if (!args.empty() && arguments) |
21f60945 JS |
1499 | { |
1500 | *arguments = args; | |
a62848fd WS |
1501 | } |
1502 | } | |
1503 | } | |
cea0869c VZ |
1504 | |
1505 | psl->Release(); | |
a62848fd | 1506 | } |
a62848fd | 1507 | return success; |
21f60945 | 1508 | } |
cea0869c VZ |
1509 | |
1510 | #endif // __WIN32__ && !__WXWINCE__ | |
21f60945 JS |
1511 | |
1512 | ||
a2fa5040 VZ |
1513 | // ---------------------------------------------------------------------------- |
1514 | // absolute/relative paths | |
1515 | // ---------------------------------------------------------------------------- | |
1516 | ||
1517 | bool wxFileName::IsAbsolute(wxPathFormat format) const | |
1518 | { | |
be5be16a VZ |
1519 | // unix paths beginning with ~ are reported as being absolute |
1520 | if ( format == wxPATH_UNIX ) | |
1521 | { | |
1522 | if ( !m_dirs.IsEmpty() ) | |
1523 | { | |
1524 | wxString dir = m_dirs[0u]; | |
1525 | ||
9a83f860 | 1526 | if (!dir.empty() && dir[0u] == wxT('~')) |
be5be16a VZ |
1527 | return true; |
1528 | } | |
1529 | } | |
1530 | ||
a2fa5040 VZ |
1531 | // if our path doesn't start with a path separator, it's not an absolute |
1532 | // path | |
1533 | if ( m_relative ) | |
f363e05c | 1534 | return false; |
a2fa5040 VZ |
1535 | |
1536 | if ( !GetVolumeSeparator(format).empty() ) | |
1537 | { | |
1538 | // this format has volumes and an absolute path must have one, it's not | |
be5be16a | 1539 | // enough to have the full path to be an absolute file under Windows |
a2fa5040 | 1540 | if ( GetVolume().empty() ) |
f363e05c | 1541 | return false; |
a2fa5040 VZ |
1542 | } |
1543 | ||
f363e05c | 1544 | return true; |
a35b27b1 RR |
1545 | } |
1546 | ||
f7d886af VZ |
1547 | bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) |
1548 | { | |
cef9731a | 1549 | wxFileName fnBase = wxFileName::DirName(pathBase, format); |
f7d886af VZ |
1550 | |
1551 | // get cwd only once - small time saving | |
1552 | wxString cwd = wxGetCwd(); | |
b5b62eea JS |
1553 | Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); |
1554 | fnBase.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); | |
f7d886af VZ |
1555 | |
1556 | bool withCase = IsCaseSensitive(format); | |
1557 | ||
1558 | // we can't do anything if the files live on different volumes | |
1559 | if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) ) | |
1560 | { | |
1561 | // nothing done | |
f363e05c | 1562 | return false; |
f7d886af VZ |
1563 | } |
1564 | ||
1565 | // same drive, so we don't need our volume | |
1566 | m_volume.clear(); | |
1567 | ||
1568 | // remove common directories starting at the top | |
1569 | while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() && | |
1570 | m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) ) | |
1571 | { | |
ae4d7004 VZ |
1572 | m_dirs.RemoveAt(0); |
1573 | fnBase.m_dirs.RemoveAt(0); | |
f7d886af VZ |
1574 | } |
1575 | ||
1576 | // add as many ".." as needed | |
1577 | size_t count = fnBase.m_dirs.GetCount(); | |
1578 | for ( size_t i = 0; i < count; i++ ) | |
1579 | { | |
1580 | m_dirs.Insert(wxT(".."), 0u); | |
1581 | } | |
90a68369 | 1582 | |
2db991f4 VZ |
1583 | if ( format == wxPATH_UNIX || format == wxPATH_DOS ) |
1584 | { | |
1585 | // a directory made relative with respect to itself is '.' under Unix | |
1586 | // and DOS, by definition (but we don't have to insert "./" for the | |
1587 | // files) | |
1588 | if ( m_dirs.IsEmpty() && IsDir() ) | |
1589 | { | |
9a83f860 | 1590 | m_dirs.Add(wxT('.')); |
0ea621cc | 1591 | } |
2db991f4 VZ |
1592 | } |
1593 | ||
f363e05c | 1594 | m_relative = true; |
f7d886af VZ |
1595 | |
1596 | // we were modified | |
f363e05c | 1597 | return true; |
f7d886af VZ |
1598 | } |
1599 | ||
844f90fb VZ |
1600 | // ---------------------------------------------------------------------------- |
1601 | // filename kind tests | |
1602 | // ---------------------------------------------------------------------------- | |
1603 | ||
2b5f62a0 | 1604 | bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const |
df5ddbca | 1605 | { |
844f90fb VZ |
1606 | wxFileName fn1 = *this, |
1607 | fn2 = filepath; | |
1608 | ||
1609 | // get cwd only once - small time saving | |
1610 | wxString cwd = wxGetCwd(); | |
55268a3a VZ |
1611 | fn1.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format); |
1612 | fn2.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format); | |
844f90fb VZ |
1613 | |
1614 | if ( fn1.GetFullPath() == fn2.GetFullPath() ) | |
f363e05c | 1615 | return true; |
844f90fb VZ |
1616 | |
1617 | // TODO: compare inodes for Unix, this works even when filenames are | |
1618 | // different but files are the same (symlinks) (VZ) | |
1619 | ||
f363e05c | 1620 | return false; |
df5ddbca RR |
1621 | } |
1622 | ||
844f90fb | 1623 | /* static */ |
df5ddbca RR |
1624 | bool wxFileName::IsCaseSensitive( wxPathFormat format ) |
1625 | { | |
04c943b1 VZ |
1626 | // only Unix filenames are truely case-sensitive |
1627 | return GetFormat(format) == wxPATH_UNIX; | |
df5ddbca RR |
1628 | } |
1629 | ||
f363e05c VZ |
1630 | /* static */ |
1631 | wxString wxFileName::GetForbiddenChars(wxPathFormat format) | |
1632 | { | |
1633 | // Inits to forbidden characters that are common to (almost) all platforms. | |
1634 | wxString strForbiddenChars = wxT("*?"); | |
1635 | ||
1c6f2414 | 1636 | // If asserts, wxPathFormat has been changed. In case of a new path format |
f363e05c | 1637 | // addition, the following code might have to be updated. |
1c6f2414 | 1638 | wxCOMPILE_TIME_ASSERT(wxPATH_MAX == 5, wxPathFormatChanged); |
f363e05c VZ |
1639 | switch ( GetFormat(format) ) |
1640 | { | |
1641 | default : | |
1642 | wxFAIL_MSG( wxT("Unknown path format") ); | |
1643 | // !! Fall through !! | |
1644 | ||
1645 | case wxPATH_UNIX: | |
1646 | break; | |
1647 | ||
1648 | case wxPATH_MAC: | |
1649 | // On a Mac even names with * and ? are allowed (Tested with OS | |
1650 | // 9.2.1 and OS X 10.2.5) | |
1651 | strForbiddenChars = wxEmptyString; | |
1652 | break; | |
1653 | ||
1654 | case wxPATH_DOS: | |
1655 | strForbiddenChars += wxT("\\/:\"<>|"); | |
1656 | break; | |
1657 | ||
1658 | case wxPATH_VMS: | |
1659 | break; | |
1660 | } | |
1661 | ||
1662 | return strForbiddenChars; | |
1663 | } | |
1664 | ||
04c943b1 | 1665 | /* static */ |
bcfa2b31 | 1666 | wxString wxFileName::GetVolumeSeparator(wxPathFormat WXUNUSED_IN_WINCE(format)) |
844f90fb | 1667 | { |
bcfa2b31 WS |
1668 | #ifdef __WXWINCE__ |
1669 | return wxEmptyString; | |
1670 | #else | |
04c943b1 VZ |
1671 | wxString sepVol; |
1672 | ||
a385b5df RR |
1673 | if ( (GetFormat(format) == wxPATH_DOS) || |
1674 | (GetFormat(format) == wxPATH_VMS) ) | |
04c943b1 | 1675 | { |
04c943b1 VZ |
1676 | sepVol = wxFILE_SEP_DSK; |
1677 | } | |
a385b5df | 1678 | //else: leave empty |
04c943b1 VZ |
1679 | |
1680 | return sepVol; | |
bcfa2b31 | 1681 | #endif |
844f90fb VZ |
1682 | } |
1683 | ||
1684 | /* static */ | |
1685 | wxString wxFileName::GetPathSeparators(wxPathFormat format) | |
1686 | { | |
1687 | wxString seps; | |
1688 | switch ( GetFormat(format) ) | |
df5ddbca | 1689 | { |
844f90fb | 1690 | case wxPATH_DOS: |
04c943b1 VZ |
1691 | // accept both as native APIs do but put the native one first as |
1692 | // this is the one we use in GetFullPath() | |
1693 | seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX; | |
844f90fb VZ |
1694 | break; |
1695 | ||
1696 | default: | |
9a83f860 | 1697 | wxFAIL_MSG( wxT("Unknown wxPATH_XXX style") ); |
844f90fb VZ |
1698 | // fall through |
1699 | ||
1700 | case wxPATH_UNIX: | |
9e8d8607 | 1701 | seps = wxFILE_SEP_PATH_UNIX; |
844f90fb VZ |
1702 | break; |
1703 | ||
1704 | case wxPATH_MAC: | |
9e8d8607 | 1705 | seps = wxFILE_SEP_PATH_MAC; |
844f90fb | 1706 | break; |
04c943b1 | 1707 | |
3c621059 JJ |
1708 | case wxPATH_VMS: |
1709 | seps = wxFILE_SEP_PATH_VMS; | |
1710 | break; | |
df5ddbca RR |
1711 | } |
1712 | ||
844f90fb | 1713 | return seps; |
df5ddbca RR |
1714 | } |
1715 | ||
f1e77933 VZ |
1716 | /* static */ |
1717 | wxString wxFileName::GetPathTerminators(wxPathFormat format) | |
1718 | { | |
1719 | format = GetFormat(format); | |
1720 | ||
1721 | // under VMS the end of the path is ']', not the path separator used to | |
1722 | // separate the components | |
9a83f860 | 1723 | return format == wxPATH_VMS ? wxString(wxT(']')) : GetPathSeparators(format); |
f1e77933 VZ |
1724 | } |
1725 | ||
844f90fb VZ |
1726 | /* static */ |
1727 | bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) | |
df5ddbca | 1728 | { |
04c943b1 | 1729 | // wxString::Find() doesn't work as expected with NUL - it will always find |
2fb5a4ce | 1730 | // it, so test for it separately |
9a83f860 | 1731 | return ch != wxT('\0') && GetPathSeparators(format).Find(ch) != wxNOT_FOUND; |
df5ddbca RR |
1732 | } |
1733 | ||
844f90fb VZ |
1734 | // ---------------------------------------------------------------------------- |
1735 | // path components manipulation | |
1736 | // ---------------------------------------------------------------------------- | |
1737 | ||
5bb9aeb2 VZ |
1738 | /* static */ bool wxFileName::IsValidDirComponent(const wxString& dir) |
1739 | { | |
1740 | if ( dir.empty() ) | |
1741 | { | |
9a83f860 | 1742 | wxFAIL_MSG( wxT("empty directory passed to wxFileName::InsertDir()") ); |
5bb9aeb2 VZ |
1743 | |
1744 | return false; | |
1745 | } | |
1746 | ||
1747 | const size_t len = dir.length(); | |
1748 | for ( size_t n = 0; n < len; n++ ) | |
1749 | { | |
1750 | if ( dir[n] == GetVolumeSeparator() || IsPathSeparator(dir[n]) ) | |
1751 | { | |
9a83f860 | 1752 | wxFAIL_MSG( wxT("invalid directory component in wxFileName") ); |
5bb9aeb2 VZ |
1753 | |
1754 | return false; | |
1755 | } | |
1756 | } | |
1757 | ||
1758 | return true; | |
1759 | } | |
1760 | ||
2458d90b | 1761 | void wxFileName::AppendDir( const wxString& dir ) |
df5ddbca | 1762 | { |
5bb9aeb2 VZ |
1763 | if ( IsValidDirComponent(dir) ) |
1764 | m_dirs.Add( dir ); | |
df5ddbca RR |
1765 | } |
1766 | ||
2458d90b | 1767 | void wxFileName::PrependDir( const wxString& dir ) |
df5ddbca | 1768 | { |
5bb9aeb2 | 1769 | InsertDir(0, dir); |
df5ddbca RR |
1770 | } |
1771 | ||
2458d90b | 1772 | void wxFileName::InsertDir(size_t before, const wxString& dir) |
df5ddbca | 1773 | { |
5bb9aeb2 | 1774 | if ( IsValidDirComponent(dir) ) |
2458d90b | 1775 | m_dirs.Insert(dir, before); |
df5ddbca RR |
1776 | } |
1777 | ||
2458d90b | 1778 | void wxFileName::RemoveDir(size_t pos) |
df5ddbca | 1779 | { |
2458d90b | 1780 | m_dirs.RemoveAt(pos); |
df5ddbca RR |
1781 | } |
1782 | ||
844f90fb VZ |
1783 | // ---------------------------------------------------------------------------- |
1784 | // accessors | |
1785 | // ---------------------------------------------------------------------------- | |
1786 | ||
7124df9b VZ |
1787 | void wxFileName::SetFullName(const wxString& fullname) |
1788 | { | |
dfecbee5 VZ |
1789 | SplitPath(fullname, NULL /* no volume */, NULL /* no path */, |
1790 | &m_name, &m_ext, &m_hasExt); | |
7124df9b VZ |
1791 | } |
1792 | ||
844f90fb | 1793 | wxString wxFileName::GetFullName() const |
a35b27b1 | 1794 | { |
844f90fb | 1795 | wxString fullname = m_name; |
dfecbee5 | 1796 | if ( m_hasExt ) |
a35b27b1 | 1797 | { |
9e8d8607 | 1798 | fullname << wxFILE_SEP_EXT << m_ext; |
a35b27b1 | 1799 | } |
a35b27b1 | 1800 | |
844f90fb | 1801 | return fullname; |
a35b27b1 RR |
1802 | } |
1803 | ||
33b97389 | 1804 | wxString wxFileName::GetPath( int flags, wxPathFormat format ) const |
df5ddbca RR |
1805 | { |
1806 | format = GetFormat( format ); | |
844f90fb | 1807 | |
353f41cb RR |
1808 | wxString fullpath; |
1809 | ||
33b97389 VZ |
1810 | // return the volume with the path as well if requested |
1811 | if ( flags & wxPATH_GET_VOLUME ) | |
1812 | { | |
1813 | fullpath += wxGetVolumeString(GetVolume(), format); | |
1814 | } | |
1815 | ||
034742fc VZ |
1816 | // the leading character |
1817 | switch ( format ) | |
1818 | { | |
1819 | case wxPATH_MAC: | |
1820 | if ( m_relative ) | |
1821 | fullpath += wxFILE_SEP_PATH_MAC; | |
1822 | break; | |
1823 | ||
1824 | case wxPATH_DOS: | |
1825 | if ( !m_relative ) | |
1826 | fullpath += wxFILE_SEP_PATH_DOS; | |
1827 | break; | |
1828 | ||
1829 | default: | |
1830 | wxFAIL_MSG( wxT("Unknown path format") ); | |
1831 | // fall through | |
1832 | ||
1833 | case wxPATH_UNIX: | |
1834 | if ( !m_relative ) | |
1835 | { | |
be5be16a | 1836 | fullpath += wxFILE_SEP_PATH_UNIX; |
034742fc VZ |
1837 | } |
1838 | break; | |
1839 | ||
1840 | case wxPATH_VMS: | |
1841 | // no leading character here but use this place to unset | |
1842 | // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense | |
1843 | // as, if I understand correctly, there should never be a dot | |
1844 | // before the closing bracket | |
1845 | flags &= ~wxPATH_GET_SEPARATOR; | |
1846 | } | |
1847 | ||
1848 | if ( m_dirs.empty() ) | |
1849 | { | |
1850 | // there is nothing more | |
1851 | return fullpath; | |
1852 | } | |
1853 | ||
1854 | // then concatenate all the path components using the path separator | |
1855 | if ( format == wxPATH_VMS ) | |
1856 | { | |
1857 | fullpath += wxT('['); | |
1858 | } | |
1859 | ||
5bb9aeb2 | 1860 | const size_t dirCount = m_dirs.GetCount(); |
034742fc | 1861 | for ( size_t i = 0; i < dirCount; i++ ) |
353f41cb | 1862 | { |
034742fc | 1863 | switch (format) |
5bb9aeb2 VZ |
1864 | { |
1865 | case wxPATH_MAC: | |
034742fc VZ |
1866 | if ( m_dirs[i] == wxT(".") ) |
1867 | { | |
1868 | // skip appending ':', this shouldn't be done in this | |
1869 | // case as "::" is interpreted as ".." under Unix | |
1870 | continue; | |
1871 | } | |
2361ce82 | 1872 | |
034742fc | 1873 | // convert back from ".." to nothing |
489f6cf7 | 1874 | if ( !m_dirs[i].IsSameAs(wxT("..")) ) |
034742fc | 1875 | fullpath += m_dirs[i]; |
5bb9aeb2 | 1876 | break; |
2361ce82 | 1877 | |
5bb9aeb2 | 1878 | default: |
034742fc VZ |
1879 | wxFAIL_MSG( wxT("Unexpected path format") ); |
1880 | // still fall through | |
2361ce82 | 1881 | |
034742fc | 1882 | case wxPATH_DOS: |
5bb9aeb2 | 1883 | case wxPATH_UNIX: |
034742fc | 1884 | fullpath += m_dirs[i]; |
5bb9aeb2 | 1885 | break; |
2361ce82 | 1886 | |
5bb9aeb2 | 1887 | case wxPATH_VMS: |
034742fc | 1888 | // TODO: What to do with ".." under VMS |
353f41cb | 1889 | |
034742fc | 1890 | // convert back from ".." to nothing |
489f6cf7 | 1891 | if ( !m_dirs[i].IsSameAs(wxT("..")) ) |
353f41cb | 1892 | fullpath += m_dirs[i]; |
034742fc | 1893 | break; |
4175794e VZ |
1894 | } |
1895 | ||
034742fc VZ |
1896 | if ( (flags & wxPATH_GET_SEPARATOR) || (i != dirCount - 1) ) |
1897 | fullpath += GetPathSeparator(format); | |
df5ddbca | 1898 | } |
034742fc VZ |
1899 | |
1900 | if ( format == wxPATH_VMS ) | |
5bb9aeb2 | 1901 | { |
034742fc | 1902 | fullpath += wxT(']'); |
5bb9aeb2 | 1903 | } |
844f90fb | 1904 | |
353f41cb | 1905 | return fullpath; |
df5ddbca RR |
1906 | } |
1907 | ||
1908 | wxString wxFileName::GetFullPath( wxPathFormat format ) const | |
1909 | { | |
4175794e VZ |
1910 | // we already have a function to get the path |
1911 | wxString fullpath = GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR, | |
1912 | format); | |
04c943b1 | 1913 | |
4175794e | 1914 | // now just add the file name and extension to it |
04c943b1 VZ |
1915 | fullpath += GetFullName(); |
1916 | ||
1917 | return fullpath; | |
df5ddbca RR |
1918 | } |
1919 | ||
9e9b65c1 JS |
1920 | // Return the short form of the path (returns identity on non-Windows platforms) |
1921 | wxString wxFileName::GetShortPath() const | |
1922 | { | |
9e9b65c1 | 1923 | wxString path(GetFullPath()); |
2f3d9587 VZ |
1924 | |
1925 | #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) | |
e0a050e3 | 1926 | DWORD sz = ::GetShortPathName(path.fn_str(), NULL, 0); |
2f3d9587 | 1927 | if ( sz != 0 ) |
9e9b65c1 | 1928 | { |
2f3d9587 VZ |
1929 | wxString pathOut; |
1930 | if ( ::GetShortPathName | |
75ef5722 | 1931 | ( |
e0a050e3 | 1932 | path.fn_str(), |
de564874 | 1933 | wxStringBuffer(pathOut, sz), |
75ef5722 | 1934 | sz |
2f3d9587 VZ |
1935 | ) != 0 ) |
1936 | { | |
1937 | return pathOut; | |
1938 | } | |
9e9b65c1 | 1939 | } |
2f3d9587 | 1940 | #endif // Windows |
5716a1ab VZ |
1941 | |
1942 | return path; | |
9e9b65c1 JS |
1943 | } |
1944 | ||
1945 | // Return the long form of the path (returns identity on non-Windows platforms) | |
1946 | wxString wxFileName::GetLongPath() const | |
1947 | { | |
52dbd056 VZ |
1948 | wxString pathOut, |
1949 | path = GetFullPath(); | |
1950 | ||
b517351b | 1951 | #if defined(__WIN32__) && !defined(__WXWINCE__) && !defined(__WXMICROWIN__) |
05e7001c | 1952 | |
0e207280 | 1953 | #if wxUSE_DYNLIB_CLASS |
62dcaed6 | 1954 | typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); |
05e7001c | 1955 | |
2f3d9587 VZ |
1956 | // this is MT-safe as in the worst case we're going to resolve the function |
1957 | // twice -- but as the result is the same in both threads, it's ok | |
1958 | static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; | |
1959 | if ( !s_pfnGetLongPathName ) | |
9e9b65c1 | 1960 | { |
2f3d9587 | 1961 | static bool s_triedToLoad = false; |
2361ce82 | 1962 | |
2f3d9587 | 1963 | if ( !s_triedToLoad ) |
05e7001c | 1964 | { |
2f3d9587 VZ |
1965 | s_triedToLoad = true; |
1966 | ||
9a83f860 | 1967 | wxDynamicLibrary dllKernel(wxT("kernel32")); |
2f3d9587 | 1968 | |
9a83f860 | 1969 | const wxChar* GetLongPathName = wxT("GetLongPathName") |
4377d3ab | 1970 | #if wxUSE_UNICODE |
9a83f860 | 1971 | wxT("W"); |
4377d3ab | 1972 | #else // ANSI |
9a83f860 | 1973 | wxT("A"); |
4377d3ab WS |
1974 | #endif // Unicode/ANSI |
1975 | ||
1976 | if ( dllKernel.HasSymbol(GetLongPathName) ) | |
05e7001c | 1977 | { |
2f3d9587 | 1978 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) |
4377d3ab | 1979 | dllKernel.GetSymbol(GetLongPathName); |
05e7001c | 1980 | } |
2f3d9587 VZ |
1981 | |
1982 | // note that kernel32.dll can be unloaded, it stays in memory | |
1983 | // anyhow as all Win32 programs link to it and so it's safe to call | |
1984 | // GetLongPathName() even after unloading it | |
05e7001c | 1985 | } |
9e9b65c1 | 1986 | } |
2361ce82 | 1987 | |
2f3d9587 VZ |
1988 | if ( s_pfnGetLongPathName ) |
1989 | { | |
e0a050e3 | 1990 | DWORD dwSize = (*s_pfnGetLongPathName)(path.fn_str(), NULL, 0); |
2f3d9587 VZ |
1991 | if ( dwSize > 0 ) |
1992 | { | |
1993 | if ( (*s_pfnGetLongPathName) | |
1994 | ( | |
e0a050e3 | 1995 | path.fn_str(), |
2f3d9587 VZ |
1996 | wxStringBuffer(pathOut, dwSize), |
1997 | dwSize | |
1998 | ) != 0 ) | |
1999 | { | |
2000 | return pathOut; | |
2001 | } | |
2002 | } | |
2003 | } | |
0e207280 | 2004 | #endif // wxUSE_DYNLIB_CLASS |
05e7001c | 2005 | |
2f3d9587 VZ |
2006 | // The OS didn't support GetLongPathName, or some other error. |
2007 | // We need to call FindFirstFile on each component in turn. | |
05e7001c | 2008 | |
2f3d9587 VZ |
2009 | WIN32_FIND_DATA findFileData; |
2010 | HANDLE hFind; | |
b5b62eea | 2011 | |
2f3d9587 VZ |
2012 | if ( HasVolume() ) |
2013 | pathOut = GetVolume() + | |
2014 | GetVolumeSeparator(wxPATH_DOS) + | |
2015 | GetPathSeparator(wxPATH_DOS); | |
2016 | else | |
2017 | pathOut = wxEmptyString; | |
05e7001c | 2018 | |
2f3d9587 VZ |
2019 | wxArrayString dirs = GetDirs(); |
2020 | dirs.Add(GetFullName()); | |
77fe02a8 | 2021 | |
2f3d9587 | 2022 | wxString tmpPath; |
5d978d07 | 2023 | |
2f3d9587 VZ |
2024 | size_t count = dirs.GetCount(); |
2025 | for ( size_t i = 0; i < count; i++ ) | |
2026 | { | |
ea6319cb VZ |
2027 | const wxString& dir = dirs[i]; |
2028 | ||
2f3d9587 VZ |
2029 | // We're using pathOut to collect the long-name path, but using a |
2030 | // temporary for appending the last path component which may be | |
2031 | // short-name | |
ea6319cb VZ |
2032 | tmpPath = pathOut + dir; |
2033 | ||
2034 | // We must not process "." or ".." here as they would be (unexpectedly) | |
2035 | // replaced by the corresponding directory names so just leave them | |
2036 | // alone | |
2037 | // | |
2038 | // And we can't pass a drive and root dir to FindFirstFile (VZ: why?) | |
2039 | if ( tmpPath.empty() || dir == '.' || dir == ".." || | |
2040 | tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) ) | |
2f3d9587 | 2041 | { |
2f3d9587 VZ |
2042 | tmpPath += wxFILE_SEP_PATH; |
2043 | pathOut = tmpPath; | |
2044 | continue; | |
2045 | } | |
05e7001c | 2046 | |
e0a050e3 | 2047 | hFind = ::FindFirstFile(tmpPath.fn_str(), &findFileData); |
2f3d9587 VZ |
2048 | if (hFind == INVALID_HANDLE_VALUE) |
2049 | { | |
2050 | // Error: most likely reason is that path doesn't exist, so | |
2051 | // append any unprocessed parts and return | |
2052 | for ( i += 1; i < count; i++ ) | |
2053 | tmpPath += wxFILE_SEP_PATH + dirs[i]; | |
b5b62eea | 2054 | |
2f3d9587 VZ |
2055 | return tmpPath; |
2056 | } | |
05e7001c | 2057 | |
2f3d9587 VZ |
2058 | pathOut += findFileData.cFileName; |
2059 | if ( (i < (count-1)) ) | |
2060 | pathOut += wxFILE_SEP_PATH; | |
52dbd056 | 2061 | |
2f3d9587 | 2062 | ::FindClose(hFind); |
05e7001c | 2063 | } |
52dbd056 VZ |
2064 | #else // !Win32 |
2065 | pathOut = path; | |
2066 | #endif // Win32/!Win32 | |
5716a1ab | 2067 | |
05e7001c | 2068 | return pathOut; |
9e9b65c1 JS |
2069 | } |
2070 | ||
df5ddbca RR |
2071 | wxPathFormat wxFileName::GetFormat( wxPathFormat format ) |
2072 | { | |
2073 | if (format == wxPATH_NATIVE) | |
2074 | { | |
5a3912f2 | 2075 | #if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__) |
df5ddbca | 2076 | format = wxPATH_DOS; |
3c621059 | 2077 | #elif defined(__VMS) |
04c943b1 | 2078 | format = wxPATH_VMS; |
844f90fb | 2079 | #else |
df5ddbca RR |
2080 | format = wxPATH_UNIX; |
2081 | #endif | |
2082 | } | |
2083 | return format; | |
2084 | } | |
a35b27b1 | 2085 | |
35c2aa4f VZ |
2086 | #ifdef wxHAS_FILESYSTEM_VOLUMES |
2087 | ||
2088 | /* static */ | |
2089 | wxString wxFileName::GetVolumeString(char drive, int flags) | |
2090 | { | |
2091 | wxASSERT_MSG( !(flags & ~wxPATH_GET_SEPARATOR), "invalid flag specified" ); | |
2092 | ||
2093 | wxString vol(drive); | |
2094 | vol += wxFILE_SEP_DSK; | |
2095 | if ( flags & wxPATH_GET_SEPARATOR ) | |
2096 | vol += wxFILE_SEP_PATH; | |
2097 | ||
2098 | return vol; | |
2099 | } | |
2100 | ||
2101 | #endif // wxHAS_FILESYSTEM_VOLUMES | |
2102 | ||
9e8d8607 VZ |
2103 | // ---------------------------------------------------------------------------- |
2104 | // path splitting function | |
2105 | // ---------------------------------------------------------------------------- | |
2106 | ||
6f91bc33 | 2107 | /* static */ |
f1e77933 VZ |
2108 | void |
2109 | wxFileName::SplitVolume(const wxString& fullpathWithVolume, | |
2110 | wxString *pstrVolume, | |
2111 | wxString *pstrPath, | |
2112 | wxPathFormat format) | |
9e8d8607 VZ |
2113 | { |
2114 | format = GetFormat(format); | |
2115 | ||
04c943b1 VZ |
2116 | wxString fullpath = fullpathWithVolume; |
2117 | ||
04c943b1 | 2118 | // special Windows UNC paths hack: transform \\share\path into share:path |
9e1c7236 | 2119 | if ( IsUNCPath(fullpath, format) ) |
9e8d8607 | 2120 | { |
9e1c7236 | 2121 | fullpath.erase(0, 2); |
04c943b1 | 2122 | |
9e1c7236 VZ |
2123 | size_t posFirstSlash = |
2124 | fullpath.find_first_of(GetPathTerminators(format)); | |
2125 | if ( posFirstSlash != wxString::npos ) | |
2126 | { | |
2127 | fullpath[posFirstSlash] = wxFILE_SEP_DSK; | |
04c943b1 | 2128 | |
9e1c7236 VZ |
2129 | // UNC paths are always absolute, right? (FIXME) |
2130 | fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS); | |
3c621059 JJ |
2131 | } |
2132 | } | |
04c943b1 | 2133 | |
a385b5df RR |
2134 | // We separate the volume here |
2135 | if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
04c943b1 | 2136 | { |
a385b5df | 2137 | wxString sepVol = GetVolumeSeparator(format); |
24eb81cb | 2138 | |
0f506ded VZ |
2139 | // we have to exclude the case of a colon in the very beginning of the |
2140 | // string as it can't be a volume separator (nor can this be a valid | |
2141 | // DOS file name at all but we'll leave dealing with this to our caller) | |
04c943b1 | 2142 | size_t posFirstColon = fullpath.find_first_of(sepVol); |
0f506ded | 2143 | if ( posFirstColon && posFirstColon != wxString::npos ) |
04c943b1 VZ |
2144 | { |
2145 | if ( pstrVolume ) | |
2146 | { | |
2147 | *pstrVolume = fullpath.Left(posFirstColon); | |
2148 | } | |
2149 | ||
2150 | // remove the volume name and the separator from the full path | |
2151 | fullpath.erase(0, posFirstColon + sepVol.length()); | |
2152 | } | |
2153 | } | |
2154 | ||
f1e77933 VZ |
2155 | if ( pstrPath ) |
2156 | *pstrPath = fullpath; | |
2157 | } | |
2158 | ||
2159 | /* static */ | |
2160 | void wxFileName::SplitPath(const wxString& fullpathWithVolume, | |
2161 | wxString *pstrVolume, | |
2162 | wxString *pstrPath, | |
2163 | wxString *pstrName, | |
2164 | wxString *pstrExt, | |
dfecbee5 | 2165 | bool *hasExt, |
f1e77933 VZ |
2166 | wxPathFormat format) |
2167 | { | |
2168 | format = GetFormat(format); | |
2169 | ||
2170 | wxString fullpath; | |
2171 | SplitVolume(fullpathWithVolume, pstrVolume, &fullpath, format); | |
2172 | ||
04c943b1 VZ |
2173 | // find the positions of the last dot and last path separator in the path |
2174 | size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); | |
f1e77933 | 2175 | size_t posLastSlash = fullpath.find_last_of(GetPathTerminators(format)); |
04c943b1 | 2176 | |
e5a573a2 | 2177 | // check whether this dot occurs at the very beginning of a path component |
04c943b1 | 2178 | if ( (posLastDot != wxString::npos) && |
e5a573a2 VZ |
2179 | (posLastDot == 0 || |
2180 | IsPathSeparator(fullpath[posLastDot - 1]) || | |
9a83f860 | 2181 | (format == wxPATH_VMS && fullpath[posLastDot - 1] == wxT(']'))) ) |
f1e77933 VZ |
2182 | { |
2183 | // dot may be (and commonly -- at least under Unix -- is) the first | |
2184 | // character of the filename, don't treat the entire filename as | |
2185 | // extension in this case | |
2186 | posLastDot = wxString::npos; | |
2187 | } | |
9e8d8607 | 2188 | |
8e7dda21 VZ |
2189 | // if we do have a dot and a slash, check that the dot is in the name part |
2190 | if ( (posLastDot != wxString::npos) && | |
2191 | (posLastSlash != wxString::npos) && | |
2192 | (posLastDot < posLastSlash) ) | |
9e8d8607 VZ |
2193 | { |
2194 | // the dot is part of the path, not the start of the extension | |
2195 | posLastDot = wxString::npos; | |
2196 | } | |
2197 | ||
2198 | // now fill in the variables provided by user | |
2199 | if ( pstrPath ) | |
2200 | { | |
2201 | if ( posLastSlash == wxString::npos ) | |
2202 | { | |
2203 | // no path at all | |
2204 | pstrPath->Empty(); | |
2205 | } | |
2206 | else | |
2207 | { | |
04c943b1 | 2208 | // take everything up to the path separator but take care to make |
353f41cb | 2209 | // the path equal to something like '/', not empty, for the files |
04c943b1 VZ |
2210 | // immediately under root directory |
2211 | size_t len = posLastSlash; | |
91b4bd63 SC |
2212 | |
2213 | // this rule does not apply to mac since we do not start with colons (sep) | |
2214 | // except for relative paths | |
2215 | if ( !len && format != wxPATH_MAC) | |
04c943b1 VZ |
2216 | len++; |
2217 | ||
2218 | *pstrPath = fullpath.Left(len); | |
2219 | ||
2220 | // special VMS hack: remove the initial bracket | |
2221 | if ( format == wxPATH_VMS ) | |
2222 | { | |
9a83f860 | 2223 | if ( (*pstrPath)[0u] == wxT('[') ) |
04c943b1 VZ |
2224 | pstrPath->erase(0, 1); |
2225 | } | |
9e8d8607 VZ |
2226 | } |
2227 | } | |
2228 | ||
2229 | if ( pstrName ) | |
2230 | { | |
42b1f941 VZ |
2231 | // take all characters starting from the one after the last slash and |
2232 | // up to, but excluding, the last dot | |
9e8d8607 | 2233 | size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; |
8e7dda21 VZ |
2234 | size_t count; |
2235 | if ( posLastDot == wxString::npos ) | |
2236 | { | |
2237 | // take all until the end | |
2238 | count = wxString::npos; | |
2239 | } | |
2240 | else if ( posLastSlash == wxString::npos ) | |
2241 | { | |
2242 | count = posLastDot; | |
2243 | } | |
2244 | else // have both dot and slash | |
2245 | { | |
2246 | count = posLastDot - posLastSlash - 1; | |
2247 | } | |
9e8d8607 VZ |
2248 | |
2249 | *pstrName = fullpath.Mid(nStart, count); | |
2250 | } | |
2251 | ||
dfecbee5 VZ |
2252 | // finally deal with the extension here: we have an added complication that |
2253 | // extension may be empty (but present) as in "foo." where trailing dot | |
2254 | // indicates the empty extension at the end -- and hence we must remember | |
2255 | // that we have it independently of pstrExt | |
2256 | if ( posLastDot == wxString::npos ) | |
9e8d8607 | 2257 | { |
dfecbee5 VZ |
2258 | // no extension |
2259 | if ( pstrExt ) | |
2260 | pstrExt->clear(); | |
2261 | if ( hasExt ) | |
2262 | *hasExt = false; | |
2263 | } | |
2264 | else | |
2265 | { | |
2266 | // take everything after the dot | |
2267 | if ( pstrExt ) | |
9e8d8607 | 2268 | *pstrExt = fullpath.Mid(posLastDot + 1); |
dfecbee5 VZ |
2269 | if ( hasExt ) |
2270 | *hasExt = true; | |
9e8d8607 VZ |
2271 | } |
2272 | } | |
951cd180 | 2273 | |
6f91bc33 VZ |
2274 | /* static */ |
2275 | void wxFileName::SplitPath(const wxString& fullpath, | |
2276 | wxString *path, | |
2277 | wxString *name, | |
2278 | wxString *ext, | |
2279 | wxPathFormat format) | |
2280 | { | |
2281 | wxString volume; | |
2282 | SplitPath(fullpath, &volume, path, name, ext, format); | |
2283 | ||
67c34f64 | 2284 | if ( path ) |
6f91bc33 | 2285 | { |
67c34f64 | 2286 | path->Prepend(wxGetVolumeString(volume, format)); |
6f91bc33 VZ |
2287 | } |
2288 | } | |
2289 | ||
181dd701 VZ |
2290 | /* static */ |
2291 | wxString wxFileName::StripExtension(const wxString& fullpath) | |
2292 | { | |
2293 | wxFileName fn(fullpath); | |
2294 | fn.SetExt(""); | |
2295 | return fn.GetFullPath(); | |
2296 | } | |
2297 | ||
951cd180 VZ |
2298 | // ---------------------------------------------------------------------------- |
2299 | // time functions | |
2300 | // ---------------------------------------------------------------------------- | |
2301 | ||
e2b87f38 VZ |
2302 | #if wxUSE_DATETIME |
2303 | ||
6dbb903b VZ |
2304 | bool wxFileName::SetTimes(const wxDateTime *dtAccess, |
2305 | const wxDateTime *dtMod, | |
d9e80dce | 2306 | const wxDateTime *dtCreate) const |
951cd180 | 2307 | { |
2b5f62a0 | 2308 | #if defined(__WIN32__) |
6bc176b4 VZ |
2309 | FILETIME ftAccess, ftCreate, ftWrite; |
2310 | ||
2311 | if ( dtCreate ) | |
2312 | ConvertWxToFileTime(&ftCreate, *dtCreate); | |
2313 | if ( dtAccess ) | |
2314 | ConvertWxToFileTime(&ftAccess, *dtAccess); | |
2315 | if ( dtMod ) | |
2316 | ConvertWxToFileTime(&ftWrite, *dtMod); | |
2317 | ||
2318 | wxString path; | |
2319 | int flags; | |
2b5f62a0 VZ |
2320 | if ( IsDir() ) |
2321 | { | |
6bc176b4 VZ |
2322 | if ( wxGetOsVersion() == wxOS_WINDOWS_9X ) |
2323 | { | |
2324 | wxLogError(_("Setting directory access times is not supported " | |
2325 | "under this OS version")); | |
2326 | return false; | |
2327 | } | |
2328 | ||
2329 | path = GetPath(); | |
2330 | flags = FILE_FLAG_BACKUP_SEMANTICS; | |
2b5f62a0 VZ |
2331 | } |
2332 | else // file | |
2333 | { | |
6bc176b4 VZ |
2334 | path = GetFullPath(); |
2335 | flags = 0; | |
2336 | } | |
2337 | ||
f3c74c8d | 2338 | wxFileHandle fh(path, wxFileHandle::WriteAttr, flags); |
6bc176b4 VZ |
2339 | if ( fh.IsOk() ) |
2340 | { | |
2341 | if ( ::SetFileTime(fh, | |
2342 | dtCreate ? &ftCreate : NULL, | |
2343 | dtAccess ? &ftAccess : NULL, | |
2344 | dtMod ? &ftWrite : NULL) ) | |
2b5f62a0 | 2345 | { |
6bc176b4 | 2346 | return true; |
2b5f62a0 VZ |
2347 | } |
2348 | } | |
2349 | #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) | |
17a1ebd1 VZ |
2350 | wxUnusedVar(dtCreate); |
2351 | ||
246c704f VZ |
2352 | if ( !dtAccess && !dtMod ) |
2353 | { | |
2354 | // can't modify the creation time anyhow, don't try | |
f363e05c | 2355 | return true; |
246c704f VZ |
2356 | } |
2357 | ||
2358 | // if dtAccess or dtMod is not specified, use the other one (which must be | |
2359 | // non NULL because of the test above) for both times | |
951cd180 | 2360 | utimbuf utm; |
246c704f VZ |
2361 | utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks(); |
2362 | utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); | |
401eb3de | 2363 | if ( utime(GetFullPath().fn_str(), &utm) == 0 ) |
951cd180 | 2364 | { |
f363e05c | 2365 | return true; |
951cd180 | 2366 | } |
951cd180 | 2367 | #else // other platform |
7a893a31 WS |
2368 | wxUnusedVar(dtAccess); |
2369 | wxUnusedVar(dtMod); | |
2370 | wxUnusedVar(dtCreate); | |
951cd180 VZ |
2371 | #endif // platforms |
2372 | ||
2373 | wxLogSysError(_("Failed to modify file times for '%s'"), | |
2374 | GetFullPath().c_str()); | |
2375 | ||
f363e05c | 2376 | return false; |
951cd180 VZ |
2377 | } |
2378 | ||
d9e80dce | 2379 | bool wxFileName::Touch() const |
951cd180 VZ |
2380 | { |
2381 | #if defined(__UNIX_LIKE__) | |
2382 | // under Unix touching file is simple: just pass NULL to utime() | |
401eb3de | 2383 | if ( utime(GetFullPath().fn_str(), NULL) == 0 ) |
951cd180 | 2384 | { |
f363e05c | 2385 | return true; |
951cd180 VZ |
2386 | } |
2387 | ||
2388 | wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str()); | |
2389 | ||
f363e05c | 2390 | return false; |
951cd180 VZ |
2391 | #else // other platform |
2392 | wxDateTime dtNow = wxDateTime::Now(); | |
2393 | ||
6dbb903b | 2394 | return SetTimes(&dtNow, &dtNow, NULL /* don't change create time */); |
951cd180 VZ |
2395 | #endif // platforms |
2396 | } | |
2397 | ||
2398 | bool wxFileName::GetTimes(wxDateTime *dtAccess, | |
2399 | wxDateTime *dtMod, | |
6dbb903b | 2400 | wxDateTime *dtCreate) const |
951cd180 | 2401 | { |
2b5f62a0 VZ |
2402 | #if defined(__WIN32__) |
2403 | // we must use different methods for the files and directories under | |
2404 | // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and | |
2405 | // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and | |
2406 | // not 9x | |
2407 | bool ok; | |
2408 | FILETIME ftAccess, ftCreate, ftWrite; | |
9a0c5c01 | 2409 | if ( IsDir() ) |
2b5f62a0 VZ |
2410 | { |
2411 | // implemented in msw/dir.cpp | |
2412 | extern bool wxGetDirectoryTimes(const wxString& dirname, | |
2413 | FILETIME *, FILETIME *, FILETIME *); | |
2414 | ||
2415 | // we should pass the path without the trailing separator to | |
2416 | // wxGetDirectoryTimes() | |
2417 | ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME), | |
2418 | &ftAccess, &ftCreate, &ftWrite); | |
2419 | } | |
2420 | else // file | |
2421 | { | |
f3c74c8d | 2422 | wxFileHandle fh(GetFullPath(), wxFileHandle::ReadAttr); |
2b5f62a0 VZ |
2423 | if ( fh.IsOk() ) |
2424 | { | |
2425 | ok = ::GetFileTime(fh, | |
2426 | dtCreate ? &ftCreate : NULL, | |
2427 | dtAccess ? &ftAccess : NULL, | |
2428 | dtMod ? &ftWrite : NULL) != 0; | |
2429 | } | |
2430 | else | |
2431 | { | |
f363e05c | 2432 | ok = false; |
2b5f62a0 VZ |
2433 | } |
2434 | } | |
2435 | ||
2436 | if ( ok ) | |
2437 | { | |
2438 | if ( dtCreate ) | |
2439 | ConvertFileTimeToWx(dtCreate, ftCreate); | |
2440 | if ( dtAccess ) | |
2441 | ConvertFileTimeToWx(dtAccess, ftAccess); | |
2442 | if ( dtMod ) | |
2443 | ConvertFileTimeToWx(dtMod, ftWrite); | |
2444 | ||
f363e05c | 2445 | return true; |
2b5f62a0 | 2446 | } |
bf58daba | 2447 | #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__)) |
51cb1a65 | 2448 | // no need to test for IsDir() here |
951cd180 | 2449 | wxStructStat stBuf; |
92980e90 | 2450 | if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 ) |
951cd180 VZ |
2451 | { |
2452 | if ( dtAccess ) | |
2453 | dtAccess->Set(stBuf.st_atime); | |
2454 | if ( dtMod ) | |
2455 | dtMod->Set(stBuf.st_mtime); | |
6dbb903b VZ |
2456 | if ( dtCreate ) |
2457 | dtCreate->Set(stBuf.st_ctime); | |
951cd180 | 2458 | |
f363e05c | 2459 | return true; |
951cd180 | 2460 | } |
951cd180 | 2461 | #else // other platform |
7a893a31 WS |
2462 | wxUnusedVar(dtAccess); |
2463 | wxUnusedVar(dtMod); | |
2464 | wxUnusedVar(dtCreate); | |
951cd180 VZ |
2465 | #endif // platforms |
2466 | ||
2467 | wxLogSysError(_("Failed to retrieve file times for '%s'"), | |
2468 | GetFullPath().c_str()); | |
2469 | ||
f363e05c | 2470 | return false; |
951cd180 VZ |
2471 | } |
2472 | ||
e2b87f38 VZ |
2473 | #endif // wxUSE_DATETIME |
2474 | ||
23b8a262 JS |
2475 | |
2476 | // ---------------------------------------------------------------------------- | |
2477 | // file size functions | |
2478 | // ---------------------------------------------------------------------------- | |
2479 | ||
bd08f2f7 VZ |
2480 | #if wxUSE_LONGLONG |
2481 | ||
23b8a262 JS |
2482 | /* static */ |
2483 | wxULongLong wxFileName::GetSize(const wxString &filename) | |
2484 | { | |
2485 | if (!wxFileExists(filename)) | |
2486 | return wxInvalidSize; | |
2487 | ||
cdbce971 WS |
2488 | #if defined(__WXPALMOS__) |
2489 | // TODO | |
2490 | return wxInvalidSize; | |
2491 | #elif defined(__WIN32__) | |
f3c74c8d | 2492 | wxFileHandle f(filename, wxFileHandle::ReadAttr); |
23b8a262 JS |
2493 | if (!f.IsOk()) |
2494 | return wxInvalidSize; | |
2495 | ||
2496 | DWORD lpFileSizeHigh; | |
2497 | DWORD ret = GetFileSize(f, &lpFileSizeHigh); | |
5ab9534b | 2498 | if ( ret == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR ) |
23b8a262 JS |
2499 | return wxInvalidSize; |
2500 | ||
5ab9534b VZ |
2501 | return wxULongLong(lpFileSizeHigh, ret); |
2502 | #else // ! __WIN32__ | |
23b8a262 JS |
2503 | wxStructStat st; |
2504 | #ifndef wxNEED_WX_UNISTD_H | |
2505 | if (wxStat( filename.fn_str() , &st) != 0) | |
2506 | #else | |
2507 | if (wxStat( filename, &st) != 0) | |
2508 | #endif | |
2509 | return wxInvalidSize; | |
2510 | return wxULongLong(st.st_size); | |
2511 | #endif | |
2512 | } | |
2513 | ||
2514 | /* static */ | |
2515 | wxString wxFileName::GetHumanReadableSize(const wxULongLong &bs, | |
2516 | const wxString &nullsize, | |
2517 | int precision) | |
2518 | { | |
2519 | static const double KILOBYTESIZE = 1024.0; | |
2520 | static const double MEGABYTESIZE = 1024.0*KILOBYTESIZE; | |
2521 | static const double GIGABYTESIZE = 1024.0*MEGABYTESIZE; | |
2522 | static const double TERABYTESIZE = 1024.0*GIGABYTESIZE; | |
2523 | ||
2524 | if (bs == 0 || bs == wxInvalidSize) | |
2525 | return nullsize; | |
2526 | ||
2527 | double bytesize = bs.ToDouble(); | |
2528 | if (bytesize < KILOBYTESIZE) | |
2529 | return wxString::Format(_("%s B"), bs.ToString().c_str()); | |
2530 | if (bytesize < MEGABYTESIZE) | |
2531 | return wxString::Format(_("%.*f kB"), precision, bytesize/KILOBYTESIZE); | |
2532 | if (bytesize < GIGABYTESIZE) | |
2533 | return wxString::Format(_("%.*f MB"), precision, bytesize/MEGABYTESIZE); | |
2534 | if (bytesize < TERABYTESIZE) | |
2535 | return wxString::Format(_("%.*f GB"), precision, bytesize/GIGABYTESIZE); | |
2536 | ||
2537 | return wxString::Format(_("%.*f TB"), precision, bytesize/TERABYTESIZE); | |
2538 | } | |
2539 | ||
2540 | wxULongLong wxFileName::GetSize() const | |
2541 | { | |
2542 | return GetSize(GetFullPath()); | |
2543 | } | |
2544 | ||
2545 | wxString wxFileName::GetHumanReadableSize(const wxString &failmsg, int precision) const | |
2546 | { | |
2547 | return GetHumanReadableSize(GetSize(), failmsg, precision); | |
2548 | } | |
2549 | ||
bd08f2f7 | 2550 | #endif // wxUSE_LONGLONG |
23b8a262 JS |
2551 | |
2552 | // ---------------------------------------------------------------------------- | |
2553 | // Mac-specific functions | |
2554 | // ---------------------------------------------------------------------------- | |
2555 | ||
26eef304 | 2556 | #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON |
4dfbcdd5 | 2557 | |
56ce942b VZ |
2558 | namespace |
2559 | { | |
2560 | ||
0ca4ae93 | 2561 | class MacDefaultExtensionRecord |
4dfbcdd5 | 2562 | { |
56ce942b VZ |
2563 | public: |
2564 | MacDefaultExtensionRecord() | |
2565 | { | |
2566 | m_type = | |
2567 | m_creator = 0 ; | |
2568 | } | |
4dfbcdd5 | 2569 | |
56ce942b | 2570 | // default copy ctor, assignment operator and dtor are ok |
0ca4ae93 | 2571 | |
56ce942b VZ |
2572 | MacDefaultExtensionRecord(const wxString& ext, OSType type, OSType creator) |
2573 | : m_ext(ext) | |
2574 | { | |
2575 | m_type = type; | |
2576 | m_creator = creator; | |
2577 | } | |
2578 | ||
2579 | wxString m_ext; | |
2580 | OSType m_type; | |
2581 | OSType m_creator; | |
2582 | }; | |
2583 | ||
2584 | WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray); | |
2585 | ||
2586 | bool gMacDefaultExtensionsInited = false; | |
0ca4ae93 | 2587 | |
4dfbcdd5 | 2588 | #include "wx/arrimpl.cpp" |
0ca4ae93 | 2589 | |
56ce942b | 2590 | WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray); |
4dfbcdd5 | 2591 | |
56ce942b | 2592 | MacDefaultExtensionArray gMacDefaultExtensions; |
4dfbcdd5 | 2593 | |
5974c3cf | 2594 | // load the default extensions |
56ce942b | 2595 | const MacDefaultExtensionRecord gDefaults[] = |
4dfbcdd5 | 2596 | { |
56ce942b VZ |
2597 | MacDefaultExtensionRecord( "txt", 'TEXT', 'ttxt' ), |
2598 | MacDefaultExtensionRecord( "tif", 'TIFF', '****' ), | |
2599 | MacDefaultExtensionRecord( "jpg", 'JPEG', '****' ), | |
2600 | }; | |
0ea621cc | 2601 | |
56ce942b | 2602 | void MacEnsureDefaultExtensionsLoaded() |
5974c3cf SC |
2603 | { |
2604 | if ( !gMacDefaultExtensionsInited ) | |
4dfbcdd5 | 2605 | { |
5974c3cf SC |
2606 | // we could load the pc exchange prefs here too |
2607 | for ( size_t i = 0 ; i < WXSIZEOF( gDefaults ) ; ++i ) | |
2608 | { | |
2609 | gMacDefaultExtensions.Add( gDefaults[i] ) ; | |
2610 | } | |
56ce942b | 2611 | gMacDefaultExtensionsInited = true; |
0ea621cc | 2612 | } |
4dfbcdd5 | 2613 | } |
a2b77260 | 2614 | |
56ce942b VZ |
2615 | } // anonymous namespace |
2616 | ||
0ea621cc | 2617 | bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) |
4dfbcdd5 | 2618 | { |
a2b77260 SC |
2619 | FSRef fsRef ; |
2620 | FSCatalogInfo catInfo; | |
2621 | FileInfo *finfo ; | |
4dfbcdd5 | 2622 | |
a2b77260 SC |
2623 | if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) |
2624 | { | |
a62848fd WS |
2625 | if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) |
2626 | { | |
2627 | finfo = (FileInfo*)&catInfo.finderInfo; | |
2628 | finfo->fileType = type ; | |
2629 | finfo->fileCreator = creator ; | |
2630 | FSSetCatalogInfo( &fsRef, kFSCatInfoFinderInfo, &catInfo ) ; | |
a2b77260 | 2631 | return true ; |
a62848fd | 2632 | } |
a2b77260 SC |
2633 | } |
2634 | return false ; | |
4dfbcdd5 SC |
2635 | } |
2636 | ||
d9e80dce | 2637 | bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) const |
4dfbcdd5 | 2638 | { |
a2b77260 SC |
2639 | FSRef fsRef ; |
2640 | FSCatalogInfo catInfo; | |
2641 | FileInfo *finfo ; | |
4dfbcdd5 | 2642 | |
a2b77260 SC |
2643 | if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) |
2644 | { | |
a62848fd WS |
2645 | if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) |
2646 | { | |
2647 | finfo = (FileInfo*)&catInfo.finderInfo; | |
2648 | *type = finfo->fileType ; | |
2649 | *creator = finfo->fileCreator ; | |
a2b77260 | 2650 | return true ; |
a62848fd | 2651 | } |
a2b77260 SC |
2652 | } |
2653 | return false ; | |
4dfbcdd5 SC |
2654 | } |
2655 | ||
2656 | bool wxFileName::MacSetDefaultTypeAndCreator() | |
2657 | { | |
2658 | wxUint32 type , creator ; | |
2659 | if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type , | |
2660 | &creator ) ) | |
2661 | { | |
f63fb56d GD |
2662 | return MacSetTypeAndCreator( type , creator ) ; |
2663 | } | |
2664 | return false; | |
4dfbcdd5 SC |
2665 | } |
2666 | ||
0ea621cc | 2667 | bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator ) |
4dfbcdd5 SC |
2668 | { |
2669 | MacEnsureDefaultExtensionsLoaded() ; | |
2670 | wxString extl = ext.Lower() ; | |
2671 | for( int i = gMacDefaultExtensions.Count() - 1 ; i >= 0 ; --i ) | |
2672 | { | |
2673 | if ( gMacDefaultExtensions.Item(i).m_ext == extl ) | |
2674 | { | |
2675 | *type = gMacDefaultExtensions.Item(i).m_type ; | |
2676 | *creator = gMacDefaultExtensions.Item(i).m_creator ; | |
2677 | return true ; | |
2678 | } | |
2679 | } | |
2680 | return false ; | |
2681 | } | |
2682 | ||
2683 | void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator ) | |
2684 | { | |
56ce942b VZ |
2685 | MacEnsureDefaultExtensionsLoaded(); |
2686 | MacDefaultExtensionRecord rec(ext.Lower(), type, creator); | |
2687 | gMacDefaultExtensions.Add( rec ); | |
4dfbcdd5 | 2688 | } |
56ce942b VZ |
2689 | |
2690 | #endif // defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON |