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