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