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