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