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