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