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