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