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