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