]>
Commit | Line | Data |
---|---|---|
df5ddbca | 1 | ///////////////////////////////////////////////////////////////////////////// |
097ead30 VZ |
2 | // Name: src/common/filename.cpp |
3 | // Purpose: wxFileName - encapsulates a file path | |
844f90fb | 4 | // Author: Robert Roebling, Vadim Zeitlin |
df5ddbca RR |
5 | // Modified by: |
6 | // Created: 28.12.2000 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 Robert Roebling | |
65571936 | 9 | // Licence: wxWindows licence |
df5ddbca RR |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
04c943b1 VZ |
12 | /* |
13 | Here are brief descriptions of the filename formats supported by this class: | |
14 | ||
2bc60417 RR |
15 | wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file |
16 | names have the form: | |
04c943b1 VZ |
17 | /dir1/dir2/.../dirN/filename, "." and ".." stand for the |
18 | current and parent directory respectively, "~" is parsed as the | |
19 | user HOME and "~username" as the HOME of that user | |
20 | ||
2bc60417 | 21 | wxPATH_DOS: DOS/Windows format, absolute file names have the form: |
04c943b1 VZ |
22 | drive:\dir1\dir2\...\dirN\filename.ext where drive is a single |
23 | letter. "." and ".." as for Unix but no "~". | |
24 | ||
e01a788e VZ |
25 | There are also UNC names of the form \\share\fullpath and |
26 | MSW unique volume names of the form \\?\Volume{GUID}\fullpath. | |
27 | ||
28 | The latter provide a uniform way to access a volume regardless of | |
29 | its current mount point, i.e. you can change a volume's mount | |
30 | point from D: to E:, or even remove it, and still be able to | |
31 | access it through its unique volume name. More on the subject can | |
32 | be found in MSDN's article "Naming a Volume" that is currently at | |
33 | http://msdn.microsoft.com/en-us/library/aa365248(VS.85).aspx. | |
34 | ||
04c943b1 | 35 | |
2415cf67 | 36 | wxPATH_MAC: Mac OS 8/9 only, not used any longer, absolute file |
2bc60417 | 37 | names have the form |
04c943b1 VZ |
38 | volume:dir1:...:dirN:filename |
39 | and the relative file names are either | |
40 | :dir1:...:dirN:filename | |
41 | or just | |
42 | filename | |
43 | (although :filename works as well). | |
a385b5df | 44 | Since the volume is just part of the file path, it is not |
353f41cb | 45 | treated like a separate entity as it is done under DOS and |
90a68369 | 46 | VMS, it is just treated as another dir. |
04c943b1 VZ |
47 | |
48 | wxPATH_VMS: VMS native format, absolute file names have the form | |
49 | <device>:[dir1.dir2.dir3]file.txt | |
50 | or | |
51 | <device>:[000000.dir1.dir2.dir3]file.txt | |
52 | ||
53 | the <device> is the physical device (i.e. disk). 000000 is the | |
54 | root directory on the device which can be omitted. | |
55 | ||
56 | Note that VMS uses different separators unlike Unix: | |
57 | : always after the device. If the path does not contain : than | |
58 | the default (the device of the current directory) is assumed. | |
ba623ba2 | 59 | [ start of directory specification |
04c943b1 VZ |
60 | . separator between directory and subdirectory |
61 | ] between directory and file | |
62 | */ | |
63 | ||
097ead30 VZ |
64 | // ============================================================================ |
65 | // declarations | |
66 | // ============================================================================ | |
67 | ||
68 | // ---------------------------------------------------------------------------- | |
69 | // headers | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
df5ddbca RR |
72 | // For compilers that support precompilation, includes "wx.h". |
73 | #include "wx/wxprec.h" | |
74 | ||
75 | #ifdef __BORLANDC__ | |
0b9ab0bd | 76 | #pragma hdrstop |
df5ddbca RR |
77 | #endif |
78 | ||
79 | #ifndef WX_PRECOMP | |
d98a58c5 | 80 | #ifdef __WINDOWS__ |
57bd4c60 WS |
81 | #include "wx/msw/wrapwin.h" // For GetShort/LongPathName |
82 | #endif | |
ad9835c9 WS |
83 | #include "wx/dynarray.h" |
84 | #include "wx/intl.h" | |
85 | #include "wx/log.h" | |
de6185e2 | 86 | #include "wx/utils.h" |
0bf751e7 | 87 | #include "wx/crt.h" |
df5ddbca RR |
88 | #endif |
89 | ||
90 | #include "wx/filename.h" | |
b70a2866 | 91 | #include "wx/private/filename.h" |
df5ddbca | 92 | #include "wx/tokenzr.h" |
844f90fb | 93 | #include "wx/config.h" // for wxExpandEnvVars |
35a6691a | 94 | #include "wx/dynlib.h" |
110c5094 | 95 | #include "wx/dir.h" |
df5ddbca | 96 | |
57bd4c60 WS |
97 | #if defined(__WIN32__) && defined(__MINGW32__) |
98 | #include "wx/msw/gccpriv.h" | |
951cd180 VZ |
99 | #endif |
100 | ||
d98a58c5 | 101 | #ifdef __WINDOWS__ |
1c193821 JS |
102 | #include "wx/msw/private.h" |
103 | #endif | |
104 | ||
76a5e5d2 | 105 | #if defined(__WXMAC__) |
c933e267 | 106 | #include "wx/osx/private.h" // includes mac headers |
76a5e5d2 SC |
107 | #endif |
108 | ||
951cd180 VZ |
109 | // utime() is POSIX so should normally be available on all Unices |
110 | #ifdef __UNIX_LIKE__ | |
0b9ab0bd RL |
111 | #include <sys/types.h> |
112 | #include <utime.h> | |
113 | #include <sys/stat.h> | |
114 | #include <unistd.h> | |
9e9b65c1 JS |
115 | #endif |
116 | ||
444d61ba VS |
117 | #ifdef __DJGPP__ |
118 | #include <unistd.h> | |
119 | #endif | |
120 | ||
d9f54bb0 | 121 | #ifdef __WATCOMC__ |
0b9ab0bd RL |
122 | #include <io.h> |
123 | #include <sys/utime.h> | |
124 | #include <sys/stat.h> | |
d9f54bb0 VS |
125 | #endif |
126 | ||
24eb81cb DW |
127 | #ifdef __VISAGECPP__ |
128 | #ifndef MAX_PATH | |
129 | #define MAX_PATH 256 | |
130 | #endif | |
131 | #endif | |
132 | ||
01c9a906 | 133 | #ifdef __EMX__ |
5d66debf | 134 | #include <os2.h> |
01c9a906 SN |
135 | #define MAX_PATH _MAX_PATH |
136 | #endif | |
137 | ||
901504c3 VZ |
138 | #ifndef S_ISREG |
139 | #define S_ISREG(mode) ((mode) & S_IFREG) | |
140 | #endif | |
141 | #ifndef S_ISDIR | |
142 | #define S_ISDIR(mode) ((mode) & S_IFDIR) | |
143 | #endif | |
23b8a262 | 144 | |
bd08f2f7 | 145 | #if wxUSE_LONGLONG |
060668a1 | 146 | extern const wxULongLong wxInvalidSize = (unsigned)-1; |
bd08f2f7 | 147 | #endif // wxUSE_LONGLONG |
23b8a262 | 148 | |
7b611a3a VZ |
149 | namespace |
150 | { | |
23b8a262 | 151 | |
951cd180 VZ |
152 | // ---------------------------------------------------------------------------- |
153 | // private classes | |
154 | // ---------------------------------------------------------------------------- | |
155 | ||
156 | // small helper class which opens and closes the file - we use it just to get | |
157 | // a file handle for the given file name to pass it to some Win32 API function | |
c67d6888 | 158 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
951cd180 VZ |
159 | |
160 | class wxFileHandle | |
161 | { | |
162 | public: | |
6dbb903b VZ |
163 | enum OpenMode |
164 | { | |
f3c74c8d VZ |
165 | ReadAttr, |
166 | WriteAttr | |
6dbb903b VZ |
167 | }; |
168 | ||
6bc176b4 | 169 | wxFileHandle(const wxString& filename, OpenMode mode, int flags = 0) |
951cd180 | 170 | { |
f3c74c8d VZ |
171 | // be careful and use FILE_{READ,WRITE}_ATTRIBUTES here instead of the |
172 | // usual GENERIC_{READ,WRITE} as we don't want the file access time to | |
173 | // be changed when we open it because this class is used for setting | |
174 | // access time (see #10567) | |
951cd180 VZ |
175 | m_hFile = ::CreateFile |
176 | ( | |
715e4f7e | 177 | filename.t_str(), // name |
f3c74c8d VZ |
178 | mode == ReadAttr ? FILE_READ_ATTRIBUTES // access mask |
179 | : FILE_WRITE_ATTRIBUTES, | |
54bcff35 VZ |
180 | FILE_SHARE_READ | // sharing mode |
181 | FILE_SHARE_WRITE, // (allow everything) | |
6dbb903b VZ |
182 | NULL, // no secutity attr |
183 | OPEN_EXISTING, // creation disposition | |
6bc176b4 | 184 | flags, // flags |
6dbb903b | 185 | NULL // no template file |
951cd180 VZ |
186 | ); |
187 | ||
188 | if ( m_hFile == INVALID_HANDLE_VALUE ) | |
189 | { | |
f3c74c8d | 190 | if ( mode == ReadAttr ) |
43b2d5e7 | 191 | { |
49a70977 VS |
192 | wxLogSysError(_("Failed to open '%s' for reading"), |
193 | filename.c_str()); | |
43b2d5e7 | 194 | } |
49a70977 | 195 | else |
43b2d5e7 | 196 | { |
49a70977 VS |
197 | wxLogSysError(_("Failed to open '%s' for writing"), |
198 | filename.c_str()); | |
43b2d5e7 | 199 | } |
951cd180 VZ |
200 | } |
201 | } | |
202 | ||
203 | ~wxFileHandle() | |
204 | { | |
205 | if ( m_hFile != INVALID_HANDLE_VALUE ) | |
206 | { | |
207 | if ( !::CloseHandle(m_hFile) ) | |
208 | { | |
209 | wxLogSysError(_("Failed to close file handle")); | |
210 | } | |
211 | } | |
212 | } | |
213 | ||
f363e05c | 214 | // return true only if the file could be opened successfully |
951cd180 VZ |
215 | bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; } |
216 | ||
217 | // get the handle | |
218 | operator HANDLE() const { return m_hFile; } | |
219 | ||
220 | private: | |
221 | HANDLE m_hFile; | |
222 | }; | |
223 | ||
224 | #endif // __WIN32__ | |
225 | ||
226 | // ---------------------------------------------------------------------------- | |
227 | // private functions | |
228 | // ---------------------------------------------------------------------------- | |
229 | ||
e2b87f38 | 230 | #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__) |
951cd180 VZ |
231 | |
232 | // convert between wxDateTime and FILETIME which is a 64-bit value representing | |
233 | // the number of 100-nanosecond intervals since January 1, 1601. | |
234 | ||
d56e2b97 | 235 | static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft) |
951cd180 | 236 | { |
752f10a8 | 237 | FILETIME ftcopy = ft; |
6dbb903b | 238 | FILETIME ftLocal; |
752f10a8 | 239 | if ( !::FileTimeToLocalFileTime(&ftcopy, &ftLocal) ) |
6dbb903b | 240 | { |
9a83f860 | 241 | wxLogLastError(wxT("FileTimeToLocalFileTime")); |
6dbb903b | 242 | } |
d56e2b97 | 243 | |
6dbb903b VZ |
244 | SYSTEMTIME st; |
245 | if ( !::FileTimeToSystemTime(&ftLocal, &st) ) | |
246 | { | |
9a83f860 | 247 | wxLogLastError(wxT("FileTimeToSystemTime")); |
6dbb903b | 248 | } |
d56e2b97 | 249 | |
6dbb903b VZ |
250 | dt->Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear, |
251 | st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); | |
951cd180 VZ |
252 | } |
253 | ||
d56e2b97 | 254 | static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) |
951cd180 | 255 | { |
6dbb903b VZ |
256 | SYSTEMTIME st; |
257 | st.wDay = dt.GetDay(); | |
ad816476 WS |
258 | st.wMonth = (WORD)(dt.GetMonth() + 1); |
259 | st.wYear = (WORD)dt.GetYear(); | |
6dbb903b VZ |
260 | st.wHour = dt.GetHour(); |
261 | st.wMinute = dt.GetMinute(); | |
262 | st.wSecond = dt.GetSecond(); | |
263 | st.wMilliseconds = dt.GetMillisecond(); | |
264 | ||
265 | FILETIME ftLocal; | |
266 | if ( !::SystemTimeToFileTime(&st, &ftLocal) ) | |
267 | { | |
9a83f860 | 268 | wxLogLastError(wxT("SystemTimeToFileTime")); |
6dbb903b | 269 | } |
d56e2b97 | 270 | |
6dbb903b VZ |
271 | if ( !::LocalFileTimeToFileTime(&ftLocal, ft) ) |
272 | { | |
9a83f860 | 273 | wxLogLastError(wxT("LocalFileTimeToFileTime")); |
6dbb903b | 274 | } |
951cd180 VZ |
275 | } |
276 | ||
e2b87f38 | 277 | #endif // wxUSE_DATETIME && __WIN32__ |
951cd180 | 278 | |
67c34f64 VZ |
279 | // return a string with the volume par |
280 | static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format) | |
281 | { | |
282 | wxString path; | |
283 | ||
284 | if ( !volume.empty() ) | |
285 | { | |
6ce27aa2 VZ |
286 | format = wxFileName::GetFormat(format); |
287 | ||
67c34f64 VZ |
288 | // Special Windows UNC paths hack, part 2: undo what we did in |
289 | // SplitPath() and make an UNC path if we have a drive which is not a | |
290 | // single letter (hopefully the network shares can't be one letter only | |
291 | // although I didn't find any authoritative docs on this) | |
292 | if ( format == wxPATH_DOS && volume.length() > 1 ) | |
293 | { | |
e01a788e VZ |
294 | // We also have to check for Windows unique volume names here and |
295 | // return it with '\\?\' prepended to it | |
296 | if ( wxFileName::IsMSWUniqueVolumeNamePath("\\\\?\\" + volume + "\\", | |
297 | format) ) | |
298 | { | |
299 | path << "\\\\?\\" << volume; | |
300 | } | |
301 | else | |
302 | { | |
303 | // it must be a UNC path | |
304 | path << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << volume; | |
305 | } | |
67c34f64 VZ |
306 | } |
307 | else if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
308 | { | |
309 | path << volume << wxFileName::GetVolumeSeparator(format); | |
310 | } | |
311 | // else ignore | |
312 | } | |
313 | ||
314 | return path; | |
315 | } | |
316 | ||
7b611a3a VZ |
317 | // return true if the character is a DOS path separator i.e. either a slash or |
318 | // a backslash | |
319 | inline bool IsDOSPathSep(wxUniChar ch) | |
320 | { | |
321 | return ch == wxFILE_SEP_PATH_DOS || ch == wxFILE_SEP_PATH_UNIX; | |
322 | } | |
323 | ||
9e1c7236 VZ |
324 | // return true if the format used is the DOS/Windows one and the string looks |
325 | // like a UNC path | |
326 | static bool IsUNCPath(const wxString& path, wxPathFormat format) | |
327 | { | |
328 | return format == wxPATH_DOS && | |
329 | path.length() >= 4 && // "\\a" can't be a UNC path | |
7b611a3a VZ |
330 | IsDOSPathSep(path[0u]) && |
331 | IsDOSPathSep(path[1u]) && | |
332 | !IsDOSPathSep(path[2u]); | |
9e1c7236 VZ |
333 | } |
334 | ||
c063adeb VZ |
335 | #ifndef __WIN32__ |
336 | ||
337 | // Under Unix-ish systems (basically everything except Windows) we may work | |
c50db847 VZ |
338 | // either with the file itself or its target if it's a symbolic link and we |
339 | // should dereference it, as determined by wxFileName::ShouldFollowLink() and | |
340 | // the absence of the wxFILE_EXISTS_NO_FOLLOW flag. StatAny() can be used to | |
341 | // stat the appropriate file with an extra twist that it also works when there | |
342 | // is no wxFileName object at all, as is the case in static methods. | |
c063adeb VZ |
343 | |
344 | // Private implementation, don't call directly, use one of the overloads below. | |
c50db847 | 345 | bool DoStatAny(wxStructStat& st, wxString path, bool dereference) |
c063adeb VZ |
346 | { |
347 | // We need to remove any trailing slashes from the path because they could | |
348 | // interfere with the symlink following decision: even if we use lstat(), | |
349 | // it would still follow the symlink if we pass it a path with a slash at | |
350 | // the end because the symlink resolution would happen while following the | |
351 | // path and not for the last path element itself. | |
352 | ||
353 | while ( wxEndsWithPathSeparator(path) ) | |
1e9b3eff VZ |
354 | { |
355 | const size_t posLast = path.length() - 1; | |
356 | if ( !posLast ) | |
357 | { | |
358 | // Don't turn "/" into empty string. | |
359 | break; | |
360 | } | |
361 | ||
362 | path.erase(posLast); | |
363 | } | |
c063adeb | 364 | |
c50db847 | 365 | int ret = dereference ? wxStat(path, &st) : wxLstat(path, &st); |
c063adeb VZ |
366 | return ret == 0; |
367 | } | |
368 | ||
369 | // Overloads to use for a case when we don't have wxFileName object and when we | |
370 | // do have one. | |
371 | inline | |
c50db847 | 372 | bool StatAny(wxStructStat& st, const wxString& path, int flags) |
c063adeb | 373 | { |
c50db847 | 374 | return DoStatAny(st, path, !(flags & wxFILE_EXISTS_NO_FOLLOW)); |
c063adeb VZ |
375 | } |
376 | ||
377 | inline | |
378 | bool StatAny(wxStructStat& st, const wxFileName& fn) | |
379 | { | |
c50db847 | 380 | return DoStatAny(st, fn.GetFullPath(), fn.ShouldFollowLink()); |
c063adeb VZ |
381 | } |
382 | ||
383 | #endif // !__WIN32__ | |
384 | ||
e01a788e VZ |
385 | // ---------------------------------------------------------------------------- |
386 | // private constants | |
387 | // ---------------------------------------------------------------------------- | |
388 | ||
389 | // length of \\?\Volume{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\ string | |
390 | static const size_t wxMSWUniqueVolumePrefixLength = 49; | |
391 | ||
7b611a3a VZ |
392 | } // anonymous namespace |
393 | ||
097ead30 VZ |
394 | // ============================================================================ |
395 | // implementation | |
396 | // ============================================================================ | |
397 | ||
398 | // ---------------------------------------------------------------------------- | |
844f90fb | 399 | // wxFileName construction |
097ead30 | 400 | // ---------------------------------------------------------------------------- |
df5ddbca | 401 | |
a35b27b1 RR |
402 | void wxFileName::Assign( const wxFileName &filepath ) |
403 | { | |
04c943b1 | 404 | m_volume = filepath.GetVolume(); |
844f90fb | 405 | m_dirs = filepath.GetDirs(); |
04c943b1 VZ |
406 | m_name = filepath.GetName(); |
407 | m_ext = filepath.GetExt(); | |
a2fa5040 | 408 | m_relative = filepath.m_relative; |
dfecbee5 | 409 | m_hasExt = filepath.m_hasExt; |
c063adeb | 410 | m_dontFollowLinks = filepath.m_dontFollowLinks; |
df5ddbca RR |
411 | } |
412 | ||
04c943b1 VZ |
413 | void wxFileName::Assign(const wxString& volume, |
414 | const wxString& path, | |
415 | const wxString& name, | |
416 | const wxString& ext, | |
dfecbee5 | 417 | bool hasExt, |
9e1c7236 | 418 | wxPathFormat format) |
a7b51bc8 | 419 | { |
9e1c7236 VZ |
420 | // we should ignore paths which look like UNC shares because we already |
421 | // have the volume here and the UNC notation (\\server\path) is only valid | |
422 | // for paths which don't start with a volume, so prevent SetPath() from | |
423 | // recognizing "\\foo\bar" in "c:\\foo\bar" as an UNC path | |
424 | // | |
425 | // note also that this is a rather ugly way to do what we want (passing | |
426 | // some kind of flag telling to ignore UNC paths to SetPath() would be | |
427 | // better) but this is the safest thing to do to avoid breaking backwards | |
428 | // compatibility in 2.8 | |
429 | if ( IsUNCPath(path, format) ) | |
430 | { | |
431 | // remove one of the 2 leading backslashes to ensure that it's not | |
432 | // recognized as an UNC path by SetPath() | |
433 | wxString pathNonUNC(path, 1, wxString::npos); | |
434 | SetPath(pathNonUNC, format); | |
435 | } | |
436 | else // no UNC complications | |
437 | { | |
438 | SetPath(path, format); | |
439 | } | |
a7b51bc8 RR |
440 | |
441 | m_volume = volume; | |
442 | m_ext = ext; | |
443 | m_name = name; | |
dfecbee5 VZ |
444 | |
445 | m_hasExt = hasExt; | |
a7b51bc8 RR |
446 | } |
447 | ||
f1e77933 | 448 | void wxFileName::SetPath( const wxString& pathOrig, wxPathFormat format ) |
df5ddbca | 449 | { |
844f90fb | 450 | m_dirs.Clear(); |
90a68369 | 451 | |
f1e77933 | 452 | if ( pathOrig.empty() ) |
df5ddbca | 453 | { |
f1e77933 VZ |
454 | // no path at all |
455 | m_relative = true; | |
a2fa5040 | 456 | |
f1e77933 VZ |
457 | return; |
458 | } | |
90a68369 | 459 | |
f1e77933 | 460 | format = GetFormat( format ); |
a2fa5040 | 461 | |
f1e77933 VZ |
462 | // 0) deal with possible volume part first |
463 | wxString volume, | |
464 | path; | |
465 | SplitVolume(pathOrig, &volume, &path, format); | |
466 | if ( !volume.empty() ) | |
467 | { | |
468 | m_relative = false; | |
a2fa5040 | 469 | |
f1e77933 VZ |
470 | SetVolume(volume); |
471 | } | |
f363e05c | 472 | |
f1e77933 | 473 | // 1) Determine if the path is relative or absolute. |
0f506ded VZ |
474 | |
475 | if ( path.empty() ) | |
476 | { | |
477 | // we had only the volume | |
478 | return; | |
479 | } | |
480 | ||
f1e77933 | 481 | wxChar leadingChar = path[0u]; |
a2fa5040 | 482 | |
f1e77933 VZ |
483 | switch (format) |
484 | { | |
485 | case wxPATH_MAC: | |
486 | m_relative = leadingChar == wxT(':'); | |
487 | ||
488 | // We then remove a leading ":". The reason is in our | |
489 | // storage form for relative paths: | |
490 | // ":dir:file.txt" actually means "./dir/file.txt" in | |
491 | // DOS notation and should get stored as | |
492 | // (relative) (dir) (file.txt) | |
493 | // "::dir:file.txt" actually means "../dir/file.txt" | |
494 | // stored as (relative) (..) (dir) (file.txt) | |
495 | // This is important only for the Mac as an empty dir | |
496 | // actually means <UP>, whereas under DOS, double | |
497 | // slashes can be ignored: "\\\\" is the same as "\\". | |
498 | if (m_relative) | |
499 | path.erase( 0, 1 ); | |
500 | break; | |
a2fa5040 | 501 | |
f1e77933 VZ |
502 | case wxPATH_VMS: |
503 | // TODO: what is the relative path format here? | |
504 | m_relative = false; | |
505 | break; | |
90a68369 | 506 | |
f1e77933 | 507 | default: |
9a83f860 | 508 | wxFAIL_MSG( wxT("Unknown path format") ); |
f1e77933 | 509 | // !! Fall through !! |
90a68369 | 510 | |
f1e77933 | 511 | case wxPATH_UNIX: |
be5be16a | 512 | m_relative = leadingChar != wxT('/'); |
f1e77933 | 513 | break; |
353f41cb | 514 | |
f1e77933 VZ |
515 | case wxPATH_DOS: |
516 | m_relative = !IsPathSeparator(leadingChar, format); | |
517 | break; | |
844f90fb | 518 | |
df5ddbca | 519 | } |
f1e77933 VZ |
520 | |
521 | // 2) Break up the path into its members. If the original path | |
522 | // was just "/" or "\\", m_dirs will be empty. We know from | |
523 | // the m_relative field, if this means "nothing" or "root dir". | |
524 | ||
525 | wxStringTokenizer tn( path, GetPathSeparators(format) ); | |
526 | ||
527 | while ( tn.HasMoreTokens() ) | |
a7b51bc8 | 528 | { |
f1e77933 VZ |
529 | wxString token = tn.GetNextToken(); |
530 | ||
531 | // Remove empty token under DOS and Unix, interpret them | |
532 | // as .. under Mac. | |
533 | if (token.empty()) | |
534 | { | |
535 | if (format == wxPATH_MAC) | |
536 | m_dirs.Add( wxT("..") ); | |
537 | // else ignore | |
538 | } | |
539 | else | |
540 | { | |
541 | m_dirs.Add( token ); | |
542 | } | |
a7b51bc8 | 543 | } |
844f90fb VZ |
544 | } |
545 | ||
546 | void wxFileName::Assign(const wxString& fullpath, | |
547 | wxPathFormat format) | |
548 | { | |
04c943b1 | 549 | wxString volume, path, name, ext; |
dfecbee5 VZ |
550 | bool hasExt; |
551 | SplitPath(fullpath, &volume, &path, &name, &ext, &hasExt, format); | |
844f90fb | 552 | |
dfecbee5 | 553 | Assign(volume, path, name, ext, hasExt, format); |
844f90fb VZ |
554 | } |
555 | ||
81f25632 | 556 | void wxFileName::Assign(const wxString& fullpathOrig, |
844f90fb VZ |
557 | const wxString& fullname, |
558 | wxPathFormat format) | |
559 | { | |
81f25632 VZ |
560 | // always recognize fullpath as directory, even if it doesn't end with a |
561 | // slash | |
562 | wxString fullpath = fullpathOrig; | |
69858116 | 563 | if ( !fullpath.empty() && !wxEndsWithPathSeparator(fullpath) ) |
81f25632 | 564 | { |
33b97389 | 565 | fullpath += GetPathSeparator(format); |
81f25632 VZ |
566 | } |
567 | ||
52dbd056 | 568 | wxString volume, path, name, ext; |
dfecbee5 | 569 | bool hasExt; |
81f25632 | 570 | |
4b6a582b VZ |
571 | // do some consistency checks: the name should be really just the filename |
572 | // and the path should be really just a path | |
dfecbee5 | 573 | wxString volDummy, pathDummy, nameDummy, extDummy; |
81f25632 | 574 | |
dfecbee5 | 575 | SplitPath(fullname, &volDummy, &pathDummy, &name, &ext, &hasExt, format); |
81f25632 | 576 | |
dfecbee5 | 577 | wxASSERT_MSG( volDummy.empty() && pathDummy.empty(), |
9a83f860 | 578 | wxT("the file name shouldn't contain the path") ); |
81f25632 VZ |
579 | |
580 | SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format); | |
581 | ||
4c914788 JJ |
582 | #ifndef __VMS |
583 | // This test makes no sense on an OpenVMS system. | |
584 | wxASSERT_MSG( nameDummy.empty() && extDummy.empty(), | |
9a83f860 | 585 | wxT("the path shouldn't contain file name nor extension") ); |
4c914788 | 586 | #endif |
dfecbee5 | 587 | Assign(volume, path, name, ext, hasExt, format); |
52dbd056 VZ |
588 | } |
589 | ||
4c2deb19 VZ |
590 | void wxFileName::Assign(const wxString& pathOrig, |
591 | const wxString& name, | |
592 | const wxString& ext, | |
593 | wxPathFormat format) | |
594 | { | |
595 | wxString volume, | |
596 | path; | |
597 | SplitVolume(pathOrig, &volume, &path, format); | |
598 | ||
599 | Assign(volume, path, name, ext, format); | |
600 | } | |
601 | ||
52dbd056 VZ |
602 | void wxFileName::AssignDir(const wxString& dir, wxPathFormat format) |
603 | { | |
b494c48b | 604 | Assign(dir, wxEmptyString, format); |
844f90fb VZ |
605 | } |
606 | ||
607 | void wxFileName::Clear() | |
608 | { | |
609 | m_dirs.Clear(); | |
04c943b1 VZ |
610 | |
611 | m_volume = | |
844f90fb VZ |
612 | m_name = |
613 | m_ext = wxEmptyString; | |
fb969475 VZ |
614 | |
615 | // we don't have any absolute path for now | |
f363e05c | 616 | m_relative = true; |
dfecbee5 VZ |
617 | |
618 | // nor any extension | |
619 | m_hasExt = false; | |
c063adeb VZ |
620 | |
621 | // follow symlinks by default | |
622 | m_dontFollowLinks = false; | |
844f90fb VZ |
623 | } |
624 | ||
625 | /* static */ | |
520200fd | 626 | wxFileName wxFileName::FileName(const wxString& file, wxPathFormat format) |
844f90fb | 627 | { |
520200fd | 628 | return wxFileName(file, format); |
844f90fb VZ |
629 | } |
630 | ||
631 | /* static */ | |
520200fd | 632 | wxFileName wxFileName::DirName(const wxString& dir, wxPathFormat format) |
844f90fb VZ |
633 | { |
634 | wxFileName fn; | |
520200fd | 635 | fn.AssignDir(dir, format); |
844f90fb | 636 | return fn; |
df5ddbca RR |
637 | } |
638 | ||
844f90fb VZ |
639 | // ---------------------------------------------------------------------------- |
640 | // existence tests | |
641 | // ---------------------------------------------------------------------------- | |
642 | ||
901504c3 | 643 | namespace |
df5ddbca | 644 | { |
a35b27b1 | 645 | |
901504c3 | 646 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
df5ddbca | 647 | |
901504c3 | 648 | void RemoveTrailingSeparatorsFromPath(wxString& strPath) |
df5ddbca | 649 | { |
5bb59666 VZ |
650 | // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists, |
651 | // so remove all trailing backslashes from the path - but don't do this for | |
e01a788e VZ |
652 | // the paths "d:\" (which are different from "d:"), for just "\" or for |
653 | // windows unique volume names ("\\?\Volume{GUID}\") | |
901504c3 | 654 | while ( wxEndsWithPathSeparator( strPath ) ) |
5bb59666 VZ |
655 | { |
656 | size_t len = strPath.length(); | |
e01a788e | 657 | if ( len == 1 || (len == 3 && strPath[len - 2] == wxT(':')) || |
901504c3 VZ |
658 | (len == wxMSWUniqueVolumePrefixLength && |
659 | wxFileName::IsMSWUniqueVolumeNamePath(strPath))) | |
e01a788e | 660 | { |
5bb59666 | 661 | break; |
e01a788e | 662 | } |
5bb59666 VZ |
663 | |
664 | strPath.Truncate(len - 1); | |
665 | } | |
901504c3 | 666 | } |
5bb59666 | 667 | |
901504c3 | 668 | #endif // __WINDOWS__ || __OS2__ |
5bb59666 | 669 | |
c063adeb | 670 | bool |
c50db847 | 671 | wxFileSystemObjectExists(const wxString& path, int flags) |
901504c3 | 672 | { |
c063adeb | 673 | |
901504c3 VZ |
674 | // Should the existence of file/directory with this name be accepted, i.e. |
675 | // result in the true return value from this function? | |
c50db847 VZ |
676 | const bool acceptFile = (flags & wxFILE_EXISTS_REGULAR) != 0; |
677 | const bool acceptDir = (flags & wxFILE_EXISTS_DIR) != 0; | |
901504c3 VZ |
678 | |
679 | wxString strPath(path); | |
680 | ||
681 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
682 | if ( acceptDir ) | |
683 | { | |
684 | // Ensure that the path doesn't have any trailing separators when | |
685 | // checking for directories. | |
686 | RemoveTrailingSeparatorsFromPath(strPath); | |
687 | } | |
688 | ||
689 | // we must use GetFileAttributes() instead of the ANSI C functions because | |
690 | // it can cope with network (UNC) paths unlike them | |
9696657f | 691 | DWORD ret = ::GetFileAttributes(strPath.t_str()); |
901504c3 VZ |
692 | |
693 | if ( ret == INVALID_FILE_ATTRIBUTES ) | |
694 | return false; | |
695 | ||
696 | if ( ret & FILE_ATTRIBUTE_DIRECTORY ) | |
697 | return acceptDir; | |
5bb59666 | 698 | |
901504c3 VZ |
699 | // Anything else must be a file (perhaps we should check for |
700 | // FILE_ATTRIBUTE_REPARSE_POINT?) | |
701 | return acceptFile; | |
5bb59666 | 702 | #elif defined(__OS2__) |
901504c3 VZ |
703 | if ( acceptDir ) |
704 | { | |
705 | // OS/2 can't handle "d:", it wants either "d:\" or "d:." | |
706 | if (strPath.length() == 2 && strPath[1u] == wxT(':')) | |
707 | strPath << wxT('.'); | |
708 | } | |
709 | ||
5bb59666 VZ |
710 | FILESTATUS3 Info = {{0}}; |
711 | APIRET rc = ::DosQueryPathInfo((PSZ)(WXSTRINGCAST strPath), FIL_STANDARD, | |
901504c3 VZ |
712 | (void*) &Info, sizeof(FILESTATUS3)); |
713 | ||
714 | if ( rc == NO_ERROR ) | |
715 | { | |
716 | if ( Info.attrFile & FILE_DIRECTORY ) | |
717 | return acceptDir; | |
718 | else | |
719 | return acceptFile; | |
720 | } | |
5bb59666 | 721 | |
901504c3 VZ |
722 | // We consider that the path must exist if we get a sharing violation for |
723 | // it but we don't know what is it in this case. | |
724 | if ( rc == ERROR_SHARING_VIOLATION ) | |
c50db847 | 725 | return flags & wxFILE_EXISTS_ANY; |
5bb59666 | 726 | |
901504c3 VZ |
727 | // Any other error (usually ERROR_PATH_NOT_FOUND), means there is nothing |
728 | // there. | |
729 | return false; | |
730 | #else // Non-MSW, non-OS/2 | |
5bb59666 | 731 | wxStructStat st; |
c50db847 | 732 | if ( !StatAny(st, strPath, flags) ) |
901504c3 | 733 | return false; |
5bb59666 | 734 | |
901504c3 VZ |
735 | if ( S_ISREG(st.st_mode) ) |
736 | return acceptFile; | |
737 | if ( S_ISDIR(st.st_mode) ) | |
738 | return acceptDir; | |
c50db847 VZ |
739 | if ( S_ISLNK(st.st_mode) ) |
740 | return (flags & wxFILE_EXISTS_SYMLINK) != 0; | |
741 | if ( S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) ) | |
742 | return (flags & wxFILE_EXISTS_DEVICE) != 0; | |
743 | if ( S_ISFIFO(st.st_mode) ) | |
744 | return (flags & wxFILE_EXISTS_FIFO) != 0; | |
745 | if ( S_ISSOCK(st.st_mode) ) | |
746 | return (flags & wxFILE_EXISTS_SOCKET) != 0; | |
747 | ||
748 | return flags & wxFILE_EXISTS_ANY; | |
901504c3 VZ |
749 | #endif // Platforms |
750 | } | |
751 | ||
752 | } // anonymous namespace | |
753 | ||
754 | bool wxFileName::FileExists() const | |
755 | { | |
c50db847 VZ |
756 | int flags = wxFILE_EXISTS_REGULAR; |
757 | if ( !ShouldFollowLink() ) | |
758 | flags |= wxFILE_EXISTS_NO_FOLLOW; | |
759 | ||
760 | return wxFileSystemObjectExists(GetFullPath(), flags); | |
901504c3 VZ |
761 | } |
762 | ||
763 | /* static */ | |
764 | bool wxFileName::FileExists( const wxString &filePath ) | |
765 | { | |
c50db847 | 766 | return wxFileSystemObjectExists(filePath, wxFILE_EXISTS_REGULAR); |
901504c3 VZ |
767 | } |
768 | ||
769 | bool wxFileName::DirExists() const | |
770 | { | |
c50db847 VZ |
771 | int flags = wxFILE_EXISTS_DIR; |
772 | if ( !ShouldFollowLink() ) | |
773 | flags |= wxFILE_EXISTS_NO_FOLLOW; | |
774 | ||
988f7eec | 775 | return Exists(GetPath(), flags); |
901504c3 VZ |
776 | } |
777 | ||
778 | /* static */ | |
779 | bool wxFileName::DirExists( const wxString &dirPath ) | |
780 | { | |
c50db847 | 781 | return wxFileSystemObjectExists(dirPath, wxFILE_EXISTS_DIR); |
df5ddbca RR |
782 | } |
783 | ||
c50db847 | 784 | bool wxFileName::Exists(int flags) const |
c063adeb | 785 | { |
c50db847 VZ |
786 | // Notice that wxFILE_EXISTS_NO_FOLLOW may be specified in the flags even |
787 | // if our DontFollowLink() hadn't been called and we do honour it then. But | |
788 | // if the user took the care of calling DontFollowLink(), it is always | |
789 | // taken into account. | |
790 | if ( !ShouldFollowLink() ) | |
791 | flags |= wxFILE_EXISTS_NO_FOLLOW; | |
792 | ||
793 | return wxFileSystemObjectExists(GetFullPath(), flags); | |
c063adeb VZ |
794 | } |
795 | ||
996d3fe3 | 796 | /* static */ |
c50db847 | 797 | bool wxFileName::Exists(const wxString& path, int flags) |
996d3fe3 | 798 | { |
c50db847 | 799 | return wxFileSystemObjectExists(path, flags); |
996d3fe3 VZ |
800 | } |
801 | ||
844f90fb VZ |
802 | // ---------------------------------------------------------------------------- |
803 | // CWD and HOME stuff | |
804 | // ---------------------------------------------------------------------------- | |
805 | ||
6f91bc33 | 806 | void wxFileName::AssignCwd(const wxString& volume) |
df5ddbca | 807 | { |
6f91bc33 | 808 | AssignDir(wxFileName::GetCwd(volume)); |
a35b27b1 RR |
809 | } |
810 | ||
844f90fb | 811 | /* static */ |
6f91bc33 | 812 | wxString wxFileName::GetCwd(const wxString& volume) |
a35b27b1 | 813 | { |
6f91bc33 VZ |
814 | // if we have the volume, we must get the current directory on this drive |
815 | // and to do this we have to chdir to this volume - at least under Windows, | |
816 | // I don't know how to get the current drive on another volume elsewhere | |
817 | // (TODO) | |
818 | wxString cwdOld; | |
819 | if ( !volume.empty() ) | |
820 | { | |
821 | cwdOld = wxGetCwd(); | |
822 | SetCwd(volume + GetVolumeSeparator()); | |
823 | } | |
824 | ||
825 | wxString cwd = ::wxGetCwd(); | |
826 | ||
827 | if ( !volume.empty() ) | |
828 | { | |
829 | SetCwd(cwdOld); | |
830 | } | |
ae4d7004 | 831 | |
6f91bc33 | 832 | return cwd; |
a35b27b1 RR |
833 | } |
834 | ||
d9e80dce | 835 | bool wxFileName::SetCwd() const |
a35b27b1 | 836 | { |
db759dde | 837 | return wxFileName::SetCwd( GetPath() ); |
df5ddbca RR |
838 | } |
839 | ||
a35b27b1 | 840 | bool wxFileName::SetCwd( const wxString &cwd ) |
df5ddbca | 841 | { |
a35b27b1 | 842 | return ::wxSetWorkingDirectory( cwd ); |
df5ddbca RR |
843 | } |
844 | ||
a35b27b1 RR |
845 | void wxFileName::AssignHomeDir() |
846 | { | |
844f90fb | 847 | AssignDir(wxFileName::GetHomeDir()); |
a35b27b1 | 848 | } |
844f90fb | 849 | |
a35b27b1 RR |
850 | wxString wxFileName::GetHomeDir() |
851 | { | |
852 | return ::wxGetHomeDir(); | |
853 | } | |
844f90fb | 854 | |
68351053 | 855 | |
b70a2866 MW |
856 | // ---------------------------------------------------------------------------- |
857 | // CreateTempFileName | |
858 | // ---------------------------------------------------------------------------- | |
859 | ||
860 | #if wxUSE_FILE || wxUSE_FFILE | |
861 | ||
862 | ||
863 | #if !defined wx_fdopen && defined HAVE_FDOPEN | |
864 | #define wx_fdopen fdopen | |
865 | #endif | |
866 | ||
867 | // NB: GetTempFileName() under Windows creates the file, so using | |
868 | // O_EXCL there would fail | |
869 | #ifdef __WINDOWS__ | |
870 | #define wxOPEN_EXCL 0 | |
871 | #else | |
872 | #define wxOPEN_EXCL O_EXCL | |
873 | #endif | |
874 | ||
875 | ||
876 | #ifdef wxOpenOSFHandle | |
877 | #define WX_HAVE_DELETE_ON_CLOSE | |
878 | // On Windows create a file with the FILE_FLAGS_DELETE_ON_CLOSE flags. | |
879 | // | |
880 | static int wxOpenWithDeleteOnClose(const wxString& filename) | |
df5ddbca | 881 | { |
b70a2866 MW |
882 | DWORD access = GENERIC_READ | GENERIC_WRITE; |
883 | ||
884 | DWORD disposition = OPEN_ALWAYS; | |
885 | ||
886 | DWORD attributes = FILE_ATTRIBUTE_TEMPORARY | | |
887 | FILE_FLAG_DELETE_ON_CLOSE; | |
888 | ||
9c505a36 | 889 | HANDLE h = ::CreateFile(filename.t_str(), access, 0, NULL, |
b70a2866 MW |
890 | disposition, attributes, NULL); |
891 | ||
693bfcaf | 892 | return wxOpenOSFHandle(h, wxO_BINARY); |
ade35f11 | 893 | } |
b70a2866 | 894 | #endif // wxOpenOSFHandle |
ade35f11 | 895 | |
b70a2866 MW |
896 | |
897 | // Helper to open the file | |
898 | // | |
899 | static int wxTempOpen(const wxString& path, bool *deleteOnClose) | |
900 | { | |
901 | #ifdef WX_HAVE_DELETE_ON_CLOSE | |
902 | if (*deleteOnClose) | |
903 | return wxOpenWithDeleteOnClose(path); | |
904 | #endif | |
905 | ||
906 | *deleteOnClose = false; | |
907 | ||
908 | return wxOpen(path, wxO_BINARY | O_RDWR | O_CREAT | wxOPEN_EXCL, 0600); | |
909 | } | |
910 | ||
911 | ||
912 | #if wxUSE_FFILE | |
913 | // Helper to open the file and attach it to the wxFFile | |
914 | // | |
915 | static bool wxTempOpen(wxFFile *file, const wxString& path, bool *deleteOnClose) | |
ade35f11 | 916 | { |
b70a2866 MW |
917 | #ifndef wx_fdopen |
918 | *deleteOnClose = false; | |
9a83f860 | 919 | return file->Open(path, wxT("w+b")); |
b70a2866 MW |
920 | #else // wx_fdopen |
921 | int fd = wxTempOpen(path, deleteOnClose); | |
693bfcaf | 922 | if (fd == -1) |
b70a2866 | 923 | return false; |
d8b13086 | 924 | file->Attach(wx_fdopen(fd, "w+b"), path); |
b70a2866 MW |
925 | return file->IsOpened(); |
926 | #endif // wx_fdopen | |
927 | } | |
928 | #endif // wxUSE_FFILE | |
929 | ||
930 | ||
931 | #if !wxUSE_FILE | |
932 | #define WXFILEARGS(x, y) y | |
933 | #elif !wxUSE_FFILE | |
934 | #define WXFILEARGS(x, y) x | |
935 | #else | |
936 | #define WXFILEARGS(x, y) x, y | |
937 | #endif | |
938 | ||
939 | ||
940 | // Implementation of wxFileName::CreateTempFileName(). | |
941 | // | |
942 | static wxString wxCreateTempImpl( | |
943 | const wxString& prefix, | |
944 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp), | |
945 | bool *deleteOnClose = NULL) | |
946 | { | |
947 | #if wxUSE_FILE && wxUSE_FFILE | |
948 | wxASSERT(fileTemp == NULL || ffileTemp == NULL); | |
949 | #endif | |
ade35f11 | 950 | wxString path, dir, name; |
b70a2866 MW |
951 | bool wantDeleteOnClose = false; |
952 | ||
953 | if (deleteOnClose) | |
954 | { | |
955 | // set the result to false initially | |
956 | wantDeleteOnClose = *deleteOnClose; | |
957 | *deleteOnClose = false; | |
958 | } | |
959 | else | |
960 | { | |
961 | // easier if it alwasys points to something | |
962 | deleteOnClose = &wantDeleteOnClose; | |
963 | } | |
ade35f11 VZ |
964 | |
965 | // use the directory specified by the prefix | |
b70a2866 | 966 | wxFileName::SplitPath(prefix, &dir, &name, NULL /* extension */); |
ade35f11 | 967 | |
6091caf0 JS |
968 | if (dir.empty()) |
969 | { | |
8d7d6dea | 970 | dir = wxFileName::GetTempDir(); |
6091caf0 JS |
971 | } |
972 | ||
1c193821 | 973 | #if defined(__WXWINCE__) |
f05ba7ff | 974 | path = dir + wxT("\\") + name; |
1c193821 | 975 | int i = 1; |
b70a2866 | 976 | while (wxFileName::FileExists(path)) |
1c193821 | 977 | { |
f05ba7ff | 978 | path = dir + wxT("\\") + name ; |
1c193821 JS |
979 | path << i; |
980 | i ++; | |
981 | } | |
ade35f11 | 982 | |
1c193821 | 983 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
715e4f7e VZ |
984 | if (!::GetTempFileName(dir.t_str(), name.t_str(), 0, |
985 | wxStringBuffer(path, MAX_PATH + 1))) | |
ade35f11 | 986 | { |
9a83f860 | 987 | wxLogLastError(wxT("GetTempFileName")); |
ade35f11 VZ |
988 | |
989 | path.clear(); | |
990 | } | |
ade35f11 | 991 | |
5a3912f2 | 992 | #else // !Windows |
ade35f11 VZ |
993 | path = dir; |
994 | ||
995 | if ( !wxEndsWithPathSeparator(dir) && | |
996 | (name.empty() || !wxIsPathSeparator(name[0u])) ) | |
997 | { | |
d9f54bb0 | 998 | path += wxFILE_SEP_PATH; |
ade35f11 VZ |
999 | } |
1000 | ||
1001 | path += name; | |
1002 | ||
7070f55b | 1003 | #if defined(HAVE_MKSTEMP) |
ade35f11 | 1004 | // scratch space for mkstemp() |
9a83f860 | 1005 | path += wxT("XXXXXX"); |
ade35f11 | 1006 | |
74cf9763 | 1007 | // we need to copy the path to the buffer in which mkstemp() can modify it |
86501081 | 1008 | wxCharBuffer buf(path.fn_str()); |
74cf9763 VZ |
1009 | |
1010 | // cast is safe because the string length doesn't change | |
ca11abde | 1011 | int fdTemp = mkstemp( (char*)(const char*) buf ); |
df22f860 | 1012 | if ( fdTemp == -1 ) |
ade35f11 VZ |
1013 | { |
1014 | // this might be not necessary as mkstemp() on most systems should have | |
1015 | // already done it but it doesn't hurt neither... | |
1016 | path.clear(); | |
1017 | } | |
df22f860 VZ |
1018 | else // mkstemp() succeeded |
1019 | { | |
ca11abde | 1020 | path = wxConvFile.cMB2WX( (const char*) buf ); |
a62848fd | 1021 | |
b70a2866 | 1022 | #if wxUSE_FILE |
df22f860 VZ |
1023 | // avoid leaking the fd |
1024 | if ( fileTemp ) | |
1025 | { | |
1026 | fileTemp->Attach(fdTemp); | |
1027 | } | |
1028 | else | |
b70a2866 MW |
1029 | #endif |
1030 | ||
1031 | #if wxUSE_FFILE | |
1032 | if ( ffileTemp ) | |
1033 | { | |
1034 | #ifdef wx_fdopen | |
d8b13086 | 1035 | ffileTemp->Attach(wx_fdopen(fdTemp, "r+b"), path); |
b70a2866 | 1036 | #else |
9a83f860 | 1037 | ffileTemp->Open(path, wxT("r+b")); |
b70a2866 MW |
1038 | close(fdTemp); |
1039 | #endif | |
1040 | } | |
1041 | else | |
1042 | #endif | |
1043 | ||
df22f860 VZ |
1044 | { |
1045 | close(fdTemp); | |
1046 | } | |
1047 | } | |
ade35f11 VZ |
1048 | #else // !HAVE_MKSTEMP |
1049 | ||
1050 | #ifdef HAVE_MKTEMP | |
1051 | // same as above | |
9a83f860 | 1052 | path += wxT("XXXXXX"); |
ade35f11 | 1053 | |
ca11abde | 1054 | wxCharBuffer buf = wxConvFile.cWX2MB( path ); |
b70a2866 | 1055 | if ( !mktemp( (char*)(const char*) buf ) ) |
ade35f11 VZ |
1056 | { |
1057 | path.clear(); | |
1058 | } | |
aed08d79 RR |
1059 | else |
1060 | { | |
ca11abde | 1061 | path = wxConvFile.cMB2WX( (const char*) buf ); |
aed08d79 | 1062 | } |
7070f55b | 1063 | #else // !HAVE_MKTEMP (includes __DOS__) |
ade35f11 | 1064 | // generate the unique file name ourselves |
2415cf67 | 1065 | #if !defined(__DOS__) |
ade35f11 | 1066 | path << (unsigned int)getpid(); |
7070f55b | 1067 | #endif |
ade35f11 VZ |
1068 | |
1069 | wxString pathTry; | |
1070 | ||
1071 | static const size_t numTries = 1000; | |
1072 | for ( size_t n = 0; n < numTries; n++ ) | |
1073 | { | |
1074 | // 3 hex digits is enough for numTries == 1000 < 4096 | |
9a83f860 | 1075 | pathTry = path + wxString::Format(wxT("%.03x"), (unsigned int) n); |
b70a2866 | 1076 | if ( !wxFileName::FileExists(pathTry) ) |
ade35f11 VZ |
1077 | { |
1078 | break; | |
1079 | } | |
1080 | ||
1081 | pathTry.clear(); | |
1082 | } | |
1083 | ||
1084 | path = pathTry; | |
1085 | #endif // HAVE_MKTEMP/!HAVE_MKTEMP | |
1086 | ||
df22f860 VZ |
1087 | #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP |
1088 | ||
1089 | #endif // Windows/!Windows | |
1090 | ||
1091 | if ( path.empty() ) | |
1092 | { | |
1093 | wxLogSysError(_("Failed to create a temporary file name")); | |
1094 | } | |
b70a2866 | 1095 | else |
df22f860 | 1096 | { |
b70a2866 MW |
1097 | bool ok = true; |
1098 | ||
df22f860 | 1099 | // open the file - of course, there is a race condition here, this is |
ade35f11 | 1100 | // why we always prefer using mkstemp()... |
b70a2866 MW |
1101 | #if wxUSE_FILE |
1102 | if ( fileTemp && !fileTemp->IsOpened() ) | |
1103 | { | |
1104 | *deleteOnClose = wantDeleteOnClose; | |
1105 | int fd = wxTempOpen(path, deleteOnClose); | |
1106 | if (fd != -1) | |
1107 | fileTemp->Attach(fd); | |
1108 | else | |
1109 | ok = false; | |
1110 | } | |
1111 | #endif | |
1112 | ||
1113 | #if wxUSE_FFILE | |
1114 | if ( ffileTemp && !ffileTemp->IsOpened() ) | |
1115 | { | |
1116 | *deleteOnClose = wantDeleteOnClose; | |
1117 | ok = wxTempOpen(ffileTemp, path, deleteOnClose); | |
1118 | } | |
1119 | #endif | |
1120 | ||
1121 | if ( !ok ) | |
ade35f11 VZ |
1122 | { |
1123 | // FIXME: If !ok here should we loop and try again with another | |
1124 | // file name? That is the standard recourse if open(O_EXCL) | |
1125 | // fails, though of course it should be protected against | |
1126 | // possible infinite looping too. | |
1127 | ||
1128 | wxLogError(_("Failed to open temporary file.")); | |
1129 | ||
1130 | path.clear(); | |
1131 | } | |
1132 | } | |
ade35f11 VZ |
1133 | |
1134 | return path; | |
df5ddbca RR |
1135 | } |
1136 | ||
b70a2866 MW |
1137 | |
1138 | static bool wxCreateTempImpl( | |
1139 | const wxString& prefix, | |
1140 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp), | |
1141 | wxString *name) | |
1142 | { | |
1143 | bool deleteOnClose = true; | |
1144 | ||
1145 | *name = wxCreateTempImpl(prefix, | |
1146 | WXFILEARGS(fileTemp, ffileTemp), | |
1147 | &deleteOnClose); | |
1148 | ||
1149 | bool ok = !name->empty(); | |
1150 | ||
1151 | if (deleteOnClose) | |
1152 | name->clear(); | |
1153 | #ifdef __UNIX__ | |
1154 | else if (ok && wxRemoveFile(*name)) | |
1155 | name->clear(); | |
1156 | #endif | |
1157 | ||
1158 | return ok; | |
1159 | } | |
1160 | ||
1161 | ||
1162 | static void wxAssignTempImpl( | |
1163 | wxFileName *fn, | |
1164 | const wxString& prefix, | |
1165 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp)) | |
1166 | { | |
1167 | wxString tempname; | |
1168 | tempname = wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, ffileTemp)); | |
1169 | ||
1170 | if ( tempname.empty() ) | |
1171 | { | |
1172 | // error, failed to get temp file name | |
1173 | fn->Clear(); | |
1174 | } | |
1175 | else // ok | |
1176 | { | |
1177 | fn->Assign(tempname); | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | ||
1182 | void wxFileName::AssignTempFileName(const wxString& prefix) | |
1183 | { | |
1184 | wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, NULL)); | |
1185 | } | |
1186 | ||
1187 | /* static */ | |
1188 | wxString wxFileName::CreateTempFileName(const wxString& prefix) | |
1189 | { | |
1190 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, NULL)); | |
1191 | } | |
1192 | ||
1193 | #endif // wxUSE_FILE || wxUSE_FFILE | |
1194 | ||
1195 | ||
1196 | #if wxUSE_FILE | |
1197 | ||
1198 | wxString wxCreateTempFileName(const wxString& prefix, | |
1199 | wxFile *fileTemp, | |
1200 | bool *deleteOnClose) | |
1201 | { | |
1202 | return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), deleteOnClose); | |
1203 | } | |
1204 | ||
1205 | bool wxCreateTempFile(const wxString& prefix, | |
1206 | wxFile *fileTemp, | |
1207 | wxString *name) | |
1208 | { | |
1209 | return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), name); | |
1210 | } | |
1211 | ||
1212 | void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp) | |
1213 | { | |
1214 | wxAssignTempImpl(this, prefix, WXFILEARGS(fileTemp, NULL)); | |
1215 | } | |
1216 | ||
1217 | /* static */ | |
1218 | wxString | |
1219 | wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) | |
1220 | { | |
1221 | return wxCreateTempFileName(prefix, fileTemp); | |
1222 | } | |
1223 | ||
68351053 WS |
1224 | #endif // wxUSE_FILE |
1225 | ||
b70a2866 MW |
1226 | |
1227 | #if wxUSE_FFILE | |
1228 | ||
1229 | wxString wxCreateTempFileName(const wxString& prefix, | |
1230 | wxFFile *fileTemp, | |
1231 | bool *deleteOnClose) | |
1232 | { | |
1233 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), deleteOnClose); | |
1234 | } | |
1235 | ||
1236 | bool wxCreateTempFile(const wxString& prefix, | |
1237 | wxFFile *fileTemp, | |
1238 | wxString *name) | |
1239 | { | |
1240 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), name); | |
1241 | ||
1242 | } | |
1243 | ||
1244 | void wxFileName::AssignTempFileName(const wxString& prefix, wxFFile *fileTemp) | |
1245 | { | |
1246 | wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, fileTemp)); | |
1247 | } | |
1248 | ||
1249 | /* static */ | |
1250 | wxString | |
1251 | wxFileName::CreateTempFileName(const wxString& prefix, wxFFile *fileTemp) | |
1252 | { | |
1253 | return wxCreateTempFileName(prefix, fileTemp); | |
1254 | } | |
1255 | ||
1256 | #endif // wxUSE_FFILE | |
1257 | ||
1258 | ||
844f90fb VZ |
1259 | // ---------------------------------------------------------------------------- |
1260 | // directory operations | |
1261 | // ---------------------------------------------------------------------------- | |
1262 | ||
8758875e VZ |
1263 | // helper of GetTempDir(): check if the given directory exists and return it if |
1264 | // it does or an empty string otherwise | |
1265 | namespace | |
8d7d6dea | 1266 | { |
8d7d6dea | 1267 | |
8758875e VZ |
1268 | wxString CheckIfDirExists(const wxString& dir) |
1269 | { | |
1270 | return wxFileName::DirExists(dir) ? dir : wxString(); | |
1271 | } | |
1272 | ||
1273 | } // anonymous namespace | |
1274 | ||
1275 | wxString wxFileName::GetTempDir() | |
1276 | { | |
1277 | // first try getting it from environment: this allows overriding the values | |
1278 | // used by default if the user wants to create temporary files in another | |
1279 | // directory | |
1280 | wxString dir = CheckIfDirExists(wxGetenv("TMPDIR")); | |
1281 | if ( dir.empty() ) | |
8d7d6dea | 1282 | { |
8758875e VZ |
1283 | dir = CheckIfDirExists(wxGetenv("TMP")); |
1284 | if ( dir.empty() ) | |
1285 | dir = CheckIfDirExists(wxGetenv("TEMP")); | |
8d7d6dea | 1286 | } |
8d7d6dea | 1287 | |
8758875e | 1288 | // if no environment variables are set, use the system default |
8d7d6dea JS |
1289 | if ( dir.empty() ) |
1290 | { | |
8758875e VZ |
1291 | #if defined(__WXWINCE__) |
1292 | dir = CheckIfDirExists(wxT("\\temp")); | |
1293 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
8d7d6dea JS |
1294 | if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) ) |
1295 | { | |
9a83f860 | 1296 | wxLogLastError(wxT("GetTempPath")); |
8d7d6dea | 1297 | } |
8758875e | 1298 | #elif defined(__WXMAC__) && wxOSX_USE_CARBON |
088a3642 | 1299 | dir = wxMacFindFolderNoSeparator(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder); |
8758875e | 1300 | #endif // systems with native way |
8d7d6dea | 1301 | } |
02f35bff VZ |
1302 | else // we got directory from an environment variable |
1303 | { | |
1304 | // remove any trailing path separators, we don't want to ever return | |
1305 | // them from this function for consistency | |
1306 | const size_t lastNonSep = dir.find_last_not_of(GetPathSeparators()); | |
1307 | if ( lastNonSep == wxString::npos ) | |
1308 | { | |
1309 | // the string consists entirely of separators, leave only one | |
1310 | dir = GetPathSeparator(); | |
1311 | } | |
1312 | else | |
1313 | { | |
1314 | dir.erase(lastNonSep + 1); | |
1315 | } | |
1316 | } | |
8d7d6dea | 1317 | |
8758875e | 1318 | // fall back to hard coded value |
8d7d6dea JS |
1319 | if ( dir.empty() ) |
1320 | { | |
8758875e VZ |
1321 | #ifdef __UNIX_LIKE__ |
1322 | dir = CheckIfDirExists("/tmp"); | |
1323 | if ( dir.empty() ) | |
1324 | #endif // __UNIX_LIKE__ | |
1325 | dir = "."; | |
8d7d6dea | 1326 | } |
8d7d6dea JS |
1327 | |
1328 | return dir; | |
1329 | } | |
1330 | ||
89391a4e | 1331 | bool wxFileName::Mkdir( int perm, int flags ) const |
a35b27b1 | 1332 | { |
db759dde | 1333 | return wxFileName::Mkdir(GetPath(), perm, flags); |
a35b27b1 RR |
1334 | } |
1335 | ||
1527281e | 1336 | bool wxFileName::Mkdir( const wxString& dir, int perm, int flags ) |
df5ddbca | 1337 | { |
1527281e | 1338 | if ( flags & wxPATH_MKDIR_FULL ) |
f0ce3409 | 1339 | { |
1527281e VZ |
1340 | // split the path in components |
1341 | wxFileName filename; | |
1342 | filename.AssignDir(dir); | |
f0ce3409 | 1343 | |
f0ce3409 | 1344 | wxString currPath; |
0ea621cc VZ |
1345 | if ( filename.HasVolume()) |
1346 | { | |
1527281e | 1347 | currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE); |
0ea621cc | 1348 | } |
f0ce3409 | 1349 | |
1527281e VZ |
1350 | wxArrayString dirs = filename.GetDirs(); |
1351 | size_t count = dirs.GetCount(); | |
1352 | for ( size_t i = 0; i < count; i++ ) | |
1353 | { | |
e0d81eac | 1354 | if ( i > 0 || filename.IsAbsolute() ) |
f0ce3409 | 1355 | currPath += wxFILE_SEP_PATH; |
1527281e | 1356 | currPath += dirs[i]; |
f0ce3409 JS |
1357 | |
1358 | if (!DirExists(currPath)) | |
1527281e | 1359 | { |
f0ce3409 | 1360 | if (!wxMkdir(currPath, perm)) |
1527281e VZ |
1361 | { |
1362 | // no need to try creating further directories | |
f363e05c | 1363 | return false; |
1527281e VZ |
1364 | } |
1365 | } | |
f0ce3409 JS |
1366 | } |
1367 | ||
f363e05c | 1368 | return true; |
f0ce3409 JS |
1369 | |
1370 | } | |
1527281e VZ |
1371 | |
1372 | return ::wxMkdir( dir, perm ); | |
df5ddbca RR |
1373 | } |
1374 | ||
89391a4e | 1375 | bool wxFileName::Rmdir(int flags) const |
df5ddbca | 1376 | { |
110c5094 | 1377 | return wxFileName::Rmdir( GetPath(), flags ); |
df5ddbca RR |
1378 | } |
1379 | ||
110c5094 | 1380 | bool wxFileName::Rmdir(const wxString& dir, int flags) |
df5ddbca | 1381 | { |
d98a58c5 | 1382 | #ifdef __WINDOWS__ |
110c5094 VZ |
1383 | if ( flags & wxPATH_RMDIR_RECURSIVE ) |
1384 | { | |
1385 | // SHFileOperation needs double null termination string | |
1386 | // but without separator at the end of the path | |
1387 | wxString path(dir); | |
1388 | if ( path.Last() == wxFILE_SEP_PATH ) | |
1389 | path.RemoveLast(); | |
9a83f860 | 1390 | path += wxT('\0'); |
110c5094 VZ |
1391 | |
1392 | SHFILEOPSTRUCT fileop; | |
1393 | wxZeroMemory(fileop); | |
1394 | fileop.wFunc = FO_DELETE; | |
9c505a36 | 1395 | fileop.pFrom = path.t_str(); |
110c5094 VZ |
1396 | fileop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; |
1397 | #ifndef __WXWINCE__ | |
1398 | // FOF_NOERRORUI is not defined in WinCE | |
1399 | fileop.fFlags |= FOF_NOERRORUI; | |
1400 | #endif | |
1401 | ||
1402 | int ret = SHFileOperation(&fileop); | |
1403 | if ( ret != 0 ) | |
1404 | { | |
1405 | // SHFileOperation may return non-Win32 error codes, so the error | |
1406 | // message can be incorrect | |
9a83f860 | 1407 | wxLogApiError(wxT("SHFileOperation"), ret); |
110c5094 VZ |
1408 | return false; |
1409 | } | |
1410 | ||
1411 | return true; | |
1412 | } | |
1413 | else if ( flags & wxPATH_RMDIR_FULL ) | |
d98a58c5 | 1414 | #else // !__WINDOWS__ |
110c5094 | 1415 | if ( flags != 0 ) // wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE |
d98a58c5 | 1416 | #endif // !__WINDOWS__ |
110c5094 | 1417 | { |
25db2c25 VZ |
1418 | #ifndef __WINDOWS__ |
1419 | if ( flags & wxPATH_RMDIR_RECURSIVE ) | |
1420 | { | |
1421 | // When deleting the tree recursively, we are supposed to delete | |
1422 | // this directory itself even when it is a symlink -- but without | |
1423 | // following it. Do it here as wxRmdir() would simply follow if | |
1424 | // called for a symlink. | |
1425 | if ( wxFileName::Exists(dir, wxFILE_EXISTS_SYMLINK | | |
1426 | wxFILE_EXISTS_NO_FOLLOW) ) | |
1427 | { | |
1428 | return wxRemoveFile(dir); | |
1429 | } | |
1430 | } | |
1431 | #endif // !__WINDOWS__ | |
1432 | ||
110c5094 VZ |
1433 | wxString path(dir); |
1434 | if ( path.Last() != wxFILE_SEP_PATH ) | |
1435 | path += wxFILE_SEP_PATH; | |
1436 | ||
1437 | wxDir d(path); | |
1438 | ||
1439 | if ( !d.IsOpened() ) | |
1440 | return false; | |
1441 | ||
1442 | wxString filename; | |
1443 | ||
25db2c25 VZ |
1444 | // First delete all subdirectories: notice that we don't follow |
1445 | // symbolic links, potentially leading outside this directory, to avoid | |
1446 | // unpleasant surprises. | |
1447 | bool cont = d.GetFirst(&filename, wxString(), | |
1448 | wxDIR_DIRS | wxDIR_HIDDEN | wxDIR_NO_FOLLOW); | |
110c5094 VZ |
1449 | while ( cont ) |
1450 | { | |
1451 | wxFileName::Rmdir(path + filename, flags); | |
1452 | cont = d.GetNext(&filename); | |
1453 | } | |
1454 | ||
d98a58c5 | 1455 | #ifndef __WINDOWS__ |
110c5094 VZ |
1456 | if ( flags & wxPATH_RMDIR_RECURSIVE ) |
1457 | { | |
25db2c25 VZ |
1458 | // Delete all files too and, for the same reasons as above, don't |
1459 | // follow symlinks which could refer to the files outside of this | |
1460 | // directory and just delete the symlinks themselves. | |
1461 | cont = d.GetFirst(&filename, wxString(), | |
1462 | wxDIR_FILES | wxDIR_HIDDEN | wxDIR_NO_FOLLOW); | |
110c5094 VZ |
1463 | while ( cont ) |
1464 | { | |
1465 | ::wxRemoveFile(path + filename); | |
1466 | cont = d.GetNext(&filename); | |
1467 | } | |
1468 | } | |
d98a58c5 | 1469 | #endif // !__WINDOWS__ |
110c5094 VZ |
1470 | } |
1471 | ||
1472 | return ::wxRmdir(dir); | |
df5ddbca RR |
1473 | } |
1474 | ||
844f90fb VZ |
1475 | // ---------------------------------------------------------------------------- |
1476 | // path normalization | |
1477 | // ---------------------------------------------------------------------------- | |
1478 | ||
32a0d013 | 1479 | bool wxFileName::Normalize(int flags, |
844f90fb VZ |
1480 | const wxString& cwd, |
1481 | wxPathFormat format) | |
a35b27b1 | 1482 | { |
05e1201c VZ |
1483 | // deal with env vars renaming first as this may seriously change the path |
1484 | if ( flags & wxPATH_NORM_ENV_VARS ) | |
1485 | { | |
1486 | wxString pathOrig = GetFullPath(format); | |
1487 | wxString path = wxExpandEnvVars(pathOrig); | |
1488 | if ( path != pathOrig ) | |
1489 | { | |
1490 | Assign(path); | |
1491 | } | |
1492 | } | |
1493 | ||
844f90fb VZ |
1494 | // the existing path components |
1495 | wxArrayString dirs = GetDirs(); | |
1496 | ||
1497 | // the path to prepend in front to make the path absolute | |
1498 | wxFileName curDir; | |
1499 | ||
1500 | format = GetFormat(format); | |
ae4d7004 | 1501 | |
bf7f7793 | 1502 | // set up the directory to use for making the path absolute later |
a2fa5040 | 1503 | if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) ) |
a35b27b1 | 1504 | { |
844f90fb | 1505 | if ( cwd.empty() ) |
6f91bc33 VZ |
1506 | { |
1507 | curDir.AssignCwd(GetVolume()); | |
1508 | } | |
1509 | else // cwd provided | |
1510 | { | |
844f90fb | 1511 | curDir.AssignDir(cwd); |
6f91bc33 | 1512 | } |
a35b27b1 | 1513 | } |
ae4d7004 | 1514 | |
844f90fb | 1515 | // handle ~ stuff under Unix only |
be5be16a | 1516 | if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) && m_relative ) |
a35b27b1 | 1517 | { |
844f90fb VZ |
1518 | if ( !dirs.IsEmpty() ) |
1519 | { | |
1520 | wxString dir = dirs[0u]; | |
9a83f860 | 1521 | if ( !dir.empty() && dir[0u] == wxT('~') ) |
844f90fb | 1522 | { |
bf7f7793 | 1523 | // to make the path absolute use the home directory |
844f90fb | 1524 | curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); |
da2fd5ac | 1525 | dirs.RemoveAt(0u); |
844f90fb VZ |
1526 | } |
1527 | } | |
a35b27b1 | 1528 | } |
844f90fb | 1529 | |
52dbd056 | 1530 | // transform relative path into abs one |
844f90fb | 1531 | if ( curDir.IsOk() ) |
a35b27b1 | 1532 | { |
bf7f7793 RR |
1533 | // this path may be relative because it doesn't have the volume name |
1534 | // and still have m_relative=true; in this case we shouldn't modify | |
1535 | // our directory components but just set the current volume | |
1536 | if ( !HasVolume() && curDir.HasVolume() ) | |
844f90fb | 1537 | { |
bf7f7793 RR |
1538 | SetVolume(curDir.GetVolume()); |
1539 | ||
1540 | if ( !m_relative ) | |
1541 | { | |
1542 | // yes, it was the case - we don't need curDir then | |
1543 | curDir.Clear(); | |
1544 | } | |
844f90fb VZ |
1545 | } |
1546 | ||
bf7f7793 RR |
1547 | // finally, prepend curDir to the dirs array |
1548 | wxArrayString dirsNew = curDir.GetDirs(); | |
1549 | WX_PREPEND_ARRAY(dirs, dirsNew); | |
1550 | ||
1551 | // if we used e.g. tilde expansion previously and wxGetUserHome didn't | |
1552 | // return for some reason an absolute path, then curDir maybe not be absolute! | |
be5be16a | 1553 | if ( !curDir.m_relative ) |
bf7f7793 RR |
1554 | { |
1555 | // we have prepended an absolute path and thus we are now an absolute | |
1556 | // file name too | |
1557 | m_relative = false; | |
1558 | } | |
1559 | // else if (flags & wxPATH_NORM_ABSOLUTE): | |
1560 | // should we warn the user that we didn't manage to make the path absolute? | |
a35b27b1 | 1561 | } |
844f90fb VZ |
1562 | |
1563 | // now deal with ".", ".." and the rest | |
1564 | m_dirs.Empty(); | |
1565 | size_t count = dirs.GetCount(); | |
1566 | for ( size_t n = 0; n < count; n++ ) | |
a35b27b1 | 1567 | { |
844f90fb VZ |
1568 | wxString dir = dirs[n]; |
1569 | ||
2bc60417 | 1570 | if ( flags & wxPATH_NORM_DOTS ) |
844f90fb VZ |
1571 | { |
1572 | if ( dir == wxT(".") ) | |
1573 | { | |
1574 | // just ignore | |
1575 | continue; | |
1576 | } | |
1577 | ||
1578 | if ( dir == wxT("..") ) | |
1579 | { | |
f822acce | 1580 | if ( m_dirs.empty() ) |
844f90fb | 1581 | { |
f822acce VZ |
1582 | // We have more ".." than directory components so far. |
1583 | // Don't treat this as an error as the path could have been | |
1584 | // entered by user so try to handle it reasonably: if the | |
1585 | // path is absolute, just ignore the extra ".." because | |
1586 | // "/.." is the same as "/". Otherwise, i.e. for relative | |
1587 | // paths, keep ".." unchanged because removing it would | |
1588 | // modify the file a relative path refers to. | |
1589 | if ( !m_relative ) | |
1590 | continue; | |
844f90fb | 1591 | |
f822acce VZ |
1592 | } |
1593 | else // Normal case, go one step up. | |
1594 | { | |
1595 | m_dirs.pop_back(); | |
1596 | continue; | |
1597 | } | |
844f90fb VZ |
1598 | } |
1599 | } | |
1600 | ||
844f90fb | 1601 | m_dirs.Add(dir); |
a35b27b1 | 1602 | } |
a62848fd | 1603 | |
6bda7391 | 1604 | #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE |
21f60945 JS |
1605 | if ( (flags & wxPATH_NORM_SHORTCUT) ) |
1606 | { | |
1607 | wxString filename; | |
1608 | if (GetShortcutTarget(GetFullPath(format), filename)) | |
1609 | { | |
21f60945 JS |
1610 | m_relative = false; |
1611 | Assign(filename); | |
1612 | } | |
1613 | } | |
1614 | #endif | |
844f90fb | 1615 | |
9a0c5c01 VZ |
1616 | #if defined(__WIN32__) |
1617 | if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) ) | |
844f90fb | 1618 | { |
9a0c5c01 VZ |
1619 | Assign(GetLongPath()); |
1620 | } | |
1621 | #endif // Win32 | |
844f90fb | 1622 | |
9a0c5c01 VZ |
1623 | // Change case (this should be kept at the end of the function, to ensure |
1624 | // that the path doesn't change any more after we normalize its case) | |
1625 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
1626 | { | |
55268a3a | 1627 | m_volume.MakeLower(); |
844f90fb VZ |
1628 | m_name.MakeLower(); |
1629 | m_ext.MakeLower(); | |
844f90fb | 1630 | |
9a0c5c01 VZ |
1631 | // directory entries must be made lower case as well |
1632 | count = m_dirs.GetCount(); | |
1633 | for ( size_t i = 0; i < count; i++ ) | |
1634 | { | |
1635 | m_dirs[i].MakeLower(); | |
1636 | } | |
9e9b65c1 | 1637 | } |
9e9b65c1 | 1638 | |
f363e05c | 1639 | return true; |
a2fa5040 VZ |
1640 | } |
1641 | ||
395f3aa8 FM |
1642 | #ifndef __WXWINCE__ |
1643 | bool wxFileName::ReplaceEnvVariable(const wxString& envname, | |
1644 | const wxString& replacementFmtString, | |
1645 | wxPathFormat format) | |
1646 | { | |
1647 | // look into stringForm for the contents of the given environment variable | |
1648 | wxString val; | |
1649 | if (envname.empty() || | |
1650 | !wxGetEnv(envname, &val)) | |
1651 | return false; | |
1652 | if (val.empty()) | |
1653 | return false; | |
1654 | ||
1655 | wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); | |
1656 | // do not touch the file name and the extension | |
1657 | ||
1658 | wxString replacement = wxString::Format(replacementFmtString, envname); | |
1659 | stringForm.Replace(val, replacement); | |
1660 | ||
1661 | // Now assign ourselves the modified path: | |
1662 | Assign(stringForm, GetFullName(), format); | |
1663 | ||
1664 | return true; | |
1665 | } | |
1666 | #endif | |
1667 | ||
1668 | bool wxFileName::ReplaceHomeDir(wxPathFormat format) | |
1669 | { | |
1670 | wxString homedir = wxGetHomeDir(); | |
1671 | if (homedir.empty()) | |
1672 | return false; | |
1673 | ||
1674 | wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); | |
1675 | // do not touch the file name and the extension | |
1676 | ||
1677 | stringForm.Replace(homedir, "~"); | |
1678 | ||
1679 | // Now assign ourselves the modified path: | |
1680 | Assign(stringForm, GetFullName(), format); | |
1681 | ||
1682 | return true; | |
1683 | } | |
1684 | ||
21f60945 JS |
1685 | // ---------------------------------------------------------------------------- |
1686 | // get the shortcut target | |
1687 | // ---------------------------------------------------------------------------- | |
1688 | ||
5671b3b6 JS |
1689 | // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions. |
1690 | // The .lnk file is a plain text file so it should be easy to | |
1691 | // make it work. Hint from Google Groups: | |
1692 | // "If you open up a lnk file, you'll see a | |
1693 | // number, followed by a pound sign (#), followed by more text. The | |
1694 | // number is the number of characters that follows the pound sign. The | |
1695 | // characters after the pound sign are the command line (which _can_ | |
1696 | // include arguments) to be executed. Any path (e.g. \windows\program | |
1697 | // files\myapp.exe) that includes spaces needs to be enclosed in | |
1698 | // quotation marks." | |
1699 | ||
6bda7391 | 1700 | #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE |
5671b3b6 JS |
1701 | // The following lines are necessary under WinCE |
1702 | // #include "wx/msw/private.h" | |
1703 | // #include <ole2.h> | |
21f60945 | 1704 | #include <shlobj.h> |
5671b3b6 JS |
1705 | #if defined(__WXWINCE__) |
1706 | #include <shlguid.h> | |
1707 | #endif | |
21f60945 | 1708 | |
cea0869c VZ |
1709 | bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, |
1710 | wxString& targetFilename, | |
d9e80dce | 1711 | wxString* arguments) const |
21f60945 | 1712 | { |
21f60945 | 1713 | wxString path, file, ext; |
bd365871 | 1714 | wxFileName::SplitPath(shortcutPath, & path, & file, & ext); |
a62848fd WS |
1715 | |
1716 | HRESULT hres; | |
1717 | IShellLink* psl; | |
1718 | bool success = false; | |
21f60945 JS |
1719 | |
1720 | // Assume it's not a shortcut if it doesn't end with lnk | |
489f6cf7 | 1721 | if (ext.CmpNoCase(wxT("lnk"))!=0) |
a62848fd WS |
1722 | return false; |
1723 | ||
1724 | // create a ShellLink object | |
1725 | hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, | |
1726 | IID_IShellLink, (LPVOID*) &psl); | |
1727 | ||
1728 | if (SUCCEEDED(hres)) | |
1729 | { | |
1730 | IPersistFile* ppf; | |
1731 | hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf); | |
1732 | if (SUCCEEDED(hres)) | |
1733 | { | |
1734 | WCHAR wsz[MAX_PATH]; | |
1735 | ||
1736 | MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz, | |
1737 | MAX_PATH); | |
1738 | ||
1739 | hres = ppf->Load(wsz, 0); | |
cea0869c VZ |
1740 | ppf->Release(); |
1741 | ||
a62848fd WS |
1742 | if (SUCCEEDED(hres)) |
1743 | { | |
21f60945 | 1744 | wxChar buf[2048]; |
59ba321b JS |
1745 | // Wrong prototype in early versions |
1746 | #if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2) | |
1747 | psl->GetPath((CHAR*) buf, 2048, NULL, SLGP_UNCPRIORITY); | |
1748 | #else | |
a62848fd | 1749 | psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY); |
59ba321b | 1750 | #endif |
a62848fd | 1751 | targetFilename = wxString(buf); |
21f60945 JS |
1752 | success = (shortcutPath != targetFilename); |
1753 | ||
a62848fd | 1754 | psl->GetArguments(buf, 2048); |
21f60945 | 1755 | wxString args(buf); |
b494c48b | 1756 | if (!args.empty() && arguments) |
21f60945 JS |
1757 | { |
1758 | *arguments = args; | |
a62848fd WS |
1759 | } |
1760 | } | |
1761 | } | |
cea0869c VZ |
1762 | |
1763 | psl->Release(); | |
a62848fd | 1764 | } |
a62848fd | 1765 | return success; |
21f60945 | 1766 | } |
cea0869c VZ |
1767 | |
1768 | #endif // __WIN32__ && !__WXWINCE__ | |
21f60945 JS |
1769 | |
1770 | ||
a2fa5040 VZ |
1771 | // ---------------------------------------------------------------------------- |
1772 | // absolute/relative paths | |
1773 | // ---------------------------------------------------------------------------- | |
1774 | ||
1775 | bool wxFileName::IsAbsolute(wxPathFormat format) const | |
1776 | { | |
be5be16a VZ |
1777 | // unix paths beginning with ~ are reported as being absolute |
1778 | if ( format == wxPATH_UNIX ) | |
1779 | { | |
1780 | if ( !m_dirs.IsEmpty() ) | |
1781 | { | |
1782 | wxString dir = m_dirs[0u]; | |
1783 | ||
9a83f860 | 1784 | if (!dir.empty() && dir[0u] == wxT('~')) |
be5be16a VZ |
1785 | return true; |
1786 | } | |
1787 | } | |
1788 | ||
a2fa5040 VZ |
1789 | // if our path doesn't start with a path separator, it's not an absolute |
1790 | // path | |
1791 | if ( m_relative ) | |
f363e05c | 1792 | return false; |
a2fa5040 VZ |
1793 | |
1794 | if ( !GetVolumeSeparator(format).empty() ) | |
1795 | { | |
1796 | // this format has volumes and an absolute path must have one, it's not | |
be5be16a | 1797 | // enough to have the full path to be an absolute file under Windows |
a2fa5040 | 1798 | if ( GetVolume().empty() ) |
f363e05c | 1799 | return false; |
a2fa5040 VZ |
1800 | } |
1801 | ||
f363e05c | 1802 | return true; |
a35b27b1 RR |
1803 | } |
1804 | ||
f7d886af VZ |
1805 | bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) |
1806 | { | |
cef9731a | 1807 | wxFileName fnBase = wxFileName::DirName(pathBase, format); |
f7d886af VZ |
1808 | |
1809 | // get cwd only once - small time saving | |
1810 | wxString cwd = wxGetCwd(); | |
b5b62eea JS |
1811 | Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); |
1812 | fnBase.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); | |
f7d886af VZ |
1813 | |
1814 | bool withCase = IsCaseSensitive(format); | |
1815 | ||
1816 | // we can't do anything if the files live on different volumes | |
1817 | if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) ) | |
1818 | { | |
1819 | // nothing done | |
f363e05c | 1820 | return false; |
f7d886af VZ |
1821 | } |
1822 | ||
1823 | // same drive, so we don't need our volume | |
1824 | m_volume.clear(); | |
1825 | ||
1826 | // remove common directories starting at the top | |
1827 | while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() && | |
1828 | m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) ) | |
1829 | { | |
ae4d7004 VZ |
1830 | m_dirs.RemoveAt(0); |
1831 | fnBase.m_dirs.RemoveAt(0); | |
f7d886af VZ |
1832 | } |
1833 | ||
1834 | // add as many ".." as needed | |
1835 | size_t count = fnBase.m_dirs.GetCount(); | |
1836 | for ( size_t i = 0; i < count; i++ ) | |
1837 | { | |
1838 | m_dirs.Insert(wxT(".."), 0u); | |
1839 | } | |
90a68369 | 1840 | |
2db991f4 VZ |
1841 | if ( format == wxPATH_UNIX || format == wxPATH_DOS ) |
1842 | { | |
1843 | // a directory made relative with respect to itself is '.' under Unix | |
1844 | // and DOS, by definition (but we don't have to insert "./" for the | |
1845 | // files) | |
1846 | if ( m_dirs.IsEmpty() && IsDir() ) | |
1847 | { | |
9a83f860 | 1848 | m_dirs.Add(wxT('.')); |
0ea621cc | 1849 | } |
2db991f4 VZ |
1850 | } |
1851 | ||
f363e05c | 1852 | m_relative = true; |
f7d886af VZ |
1853 | |
1854 | // we were modified | |
f363e05c | 1855 | return true; |
f7d886af VZ |
1856 | } |
1857 | ||
844f90fb VZ |
1858 | // ---------------------------------------------------------------------------- |
1859 | // filename kind tests | |
1860 | // ---------------------------------------------------------------------------- | |
1861 | ||
2b5f62a0 | 1862 | bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const |
df5ddbca | 1863 | { |
844f90fb VZ |
1864 | wxFileName fn1 = *this, |
1865 | fn2 = filepath; | |
1866 | ||
1867 | // get cwd only once - small time saving | |
1868 | wxString cwd = wxGetCwd(); | |
55268a3a VZ |
1869 | fn1.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format); |
1870 | fn2.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format); | |
844f90fb VZ |
1871 | |
1872 | if ( fn1.GetFullPath() == fn2.GetFullPath() ) | |
f363e05c | 1873 | return true; |
844f90fb | 1874 | |
7c9b6c91 VZ |
1875 | #if defined(__UNIX__) |
1876 | wxStructStat st1, st2; | |
c063adeb | 1877 | if ( StatAny(st1, fn1) && StatAny(st2, fn2) ) |
7c9b6c91 VZ |
1878 | { |
1879 | if ( st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev ) | |
1880 | return true; | |
1881 | } | |
1882 | //else: It's not an error if one or both files don't exist. | |
1883 | #endif // defined __UNIX__ | |
844f90fb | 1884 | |
f363e05c | 1885 | return false; |
df5ddbca RR |
1886 | } |
1887 | ||
844f90fb | 1888 | /* static */ |
df5ddbca RR |
1889 | bool wxFileName::IsCaseSensitive( wxPathFormat format ) |
1890 | { | |
37424888 | 1891 | // only Unix filenames are truly case-sensitive |
04c943b1 | 1892 | return GetFormat(format) == wxPATH_UNIX; |
df5ddbca RR |
1893 | } |
1894 | ||
f363e05c VZ |
1895 | /* static */ |
1896 | wxString wxFileName::GetForbiddenChars(wxPathFormat format) | |
1897 | { | |
1898 | // Inits to forbidden characters that are common to (almost) all platforms. | |
1899 | wxString strForbiddenChars = wxT("*?"); | |
1900 | ||
1c6f2414 | 1901 | // If asserts, wxPathFormat has been changed. In case of a new path format |
f363e05c | 1902 | // addition, the following code might have to be updated. |
1c6f2414 | 1903 | wxCOMPILE_TIME_ASSERT(wxPATH_MAX == 5, wxPathFormatChanged); |
f363e05c VZ |
1904 | switch ( GetFormat(format) ) |
1905 | { | |
1906 | default : | |
1907 | wxFAIL_MSG( wxT("Unknown path format") ); | |
1908 | // !! Fall through !! | |
1909 | ||
1910 | case wxPATH_UNIX: | |
1911 | break; | |
1912 | ||
1913 | case wxPATH_MAC: | |
1914 | // On a Mac even names with * and ? are allowed (Tested with OS | |
1915 | // 9.2.1 and OS X 10.2.5) | |
1916 | strForbiddenChars = wxEmptyString; | |
1917 | break; | |
1918 | ||
1919 | case wxPATH_DOS: | |
1920 | strForbiddenChars += wxT("\\/:\"<>|"); | |
1921 | break; | |
1922 | ||
1923 | case wxPATH_VMS: | |
1924 | break; | |
1925 | } | |
1926 | ||
1927 | return strForbiddenChars; | |
1928 | } | |
1929 | ||
04c943b1 | 1930 | /* static */ |
bcfa2b31 | 1931 | wxString wxFileName::GetVolumeSeparator(wxPathFormat WXUNUSED_IN_WINCE(format)) |
844f90fb | 1932 | { |
bcfa2b31 WS |
1933 | #ifdef __WXWINCE__ |
1934 | return wxEmptyString; | |
1935 | #else | |
04c943b1 VZ |
1936 | wxString sepVol; |
1937 | ||
a385b5df RR |
1938 | if ( (GetFormat(format) == wxPATH_DOS) || |
1939 | (GetFormat(format) == wxPATH_VMS) ) | |
04c943b1 | 1940 | { |
04c943b1 VZ |
1941 | sepVol = wxFILE_SEP_DSK; |
1942 | } | |
a385b5df | 1943 | //else: leave empty |
04c943b1 VZ |
1944 | |
1945 | return sepVol; | |
bcfa2b31 | 1946 | #endif |
844f90fb VZ |
1947 | } |
1948 | ||
1949 | /* static */ | |
1950 | wxString wxFileName::GetPathSeparators(wxPathFormat format) | |
1951 | { | |
1952 | wxString seps; | |
1953 | switch ( GetFormat(format) ) | |
df5ddbca | 1954 | { |
844f90fb | 1955 | case wxPATH_DOS: |
04c943b1 VZ |
1956 | // accept both as native APIs do but put the native one first as |
1957 | // this is the one we use in GetFullPath() | |
1958 | seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX; | |
844f90fb VZ |
1959 | break; |
1960 | ||
1961 | default: | |
9a83f860 | 1962 | wxFAIL_MSG( wxT("Unknown wxPATH_XXX style") ); |
844f90fb VZ |
1963 | // fall through |
1964 | ||
1965 | case wxPATH_UNIX: | |
9e8d8607 | 1966 | seps = wxFILE_SEP_PATH_UNIX; |
844f90fb VZ |
1967 | break; |
1968 | ||
1969 | case wxPATH_MAC: | |
9e8d8607 | 1970 | seps = wxFILE_SEP_PATH_MAC; |
844f90fb | 1971 | break; |
04c943b1 | 1972 | |
3c621059 JJ |
1973 | case wxPATH_VMS: |
1974 | seps = wxFILE_SEP_PATH_VMS; | |
1975 | break; | |
df5ddbca RR |
1976 | } |
1977 | ||
844f90fb | 1978 | return seps; |
df5ddbca RR |
1979 | } |
1980 | ||
f1e77933 VZ |
1981 | /* static */ |
1982 | wxString wxFileName::GetPathTerminators(wxPathFormat format) | |
1983 | { | |
1984 | format = GetFormat(format); | |
1985 | ||
1986 | // under VMS the end of the path is ']', not the path separator used to | |
1987 | // separate the components | |
9a83f860 | 1988 | return format == wxPATH_VMS ? wxString(wxT(']')) : GetPathSeparators(format); |
f1e77933 VZ |
1989 | } |
1990 | ||
844f90fb VZ |
1991 | /* static */ |
1992 | bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) | |
df5ddbca | 1993 | { |
04c943b1 | 1994 | // wxString::Find() doesn't work as expected with NUL - it will always find |
2fb5a4ce | 1995 | // it, so test for it separately |
9a83f860 | 1996 | return ch != wxT('\0') && GetPathSeparators(format).Find(ch) != wxNOT_FOUND; |
df5ddbca RR |
1997 | } |
1998 | ||
e01a788e VZ |
1999 | /* static */ |
2000 | bool | |
2001 | wxFileName::IsMSWUniqueVolumeNamePath(const wxString& path, wxPathFormat format) | |
2002 | { | |
2003 | // return true if the format used is the DOS/Windows one and the string begins | |
2004 | // with a Windows unique volume name ("\\?\Volume{guid}\") | |
2005 | return format == wxPATH_DOS && | |
2006 | path.length() >= wxMSWUniqueVolumePrefixLength && | |
2007 | path.StartsWith(wxS("\\\\?\\Volume{")) && | |
2008 | path[wxMSWUniqueVolumePrefixLength - 1] == wxFILE_SEP_PATH_DOS; | |
2009 | } | |
2010 | ||
844f90fb VZ |
2011 | // ---------------------------------------------------------------------------- |
2012 | // path components manipulation | |
2013 | // ---------------------------------------------------------------------------- | |
2014 | ||
5bb9aeb2 VZ |
2015 | /* static */ bool wxFileName::IsValidDirComponent(const wxString& dir) |
2016 | { | |
2017 | if ( dir.empty() ) | |
2018 | { | |
9a83f860 | 2019 | wxFAIL_MSG( wxT("empty directory passed to wxFileName::InsertDir()") ); |
5bb9aeb2 VZ |
2020 | |
2021 | return false; | |
2022 | } | |
2023 | ||
2024 | const size_t len = dir.length(); | |
2025 | for ( size_t n = 0; n < len; n++ ) | |
2026 | { | |
2027 | if ( dir[n] == GetVolumeSeparator() || IsPathSeparator(dir[n]) ) | |
2028 | { | |
9a83f860 | 2029 | wxFAIL_MSG( wxT("invalid directory component in wxFileName") ); |
5bb9aeb2 VZ |
2030 | |
2031 | return false; | |
2032 | } | |
2033 | } | |
2034 | ||
2035 | return true; | |
2036 | } | |
2037 | ||
2458d90b | 2038 | void wxFileName::AppendDir( const wxString& dir ) |
df5ddbca | 2039 | { |
5bb9aeb2 VZ |
2040 | if ( IsValidDirComponent(dir) ) |
2041 | m_dirs.Add( dir ); | |
df5ddbca RR |
2042 | } |
2043 | ||
2458d90b | 2044 | void wxFileName::PrependDir( const wxString& dir ) |
df5ddbca | 2045 | { |
5bb9aeb2 | 2046 | InsertDir(0, dir); |
df5ddbca RR |
2047 | } |
2048 | ||
2458d90b | 2049 | void wxFileName::InsertDir(size_t before, const wxString& dir) |
df5ddbca | 2050 | { |
5bb9aeb2 | 2051 | if ( IsValidDirComponent(dir) ) |
2458d90b | 2052 | m_dirs.Insert(dir, before); |
df5ddbca RR |
2053 | } |
2054 | ||
2458d90b | 2055 | void wxFileName::RemoveDir(size_t pos) |
df5ddbca | 2056 | { |
2458d90b | 2057 | m_dirs.RemoveAt(pos); |
df5ddbca RR |
2058 | } |
2059 | ||
844f90fb VZ |
2060 | // ---------------------------------------------------------------------------- |
2061 | // accessors | |
2062 | // ---------------------------------------------------------------------------- | |
2063 | ||
7124df9b VZ |
2064 | void wxFileName::SetFullName(const wxString& fullname) |
2065 | { | |
dfecbee5 VZ |
2066 | SplitPath(fullname, NULL /* no volume */, NULL /* no path */, |
2067 | &m_name, &m_ext, &m_hasExt); | |
7124df9b VZ |
2068 | } |
2069 | ||
844f90fb | 2070 | wxString wxFileName::GetFullName() const |
a35b27b1 | 2071 | { |
844f90fb | 2072 | wxString fullname = m_name; |
dfecbee5 | 2073 | if ( m_hasExt ) |
a35b27b1 | 2074 | { |
9e8d8607 | 2075 | fullname << wxFILE_SEP_EXT << m_ext; |
a35b27b1 | 2076 | } |
a35b27b1 | 2077 | |
844f90fb | 2078 | return fullname; |
a35b27b1 RR |
2079 | } |
2080 | ||
33b97389 | 2081 | wxString wxFileName::GetPath( int flags, wxPathFormat format ) const |
df5ddbca RR |
2082 | { |
2083 | format = GetFormat( format ); | |
844f90fb | 2084 | |
353f41cb RR |
2085 | wxString fullpath; |
2086 | ||
33b97389 VZ |
2087 | // return the volume with the path as well if requested |
2088 | if ( flags & wxPATH_GET_VOLUME ) | |
2089 | { | |
2090 | fullpath += wxGetVolumeString(GetVolume(), format); | |
2091 | } | |
2092 | ||
034742fc VZ |
2093 | // the leading character |
2094 | switch ( format ) | |
2095 | { | |
2096 | case wxPATH_MAC: | |
2097 | if ( m_relative ) | |
2098 | fullpath += wxFILE_SEP_PATH_MAC; | |
2099 | break; | |
2100 | ||
2101 | case wxPATH_DOS: | |
2102 | if ( !m_relative ) | |
2103 | fullpath += wxFILE_SEP_PATH_DOS; | |
2104 | break; | |
2105 | ||
2106 | default: | |
2107 | wxFAIL_MSG( wxT("Unknown path format") ); | |
2108 | // fall through | |
2109 | ||
2110 | case wxPATH_UNIX: | |
2111 | if ( !m_relative ) | |
2112 | { | |
be5be16a | 2113 | fullpath += wxFILE_SEP_PATH_UNIX; |
034742fc VZ |
2114 | } |
2115 | break; | |
2116 | ||
2117 | case wxPATH_VMS: | |
2118 | // no leading character here but use this place to unset | |
2119 | // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense | |
2120 | // as, if I understand correctly, there should never be a dot | |
2121 | // before the closing bracket | |
2122 | flags &= ~wxPATH_GET_SEPARATOR; | |
2123 | } | |
2124 | ||
2125 | if ( m_dirs.empty() ) | |
2126 | { | |
2127 | // there is nothing more | |
2128 | return fullpath; | |
2129 | } | |
2130 | ||
2131 | // then concatenate all the path components using the path separator | |
2132 | if ( format == wxPATH_VMS ) | |
2133 | { | |
2134 | fullpath += wxT('['); | |
2135 | } | |
2136 | ||
5bb9aeb2 | 2137 | const size_t dirCount = m_dirs.GetCount(); |
034742fc | 2138 | for ( size_t i = 0; i < dirCount; i++ ) |
353f41cb | 2139 | { |
034742fc | 2140 | switch (format) |
5bb9aeb2 VZ |
2141 | { |
2142 | case wxPATH_MAC: | |
034742fc VZ |
2143 | if ( m_dirs[i] == wxT(".") ) |
2144 | { | |
2145 | // skip appending ':', this shouldn't be done in this | |
2146 | // case as "::" is interpreted as ".." under Unix | |
2147 | continue; | |
2148 | } | |
2361ce82 | 2149 | |
034742fc | 2150 | // convert back from ".." to nothing |
489f6cf7 | 2151 | if ( !m_dirs[i].IsSameAs(wxT("..")) ) |
034742fc | 2152 | fullpath += m_dirs[i]; |
5bb9aeb2 | 2153 | break; |
2361ce82 | 2154 | |
5bb9aeb2 | 2155 | default: |
034742fc VZ |
2156 | wxFAIL_MSG( wxT("Unexpected path format") ); |
2157 | // still fall through | |
2361ce82 | 2158 | |
034742fc | 2159 | case wxPATH_DOS: |
5bb9aeb2 | 2160 | case wxPATH_UNIX: |
034742fc | 2161 | fullpath += m_dirs[i]; |
5bb9aeb2 | 2162 | break; |
2361ce82 | 2163 | |
5bb9aeb2 | 2164 | case wxPATH_VMS: |
034742fc | 2165 | // TODO: What to do with ".." under VMS |
353f41cb | 2166 | |
034742fc | 2167 | // convert back from ".." to nothing |
489f6cf7 | 2168 | if ( !m_dirs[i].IsSameAs(wxT("..")) ) |
353f41cb | 2169 | fullpath += m_dirs[i]; |
034742fc | 2170 | break; |
4175794e VZ |
2171 | } |
2172 | ||
034742fc VZ |
2173 | if ( (flags & wxPATH_GET_SEPARATOR) || (i != dirCount - 1) ) |
2174 | fullpath += GetPathSeparator(format); | |
df5ddbca | 2175 | } |
034742fc VZ |
2176 | |
2177 | if ( format == wxPATH_VMS ) | |
5bb9aeb2 | 2178 | { |
034742fc | 2179 | fullpath += wxT(']'); |
5bb9aeb2 | 2180 | } |
844f90fb | 2181 | |
353f41cb | 2182 | return fullpath; |
df5ddbca RR |
2183 | } |
2184 | ||
2185 | wxString wxFileName::GetFullPath( wxPathFormat format ) const | |
2186 | { | |
4175794e VZ |
2187 | // we already have a function to get the path |
2188 | wxString fullpath = GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR, | |
2189 | format); | |
04c943b1 | 2190 | |
4175794e | 2191 | // now just add the file name and extension to it |
04c943b1 VZ |
2192 | fullpath += GetFullName(); |
2193 | ||
2194 | return fullpath; | |
df5ddbca RR |
2195 | } |
2196 | ||
9e9b65c1 JS |
2197 | // Return the short form of the path (returns identity on non-Windows platforms) |
2198 | wxString wxFileName::GetShortPath() const | |
2199 | { | |
9e9b65c1 | 2200 | wxString path(GetFullPath()); |
2f3d9587 | 2201 | |
d98a58c5 | 2202 | #if defined(__WINDOWS__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
715e4f7e | 2203 | DWORD sz = ::GetShortPathName(path.t_str(), NULL, 0); |
2f3d9587 | 2204 | if ( sz != 0 ) |
9e9b65c1 | 2205 | { |
2f3d9587 VZ |
2206 | wxString pathOut; |
2207 | if ( ::GetShortPathName | |
75ef5722 | 2208 | ( |
715e4f7e | 2209 | path.t_str(), |
de564874 | 2210 | wxStringBuffer(pathOut, sz), |
75ef5722 | 2211 | sz |
2f3d9587 VZ |
2212 | ) != 0 ) |
2213 | { | |
2214 | return pathOut; | |
2215 | } | |
9e9b65c1 | 2216 | } |
2f3d9587 | 2217 | #endif // Windows |
5716a1ab VZ |
2218 | |
2219 | return path; | |
9e9b65c1 JS |
2220 | } |
2221 | ||
2222 | // Return the long form of the path (returns identity on non-Windows platforms) | |
2223 | wxString wxFileName::GetLongPath() const | |
2224 | { | |
52dbd056 VZ |
2225 | wxString pathOut, |
2226 | path = GetFullPath(); | |
2227 | ||
b517351b | 2228 | #if defined(__WIN32__) && !defined(__WXWINCE__) && !defined(__WXMICROWIN__) |
05e7001c | 2229 | |
0e207280 | 2230 | #if wxUSE_DYNLIB_CLASS |
62dcaed6 | 2231 | typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); |
05e7001c | 2232 | |
2f3d9587 VZ |
2233 | // this is MT-safe as in the worst case we're going to resolve the function |
2234 | // twice -- but as the result is the same in both threads, it's ok | |
2235 | static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; | |
2236 | if ( !s_pfnGetLongPathName ) | |
9e9b65c1 | 2237 | { |
2f3d9587 | 2238 | static bool s_triedToLoad = false; |
2361ce82 | 2239 | |
2f3d9587 | 2240 | if ( !s_triedToLoad ) |
05e7001c | 2241 | { |
2f3d9587 VZ |
2242 | s_triedToLoad = true; |
2243 | ||
9a83f860 | 2244 | wxDynamicLibrary dllKernel(wxT("kernel32")); |
2f3d9587 | 2245 | |
9a83f860 | 2246 | const wxChar* GetLongPathName = wxT("GetLongPathName") |
4377d3ab | 2247 | #if wxUSE_UNICODE |
9a83f860 | 2248 | wxT("W"); |
4377d3ab | 2249 | #else // ANSI |
9a83f860 | 2250 | wxT("A"); |
4377d3ab WS |
2251 | #endif // Unicode/ANSI |
2252 | ||
2253 | if ( dllKernel.HasSymbol(GetLongPathName) ) | |
05e7001c | 2254 | { |
2f3d9587 | 2255 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) |
4377d3ab | 2256 | dllKernel.GetSymbol(GetLongPathName); |
05e7001c | 2257 | } |
2f3d9587 VZ |
2258 | |
2259 | // note that kernel32.dll can be unloaded, it stays in memory | |
2260 | // anyhow as all Win32 programs link to it and so it's safe to call | |
2261 | // GetLongPathName() even after unloading it | |
05e7001c | 2262 | } |
9e9b65c1 | 2263 | } |
2361ce82 | 2264 | |
2f3d9587 VZ |
2265 | if ( s_pfnGetLongPathName ) |
2266 | { | |
715e4f7e | 2267 | DWORD dwSize = (*s_pfnGetLongPathName)(path.t_str(), NULL, 0); |
2f3d9587 VZ |
2268 | if ( dwSize > 0 ) |
2269 | { | |
2270 | if ( (*s_pfnGetLongPathName) | |
2271 | ( | |
715e4f7e | 2272 | path.t_str(), |
2f3d9587 VZ |
2273 | wxStringBuffer(pathOut, dwSize), |
2274 | dwSize | |
2275 | ) != 0 ) | |
2276 | { | |
2277 | return pathOut; | |
2278 | } | |
2279 | } | |
2280 | } | |
0e207280 | 2281 | #endif // wxUSE_DYNLIB_CLASS |
05e7001c | 2282 | |
2f3d9587 VZ |
2283 | // The OS didn't support GetLongPathName, or some other error. |
2284 | // We need to call FindFirstFile on each component in turn. | |
05e7001c | 2285 | |
2f3d9587 VZ |
2286 | WIN32_FIND_DATA findFileData; |
2287 | HANDLE hFind; | |
b5b62eea | 2288 | |
2f3d9587 VZ |
2289 | if ( HasVolume() ) |
2290 | pathOut = GetVolume() + | |
2291 | GetVolumeSeparator(wxPATH_DOS) + | |
2292 | GetPathSeparator(wxPATH_DOS); | |
2293 | else | |
2294 | pathOut = wxEmptyString; | |
05e7001c | 2295 | |
2f3d9587 VZ |
2296 | wxArrayString dirs = GetDirs(); |
2297 | dirs.Add(GetFullName()); | |
77fe02a8 | 2298 | |
2f3d9587 | 2299 | wxString tmpPath; |
5d978d07 | 2300 | |
2f3d9587 VZ |
2301 | size_t count = dirs.GetCount(); |
2302 | for ( size_t i = 0; i < count; i++ ) | |
2303 | { | |
ea6319cb VZ |
2304 | const wxString& dir = dirs[i]; |
2305 | ||
2f3d9587 VZ |
2306 | // We're using pathOut to collect the long-name path, but using a |
2307 | // temporary for appending the last path component which may be | |
2308 | // short-name | |
ea6319cb VZ |
2309 | tmpPath = pathOut + dir; |
2310 | ||
2311 | // We must not process "." or ".." here as they would be (unexpectedly) | |
2312 | // replaced by the corresponding directory names so just leave them | |
2313 | // alone | |
2314 | // | |
2315 | // And we can't pass a drive and root dir to FindFirstFile (VZ: why?) | |
2316 | if ( tmpPath.empty() || dir == '.' || dir == ".." || | |
2317 | tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) ) | |
2f3d9587 | 2318 | { |
2f3d9587 VZ |
2319 | tmpPath += wxFILE_SEP_PATH; |
2320 | pathOut = tmpPath; | |
2321 | continue; | |
2322 | } | |
05e7001c | 2323 | |
715e4f7e | 2324 | hFind = ::FindFirstFile(tmpPath.t_str(), &findFileData); |
2f3d9587 VZ |
2325 | if (hFind == INVALID_HANDLE_VALUE) |
2326 | { | |
2327 | // Error: most likely reason is that path doesn't exist, so | |
2328 | // append any unprocessed parts and return | |
2329 | for ( i += 1; i < count; i++ ) | |
2330 | tmpPath += wxFILE_SEP_PATH + dirs[i]; | |
b5b62eea | 2331 | |
2f3d9587 VZ |
2332 | return tmpPath; |
2333 | } | |
05e7001c | 2334 | |
2f3d9587 VZ |
2335 | pathOut += findFileData.cFileName; |
2336 | if ( (i < (count-1)) ) | |
2337 | pathOut += wxFILE_SEP_PATH; | |
52dbd056 | 2338 | |
2f3d9587 | 2339 | ::FindClose(hFind); |
05e7001c | 2340 | } |
52dbd056 VZ |
2341 | #else // !Win32 |
2342 | pathOut = path; | |
2343 | #endif // Win32/!Win32 | |
5716a1ab | 2344 | |
05e7001c | 2345 | return pathOut; |
9e9b65c1 JS |
2346 | } |
2347 | ||
df5ddbca RR |
2348 | wxPathFormat wxFileName::GetFormat( wxPathFormat format ) |
2349 | { | |
2350 | if (format == wxPATH_NATIVE) | |
2351 | { | |
d98a58c5 | 2352 | #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__) |
df5ddbca | 2353 | format = wxPATH_DOS; |
3c621059 | 2354 | #elif defined(__VMS) |
04c943b1 | 2355 | format = wxPATH_VMS; |
844f90fb | 2356 | #else |
df5ddbca RR |
2357 | format = wxPATH_UNIX; |
2358 | #endif | |
2359 | } | |
2360 | return format; | |
2361 | } | |
a35b27b1 | 2362 | |
35c2aa4f VZ |
2363 | #ifdef wxHAS_FILESYSTEM_VOLUMES |
2364 | ||
2365 | /* static */ | |
2366 | wxString wxFileName::GetVolumeString(char drive, int flags) | |
2367 | { | |
2368 | wxASSERT_MSG( !(flags & ~wxPATH_GET_SEPARATOR), "invalid flag specified" ); | |
2369 | ||
2370 | wxString vol(drive); | |
2371 | vol += wxFILE_SEP_DSK; | |
2372 | if ( flags & wxPATH_GET_SEPARATOR ) | |
2373 | vol += wxFILE_SEP_PATH; | |
2374 | ||
2375 | return vol; | |
2376 | } | |
2377 | ||
2378 | #endif // wxHAS_FILESYSTEM_VOLUMES | |
2379 | ||
9e8d8607 VZ |
2380 | // ---------------------------------------------------------------------------- |
2381 | // path splitting function | |
2382 | // ---------------------------------------------------------------------------- | |
2383 | ||
6f91bc33 | 2384 | /* static */ |
f1e77933 VZ |
2385 | void |
2386 | wxFileName::SplitVolume(const wxString& fullpathWithVolume, | |
2387 | wxString *pstrVolume, | |
2388 | wxString *pstrPath, | |
2389 | wxPathFormat format) | |
9e8d8607 VZ |
2390 | { |
2391 | format = GetFormat(format); | |
2392 | ||
04c943b1 VZ |
2393 | wxString fullpath = fullpathWithVolume; |
2394 | ||
e01a788e | 2395 | if ( IsMSWUniqueVolumeNamePath(fullpath, format) ) |
9e8d8607 | 2396 | { |
e01a788e VZ |
2397 | // special Windows unique volume names hack: transform |
2398 | // \\?\Volume{guid}\path into Volume{guid}:path | |
2399 | // note: this check must be done before the check for UNC path | |
2400 | ||
2401 | // we know the last backslash from the unique volume name is located | |
2402 | // there from IsMSWUniqueVolumeNamePath | |
2403 | fullpath[wxMSWUniqueVolumePrefixLength - 1] = wxFILE_SEP_DSK; | |
2404 | ||
2405 | // paths starting with a unique volume name should always be absolute | |
2406 | fullpath.insert(wxMSWUniqueVolumePrefixLength, 1, wxFILE_SEP_PATH_DOS); | |
2407 | ||
2408 | // remove the leading "\\?\" part | |
2409 | fullpath.erase(0, 4); | |
2410 | } | |
2411 | else if ( IsUNCPath(fullpath, format) ) | |
2412 | { | |
2413 | // special Windows UNC paths hack: transform \\share\path into share:path | |
2414 | ||
9e1c7236 | 2415 | fullpath.erase(0, 2); |
04c943b1 | 2416 | |
9e1c7236 VZ |
2417 | size_t posFirstSlash = |
2418 | fullpath.find_first_of(GetPathTerminators(format)); | |
2419 | if ( posFirstSlash != wxString::npos ) | |
2420 | { | |
2421 | fullpath[posFirstSlash] = wxFILE_SEP_DSK; | |
04c943b1 | 2422 | |
9e1c7236 VZ |
2423 | // UNC paths are always absolute, right? (FIXME) |
2424 | fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS); | |
3c621059 JJ |
2425 | } |
2426 | } | |
04c943b1 | 2427 | |
a385b5df RR |
2428 | // We separate the volume here |
2429 | if ( format == wxPATH_DOS || format == wxPATH_VMS ) | |
04c943b1 | 2430 | { |
a385b5df | 2431 | wxString sepVol = GetVolumeSeparator(format); |
24eb81cb | 2432 | |
0f506ded VZ |
2433 | // we have to exclude the case of a colon in the very beginning of the |
2434 | // string as it can't be a volume separator (nor can this be a valid | |
2435 | // DOS file name at all but we'll leave dealing with this to our caller) | |
04c943b1 | 2436 | size_t posFirstColon = fullpath.find_first_of(sepVol); |
0f506ded | 2437 | if ( posFirstColon && posFirstColon != wxString::npos ) |
04c943b1 VZ |
2438 | { |
2439 | if ( pstrVolume ) | |
2440 | { | |
2441 | *pstrVolume = fullpath.Left(posFirstColon); | |
2442 | } | |
2443 | ||
2444 | // remove the volume name and the separator from the full path | |
2445 | fullpath.erase(0, posFirstColon + sepVol.length()); | |
2446 | } | |
2447 | } | |
2448 | ||
f1e77933 VZ |
2449 | if ( pstrPath ) |
2450 | *pstrPath = fullpath; | |
2451 | } | |
2452 | ||
2453 | /* static */ | |
2454 | void wxFileName::SplitPath(const wxString& fullpathWithVolume, | |
2455 | wxString *pstrVolume, | |
2456 | wxString *pstrPath, | |
2457 | wxString *pstrName, | |
2458 | wxString *pstrExt, | |
dfecbee5 | 2459 | bool *hasExt, |
f1e77933 VZ |
2460 | wxPathFormat format) |
2461 | { | |
2462 | format = GetFormat(format); | |
2463 | ||
2464 | wxString fullpath; | |
2465 | SplitVolume(fullpathWithVolume, pstrVolume, &fullpath, format); | |
2466 | ||
04c943b1 VZ |
2467 | // find the positions of the last dot and last path separator in the path |
2468 | size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); | |
f1e77933 | 2469 | size_t posLastSlash = fullpath.find_last_of(GetPathTerminators(format)); |
04c943b1 | 2470 | |
e5a573a2 | 2471 | // check whether this dot occurs at the very beginning of a path component |
04c943b1 | 2472 | if ( (posLastDot != wxString::npos) && |
e5a573a2 VZ |
2473 | (posLastDot == 0 || |
2474 | IsPathSeparator(fullpath[posLastDot - 1]) || | |
9a83f860 | 2475 | (format == wxPATH_VMS && fullpath[posLastDot - 1] == wxT(']'))) ) |
f1e77933 VZ |
2476 | { |
2477 | // dot may be (and commonly -- at least under Unix -- is) the first | |
2478 | // character of the filename, don't treat the entire filename as | |
2479 | // extension in this case | |
2480 | posLastDot = wxString::npos; | |
2481 | } | |
9e8d8607 | 2482 | |
8e7dda21 VZ |
2483 | // if we do have a dot and a slash, check that the dot is in the name part |
2484 | if ( (posLastDot != wxString::npos) && | |
2485 | (posLastSlash != wxString::npos) && | |
2486 | (posLastDot < posLastSlash) ) | |
9e8d8607 VZ |
2487 | { |
2488 | // the dot is part of the path, not the start of the extension | |
2489 | posLastDot = wxString::npos; | |
2490 | } | |
2491 | ||
2492 | // now fill in the variables provided by user | |
2493 | if ( pstrPath ) | |
2494 | { | |
2495 | if ( posLastSlash == wxString::npos ) | |
2496 | { | |
2497 | // no path at all | |
2498 | pstrPath->Empty(); | |
2499 | } | |
2500 | else | |
2501 | { | |
04c943b1 | 2502 | // take everything up to the path separator but take care to make |
353f41cb | 2503 | // the path equal to something like '/', not empty, for the files |
04c943b1 VZ |
2504 | // immediately under root directory |
2505 | size_t len = posLastSlash; | |
91b4bd63 SC |
2506 | |
2507 | // this rule does not apply to mac since we do not start with colons (sep) | |
2508 | // except for relative paths | |
2509 | if ( !len && format != wxPATH_MAC) | |
04c943b1 VZ |
2510 | len++; |
2511 | ||
2512 | *pstrPath = fullpath.Left(len); | |
2513 | ||
2514 | // special VMS hack: remove the initial bracket | |
2515 | if ( format == wxPATH_VMS ) | |
2516 | { | |
9a83f860 | 2517 | if ( (*pstrPath)[0u] == wxT('[') ) |
04c943b1 VZ |
2518 | pstrPath->erase(0, 1); |
2519 | } | |
9e8d8607 VZ |
2520 | } |
2521 | } | |
2522 | ||
2523 | if ( pstrName ) | |
2524 | { | |
42b1f941 VZ |
2525 | // take all characters starting from the one after the last slash and |
2526 | // up to, but excluding, the last dot | |
9e8d8607 | 2527 | size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; |
8e7dda21 VZ |
2528 | size_t count; |
2529 | if ( posLastDot == wxString::npos ) | |
2530 | { | |
2531 | // take all until the end | |
2532 | count = wxString::npos; | |
2533 | } | |
2534 | else if ( posLastSlash == wxString::npos ) | |
2535 | { | |
2536 | count = posLastDot; | |
2537 | } | |
2538 | else // have both dot and slash | |
2539 | { | |
2540 | count = posLastDot - posLastSlash - 1; | |
2541 | } | |
9e8d8607 VZ |
2542 | |
2543 | *pstrName = fullpath.Mid(nStart, count); | |
2544 | } | |
2545 | ||
dfecbee5 VZ |
2546 | // finally deal with the extension here: we have an added complication that |
2547 | // extension may be empty (but present) as in "foo." where trailing dot | |
2548 | // indicates the empty extension at the end -- and hence we must remember | |
2549 | // that we have it independently of pstrExt | |
2550 | if ( posLastDot == wxString::npos ) | |
9e8d8607 | 2551 | { |
dfecbee5 VZ |
2552 | // no extension |
2553 | if ( pstrExt ) | |
2554 | pstrExt->clear(); | |
2555 | if ( hasExt ) | |
2556 | *hasExt = false; | |
2557 | } | |
2558 | else | |
2559 | { | |
2560 | // take everything after the dot | |
2561 | if ( pstrExt ) | |
9e8d8607 | 2562 | *pstrExt = fullpath.Mid(posLastDot + 1); |
dfecbee5 VZ |
2563 | if ( hasExt ) |
2564 | *hasExt = true; | |
9e8d8607 VZ |
2565 | } |
2566 | } | |
951cd180 | 2567 | |
6f91bc33 VZ |
2568 | /* static */ |
2569 | void wxFileName::SplitPath(const wxString& fullpath, | |
2570 | wxString *path, | |
2571 | wxString *name, | |
2572 | wxString *ext, | |
2573 | wxPathFormat format) | |
2574 | { | |
2575 | wxString volume; | |
2576 | SplitPath(fullpath, &volume, path, name, ext, format); | |
2577 | ||
67c34f64 | 2578 | if ( path ) |
6f91bc33 | 2579 | { |
67c34f64 | 2580 | path->Prepend(wxGetVolumeString(volume, format)); |
6f91bc33 VZ |
2581 | } |
2582 | } | |
2583 | ||
181dd701 VZ |
2584 | /* static */ |
2585 | wxString wxFileName::StripExtension(const wxString& fullpath) | |
2586 | { | |
2587 | wxFileName fn(fullpath); | |
2588 | fn.SetExt(""); | |
2589 | return fn.GetFullPath(); | |
2590 | } | |
2591 | ||
951cd180 VZ |
2592 | // ---------------------------------------------------------------------------- |
2593 | // time functions | |
2594 | // ---------------------------------------------------------------------------- | |
2595 | ||
e2b87f38 VZ |
2596 | #if wxUSE_DATETIME |
2597 | ||
6dbb903b VZ |
2598 | bool wxFileName::SetTimes(const wxDateTime *dtAccess, |
2599 | const wxDateTime *dtMod, | |
d9e80dce | 2600 | const wxDateTime *dtCreate) const |
951cd180 | 2601 | { |
2b5f62a0 | 2602 | #if defined(__WIN32__) |
6bc176b4 VZ |
2603 | FILETIME ftAccess, ftCreate, ftWrite; |
2604 | ||
2605 | if ( dtCreate ) | |
2606 | ConvertWxToFileTime(&ftCreate, *dtCreate); | |
2607 | if ( dtAccess ) | |
2608 | ConvertWxToFileTime(&ftAccess, *dtAccess); | |
2609 | if ( dtMod ) | |
2610 | ConvertWxToFileTime(&ftWrite, *dtMod); | |
2611 | ||
2612 | wxString path; | |
2613 | int flags; | |
2b5f62a0 VZ |
2614 | if ( IsDir() ) |
2615 | { | |
6bc176b4 VZ |
2616 | if ( wxGetOsVersion() == wxOS_WINDOWS_9X ) |
2617 | { | |
2618 | wxLogError(_("Setting directory access times is not supported " | |
2619 | "under this OS version")); | |
2620 | return false; | |
2621 | } | |
2622 | ||
2623 | path = GetPath(); | |
2624 | flags = FILE_FLAG_BACKUP_SEMANTICS; | |
2b5f62a0 VZ |
2625 | } |
2626 | else // file | |
2627 | { | |
6bc176b4 VZ |
2628 | path = GetFullPath(); |
2629 | flags = 0; | |
2630 | } | |
2631 | ||
f3c74c8d | 2632 | wxFileHandle fh(path, wxFileHandle::WriteAttr, flags); |
6bc176b4 VZ |
2633 | if ( fh.IsOk() ) |
2634 | { | |
2635 | if ( ::SetFileTime(fh, | |
2636 | dtCreate ? &ftCreate : NULL, | |
2637 | dtAccess ? &ftAccess : NULL, | |
2638 | dtMod ? &ftWrite : NULL) ) | |
2b5f62a0 | 2639 | { |
6bc176b4 | 2640 | return true; |
2b5f62a0 VZ |
2641 | } |
2642 | } | |
2643 | #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) | |
17a1ebd1 VZ |
2644 | wxUnusedVar(dtCreate); |
2645 | ||
246c704f VZ |
2646 | if ( !dtAccess && !dtMod ) |
2647 | { | |
2648 | // can't modify the creation time anyhow, don't try | |
f363e05c | 2649 | return true; |
246c704f VZ |
2650 | } |
2651 | ||
2652 | // if dtAccess or dtMod is not specified, use the other one (which must be | |
2653 | // non NULL because of the test above) for both times | |
951cd180 | 2654 | utimbuf utm; |
246c704f VZ |
2655 | utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks(); |
2656 | utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); | |
401eb3de | 2657 | if ( utime(GetFullPath().fn_str(), &utm) == 0 ) |
951cd180 | 2658 | { |
f363e05c | 2659 | return true; |
951cd180 | 2660 | } |
951cd180 | 2661 | #else // other platform |
7a893a31 WS |
2662 | wxUnusedVar(dtAccess); |
2663 | wxUnusedVar(dtMod); | |
2664 | wxUnusedVar(dtCreate); | |
951cd180 VZ |
2665 | #endif // platforms |
2666 | ||
2667 | wxLogSysError(_("Failed to modify file times for '%s'"), | |
2668 | GetFullPath().c_str()); | |
2669 | ||
f363e05c | 2670 | return false; |
951cd180 VZ |
2671 | } |
2672 | ||
d9e80dce | 2673 | bool wxFileName::Touch() const |
951cd180 VZ |
2674 | { |
2675 | #if defined(__UNIX_LIKE__) | |
2676 | // under Unix touching file is simple: just pass NULL to utime() | |
401eb3de | 2677 | if ( utime(GetFullPath().fn_str(), NULL) == 0 ) |
951cd180 | 2678 | { |
f363e05c | 2679 | return true; |
951cd180 VZ |
2680 | } |
2681 | ||
2682 | wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str()); | |
2683 | ||
f363e05c | 2684 | return false; |
951cd180 VZ |
2685 | #else // other platform |
2686 | wxDateTime dtNow = wxDateTime::Now(); | |
2687 | ||
6dbb903b | 2688 | return SetTimes(&dtNow, &dtNow, NULL /* don't change create time */); |
951cd180 VZ |
2689 | #endif // platforms |
2690 | } | |
2691 | ||
2692 | bool wxFileName::GetTimes(wxDateTime *dtAccess, | |
2693 | wxDateTime *dtMod, | |
6dbb903b | 2694 | wxDateTime *dtCreate) const |
951cd180 | 2695 | { |
2b5f62a0 VZ |
2696 | #if defined(__WIN32__) |
2697 | // we must use different methods for the files and directories under | |
2698 | // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and | |
2699 | // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and | |
2700 | // not 9x | |
2701 | bool ok; | |
2702 | FILETIME ftAccess, ftCreate, ftWrite; | |
9a0c5c01 | 2703 | if ( IsDir() ) |
2b5f62a0 VZ |
2704 | { |
2705 | // implemented in msw/dir.cpp | |
2706 | extern bool wxGetDirectoryTimes(const wxString& dirname, | |
2707 | FILETIME *, FILETIME *, FILETIME *); | |
2708 | ||
2709 | // we should pass the path without the trailing separator to | |
2710 | // wxGetDirectoryTimes() | |
2711 | ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME), | |
2712 | &ftAccess, &ftCreate, &ftWrite); | |
2713 | } | |
2714 | else // file | |
2715 | { | |
f3c74c8d | 2716 | wxFileHandle fh(GetFullPath(), wxFileHandle::ReadAttr); |
2b5f62a0 VZ |
2717 | if ( fh.IsOk() ) |
2718 | { | |
2719 | ok = ::GetFileTime(fh, | |
2720 | dtCreate ? &ftCreate : NULL, | |
2721 | dtAccess ? &ftAccess : NULL, | |
2722 | dtMod ? &ftWrite : NULL) != 0; | |
2723 | } | |
2724 | else | |
2725 | { | |
f363e05c | 2726 | ok = false; |
2b5f62a0 VZ |
2727 | } |
2728 | } | |
2729 | ||
2730 | if ( ok ) | |
2731 | { | |
2732 | if ( dtCreate ) | |
2733 | ConvertFileTimeToWx(dtCreate, ftCreate); | |
2734 | if ( dtAccess ) | |
2735 | ConvertFileTimeToWx(dtAccess, ftAccess); | |
2736 | if ( dtMod ) | |
2737 | ConvertFileTimeToWx(dtMod, ftWrite); | |
2738 | ||
f363e05c | 2739 | return true; |
2b5f62a0 | 2740 | } |
bf58daba | 2741 | #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__)) |
51cb1a65 | 2742 | // no need to test for IsDir() here |
951cd180 | 2743 | wxStructStat stBuf; |
c063adeb | 2744 | if ( StatAny(stBuf, *this) ) |
951cd180 | 2745 | { |
1b4bff82 VZ |
2746 | // Android defines st_*time fields as unsigned long, but time_t as long, |
2747 | // hence the static_casts. | |
951cd180 | 2748 | if ( dtAccess ) |
1b4bff82 | 2749 | dtAccess->Set(static_cast<time_t>(stBuf.st_atime)); |
951cd180 | 2750 | if ( dtMod ) |
1b4bff82 | 2751 | dtMod->Set(static_cast<time_t>(stBuf.st_mtime)); |
6dbb903b | 2752 | if ( dtCreate ) |
1b4bff82 | 2753 | dtCreate->Set(static_cast<time_t>(stBuf.st_ctime)); |
951cd180 | 2754 | |
f363e05c | 2755 | return true; |
951cd180 | 2756 | } |
951cd180 | 2757 | #else // other platform |
7a893a31 WS |
2758 | wxUnusedVar(dtAccess); |
2759 | wxUnusedVar(dtMod); | |
2760 | wxUnusedVar(dtCreate); | |
951cd180 VZ |
2761 | #endif // platforms |
2762 | ||
2763 | wxLogSysError(_("Failed to retrieve file times for '%s'"), | |
2764 | GetFullPath().c_str()); | |
2765 | ||
f363e05c | 2766 | return false; |
951cd180 VZ |
2767 | } |
2768 | ||
e2b87f38 VZ |
2769 | #endif // wxUSE_DATETIME |
2770 | ||
23b8a262 JS |
2771 | |
2772 | // ---------------------------------------------------------------------------- | |
2773 | // file size functions | |
2774 | // ---------------------------------------------------------------------------- | |
2775 | ||
bd08f2f7 VZ |
2776 | #if wxUSE_LONGLONG |
2777 | ||
23b8a262 JS |
2778 | /* static */ |
2779 | wxULongLong wxFileName::GetSize(const wxString &filename) | |
2780 | { | |
2781 | if (!wxFileExists(filename)) | |
2782 | return wxInvalidSize; | |
2783 | ||
bd362275 | 2784 | #if defined(__WIN32__) |
f3c74c8d | 2785 | wxFileHandle f(filename, wxFileHandle::ReadAttr); |
23b8a262 JS |
2786 | if (!f.IsOk()) |
2787 | return wxInvalidSize; | |
2788 | ||
2789 | DWORD lpFileSizeHigh; | |
2790 | DWORD ret = GetFileSize(f, &lpFileSizeHigh); | |
5ab9534b | 2791 | if ( ret == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR ) |
23b8a262 JS |
2792 | return wxInvalidSize; |
2793 | ||
5ab9534b VZ |
2794 | return wxULongLong(lpFileSizeHigh, ret); |
2795 | #else // ! __WIN32__ | |
23b8a262 | 2796 | wxStructStat st; |
23b8a262 | 2797 | if (wxStat( filename, &st) != 0) |
23b8a262 JS |
2798 | return wxInvalidSize; |
2799 | return wxULongLong(st.st_size); | |
2800 | #endif | |
2801 | } | |
2802 | ||
2803 | /* static */ | |
2804 | wxString wxFileName::GetHumanReadableSize(const wxULongLong &bs, | |
2805 | const wxString &nullsize, | |
b2edb8f3 VZ |
2806 | int precision, |
2807 | wxSizeConvention conv) | |
23b8a262 | 2808 | { |
b2edb8f3 VZ |
2809 | // deal with trivial case first |
2810 | if ( bs == 0 || bs == wxInvalidSize ) | |
23b8a262 JS |
2811 | return nullsize; |
2812 | ||
b2edb8f3 VZ |
2813 | // depending on the convention used the multiplier may be either 1000 or |
2814 | // 1024 and the binary infix may be empty (for "KB") or "i" (for "KiB") | |
7f19ef40 | 2815 | double multiplier = 1024.; |
b2edb8f3 VZ |
2816 | wxString biInfix; |
2817 | ||
2818 | switch ( conv ) | |
2819 | { | |
7f19ef40 VZ |
2820 | case wxSIZE_CONV_TRADITIONAL: |
2821 | // nothing to do, this corresponds to the default values of both | |
2822 | // the multiplier and infix string | |
2823 | break; | |
2824 | ||
b2edb8f3 VZ |
2825 | case wxSIZE_CONV_IEC: |
2826 | biInfix = "i"; | |
b2edb8f3 VZ |
2827 | break; |
2828 | ||
2829 | case wxSIZE_CONV_SI: | |
2830 | multiplier = 1000; | |
2831 | break; | |
2832 | } | |
2833 | ||
2834 | const double kiloByteSize = multiplier; | |
2835 | const double megaByteSize = multiplier * kiloByteSize; | |
2836 | const double gigaByteSize = multiplier * megaByteSize; | |
2837 | const double teraByteSize = multiplier * gigaByteSize; | |
2838 | ||
2839 | const double bytesize = bs.ToDouble(); | |
2840 | ||
2841 | wxString result; | |
2842 | if ( bytesize < kiloByteSize ) | |
2843 | result.Printf("%s B", bs.ToString()); | |
2844 | else if ( bytesize < megaByteSize ) | |
2845 | result.Printf("%.*f K%sB", precision, bytesize/kiloByteSize, biInfix); | |
2846 | else if (bytesize < gigaByteSize) | |
2847 | result.Printf("%.*f M%sB", precision, bytesize/megaByteSize, biInfix); | |
2848 | else if (bytesize < teraByteSize) | |
2849 | result.Printf("%.*f G%sB", precision, bytesize/gigaByteSize, biInfix); | |
2850 | else | |
2851 | result.Printf("%.*f T%sB", precision, bytesize/teraByteSize, biInfix); | |
23b8a262 | 2852 | |
b2edb8f3 | 2853 | return result; |
23b8a262 JS |
2854 | } |
2855 | ||
2856 | wxULongLong wxFileName::GetSize() const | |
2857 | { | |
2858 | return GetSize(GetFullPath()); | |
2859 | } | |
2860 | ||
b2edb8f3 VZ |
2861 | wxString wxFileName::GetHumanReadableSize(const wxString& failmsg, |
2862 | int precision, | |
2863 | wxSizeConvention conv) const | |
23b8a262 | 2864 | { |
b2edb8f3 | 2865 | return GetHumanReadableSize(GetSize(), failmsg, precision, conv); |
23b8a262 JS |
2866 | } |
2867 | ||
bd08f2f7 | 2868 | #endif // wxUSE_LONGLONG |
23b8a262 JS |
2869 | |
2870 | // ---------------------------------------------------------------------------- | |
2871 | // Mac-specific functions | |
2872 | // ---------------------------------------------------------------------------- | |
2873 | ||
26eef304 | 2874 | #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON |
4dfbcdd5 | 2875 | |
56ce942b VZ |
2876 | namespace |
2877 | { | |
2878 | ||
0ca4ae93 | 2879 | class MacDefaultExtensionRecord |
4dfbcdd5 | 2880 | { |
56ce942b VZ |
2881 | public: |
2882 | MacDefaultExtensionRecord() | |
2883 | { | |
2884 | m_type = | |
2885 | m_creator = 0 ; | |
2886 | } | |
4dfbcdd5 | 2887 | |
56ce942b | 2888 | // default copy ctor, assignment operator and dtor are ok |
0ca4ae93 | 2889 | |
56ce942b VZ |
2890 | MacDefaultExtensionRecord(const wxString& ext, OSType type, OSType creator) |
2891 | : m_ext(ext) | |
2892 | { | |
2893 | m_type = type; | |
2894 | m_creator = creator; | |
2895 | } | |
2896 | ||
2897 | wxString m_ext; | |
2898 | OSType m_type; | |
2899 | OSType m_creator; | |
2900 | }; | |
2901 | ||
2902 | WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray); | |
2903 | ||
2904 | bool gMacDefaultExtensionsInited = false; | |
0ca4ae93 | 2905 | |
4dfbcdd5 | 2906 | #include "wx/arrimpl.cpp" |
0ca4ae93 | 2907 | |
56ce942b | 2908 | WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray); |
4dfbcdd5 | 2909 | |
56ce942b | 2910 | MacDefaultExtensionArray gMacDefaultExtensions; |
4dfbcdd5 | 2911 | |
5974c3cf | 2912 | // load the default extensions |
56ce942b | 2913 | const MacDefaultExtensionRecord gDefaults[] = |
4dfbcdd5 | 2914 | { |
56ce942b VZ |
2915 | MacDefaultExtensionRecord( "txt", 'TEXT', 'ttxt' ), |
2916 | MacDefaultExtensionRecord( "tif", 'TIFF', '****' ), | |
2917 | MacDefaultExtensionRecord( "jpg", 'JPEG', '****' ), | |
2918 | }; | |
0ea621cc | 2919 | |
56ce942b | 2920 | void MacEnsureDefaultExtensionsLoaded() |
5974c3cf SC |
2921 | { |
2922 | if ( !gMacDefaultExtensionsInited ) | |
4dfbcdd5 | 2923 | { |
5974c3cf SC |
2924 | // we could load the pc exchange prefs here too |
2925 | for ( size_t i = 0 ; i < WXSIZEOF( gDefaults ) ; ++i ) | |
2926 | { | |
2927 | gMacDefaultExtensions.Add( gDefaults[i] ) ; | |
2928 | } | |
56ce942b | 2929 | gMacDefaultExtensionsInited = true; |
0ea621cc | 2930 | } |
4dfbcdd5 | 2931 | } |
a2b77260 | 2932 | |
56ce942b VZ |
2933 | } // anonymous namespace |
2934 | ||
0ea621cc | 2935 | bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) |
4dfbcdd5 | 2936 | { |
a2b77260 SC |
2937 | FSRef fsRef ; |
2938 | FSCatalogInfo catInfo; | |
2939 | FileInfo *finfo ; | |
4dfbcdd5 | 2940 | |
a2b77260 SC |
2941 | if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) |
2942 | { | |
a62848fd WS |
2943 | if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) |
2944 | { | |
2945 | finfo = (FileInfo*)&catInfo.finderInfo; | |
2946 | finfo->fileType = type ; | |
2947 | finfo->fileCreator = creator ; | |
2948 | FSSetCatalogInfo( &fsRef, kFSCatInfoFinderInfo, &catInfo ) ; | |
a2b77260 | 2949 | return true ; |
a62848fd | 2950 | } |
a2b77260 SC |
2951 | } |
2952 | return false ; | |
4dfbcdd5 SC |
2953 | } |
2954 | ||
d9e80dce | 2955 | bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) const |
4dfbcdd5 | 2956 | { |
a2b77260 SC |
2957 | FSRef fsRef ; |
2958 | FSCatalogInfo catInfo; | |
2959 | FileInfo *finfo ; | |
4dfbcdd5 | 2960 | |
a2b77260 SC |
2961 | if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) |
2962 | { | |
a62848fd WS |
2963 | if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) |
2964 | { | |
2965 | finfo = (FileInfo*)&catInfo.finderInfo; | |
2966 | *type = finfo->fileType ; | |
2967 | *creator = finfo->fileCreator ; | |
a2b77260 | 2968 | return true ; |
a62848fd | 2969 | } |
a2b77260 SC |
2970 | } |
2971 | return false ; | |
4dfbcdd5 SC |
2972 | } |
2973 | ||
2974 | bool wxFileName::MacSetDefaultTypeAndCreator() | |
2975 | { | |
2976 | wxUint32 type , creator ; | |
2977 | if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type , | |
2978 | &creator ) ) | |
2979 | { | |
f63fb56d GD |
2980 | return MacSetTypeAndCreator( type , creator ) ; |
2981 | } | |
2982 | return false; | |
4dfbcdd5 SC |
2983 | } |
2984 | ||
0ea621cc | 2985 | bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator ) |
4dfbcdd5 SC |
2986 | { |
2987 | MacEnsureDefaultExtensionsLoaded() ; | |
2988 | wxString extl = ext.Lower() ; | |
2989 | for( int i = gMacDefaultExtensions.Count() - 1 ; i >= 0 ; --i ) | |
2990 | { | |
2991 | if ( gMacDefaultExtensions.Item(i).m_ext == extl ) | |
2992 | { | |
2993 | *type = gMacDefaultExtensions.Item(i).m_type ; | |
2994 | *creator = gMacDefaultExtensions.Item(i).m_creator ; | |
2995 | return true ; | |
2996 | } | |
2997 | } | |
2998 | return false ; | |
2999 | } | |
3000 | ||
3001 | void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator ) | |
3002 | { | |
56ce942b VZ |
3003 | MacEnsureDefaultExtensionsLoaded(); |
3004 | MacDefaultExtensionRecord rec(ext.Lower(), type, creator); | |
3005 | gMacDefaultExtensions.Add( rec ); | |
4dfbcdd5 | 3006 | } |
56ce942b VZ |
3007 | |
3008 | #endif // defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON |