]>
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 | |
9 | // Licence: wxWindows license | |
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 | ||
df5ddbca | 63 | #ifdef __GNUG__ |
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" |
03169422 | 85 | //#include "wx/dynlib.h" // see GetLongPath below, code disabled. |
df5ddbca | 86 | |
9e9b65c1 JS |
87 | // For GetShort/LongPathName |
88 | #ifdef __WIN32__ | |
0b9ab0bd RL |
89 | #include <windows.h> |
90 | #include "wx/msw/winundef.h" | |
951cd180 VZ |
91 | #endif |
92 | ||
93 | // utime() is POSIX so should normally be available on all Unices | |
94 | #ifdef __UNIX_LIKE__ | |
0b9ab0bd RL |
95 | #include <sys/types.h> |
96 | #include <utime.h> | |
97 | #include <sys/stat.h> | |
98 | #include <unistd.h> | |
9e9b65c1 JS |
99 | #endif |
100 | ||
444d61ba VS |
101 | #ifdef __DJGPP__ |
102 | #include <unistd.h> | |
103 | #endif | |
104 | ||
01d981ec | 105 | #ifdef __MWERKS__ |
0b9ab0bd RL |
106 | #include <stat.h> |
107 | #include <unistd.h> | |
108 | #include <unix.h> | |
01d981ec RR |
109 | #endif |
110 | ||
d9f54bb0 | 111 | #ifdef __WATCOMC__ |
0b9ab0bd RL |
112 | #include <io.h> |
113 | #include <sys/utime.h> | |
114 | #include <sys/stat.h> | |
d9f54bb0 VS |
115 | #endif |
116 | ||
24eb81cb DW |
117 | #ifdef __VISAGECPP__ |
118 | #ifndef MAX_PATH | |
119 | #define MAX_PATH 256 | |
120 | #endif | |
121 | #endif | |
122 | ||
951cd180 VZ |
123 | // ---------------------------------------------------------------------------- |
124 | // private classes | |
125 | // ---------------------------------------------------------------------------- | |
126 | ||
127 | // small helper class which opens and closes the file - we use it just to get | |
128 | // a file handle for the given file name to pass it to some Win32 API function | |
c67d6888 | 129 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
951cd180 VZ |
130 | |
131 | class wxFileHandle | |
132 | { | |
133 | public: | |
134 | wxFileHandle(const wxString& filename) | |
135 | { | |
136 | m_hFile = ::CreateFile | |
137 | ( | |
138 | filename, // name | |
139 | GENERIC_READ, // access mask | |
140 | 0, // no sharing | |
141 | NULL, // no secutity attr | |
142 | OPEN_EXISTING, // creation disposition | |
143 | 0, // no flags | |
144 | NULL // no template file | |
145 | ); | |
146 | ||
147 | if ( m_hFile == INVALID_HANDLE_VALUE ) | |
148 | { | |
149 | wxLogSysError(_("Failed to open '%s' for reading"), | |
150 | filename.c_str()); | |
151 | } | |
152 | } | |
153 | ||
154 | ~wxFileHandle() | |
155 | { | |
156 | if ( m_hFile != INVALID_HANDLE_VALUE ) | |
157 | { | |
158 | if ( !::CloseHandle(m_hFile) ) | |
159 | { | |
160 | wxLogSysError(_("Failed to close file handle")); | |
161 | } | |
162 | } | |
163 | } | |
164 | ||
165 | // return TRUE only if the file could be opened successfully | |
166 | bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; } | |
167 | ||
168 | // get the handle | |
169 | operator HANDLE() const { return m_hFile; } | |
170 | ||
171 | private: | |
172 | HANDLE m_hFile; | |
173 | }; | |
174 | ||
175 | #endif // __WIN32__ | |
176 | ||
177 | // ---------------------------------------------------------------------------- | |
178 | // private functions | |
179 | // ---------------------------------------------------------------------------- | |
180 | ||
c67d6888 | 181 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
951cd180 VZ |
182 | |
183 | // convert between wxDateTime and FILETIME which is a 64-bit value representing | |
184 | // the number of 100-nanosecond intervals since January 1, 1601. | |
185 | ||
d56e2b97 VZ |
186 | // the number of milliseconds between the Unix Epoch (January 1, 1970) and the |
187 | // FILETIME reference point (January 1, 1601) | |
188 | static const wxLongLong FILETIME_EPOCH_OFFSET = wxLongLong(0xa97, 0x30b66800); | |
189 | ||
190 | static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft) | |
951cd180 | 191 | { |
d56e2b97 VZ |
192 | wxLongLong ll(ft.dwHighDateTime, ft.dwLowDateTime); |
193 | ||
194 | // convert 100ns to ms | |
195 | ll /= 10000; | |
196 | ||
197 | // move it to our Epoch | |
198 | ll -= FILETIME_EPOCH_OFFSET; | |
199 | ||
200 | *dt = wxDateTime(ll); | |
951cd180 VZ |
201 | } |
202 | ||
d56e2b97 | 203 | static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) |
951cd180 | 204 | { |
d56e2b97 VZ |
205 | // do the reverse of ConvertFileTimeToWx() |
206 | wxLongLong ll = dt.GetValue(); | |
207 | ll *= 10000; | |
208 | ll += FILETIME_EPOCH_OFFSET; | |
209 | ||
210 | ft->dwHighDateTime = ll.GetHi(); | |
211 | ft->dwLowDateTime = ll.GetLo(); | |
951cd180 VZ |
212 | } |
213 | ||
214 | #endif // __WIN32__ | |
215 | ||
097ead30 VZ |
216 | // ============================================================================ |
217 | // implementation | |
218 | // ============================================================================ | |
219 | ||
220 | // ---------------------------------------------------------------------------- | |
844f90fb | 221 | // wxFileName construction |
097ead30 | 222 | // ---------------------------------------------------------------------------- |
df5ddbca | 223 | |
a35b27b1 RR |
224 | void wxFileName::Assign( const wxFileName &filepath ) |
225 | { | |
04c943b1 | 226 | m_volume = filepath.GetVolume(); |
844f90fb | 227 | m_dirs = filepath.GetDirs(); |
04c943b1 VZ |
228 | m_name = filepath.GetName(); |
229 | m_ext = filepath.GetExt(); | |
353f41cb | 230 | m_relative = filepath.IsRelative(); |
df5ddbca RR |
231 | } |
232 | ||
04c943b1 VZ |
233 | void wxFileName::Assign(const wxString& volume, |
234 | const wxString& path, | |
235 | const wxString& name, | |
236 | const wxString& ext, | |
237 | wxPathFormat format ) | |
a7b51bc8 RR |
238 | { |
239 | SetPath( path, format ); | |
240 | ||
241 | m_volume = volume; | |
242 | m_ext = ext; | |
243 | m_name = name; | |
244 | } | |
245 | ||
246 | void wxFileName::SetPath( const wxString &path, wxPathFormat format ) | |
df5ddbca | 247 | { |
353f41cb RR |
248 | wxPathFormat my_format = GetFormat( format ); |
249 | wxString my_path = path; | |
90a68369 | 250 | |
844f90fb | 251 | m_dirs.Clear(); |
90a68369 | 252 | |
353f41cb | 253 | if (!my_path.empty()) |
df5ddbca | 254 | { |
353f41cb | 255 | // 1) Determine if the path is relative or absolute. |
90a68369 | 256 | |
353f41cb RR |
257 | switch (my_format) |
258 | { | |
259 | case wxPATH_MAC: | |
260 | m_relative = ( my_path[0u] == wxT(':') ); | |
261 | // We then remove a leading ":". The reason is in our | |
262 | // storage form for relative paths: | |
263 | // ":dir:file.txt" actually means "./dir/file.txt" in | |
90a68369 | 264 | // DOS notation and should get stored as |
353f41cb RR |
265 | // (relative) (dir) (file.txt) |
266 | // "::dir:file.txt" actually means "../dir/file.txt" | |
267 | // stored as (relative) (..) (dir) (file.txt) | |
268 | // This is important only for the Mac as an empty dir | |
269 | // actually means <UP>, whereas under DOS, double | |
270 | // slashes can be ignored: "\\\\" is the same as "\\". | |
271 | if (m_relative) | |
272 | my_path.Remove( 0, 1 ); | |
273 | break; | |
274 | case wxPATH_VMS: | |
275 | // TODO: what is the relative path format here? | |
276 | m_relative = FALSE; | |
277 | break; | |
278 | case wxPATH_UNIX: | |
279 | m_relative = ( my_path[0u] != wxT('/') ); | |
280 | break; | |
281 | case wxPATH_DOS: | |
282 | m_relative = ( (my_path[0u] != wxT('/')) && (my_path[0u] != wxT('\\')) ); | |
283 | break; | |
284 | default: | |
7613582b | 285 | wxFAIL_MSG( wxT("error") ); |
353f41cb RR |
286 | break; |
287 | } | |
90a68369 | 288 | |
353f41cb RR |
289 | // 2) Break up the path into its members. If the original path |
290 | // was just "/" or "\\", m_dirs will be empty. We know from | |
291 | // the m_relative field, if this means "nothing" or "root dir". | |
90a68369 | 292 | |
353f41cb RR |
293 | wxStringTokenizer tn( my_path, GetPathSeparators(my_format) ); |
294 | ||
295 | while ( tn.HasMoreTokens() ) | |
296 | { | |
297 | wxString token = tn.GetNextToken(); | |
844f90fb | 298 | |
353f41cb RR |
299 | // Remove empty token under DOS and Unix, interpret them |
300 | // as .. under Mac. | |
301 | if (token.empty()) | |
302 | { | |
303 | if (my_format == wxPATH_MAC) | |
304 | m_dirs.Add( wxT("..") ); | |
305 | // else ignore | |
306 | } | |
307 | else | |
308 | { | |
309 | m_dirs.Add( token ); | |
310 | } | |
311 | } | |
df5ddbca | 312 | } |
a7b51bc8 RR |
313 | else |
314 | { | |
315 | m_relative = TRUE; | |
316 | } | |
844f90fb VZ |
317 | } |
318 | ||
319 | void wxFileName::Assign(const wxString& fullpath, | |
320 | wxPathFormat format) | |
321 | { | |
04c943b1 VZ |
322 | wxString volume, path, name, ext; |
323 | SplitPath(fullpath, &volume, &path, &name, &ext, format); | |
844f90fb | 324 | |
04c943b1 | 325 | Assign(volume, path, name, ext, format); |
844f90fb VZ |
326 | } |
327 | ||
81f25632 | 328 | void wxFileName::Assign(const wxString& fullpathOrig, |
844f90fb VZ |
329 | const wxString& fullname, |
330 | wxPathFormat format) | |
331 | { | |
81f25632 VZ |
332 | // always recognize fullpath as directory, even if it doesn't end with a |
333 | // slash | |
334 | wxString fullpath = fullpathOrig; | |
335 | if ( !wxEndsWithPathSeparator(fullpath) ) | |
336 | { | |
337 | fullpath += GetPathSeparators(format)[0u]; | |
338 | } | |
339 | ||
52dbd056 | 340 | wxString volume, path, name, ext; |
81f25632 VZ |
341 | |
342 | // do some consistency checks in debug mode: the name should be really just | |
353f41cb | 343 | // the filename and the path should be really just a path |
81f25632 VZ |
344 | #ifdef __WXDEBUG__ |
345 | wxString pathDummy, nameDummy, extDummy; | |
346 | ||
347 | SplitPath(fullname, &pathDummy, &name, &ext, format); | |
348 | ||
349 | wxASSERT_MSG( pathDummy.empty(), | |
350 | _T("the file name shouldn't contain the path") ); | |
351 | ||
352 | SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format); | |
353 | ||
354 | wxASSERT_MSG( nameDummy.empty() && extDummy.empty(), | |
355 | _T("the path shouldn't contain file name nor extension") ); | |
356 | ||
357 | #else // !__WXDEBUG__ | |
9e8d8607 | 358 | SplitPath(fullname, NULL /* no path */, &name, &ext, format); |
52dbd056 | 359 | SplitPath(fullpath, &volume, &path, NULL, NULL, format); |
81f25632 | 360 | #endif // __WXDEBUG__/!__WXDEBUG__ |
844f90fb | 361 | |
52dbd056 VZ |
362 | Assign(volume, path, name, ext, format); |
363 | } | |
364 | ||
365 | void wxFileName::AssignDir(const wxString& dir, wxPathFormat format) | |
366 | { | |
81f25632 | 367 | Assign(dir, _T(""), format); |
844f90fb VZ |
368 | } |
369 | ||
370 | void wxFileName::Clear() | |
371 | { | |
372 | m_dirs.Clear(); | |
04c943b1 VZ |
373 | |
374 | m_volume = | |
844f90fb VZ |
375 | m_name = |
376 | m_ext = wxEmptyString; | |
377 | } | |
378 | ||
379 | /* static */ | |
380 | wxFileName wxFileName::FileName(const wxString& file) | |
381 | { | |
382 | return wxFileName(file); | |
383 | } | |
384 | ||
385 | /* static */ | |
386 | wxFileName wxFileName::DirName(const wxString& dir) | |
387 | { | |
388 | wxFileName fn; | |
389 | fn.AssignDir(dir); | |
390 | return fn; | |
df5ddbca RR |
391 | } |
392 | ||
844f90fb VZ |
393 | // ---------------------------------------------------------------------------- |
394 | // existence tests | |
395 | // ---------------------------------------------------------------------------- | |
396 | ||
df5ddbca RR |
397 | bool wxFileName::FileExists() |
398 | { | |
a35b27b1 RR |
399 | return wxFileName::FileExists( GetFullPath() ); |
400 | } | |
401 | ||
402 | bool wxFileName::FileExists( const wxString &file ) | |
403 | { | |
404 | return ::wxFileExists( file ); | |
df5ddbca RR |
405 | } |
406 | ||
407 | bool wxFileName::DirExists() | |
408 | { | |
a35b27b1 RR |
409 | return wxFileName::DirExists( GetFullPath() ); |
410 | } | |
411 | ||
412 | bool wxFileName::DirExists( const wxString &dir ) | |
413 | { | |
414 | return ::wxDirExists( dir ); | |
df5ddbca RR |
415 | } |
416 | ||
844f90fb VZ |
417 | // ---------------------------------------------------------------------------- |
418 | // CWD and HOME stuff | |
419 | // ---------------------------------------------------------------------------- | |
420 | ||
6f91bc33 | 421 | void wxFileName::AssignCwd(const wxString& volume) |
df5ddbca | 422 | { |
6f91bc33 | 423 | AssignDir(wxFileName::GetCwd(volume)); |
a35b27b1 RR |
424 | } |
425 | ||
844f90fb | 426 | /* static */ |
6f91bc33 | 427 | wxString wxFileName::GetCwd(const wxString& volume) |
a35b27b1 | 428 | { |
6f91bc33 VZ |
429 | // if we have the volume, we must get the current directory on this drive |
430 | // and to do this we have to chdir to this volume - at least under Windows, | |
431 | // I don't know how to get the current drive on another volume elsewhere | |
432 | // (TODO) | |
433 | wxString cwdOld; | |
434 | if ( !volume.empty() ) | |
435 | { | |
436 | cwdOld = wxGetCwd(); | |
437 | SetCwd(volume + GetVolumeSeparator()); | |
438 | } | |
439 | ||
440 | wxString cwd = ::wxGetCwd(); | |
441 | ||
442 | if ( !volume.empty() ) | |
443 | { | |
444 | SetCwd(cwdOld); | |
445 | } | |
ae4d7004 | 446 | |
6f91bc33 | 447 | return cwd; |
a35b27b1 RR |
448 | } |
449 | ||
450 | bool wxFileName::SetCwd() | |
451 | { | |
452 | return wxFileName::SetCwd( GetFullPath() ); | |
df5ddbca RR |
453 | } |
454 | ||
a35b27b1 | 455 | bool wxFileName::SetCwd( const wxString &cwd ) |
df5ddbca | 456 | { |
a35b27b1 | 457 | return ::wxSetWorkingDirectory( cwd ); |
df5ddbca RR |
458 | } |
459 | ||
a35b27b1 RR |
460 | void wxFileName::AssignHomeDir() |
461 | { | |
844f90fb | 462 | AssignDir(wxFileName::GetHomeDir()); |
a35b27b1 | 463 | } |
844f90fb | 464 | |
a35b27b1 RR |
465 | wxString wxFileName::GetHomeDir() |
466 | { | |
467 | return ::wxGetHomeDir(); | |
468 | } | |
844f90fb | 469 | |
df22f860 | 470 | void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp) |
df5ddbca | 471 | { |
df22f860 | 472 | wxString tempname = CreateTempFileName(prefix, fileTemp); |
ade35f11 | 473 | if ( tempname.empty() ) |
844f90fb | 474 | { |
ade35f11 VZ |
475 | // error, failed to get temp file name |
476 | Clear(); | |
844f90fb | 477 | } |
ade35f11 | 478 | else // ok |
844f90fb | 479 | { |
ade35f11 VZ |
480 | Assign(tempname); |
481 | } | |
482 | } | |
483 | ||
484 | /* static */ | |
df22f860 VZ |
485 | wxString |
486 | wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) | |
ade35f11 VZ |
487 | { |
488 | wxString path, dir, name; | |
489 | ||
490 | // use the directory specified by the prefix | |
491 | SplitPath(prefix, &dir, &name, NULL /* extension */); | |
492 | ||
493 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
494 | ||
495 | #ifdef __WIN32__ | |
496 | if ( dir.empty() ) | |
497 | { | |
498 | if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) ) | |
499 | { | |
500 | wxLogLastError(_T("GetTempPath")); | |
501 | } | |
502 | ||
503 | if ( dir.empty() ) | |
504 | { | |
505 | // GetTempFileName() fails if we pass it an empty string | |
506 | dir = _T('.'); | |
507 | } | |
508 | } | |
509 | ||
510 | if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) ) | |
511 | { | |
512 | wxLogLastError(_T("GetTempFileName")); | |
513 | ||
514 | path.clear(); | |
515 | } | |
516 | #else // Win16 | |
517 | if ( !::GetTempFileName(NULL, prefix, 0, wxStringBuffer(path, 1025)) ) | |
518 | { | |
519 | path.clear(); | |
520 | } | |
521 | #endif // Win32/16 | |
522 | ||
523 | #elif defined(__WXPM__) | |
524 | // for now just create a file | |
525 | // | |
526 | // future enhancements can be to set some extended attributes for file | |
527 | // systems OS/2 supports that have them (HPFS, FAT32) and security | |
528 | // (HPFS386) | |
529 | static const wxChar *szMktempSuffix = wxT("XXX"); | |
530 | path << dir << _T('/') << name << szMktempSuffix; | |
531 | ||
532 | // Temporarily remove - MN | |
533 | #ifndef __WATCOMC__ | |
24eb81cb | 534 | ::DosCreateDir(wxStringBuffer(path, MAX_PATH), NULL); |
ade35f11 | 535 | #endif |
90a68369 | 536 | |
7070f55b | 537 | #else // !Windows, !OS/2 |
ade35f11 VZ |
538 | if ( dir.empty() ) |
539 | { | |
4c4d66e1 SC |
540 | #if defined( __WXMAC__ ) && !defined(__DARWIN__) |
541 | dir = wxMacFindFolder( (short) kOnSystemDisk, pTemporaryFolder, kCreateFolder ) ; | |
542 | #else | |
ade35f11 VZ |
543 | dir = wxGetenv(_T("TMP")); |
544 | if ( path.empty() ) | |
545 | { | |
546 | dir = wxGetenv(_T("TEMP")); | |
547 | } | |
548 | ||
549 | if ( dir.empty() ) | |
550 | { | |
551 | // default | |
d9f54bb0 VS |
552 | #ifdef __DOS__ |
553 | dir = _T("."); | |
554 | #else | |
ade35f11 | 555 | dir = _T("/tmp"); |
d9f54bb0 | 556 | #endif |
ade35f11 | 557 | } |
4c4d66e1 | 558 | #endif |
844f90fb | 559 | } |
ade35f11 VZ |
560 | |
561 | path = dir; | |
562 | ||
563 | if ( !wxEndsWithPathSeparator(dir) && | |
564 | (name.empty() || !wxIsPathSeparator(name[0u])) ) | |
565 | { | |
d9f54bb0 | 566 | path += wxFILE_SEP_PATH; |
ade35f11 VZ |
567 | } |
568 | ||
569 | path += name; | |
570 | ||
7070f55b | 571 | #if defined(HAVE_MKSTEMP) |
ade35f11 VZ |
572 | // scratch space for mkstemp() |
573 | path += _T("XXXXXX"); | |
574 | ||
575 | // can use the cast here because the length doesn't change and the string | |
576 | // is not shared | |
df22f860 VZ |
577 | int fdTemp = mkstemp((char *)path.mb_str()); |
578 | if ( fdTemp == -1 ) | |
ade35f11 VZ |
579 | { |
580 | // this might be not necessary as mkstemp() on most systems should have | |
581 | // already done it but it doesn't hurt neither... | |
582 | path.clear(); | |
583 | } | |
df22f860 VZ |
584 | else // mkstemp() succeeded |
585 | { | |
586 | // avoid leaking the fd | |
587 | if ( fileTemp ) | |
588 | { | |
589 | fileTemp->Attach(fdTemp); | |
590 | } | |
591 | else | |
592 | { | |
593 | close(fdTemp); | |
594 | } | |
595 | } | |
ade35f11 VZ |
596 | #else // !HAVE_MKSTEMP |
597 | ||
598 | #ifdef HAVE_MKTEMP | |
599 | // same as above | |
600 | path += _T("XXXXXX"); | |
601 | ||
602 | if ( !mktemp((char *)path.mb_str()) ) | |
603 | { | |
604 | path.clear(); | |
605 | } | |
7070f55b | 606 | #else // !HAVE_MKTEMP (includes __DOS__) |
ade35f11 | 607 | // generate the unique file name ourselves |
7070f55b | 608 | #ifndef __DOS__ |
ade35f11 | 609 | path << (unsigned int)getpid(); |
7070f55b | 610 | #endif |
ade35f11 VZ |
611 | |
612 | wxString pathTry; | |
613 | ||
614 | static const size_t numTries = 1000; | |
615 | for ( size_t n = 0; n < numTries; n++ ) | |
616 | { | |
617 | // 3 hex digits is enough for numTries == 1000 < 4096 | |
618 | pathTry = path + wxString::Format(_T("%.03x"), n); | |
619 | if ( !wxFile::Exists(pathTry) ) | |
620 | { | |
621 | break; | |
622 | } | |
623 | ||
624 | pathTry.clear(); | |
625 | } | |
626 | ||
627 | path = pathTry; | |
628 | #endif // HAVE_MKTEMP/!HAVE_MKTEMP | |
629 | ||
630 | if ( !path.empty() ) | |
631 | { | |
df22f860 VZ |
632 | } |
633 | #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP | |
634 | ||
635 | #endif // Windows/!Windows | |
636 | ||
637 | if ( path.empty() ) | |
638 | { | |
639 | wxLogSysError(_("Failed to create a temporary file name")); | |
640 | } | |
641 | else if ( fileTemp && !fileTemp->IsOpened() ) | |
642 | { | |
643 | // open the file - of course, there is a race condition here, this is | |
ade35f11 | 644 | // why we always prefer using mkstemp()... |
90a68369 VZ |
645 | // |
646 | // NB: GetTempFileName() under Windows creates the file, so using | |
647 | // write_excl there would fail | |
648 | if ( !fileTemp->Open(path, | |
649 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
650 | wxFile::write, | |
651 | #else | |
652 | wxFile::write_excl, | |
653 | #endif | |
654 | wxS_IRUSR | wxS_IWUSR) ) | |
ade35f11 VZ |
655 | { |
656 | // FIXME: If !ok here should we loop and try again with another | |
657 | // file name? That is the standard recourse if open(O_EXCL) | |
658 | // fails, though of course it should be protected against | |
659 | // possible infinite looping too. | |
660 | ||
661 | wxLogError(_("Failed to open temporary file.")); | |
662 | ||
663 | path.clear(); | |
664 | } | |
665 | } | |
ade35f11 VZ |
666 | |
667 | return path; | |
df5ddbca RR |
668 | } |
669 | ||
844f90fb VZ |
670 | // ---------------------------------------------------------------------------- |
671 | // directory operations | |
672 | // ---------------------------------------------------------------------------- | |
673 | ||
f0ce3409 | 674 | bool wxFileName::Mkdir( int perm, bool full ) |
a35b27b1 | 675 | { |
f0ce3409 | 676 | return wxFileName::Mkdir( GetFullPath(), perm, full ); |
a35b27b1 RR |
677 | } |
678 | ||
f0ce3409 | 679 | bool wxFileName::Mkdir( const wxString &dir, int perm, bool full ) |
df5ddbca | 680 | { |
f0ce3409 JS |
681 | if (full) |
682 | { | |
683 | wxFileName filename(dir); | |
684 | wxArrayString dirs = filename.GetDirs(); | |
77fe02a8 | 685 | dirs.Add(filename.GetName()); |
f0ce3409 JS |
686 | |
687 | size_t count = dirs.GetCount(); | |
688 | size_t i; | |
689 | wxString currPath; | |
690 | int noErrors = 0; | |
691 | for ( i = 0; i < count; i++ ) | |
692 | { | |
693 | currPath += dirs[i]; | |
694 | ||
695 | if (currPath.Last() == wxT(':')) | |
696 | { | |
697 | // Can't create a root directory so continue to next dir | |
698 | currPath += wxFILE_SEP_PATH; | |
699 | continue; | |
700 | } | |
701 | ||
702 | if (!DirExists(currPath)) | |
703 | if (!wxMkdir(currPath, perm)) | |
704 | noErrors ++; | |
705 | ||
706 | if ( (i < (count-1)) ) | |
707 | currPath += wxFILE_SEP_PATH; | |
708 | } | |
709 | ||
710 | return (noErrors == 0); | |
711 | ||
712 | } | |
713 | else | |
714 | return ::wxMkdir( dir, perm ); | |
df5ddbca RR |
715 | } |
716 | ||
a35b27b1 | 717 | bool wxFileName::Rmdir() |
df5ddbca | 718 | { |
a35b27b1 | 719 | return wxFileName::Rmdir( GetFullPath() ); |
df5ddbca RR |
720 | } |
721 | ||
a35b27b1 | 722 | bool wxFileName::Rmdir( const wxString &dir ) |
df5ddbca | 723 | { |
a35b27b1 | 724 | return ::wxRmdir( dir ); |
df5ddbca RR |
725 | } |
726 | ||
844f90fb VZ |
727 | // ---------------------------------------------------------------------------- |
728 | // path normalization | |
729 | // ---------------------------------------------------------------------------- | |
730 | ||
731 | bool wxFileName::Normalize(wxPathNormalize flags, | |
732 | const wxString& cwd, | |
733 | wxPathFormat format) | |
a35b27b1 | 734 | { |
844f90fb VZ |
735 | // the existing path components |
736 | wxArrayString dirs = GetDirs(); | |
737 | ||
738 | // the path to prepend in front to make the path absolute | |
739 | wxFileName curDir; | |
740 | ||
741 | format = GetFormat(format); | |
ae4d7004 | 742 | |
844f90fb | 743 | // make the path absolute |
353f41cb | 744 | if ( (flags & wxPATH_NORM_ABSOLUTE) && m_relative ) |
a35b27b1 | 745 | { |
844f90fb | 746 | if ( cwd.empty() ) |
6f91bc33 VZ |
747 | { |
748 | curDir.AssignCwd(GetVolume()); | |
749 | } | |
750 | else // cwd provided | |
751 | { | |
844f90fb | 752 | curDir.AssignDir(cwd); |
6f91bc33 | 753 | } |
52dbd056 | 754 | |
353f41cb | 755 | #if 0 |
52dbd056 VZ |
756 | // the path may be not absolute because it doesn't have the volume name |
757 | // but in this case we shouldn't modify the directory components of it | |
758 | // but just set the current volume | |
759 | if ( !HasVolume() && curDir.HasVolume() ) | |
760 | { | |
761 | SetVolume(curDir.GetVolume()); | |
762 | ||
763 | if ( IsAbsolute() ) | |
764 | { | |
765 | // yes, it was the case - we don't need curDir then | |
766 | curDir.Clear(); | |
767 | } | |
768 | } | |
353f41cb | 769 | #endif |
5fc67e5c | 770 | m_relative = FALSE; |
a35b27b1 | 771 | } |
ae4d7004 | 772 | |
844f90fb VZ |
773 | // handle ~ stuff under Unix only |
774 | if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) ) | |
a35b27b1 | 775 | { |
844f90fb VZ |
776 | if ( !dirs.IsEmpty() ) |
777 | { | |
778 | wxString dir = dirs[0u]; | |
779 | if ( !dir.empty() && dir[0u] == _T('~') ) | |
780 | { | |
781 | curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); | |
782 | ||
da2fd5ac | 783 | dirs.RemoveAt(0u); |
844f90fb VZ |
784 | } |
785 | } | |
a35b27b1 | 786 | } |
844f90fb | 787 | |
52dbd056 | 788 | // transform relative path into abs one |
844f90fb | 789 | if ( curDir.IsOk() ) |
a35b27b1 | 790 | { |
844f90fb VZ |
791 | wxArrayString dirsNew = curDir.GetDirs(); |
792 | size_t count = dirs.GetCount(); | |
793 | for ( size_t n = 0; n < count; n++ ) | |
794 | { | |
795 | dirsNew.Add(dirs[n]); | |
796 | } | |
797 | ||
798 | dirs = dirsNew; | |
a35b27b1 | 799 | } |
844f90fb VZ |
800 | |
801 | // now deal with ".", ".." and the rest | |
802 | m_dirs.Empty(); | |
803 | size_t count = dirs.GetCount(); | |
804 | for ( size_t n = 0; n < count; n++ ) | |
a35b27b1 | 805 | { |
844f90fb VZ |
806 | wxString dir = dirs[n]; |
807 | ||
2bc60417 | 808 | if ( flags & wxPATH_NORM_DOTS ) |
844f90fb VZ |
809 | { |
810 | if ( dir == wxT(".") ) | |
811 | { | |
812 | // just ignore | |
813 | continue; | |
814 | } | |
815 | ||
816 | if ( dir == wxT("..") ) | |
817 | { | |
818 | if ( m_dirs.IsEmpty() ) | |
819 | { | |
820 | wxLogError(_("The path '%s' contains too many \"..\"!"), | |
821 | GetFullPath().c_str()); | |
822 | return FALSE; | |
823 | } | |
824 | ||
ae4d7004 | 825 | m_dirs.RemoveAt(m_dirs.GetCount() - 1); |
844f90fb VZ |
826 | continue; |
827 | } | |
828 | } | |
829 | ||
830 | if ( flags & wxPATH_NORM_ENV_VARS ) | |
a35b27b1 | 831 | { |
844f90fb | 832 | dir = wxExpandEnvVars(dir); |
a35b27b1 | 833 | } |
844f90fb VZ |
834 | |
835 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
836 | { | |
837 | dir.MakeLower(); | |
838 | } | |
839 | ||
840 | m_dirs.Add(dir); | |
a35b27b1 | 841 | } |
844f90fb VZ |
842 | |
843 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
844 | { | |
845 | // VZ: expand env vars here too? | |
846 | ||
847 | m_name.MakeLower(); | |
848 | m_ext.MakeLower(); | |
849 | } | |
850 | ||
52dbd056 VZ |
851 | #if defined(__WIN32__) |
852 | if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) ) | |
9e9b65c1 JS |
853 | { |
854 | Assign(GetLongPath()); | |
855 | } | |
52dbd056 | 856 | #endif // Win32 |
9e9b65c1 | 857 | |
a35b27b1 RR |
858 | return TRUE; |
859 | } | |
860 | ||
f7d886af VZ |
861 | bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) |
862 | { | |
863 | wxFileName fnBase(pathBase, format); | |
864 | ||
865 | // get cwd only once - small time saving | |
866 | wxString cwd = wxGetCwd(); | |
867 | Normalize(wxPATH_NORM_ALL, cwd, format); | |
868 | fnBase.Normalize(wxPATH_NORM_ALL, cwd, format); | |
869 | ||
870 | bool withCase = IsCaseSensitive(format); | |
871 | ||
872 | // we can't do anything if the files live on different volumes | |
873 | if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) ) | |
874 | { | |
875 | // nothing done | |
876 | return FALSE; | |
877 | } | |
878 | ||
879 | // same drive, so we don't need our volume | |
880 | m_volume.clear(); | |
881 | ||
882 | // remove common directories starting at the top | |
883 | while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() && | |
884 | m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) ) | |
885 | { | |
ae4d7004 VZ |
886 | m_dirs.RemoveAt(0); |
887 | fnBase.m_dirs.RemoveAt(0); | |
f7d886af VZ |
888 | } |
889 | ||
890 | // add as many ".." as needed | |
891 | size_t count = fnBase.m_dirs.GetCount(); | |
892 | for ( size_t i = 0; i < count; i++ ) | |
893 | { | |
894 | m_dirs.Insert(wxT(".."), 0u); | |
895 | } | |
90a68369 | 896 | |
353f41cb | 897 | m_relative = TRUE; |
f7d886af VZ |
898 | |
899 | // we were modified | |
900 | return TRUE; | |
901 | } | |
902 | ||
844f90fb VZ |
903 | // ---------------------------------------------------------------------------- |
904 | // filename kind tests | |
905 | // ---------------------------------------------------------------------------- | |
906 | ||
f7d886af | 907 | bool wxFileName::SameAs(const wxFileName &filepath, wxPathFormat format) |
df5ddbca | 908 | { |
844f90fb VZ |
909 | wxFileName fn1 = *this, |
910 | fn2 = filepath; | |
911 | ||
912 | // get cwd only once - small time saving | |
913 | wxString cwd = wxGetCwd(); | |
914 | fn1.Normalize(wxPATH_NORM_ALL, cwd, format); | |
915 | fn2.Normalize(wxPATH_NORM_ALL, cwd, format); | |
916 | ||
917 | if ( fn1.GetFullPath() == fn2.GetFullPath() ) | |
918 | return TRUE; | |
919 | ||
920 | // TODO: compare inodes for Unix, this works even when filenames are | |
921 | // different but files are the same (symlinks) (VZ) | |
922 | ||
923 | return FALSE; | |
df5ddbca RR |
924 | } |
925 | ||
844f90fb | 926 | /* static */ |
df5ddbca RR |
927 | bool wxFileName::IsCaseSensitive( wxPathFormat format ) |
928 | { | |
04c943b1 VZ |
929 | // only Unix filenames are truely case-sensitive |
930 | return GetFormat(format) == wxPATH_UNIX; | |
df5ddbca RR |
931 | } |
932 | ||
04c943b1 VZ |
933 | /* static */ |
934 | wxString wxFileName::GetVolumeSeparator(wxPathFormat format) | |
844f90fb | 935 | { |
04c943b1 VZ |
936 | wxString sepVol; |
937 | ||
a385b5df RR |
938 | if ( (GetFormat(format) == wxPATH_DOS) || |
939 | (GetFormat(format) == wxPATH_VMS) ) | |
04c943b1 | 940 | { |
04c943b1 VZ |
941 | sepVol = wxFILE_SEP_DSK; |
942 | } | |
a385b5df | 943 | //else: leave empty |
04c943b1 VZ |
944 | |
945 | return sepVol; | |
844f90fb VZ |
946 | } |
947 | ||
948 | /* static */ | |
949 | wxString wxFileName::GetPathSeparators(wxPathFormat format) | |
950 | { | |
951 | wxString seps; | |
952 | switch ( GetFormat(format) ) | |
df5ddbca | 953 | { |
844f90fb | 954 | case wxPATH_DOS: |
04c943b1 VZ |
955 | // accept both as native APIs do but put the native one first as |
956 | // this is the one we use in GetFullPath() | |
957 | seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX; | |
844f90fb VZ |
958 | break; |
959 | ||
960 | default: | |
961 | wxFAIL_MSG( _T("unknown wxPATH_XXX style") ); | |
962 | // fall through | |
963 | ||
964 | case wxPATH_UNIX: | |
9e8d8607 | 965 | seps = wxFILE_SEP_PATH_UNIX; |
844f90fb VZ |
966 | break; |
967 | ||
968 | case wxPATH_MAC: | |
9e8d8607 | 969 | seps = wxFILE_SEP_PATH_MAC; |
844f90fb | 970 | break; |
04c943b1 | 971 | |
3c621059 JJ |
972 | case wxPATH_VMS: |
973 | seps = wxFILE_SEP_PATH_VMS; | |
974 | break; | |
df5ddbca RR |
975 | } |
976 | ||
844f90fb | 977 | return seps; |
df5ddbca RR |
978 | } |
979 | ||
844f90fb VZ |
980 | /* static */ |
981 | bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) | |
df5ddbca | 982 | { |
04c943b1 VZ |
983 | // wxString::Find() doesn't work as expected with NUL - it will always find |
984 | // it, so it is almost surely a bug if this function is called with NUL arg | |
985 | wxASSERT_MSG( ch != _T('\0'), _T("shouldn't be called with NUL") ); | |
986 | ||
844f90fb | 987 | return GetPathSeparators(format).Find(ch) != wxNOT_FOUND; |
df5ddbca RR |
988 | } |
989 | ||
990 | bool wxFileName::IsWild( wxPathFormat format ) | |
991 | { | |
844f90fb VZ |
992 | // FIXME: this is probably false for Mac and this is surely wrong for most |
993 | // of Unix shells (think about "[...]") | |
04c943b1 | 994 | (void)format; |
844f90fb | 995 | return m_name.find_first_of(_T("*?")) != wxString::npos; |
df5ddbca RR |
996 | } |
997 | ||
844f90fb VZ |
998 | // ---------------------------------------------------------------------------- |
999 | // path components manipulation | |
1000 | // ---------------------------------------------------------------------------- | |
1001 | ||
df5ddbca RR |
1002 | void wxFileName::AppendDir( const wxString &dir ) |
1003 | { | |
1004 | m_dirs.Add( dir ); | |
1005 | } | |
1006 | ||
1007 | void wxFileName::PrependDir( const wxString &dir ) | |
1008 | { | |
1009 | m_dirs.Insert( dir, 0 ); | |
1010 | } | |
1011 | ||
1012 | void wxFileName::InsertDir( int before, const wxString &dir ) | |
1013 | { | |
1014 | m_dirs.Insert( dir, before ); | |
1015 | } | |
1016 | ||
1017 | void wxFileName::RemoveDir( int pos ) | |
1018 | { | |
1019 | m_dirs.Remove( (size_t)pos ); | |
1020 | } | |
1021 | ||
844f90fb VZ |
1022 | // ---------------------------------------------------------------------------- |
1023 | // accessors | |
1024 | // ---------------------------------------------------------------------------- | |
1025 | ||
7124df9b VZ |
1026 | void wxFileName::SetFullName(const wxString& fullname) |
1027 | { | |
1028 | SplitPath(fullname, NULL /* no path */, &m_name, &m_ext); | |
1029 | } | |
1030 | ||
844f90fb | 1031 | wxString wxFileName::GetFullName() const |
a35b27b1 | 1032 | { |
844f90fb VZ |
1033 | wxString fullname = m_name; |
1034 | if ( !m_ext.empty() ) | |
a35b27b1 | 1035 | { |
9e8d8607 | 1036 | fullname << wxFILE_SEP_EXT << m_ext; |
a35b27b1 | 1037 | } |
a35b27b1 | 1038 | |
844f90fb | 1039 | return fullname; |
a35b27b1 RR |
1040 | } |
1041 | ||
1042 | wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const | |
df5ddbca RR |
1043 | { |
1044 | format = GetFormat( format ); | |
844f90fb | 1045 | |
353f41cb RR |
1046 | wxString fullpath; |
1047 | ||
1048 | // the leading character | |
1049 | if ( format == wxPATH_MAC && m_relative ) | |
1050 | { | |
1051 | fullpath += wxFILE_SEP_PATH_MAC; | |
1052 | } | |
1053 | else if ( format == wxPATH_DOS ) | |
1054 | { | |
1055 | if (!m_relative) | |
1056 | fullpath += wxFILE_SEP_PATH_DOS; | |
1057 | } | |
1058 | else if ( format == wxPATH_UNIX ) | |
1059 | { | |
1060 | if (!m_relative) | |
1061 | fullpath += wxFILE_SEP_PATH_UNIX; | |
1062 | } | |
90a68369 | 1063 | |
353f41cb RR |
1064 | // then concatenate all the path components using the path separator |
1065 | size_t dirCount = m_dirs.GetCount(); | |
1066 | if ( dirCount ) | |
df5ddbca | 1067 | { |
353f41cb RR |
1068 | if ( format == wxPATH_VMS ) |
1069 | { | |
1070 | fullpath += wxT('['); | |
1071 | } | |
1072 | ||
1073 | ||
1074 | for ( size_t i = 0; i < dirCount; i++ ) | |
1075 | { | |
1076 | // TODO: What to do with ".." under VMS | |
1077 | ||
1078 | switch (format) | |
1079 | { | |
1080 | case wxPATH_MAC: | |
1081 | { | |
1082 | if (m_dirs[i] == wxT(".")) | |
1083 | break; | |
1084 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1085 | fullpath += m_dirs[i]; | |
1086 | fullpath += wxT(':'); | |
1087 | break; | |
1088 | } | |
1089 | case wxPATH_DOS: | |
1090 | { | |
1091 | fullpath += m_dirs[i]; | |
1092 | fullpath += wxT('\\'); | |
1093 | break; | |
1094 | } | |
1095 | case wxPATH_UNIX: | |
1096 | { | |
1097 | fullpath += m_dirs[i]; | |
1098 | fullpath += wxT('/'); | |
1099 | break; | |
1100 | } | |
1101 | case wxPATH_VMS: | |
1102 | { | |
1103 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1104 | fullpath += m_dirs[i]; | |
1105 | if (i == dirCount-1) | |
1106 | fullpath += wxT(']'); | |
1107 | else | |
1108 | fullpath += wxT('.'); | |
1109 | break; | |
1110 | } | |
1111 | default: | |
1112 | { | |
7613582b | 1113 | wxFAIL_MSG( wxT("error") ); |
353f41cb RR |
1114 | } |
1115 | } | |
1116 | } | |
df5ddbca | 1117 | } |
844f90fb | 1118 | |
353f41cb RR |
1119 | |
1120 | ||
1121 | return fullpath; | |
df5ddbca RR |
1122 | } |
1123 | ||
1124 | wxString wxFileName::GetFullPath( wxPathFormat format ) const | |
1125 | { | |
04c943b1 VZ |
1126 | format = GetFormat(format); |
1127 | ||
1128 | wxString fullpath; | |
1129 | ||
1130 | // first put the volume | |
1131 | if ( !m_volume.empty() ) | |
dcf0fce4 | 1132 | { |
5fc67e5c RR |
1133 | { |
1134 | // Special Windows UNC paths hack, part 2: undo what we did in | |
1135 | // SplitPath() and make an UNC path if we have a drive which is not a | |
1136 | // single letter (hopefully the network shares can't be one letter only | |
1137 | // although I didn't find any authoritative docs on this) | |
1138 | if ( format == wxPATH_DOS && m_volume.length() > 1 ) | |
1139 | { | |
1140 | fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume; | |
1141 | } | |
1142 | else if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
1143 | { | |
1144 | fullpath << m_volume << GetVolumeSeparator(format); | |
a385b5df | 1145 | } |
353f41cb | 1146 | // else ignore |
dcf0fce4 RR |
1147 | } |
1148 | } | |
90a68369 | 1149 | |
353f41cb RR |
1150 | // the leading character |
1151 | if ( format == wxPATH_MAC && m_relative ) | |
1152 | { | |
1153 | fullpath += wxFILE_SEP_PATH_MAC; | |
1154 | } | |
1155 | else if ( format == wxPATH_DOS ) | |
1156 | { | |
1157 | if (!m_relative) | |
1158 | fullpath += wxFILE_SEP_PATH_DOS; | |
1159 | } | |
1160 | else if ( format == wxPATH_UNIX ) | |
1161 | { | |
1162 | if (!m_relative) | |
1163 | fullpath += wxFILE_SEP_PATH_UNIX; | |
1164 | } | |
90a68369 | 1165 | |
04c943b1 VZ |
1166 | // then concatenate all the path components using the path separator |
1167 | size_t dirCount = m_dirs.GetCount(); | |
1168 | if ( dirCount ) | |
3c621059 | 1169 | { |
04c943b1 | 1170 | if ( format == wxPATH_VMS ) |
dcf0fce4 | 1171 | { |
353f41cb | 1172 | fullpath += wxT('['); |
04c943b1 VZ |
1173 | } |
1174 | ||
353f41cb | 1175 | |
04c943b1 VZ |
1176 | for ( size_t i = 0; i < dirCount; i++ ) |
1177 | { | |
353f41cb | 1178 | // TODO: What to do with ".." under VMS |
04c943b1 | 1179 | |
353f41cb RR |
1180 | switch (format) |
1181 | { | |
1182 | case wxPATH_MAC: | |
1183 | { | |
1184 | if (m_dirs[i] == wxT(".")) | |
1185 | break; | |
1186 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1187 | fullpath += m_dirs[i]; | |
1188 | fullpath += wxT(':'); | |
1189 | break; | |
1190 | } | |
1191 | case wxPATH_DOS: | |
1192 | { | |
1193 | fullpath += m_dirs[i]; | |
1194 | fullpath += wxT('\\'); | |
1195 | break; | |
1196 | } | |
1197 | case wxPATH_UNIX: | |
1198 | { | |
1199 | fullpath += m_dirs[i]; | |
1200 | fullpath += wxT('/'); | |
1201 | break; | |
1202 | } | |
1203 | case wxPATH_VMS: | |
1204 | { | |
1205 | if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing | |
1206 | fullpath += m_dirs[i]; | |
1207 | if (i == dirCount-1) | |
1208 | fullpath += wxT(']'); | |
1209 | else | |
1210 | fullpath += wxT('.'); | |
1211 | break; | |
1212 | } | |
1213 | default: | |
1214 | { | |
7613582b | 1215 | wxFAIL_MSG( wxT("error") ); |
353f41cb RR |
1216 | } |
1217 | } | |
dcf0fce4 RR |
1218 | } |
1219 | } | |
04c943b1 VZ |
1220 | |
1221 | // finally add the file name and extension | |
1222 | fullpath += GetFullName(); | |
1223 | ||
1224 | return fullpath; | |
df5ddbca RR |
1225 | } |
1226 | ||
9e9b65c1 JS |
1227 | // Return the short form of the path (returns identity on non-Windows platforms) |
1228 | wxString wxFileName::GetShortPath() const | |
1229 | { | |
8cb172b4 | 1230 | #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) |
9e9b65c1 | 1231 | wxString path(GetFullPath()); |
75ef5722 JS |
1232 | wxString pathOut; |
1233 | DWORD sz = ::GetShortPathName(path, NULL, 0); | |
1234 | bool ok = sz != 0; | |
1235 | if ( ok ) | |
9e9b65c1 | 1236 | { |
75ef5722 JS |
1237 | ok = ::GetShortPathName |
1238 | ( | |
1239 | path, | |
1240 | pathOut.GetWriteBuf(sz), | |
1241 | sz | |
1242 | ) != 0; | |
1243 | pathOut.UngetWriteBuf(); | |
9e9b65c1 | 1244 | } |
75ef5722 JS |
1245 | if (ok) |
1246 | return pathOut; | |
5716a1ab VZ |
1247 | |
1248 | return path; | |
9e9b65c1 JS |
1249 | #else |
1250 | return GetFullPath(); | |
1251 | #endif | |
1252 | } | |
1253 | ||
1254 | // Return the long form of the path (returns identity on non-Windows platforms) | |
1255 | wxString wxFileName::GetLongPath() const | |
1256 | { | |
52dbd056 VZ |
1257 | wxString pathOut, |
1258 | path = GetFullPath(); | |
1259 | ||
1260 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
05e7001c JS |
1261 | bool success = FALSE; |
1262 | ||
5716a1ab | 1263 | // VZ: this code was disabled, why? |
0b9ab0bd | 1264 | #if 0 // wxUSE_DYNAMIC_LOADER |
05e7001c JS |
1265 | typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); |
1266 | ||
1267 | static bool s_triedToLoad = FALSE; | |
05e7001c JS |
1268 | |
1269 | if ( !s_triedToLoad ) | |
9e9b65c1 | 1270 | { |
05e7001c | 1271 | s_triedToLoad = TRUE; |
4f89dbc4 RL |
1272 | wxDynamicLibrary dllKernel(_T("kernel32")); |
1273 | if ( dllKernel.IsLoaded() ) | |
05e7001c JS |
1274 | { |
1275 | // may succeed or fail depending on the Windows version | |
04c943b1 | 1276 | static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; |
5d978d07 | 1277 | #ifdef _UNICODE |
4f89dbc4 | 1278 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameW")); |
5d978d07 | 1279 | #else |
4f89dbc4 | 1280 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameA")); |
5d978d07 | 1281 | #endif |
05e7001c | 1282 | |
05e7001c JS |
1283 | if ( s_pfnGetLongPathName ) |
1284 | { | |
1285 | DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0); | |
1286 | bool ok = dwSize > 0; | |
1287 | ||
1288 | if ( ok ) | |
1289 | { | |
1290 | DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0); | |
1291 | ok = sz != 0; | |
1292 | if ( ok ) | |
1293 | { | |
1294 | ok = (*s_pfnGetLongPathName) | |
1295 | ( | |
1296 | path, | |
1297 | pathOut.GetWriteBuf(sz), | |
1298 | sz | |
1299 | ) != 0; | |
1300 | pathOut.UngetWriteBuf(); | |
1301 | ||
1302 | success = TRUE; | |
1303 | } | |
1304 | } | |
1305 | } | |
1306 | } | |
9e9b65c1 | 1307 | } |
05e7001c | 1308 | if (success) |
75ef5722 | 1309 | return pathOut; |
0b9ab0bd | 1310 | #endif // wxUSE_DYNAMIC_LOADER |
05e7001c JS |
1311 | |
1312 | if (!success) | |
1313 | { | |
1314 | // The OS didn't support GetLongPathName, or some other error. | |
1315 | // We need to call FindFirstFile on each component in turn. | |
1316 | ||
1317 | WIN32_FIND_DATA findFileData; | |
1318 | HANDLE hFind; | |
1319 | pathOut = wxEmptyString; | |
1320 | ||
77fe02a8 | 1321 | wxArrayString dirs = GetDirs(); |
bcbe86e5 | 1322 | dirs.Add(GetFullName()); |
77fe02a8 | 1323 | |
05e7001c | 1324 | wxString tmpPath; |
5d978d07 | 1325 | |
52dbd056 VZ |
1326 | size_t count = dirs.GetCount(); |
1327 | for ( size_t i = 0; i < count; i++ ) | |
05e7001c | 1328 | { |
52dbd056 VZ |
1329 | // We're using pathOut to collect the long-name path, but using a |
1330 | // temporary for appending the last path component which may be | |
1331 | // short-name | |
77fe02a8 | 1332 | tmpPath = pathOut + dirs[i]; |
05e7001c | 1333 | |
52dbd056 VZ |
1334 | if ( tmpPath.empty() ) |
1335 | continue; | |
1336 | ||
1337 | if ( tmpPath.Last() == wxT(':') ) | |
5d978d07 JS |
1338 | { |
1339 | // Can't pass a drive and root dir to FindFirstFile, | |
1340 | // so continue to next dir | |
05e7001c | 1341 | tmpPath += wxFILE_SEP_PATH; |
5d978d07 JS |
1342 | pathOut = tmpPath; |
1343 | continue; | |
1344 | } | |
05e7001c JS |
1345 | |
1346 | hFind = ::FindFirstFile(tmpPath, &findFileData); | |
1347 | if (hFind == INVALID_HANDLE_VALUE) | |
1348 | { | |
1349 | // Error: return immediately with the original path | |
1350 | return path; | |
1351 | } | |
05e7001c | 1352 | |
52dbd056 VZ |
1353 | pathOut += findFileData.cFileName; |
1354 | if ( (i < (count-1)) ) | |
1355 | pathOut += wxFILE_SEP_PATH; | |
1356 | ||
1357 | ::FindClose(hFind); | |
05e7001c JS |
1358 | } |
1359 | } | |
52dbd056 VZ |
1360 | #else // !Win32 |
1361 | pathOut = path; | |
1362 | #endif // Win32/!Win32 | |
5716a1ab | 1363 | |
05e7001c | 1364 | return pathOut; |
9e9b65c1 JS |
1365 | } |
1366 | ||
df5ddbca RR |
1367 | wxPathFormat wxFileName::GetFormat( wxPathFormat format ) |
1368 | { | |
1369 | if (format == wxPATH_NATIVE) | |
1370 | { | |
d9f54bb0 | 1371 | #if defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__) |
df5ddbca | 1372 | format = wxPATH_DOS; |
4d293f8e | 1373 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
04c943b1 | 1374 | format = wxPATH_MAC; |
3c621059 | 1375 | #elif defined(__VMS) |
04c943b1 | 1376 | format = wxPATH_VMS; |
844f90fb | 1377 | #else |
df5ddbca RR |
1378 | format = wxPATH_UNIX; |
1379 | #endif | |
1380 | } | |
1381 | return format; | |
1382 | } | |
a35b27b1 | 1383 | |
9e8d8607 VZ |
1384 | // ---------------------------------------------------------------------------- |
1385 | // path splitting function | |
1386 | // ---------------------------------------------------------------------------- | |
1387 | ||
6f91bc33 | 1388 | /* static */ |
04c943b1 VZ |
1389 | void wxFileName::SplitPath(const wxString& fullpathWithVolume, |
1390 | wxString *pstrVolume, | |
9e8d8607 VZ |
1391 | wxString *pstrPath, |
1392 | wxString *pstrName, | |
1393 | wxString *pstrExt, | |
1394 | wxPathFormat format) | |
1395 | { | |
1396 | format = GetFormat(format); | |
1397 | ||
04c943b1 VZ |
1398 | wxString fullpath = fullpathWithVolume; |
1399 | ||
1400 | // under VMS the end of the path is ']', not the path separator used to | |
1401 | // separate the components | |
ab3edace | 1402 | wxString sepPath = format == wxPATH_VMS ? wxString(_T(']')) |
04c943b1 | 1403 | : GetPathSeparators(format); |
9e8d8607 | 1404 | |
04c943b1 VZ |
1405 | // special Windows UNC paths hack: transform \\share\path into share:path |
1406 | if ( format == wxPATH_DOS ) | |
9e8d8607 | 1407 | { |
04c943b1 VZ |
1408 | if ( fullpath.length() >= 4 && |
1409 | fullpath[0u] == wxFILE_SEP_PATH_DOS && | |
1410 | fullpath[1u] == wxFILE_SEP_PATH_DOS ) | |
9e8d8607 | 1411 | { |
04c943b1 VZ |
1412 | fullpath.erase(0, 2); |
1413 | ||
1414 | size_t posFirstSlash = fullpath.find_first_of(sepPath); | |
1415 | if ( posFirstSlash != wxString::npos ) | |
1416 | { | |
1417 | fullpath[posFirstSlash] = wxFILE_SEP_DSK; | |
1418 | ||
1419 | // UNC paths are always absolute, right? (FIXME) | |
1420 | fullpath.insert(posFirstSlash + 1, wxFILE_SEP_PATH_DOS); | |
1421 | } | |
3c621059 JJ |
1422 | } |
1423 | } | |
04c943b1 | 1424 | |
a385b5df RR |
1425 | // We separate the volume here |
1426 | if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
04c943b1 | 1427 | { |
a385b5df | 1428 | wxString sepVol = GetVolumeSeparator(format); |
24eb81cb | 1429 | |
04c943b1 VZ |
1430 | size_t posFirstColon = fullpath.find_first_of(sepVol); |
1431 | if ( posFirstColon != wxString::npos ) | |
1432 | { | |
1433 | if ( pstrVolume ) | |
1434 | { | |
1435 | *pstrVolume = fullpath.Left(posFirstColon); | |
1436 | } | |
1437 | ||
1438 | // remove the volume name and the separator from the full path | |
1439 | fullpath.erase(0, posFirstColon + sepVol.length()); | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | // find the positions of the last dot and last path separator in the path | |
1444 | size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); | |
1445 | size_t posLastSlash = fullpath.find_last_of(sepPath); | |
1446 | ||
1447 | if ( (posLastDot != wxString::npos) && | |
1448 | ((format == wxPATH_UNIX) || (format == wxPATH_VMS)) ) | |
3c621059 JJ |
1449 | { |
1450 | if ( (posLastDot == 0) || | |
04c943b1 | 1451 | (fullpath[posLastDot - 1] == sepPath[0u] ) ) |
3c621059 | 1452 | { |
04c943b1 VZ |
1453 | // under Unix and VMS, dot may be (and commonly is) the first |
1454 | // character of the filename, don't treat the entire filename as | |
1455 | // extension in this case | |
9e8d8607 VZ |
1456 | posLastDot = wxString::npos; |
1457 | } | |
1458 | } | |
1459 | ||
8e7dda21 VZ |
1460 | // if we do have a dot and a slash, check that the dot is in the name part |
1461 | if ( (posLastDot != wxString::npos) && | |
1462 | (posLastSlash != wxString::npos) && | |
1463 | (posLastDot < posLastSlash) ) | |
9e8d8607 VZ |
1464 | { |
1465 | // the dot is part of the path, not the start of the extension | |
1466 | posLastDot = wxString::npos; | |
1467 | } | |
1468 | ||
1469 | // now fill in the variables provided by user | |
1470 | if ( pstrPath ) | |
1471 | { | |
1472 | if ( posLastSlash == wxString::npos ) | |
1473 | { | |
1474 | // no path at all | |
1475 | pstrPath->Empty(); | |
1476 | } | |
1477 | else | |
1478 | { | |
04c943b1 | 1479 | // take everything up to the path separator but take care to make |
353f41cb | 1480 | // the path equal to something like '/', not empty, for the files |
04c943b1 VZ |
1481 | // immediately under root directory |
1482 | size_t len = posLastSlash; | |
1483 | if ( !len ) | |
1484 | len++; | |
1485 | ||
1486 | *pstrPath = fullpath.Left(len); | |
1487 | ||
1488 | // special VMS hack: remove the initial bracket | |
1489 | if ( format == wxPATH_VMS ) | |
1490 | { | |
1491 | if ( (*pstrPath)[0u] == _T('[') ) | |
1492 | pstrPath->erase(0, 1); | |
1493 | } | |
9e8d8607 VZ |
1494 | } |
1495 | } | |
1496 | ||
1497 | if ( pstrName ) | |
1498 | { | |
42b1f941 VZ |
1499 | // take all characters starting from the one after the last slash and |
1500 | // up to, but excluding, the last dot | |
9e8d8607 | 1501 | size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; |
8e7dda21 VZ |
1502 | size_t count; |
1503 | if ( posLastDot == wxString::npos ) | |
1504 | { | |
1505 | // take all until the end | |
1506 | count = wxString::npos; | |
1507 | } | |
1508 | else if ( posLastSlash == wxString::npos ) | |
1509 | { | |
1510 | count = posLastDot; | |
1511 | } | |
1512 | else // have both dot and slash | |
1513 | { | |
1514 | count = posLastDot - posLastSlash - 1; | |
1515 | } | |
9e8d8607 VZ |
1516 | |
1517 | *pstrName = fullpath.Mid(nStart, count); | |
1518 | } | |
1519 | ||
1520 | if ( pstrExt ) | |
1521 | { | |
1522 | if ( posLastDot == wxString::npos ) | |
1523 | { | |
1524 | // no extension | |
1525 | pstrExt->Empty(); | |
1526 | } | |
1527 | else | |
1528 | { | |
1529 | // take everything after the dot | |
1530 | *pstrExt = fullpath.Mid(posLastDot + 1); | |
1531 | } | |
1532 | } | |
1533 | } | |
951cd180 | 1534 | |
6f91bc33 VZ |
1535 | /* static */ |
1536 | void wxFileName::SplitPath(const wxString& fullpath, | |
1537 | wxString *path, | |
1538 | wxString *name, | |
1539 | wxString *ext, | |
1540 | wxPathFormat format) | |
1541 | { | |
1542 | wxString volume; | |
1543 | SplitPath(fullpath, &volume, path, name, ext, format); | |
1544 | ||
1545 | if ( path && !volume.empty() ) | |
1546 | { | |
1547 | path->Prepend(volume + GetVolumeSeparator(format)); | |
1548 | } | |
1549 | } | |
1550 | ||
951cd180 VZ |
1551 | // ---------------------------------------------------------------------------- |
1552 | // time functions | |
1553 | // ---------------------------------------------------------------------------- | |
1554 | ||
1555 | bool wxFileName::SetTimes(const wxDateTime *dtCreate, | |
1556 | const wxDateTime *dtAccess, | |
1557 | const wxDateTime *dtMod) | |
1558 | { | |
d9f54bb0 | 1559 | #if defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) |
246c704f VZ |
1560 | if ( !dtAccess && !dtMod ) |
1561 | { | |
1562 | // can't modify the creation time anyhow, don't try | |
1563 | return TRUE; | |
1564 | } | |
1565 | ||
1566 | // if dtAccess or dtMod is not specified, use the other one (which must be | |
1567 | // non NULL because of the test above) for both times | |
951cd180 | 1568 | utimbuf utm; |
246c704f VZ |
1569 | utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks(); |
1570 | utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); | |
951cd180 VZ |
1571 | if ( utime(GetFullPath(), &utm) == 0 ) |
1572 | { | |
1573 | return TRUE; | |
1574 | } | |
1575 | #elif defined(__WIN32__) | |
1576 | wxFileHandle fh(GetFullPath()); | |
1577 | if ( fh.IsOk() ) | |
1578 | { | |
1579 | FILETIME ftAccess, ftCreate, ftWrite; | |
1580 | ||
1581 | if ( dtCreate ) | |
1582 | ConvertWxToFileTime(&ftCreate, *dtCreate); | |
1583 | if ( dtAccess ) | |
1584 | ConvertWxToFileTime(&ftAccess, *dtAccess); | |
1585 | if ( dtMod ) | |
1586 | ConvertWxToFileTime(&ftWrite, *dtMod); | |
1587 | ||
1588 | if ( ::SetFileTime(fh, | |
1589 | dtCreate ? &ftCreate : NULL, | |
1590 | dtAccess ? &ftAccess : NULL, | |
1591 | dtMod ? &ftWrite : NULL) ) | |
1592 | { | |
1593 | return TRUE; | |
1594 | } | |
1595 | } | |
1596 | #else // other platform | |
1597 | #endif // platforms | |
1598 | ||
1599 | wxLogSysError(_("Failed to modify file times for '%s'"), | |
1600 | GetFullPath().c_str()); | |
1601 | ||
1602 | return FALSE; | |
1603 | } | |
1604 | ||
1605 | bool wxFileName::Touch() | |
1606 | { | |
1607 | #if defined(__UNIX_LIKE__) | |
1608 | // under Unix touching file is simple: just pass NULL to utime() | |
1609 | if ( utime(GetFullPath(), NULL) == 0 ) | |
1610 | { | |
1611 | return TRUE; | |
1612 | } | |
1613 | ||
1614 | wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str()); | |
1615 | ||
1616 | return FALSE; | |
1617 | #else // other platform | |
1618 | wxDateTime dtNow = wxDateTime::Now(); | |
1619 | ||
1620 | return SetTimes(NULL /* don't change create time */, &dtNow, &dtNow); | |
1621 | #endif // platforms | |
1622 | } | |
1623 | ||
1624 | bool wxFileName::GetTimes(wxDateTime *dtAccess, | |
1625 | wxDateTime *dtMod, | |
1626 | wxDateTime *dtChange) const | |
1627 | { | |
d9f54bb0 | 1628 | #if defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__)) |
951cd180 VZ |
1629 | wxStructStat stBuf; |
1630 | if ( wxStat(GetFullPath(), &stBuf) == 0 ) | |
1631 | { | |
1632 | if ( dtAccess ) | |
1633 | dtAccess->Set(stBuf.st_atime); | |
1634 | if ( dtMod ) | |
1635 | dtMod->Set(stBuf.st_mtime); | |
1636 | if ( dtChange ) | |
1637 | dtChange->Set(stBuf.st_ctime); | |
1638 | ||
1639 | return TRUE; | |
1640 | } | |
1641 | #elif defined(__WIN32__) | |
1642 | wxFileHandle fh(GetFullPath()); | |
1643 | if ( fh.IsOk() ) | |
1644 | { | |
1645 | FILETIME ftAccess, ftCreate, ftWrite; | |
1646 | ||
1647 | if ( ::GetFileTime(fh, | |
1648 | dtMod ? &ftCreate : NULL, | |
1649 | dtAccess ? &ftAccess : NULL, | |
1650 | dtChange ? &ftWrite : NULL) ) | |
1651 | { | |
1652 | if ( dtMod ) | |
1653 | ConvertFileTimeToWx(dtMod, ftCreate); | |
1654 | if ( dtAccess ) | |
1655 | ConvertFileTimeToWx(dtAccess, ftAccess); | |
1656 | if ( dtChange ) | |
1657 | ConvertFileTimeToWx(dtChange, ftWrite); | |
1658 | ||
1659 | return TRUE; | |
1660 | } | |
1661 | } | |
1662 | #else // other platform | |
1663 | #endif // platforms | |
1664 | ||
1665 | wxLogSysError(_("Failed to retrieve file times for '%s'"), | |
1666 | GetFullPath().c_str()); | |
1667 | ||
1668 | return FALSE; | |
1669 | } | |
1670 |