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