]> git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
#ifdefed out the joystick code, and removed the wxBitmapList addition
[wxWidgets.git] / src / common / filename.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filename.cpp
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling, Vadim Zeitlin
5 // Modified by:
6 // Created: 28.12.2000
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "filename.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif
35
36 #include "wx/filename.h"
37 #include "wx/tokenzr.h"
38 #include "wx/config.h" // for wxExpandEnvVars
39 #include "wx/utils.h"
40
41 #if wxUSE_DYNLIB_CLASS
42 #include "wx/dynlib.h"
43 #endif
44
45 // For GetShort/LongPathName
46 #ifdef __WIN32__
47 #include <windows.h>
48
49 #include "wx/msw/winundef.h"
50 #endif
51
52 // utime() is POSIX so should normally be available on all Unices
53 #ifdef __UNIX_LIKE__
54 #include <sys/types.h>
55 #include <utime.h>
56 #include <sys/stat.h>
57 #include <unistd.h>
58 #endif
59
60 // ----------------------------------------------------------------------------
61 // private classes
62 // ----------------------------------------------------------------------------
63
64 // small helper class which opens and closes the file - we use it just to get
65 // a file handle for the given file name to pass it to some Win32 API function
66 #ifdef __WIN32__
67
68 class wxFileHandle
69 {
70 public:
71 wxFileHandle(const wxString& filename)
72 {
73 m_hFile = ::CreateFile
74 (
75 filename, // name
76 GENERIC_READ, // access mask
77 0, // no sharing
78 NULL, // no secutity attr
79 OPEN_EXISTING, // creation disposition
80 0, // no flags
81 NULL // no template file
82 );
83
84 if ( m_hFile == INVALID_HANDLE_VALUE )
85 {
86 wxLogSysError(_("Failed to open '%s' for reading"),
87 filename.c_str());
88 }
89 }
90
91 ~wxFileHandle()
92 {
93 if ( m_hFile != INVALID_HANDLE_VALUE )
94 {
95 if ( !::CloseHandle(m_hFile) )
96 {
97 wxLogSysError(_("Failed to close file handle"));
98 }
99 }
100 }
101
102 // return TRUE only if the file could be opened successfully
103 bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; }
104
105 // get the handle
106 operator HANDLE() const { return m_hFile; }
107
108 private:
109 HANDLE m_hFile;
110 };
111
112 #endif // __WIN32__
113
114 // ----------------------------------------------------------------------------
115 // private functions
116 // ----------------------------------------------------------------------------
117
118 #ifdef __WIN32__
119
120 // convert between wxDateTime and FILETIME which is a 64-bit value representing
121 // the number of 100-nanosecond intervals since January 1, 1601.
122
123 // the number of milliseconds between the Unix Epoch (January 1, 1970) and the
124 // FILETIME reference point (January 1, 1601)
125 static const wxLongLong FILETIME_EPOCH_OFFSET = wxLongLong(0xa97, 0x30b66800);
126
127 static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft)
128 {
129 wxLongLong ll(ft.dwHighDateTime, ft.dwLowDateTime);
130
131 // convert 100ns to ms
132 ll /= 10000;
133
134 // move it to our Epoch
135 ll -= FILETIME_EPOCH_OFFSET;
136
137 *dt = wxDateTime(ll);
138 }
139
140 static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt)
141 {
142 // do the reverse of ConvertFileTimeToWx()
143 wxLongLong ll = dt.GetValue();
144 ll *= 10000;
145 ll += FILETIME_EPOCH_OFFSET;
146
147 ft->dwHighDateTime = ll.GetHi();
148 ft->dwLowDateTime = ll.GetLo();
149 }
150
151 #endif // __WIN32__
152
153 // ============================================================================
154 // implementation
155 // ============================================================================
156
157 // ----------------------------------------------------------------------------
158 // wxFileName construction
159 // ----------------------------------------------------------------------------
160
161 void wxFileName::Assign( const wxFileName &filepath )
162 {
163 m_ext = filepath.GetExt();
164 m_name = filepath.GetName();
165 m_dirs = filepath.GetDirs();
166 }
167
168 void wxFileName::Assign( const wxString& path,
169 const wxString& name,
170 const wxString& ext,
171 wxPathFormat format )
172 {
173 wxStringTokenizer tn(path, GetPathSeparators(format),
174 wxTOKEN_RET_EMPTY_ALL);
175 int i = 0;
176 m_dirs.Clear();
177 while ( tn.HasMoreTokens() )
178 {
179 wxString token = tn.GetNextToken();
180
181 // If the path starts with a slash (or two for a network path),
182 // we need the first dir entry to be an empty for later reassembly.
183 if ((i < 2) || !token.IsEmpty())
184 m_dirs.Add( token );
185
186 i ++;
187 }
188
189 m_ext = ext;
190 m_name = name;
191 }
192
193 void wxFileName::Assign(const wxString& fullpath,
194 wxPathFormat format)
195 {
196 wxString path, name, ext;
197 SplitPath(fullpath, &path, &name, &ext, format);
198
199 Assign(path, name, ext, format);
200 }
201
202 void wxFileName::Assign(const wxString& path,
203 const wxString& fullname,
204 wxPathFormat format)
205 {
206 wxString name, ext;
207 SplitPath(fullname, NULL /* no path */, &name, &ext, format);
208
209 Assign(path, name, ext, format);
210 }
211
212 void wxFileName::Clear()
213 {
214 m_dirs.Clear();
215 m_name =
216 m_ext = wxEmptyString;
217 }
218
219 /* static */
220 wxFileName wxFileName::FileName(const wxString& file)
221 {
222 return wxFileName(file);
223 }
224
225 /* static */
226 wxFileName wxFileName::DirName(const wxString& dir)
227 {
228 wxFileName fn;
229 fn.AssignDir(dir);
230 return fn;
231 }
232
233 // ----------------------------------------------------------------------------
234 // existence tests
235 // ----------------------------------------------------------------------------
236
237 bool wxFileName::FileExists()
238 {
239 return wxFileName::FileExists( GetFullPath() );
240 }
241
242 bool wxFileName::FileExists( const wxString &file )
243 {
244 return ::wxFileExists( file );
245 }
246
247 bool wxFileName::DirExists()
248 {
249 return wxFileName::DirExists( GetFullPath() );
250 }
251
252 bool wxFileName::DirExists( const wxString &dir )
253 {
254 return ::wxDirExists( dir );
255 }
256
257 // ----------------------------------------------------------------------------
258 // CWD and HOME stuff
259 // ----------------------------------------------------------------------------
260
261 void wxFileName::AssignCwd()
262 {
263 AssignDir(wxFileName::GetCwd());
264 }
265
266 /* static */
267 wxString wxFileName::GetCwd()
268 {
269 return ::wxGetCwd();
270 }
271
272 bool wxFileName::SetCwd()
273 {
274 return wxFileName::SetCwd( GetFullPath() );
275 }
276
277 bool wxFileName::SetCwd( const wxString &cwd )
278 {
279 return ::wxSetWorkingDirectory( cwd );
280 }
281
282 void wxFileName::AssignHomeDir()
283 {
284 AssignDir(wxFileName::GetHomeDir());
285 }
286
287 wxString wxFileName::GetHomeDir()
288 {
289 return ::wxGetHomeDir();
290 }
291
292 void wxFileName::AssignTempFileName( const wxString &prefix )
293 {
294 wxString fullname;
295 if ( wxGetTempFileName(prefix, fullname) )
296 {
297 Assign(fullname);
298 }
299 else // error
300 {
301 Clear();
302 }
303 }
304
305 // ----------------------------------------------------------------------------
306 // directory operations
307 // ----------------------------------------------------------------------------
308
309 bool wxFileName::Mkdir( int perm, bool full )
310 {
311 return wxFileName::Mkdir( GetFullPath(), perm, full );
312 }
313
314 bool wxFileName::Mkdir( const wxString &dir, int perm, bool full )
315 {
316 if (full)
317 {
318 wxFileName filename(dir);
319 wxArrayString dirs = filename.GetDirs();
320 dirs.Add(filename.GetName());
321
322 size_t count = dirs.GetCount();
323 size_t i;
324 wxString currPath;
325 int noErrors = 0;
326 for ( i = 0; i < count; i++ )
327 {
328 currPath += dirs[i];
329
330 if (currPath.Last() == wxT(':'))
331 {
332 // Can't create a root directory so continue to next dir
333 currPath += wxFILE_SEP_PATH;
334 continue;
335 }
336
337 if (!DirExists(currPath))
338 if (!wxMkdir(currPath, perm))
339 noErrors ++;
340
341 if ( (i < (count-1)) )
342 currPath += wxFILE_SEP_PATH;
343 }
344
345 return (noErrors == 0);
346
347 }
348 else
349 return ::wxMkdir( dir, perm );
350 }
351
352 bool wxFileName::Rmdir()
353 {
354 return wxFileName::Rmdir( GetFullPath() );
355 }
356
357 bool wxFileName::Rmdir( const wxString &dir )
358 {
359 return ::wxRmdir( dir );
360 }
361
362 // ----------------------------------------------------------------------------
363 // path normalization
364 // ----------------------------------------------------------------------------
365
366 bool wxFileName::Normalize(wxPathNormalize flags,
367 const wxString& cwd,
368 wxPathFormat format)
369 {
370 // the existing path components
371 wxArrayString dirs = GetDirs();
372
373 // the path to prepend in front to make the path absolute
374 wxFileName curDir;
375
376 format = GetFormat(format);
377
378 // make the path absolute
379 if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute() )
380 {
381 if ( cwd.empty() )
382 curDir.AssignCwd();
383 else
384 curDir.AssignDir(cwd);
385 }
386
387 // handle ~ stuff under Unix only
388 if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
389 {
390 if ( !dirs.IsEmpty() )
391 {
392 wxString dir = dirs[0u];
393 if ( !dir.empty() && dir[0u] == _T('~') )
394 {
395 curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
396
397 dirs.RemoveAt(0u);
398 }
399 }
400 }
401
402 if ( curDir.IsOk() )
403 {
404 wxArrayString dirsNew = curDir.GetDirs();
405 size_t count = dirs.GetCount();
406 for ( size_t n = 0; n < count; n++ )
407 {
408 dirsNew.Add(dirs[n]);
409 }
410
411 dirs = dirsNew;
412 }
413
414 // now deal with ".", ".." and the rest
415 m_dirs.Empty();
416 size_t count = dirs.GetCount();
417 for ( size_t n = 0; n < count; n++ )
418 {
419 wxString dir = dirs[n];
420
421 if ( flags && wxPATH_NORM_DOTS )
422 {
423 if ( dir == wxT(".") )
424 {
425 // just ignore
426 continue;
427 }
428
429 if ( dir == wxT("..") )
430 {
431 if ( m_dirs.IsEmpty() )
432 {
433 wxLogError(_("The path '%s' contains too many \"..\"!"),
434 GetFullPath().c_str());
435 return FALSE;
436 }
437
438 m_dirs.Remove(m_dirs.GetCount() - 1);
439 continue;
440 }
441 }
442
443 if ( flags & wxPATH_NORM_ENV_VARS )
444 {
445 dir = wxExpandEnvVars(dir);
446 }
447
448 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
449 {
450 dir.MakeLower();
451 }
452
453 m_dirs.Add(dir);
454 }
455
456 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
457 {
458 // VZ: expand env vars here too?
459
460 m_name.MakeLower();
461 m_ext.MakeLower();
462 }
463
464 #if defined(__WXMSW__) && defined(__WIN32__)
465 if (flags & wxPATH_NORM_LONG)
466 {
467 Assign(GetLongPath());
468 }
469 #endif
470
471 return TRUE;
472 }
473
474 // ----------------------------------------------------------------------------
475 // filename kind tests
476 // ----------------------------------------------------------------------------
477
478 bool wxFileName::SameAs( const wxFileName &filepath, wxPathFormat format)
479 {
480 wxFileName fn1 = *this,
481 fn2 = filepath;
482
483 // get cwd only once - small time saving
484 wxString cwd = wxGetCwd();
485 fn1.Normalize(wxPATH_NORM_ALL, cwd, format);
486 fn2.Normalize(wxPATH_NORM_ALL, cwd, format);
487
488 if ( fn1.GetFullPath() == fn2.GetFullPath() )
489 return TRUE;
490
491 // TODO: compare inodes for Unix, this works even when filenames are
492 // different but files are the same (symlinks) (VZ)
493
494 return FALSE;
495 }
496
497 /* static */
498 bool wxFileName::IsCaseSensitive( wxPathFormat format )
499 {
500 // only DOS filenames are case-sensitive
501 return GetFormat(format) != wxPATH_DOS;
502 }
503
504 bool wxFileName::IsRelative( wxPathFormat format )
505 {
506 return !IsAbsolute(format);
507 }
508
509 bool wxFileName::IsAbsolute( wxPathFormat format )
510 {
511 wxChar ch = m_dirs.IsEmpty() ? _T('\0') : m_dirs[0u][0u];
512
513 // Hack to cope with e.g. c:\thing - need something better
514 wxChar driveSep = _T('\0');
515 if (!m_dirs.IsEmpty() && m_dirs[0].Length() > 1)
516 driveSep = m_dirs[0u][1u];
517
518 // the path is absolute if it starts with a path separator or, only for
519 // Unix filenames, with "~" or "~user"
520 return IsPathSeparator(ch, format) ||
521 driveSep == _T(':') ||
522 (GetFormat(format) == wxPATH_UNIX && ch == _T('~') );
523 }
524
525 /* static */
526 wxString wxFileName::GetPathSeparators(wxPathFormat format)
527 {
528 wxString seps;
529 switch ( GetFormat(format) )
530 {
531 case wxPATH_DOS:
532 // accept both as native APIs do
533 seps << wxFILE_SEP_PATH_UNIX << wxFILE_SEP_PATH_DOS;
534 break;
535
536 default:
537 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
538 // fall through
539
540 case wxPATH_UNIX:
541 seps = wxFILE_SEP_PATH_UNIX;
542 break;
543
544 case wxPATH_MAC:
545 seps = wxFILE_SEP_PATH_MAC;
546 break;
547 }
548
549 return seps;
550 }
551
552 /* static */
553 bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
554 {
555 return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
556 }
557
558 bool wxFileName::IsWild( wxPathFormat format )
559 {
560 // FIXME: this is probably false for Mac and this is surely wrong for most
561 // of Unix shells (think about "[...]")
562 (void)format;
563 return m_name.find_first_of(_T("*?")) != wxString::npos;
564 }
565
566 // ----------------------------------------------------------------------------
567 // path components manipulation
568 // ----------------------------------------------------------------------------
569
570 void wxFileName::AppendDir( const wxString &dir )
571 {
572 m_dirs.Add( dir );
573 }
574
575 void wxFileName::PrependDir( const wxString &dir )
576 {
577 m_dirs.Insert( dir, 0 );
578 }
579
580 void wxFileName::InsertDir( int before, const wxString &dir )
581 {
582 m_dirs.Insert( dir, before );
583 }
584
585 void wxFileName::RemoveDir( int pos )
586 {
587 m_dirs.Remove( (size_t)pos );
588 }
589
590 // ----------------------------------------------------------------------------
591 // accessors
592 // ----------------------------------------------------------------------------
593
594 void wxFileName::SetFullName(const wxString& fullname)
595 {
596 SplitPath(fullname, NULL /* no path */, &m_name, &m_ext);
597 }
598
599 wxString wxFileName::GetFullName() const
600 {
601 wxString fullname = m_name;
602 if ( !m_ext.empty() )
603 {
604 fullname << wxFILE_SEP_EXT << m_ext;
605 }
606
607 return fullname;
608 }
609
610 wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const
611 {
612 format = GetFormat( format );
613
614 wxString ret;
615 size_t count = m_dirs.GetCount();
616 for ( size_t i = 0; i < count; i++ )
617 {
618 ret += m_dirs[i];
619 if ( add_separator || (i < count) )
620 ret += wxFILE_SEP_PATH;
621 }
622
623 return ret;
624 }
625
626 wxString wxFileName::GetFullPath( wxPathFormat format ) const
627 {
628 format = GetFormat( format );
629
630 wxString ret;
631 if (format == wxPATH_DOS)
632 {
633 for (size_t i = 0; i < m_dirs.GetCount(); i++)
634 {
635 ret += m_dirs[i];
636 ret += '\\';
637 }
638 }
639 else
640 if (format == wxPATH_UNIX)
641 {
642 for (size_t i = 0; i < m_dirs.GetCount(); i++)
643 {
644 ret += m_dirs[i];
645 ret += '/';
646 }
647 }
648 else
649 {
650 for (size_t i = 0; i < m_dirs.GetCount(); i++)
651 {
652 ret += m_dirs[i];
653 ret += ':';
654 }
655 }
656
657 ret += m_name;
658
659 if (!m_ext.IsEmpty())
660 {
661 ret += '.';
662 ret += m_ext;
663 }
664
665 return ret;
666 }
667
668 // Return the short form of the path (returns identity on non-Windows platforms)
669 wxString wxFileName::GetShortPath() const
670 {
671 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
672 wxString path(GetFullPath());
673 wxString pathOut;
674 DWORD sz = ::GetShortPathName(path, NULL, 0);
675 bool ok = sz != 0;
676 if ( ok )
677 {
678 ok = ::GetShortPathName
679 (
680 path,
681 pathOut.GetWriteBuf(sz),
682 sz
683 ) != 0;
684 pathOut.UngetWriteBuf();
685 }
686 if (ok)
687 return pathOut;
688
689 return path;
690 #else
691 return GetFullPath();
692 #endif
693 }
694
695 // Return the long form of the path (returns identity on non-Windows platforms)
696 wxString wxFileName::GetLongPath() const
697 {
698 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
699 wxString path(GetFullPath());
700 wxString pathOut;
701 bool success = FALSE;
702
703 // VZ: this code was disabled, why?
704 #if 0 // wxUSE_DYNLIB_CLASS
705 typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
706
707 static bool s_triedToLoad = FALSE;
708
709 if ( !s_triedToLoad )
710 {
711 s_triedToLoad = TRUE;
712 wxDllType dllKernel = wxDllLoader::LoadLibrary(_T("kernel32"));
713 if ( dllKernel )
714 {
715 // may succeed or fail depending on the Windows version
716 static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
717 #ifdef _UNICODE
718 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameW"));
719 #else
720 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameA"));
721 #endif
722
723 wxDllLoader::UnloadLibrary(dllKernel);
724
725 if ( s_pfnGetLongPathName )
726 {
727 DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
728 bool ok = dwSize > 0;
729
730 if ( ok )
731 {
732 DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0);
733 ok = sz != 0;
734 if ( ok )
735 {
736 ok = (*s_pfnGetLongPathName)
737 (
738 path,
739 pathOut.GetWriteBuf(sz),
740 sz
741 ) != 0;
742 pathOut.UngetWriteBuf();
743
744 success = TRUE;
745 }
746 }
747 }
748 }
749 }
750 if (success)
751 return pathOut;
752 #endif // wxUSE_DYNLIB_CLASS
753
754 if (!success)
755 {
756 // The OS didn't support GetLongPathName, or some other error.
757 // We need to call FindFirstFile on each component in turn.
758
759 WIN32_FIND_DATA findFileData;
760 HANDLE hFind;
761 pathOut = wxEmptyString;
762
763 wxArrayString dirs = GetDirs();
764 dirs.Add(GetFullName());
765
766 size_t count = dirs.GetCount();
767 size_t i;
768 wxString tmpPath;
769
770 for ( i = 0; i < count; i++ )
771 {
772 // We're using pathOut to collect the long-name path,
773 // but using a temporary for appending the last path component which may be short-name
774 tmpPath = pathOut + dirs[i];
775
776 if (tmpPath.Last() == wxT(':'))
777 {
778 // Can't pass a drive and root dir to FindFirstFile,
779 // so continue to next dir
780 tmpPath += wxFILE_SEP_PATH;
781 pathOut = tmpPath;
782 continue;
783 }
784
785 hFind = ::FindFirstFile(tmpPath, &findFileData);
786 if (hFind == INVALID_HANDLE_VALUE)
787 {
788 // Error: return immediately with the original path
789 return path;
790 }
791 else
792 {
793 pathOut += findFileData.cFileName;
794 if ( (i < (count-1)) )
795 pathOut += wxFILE_SEP_PATH;
796
797 ::FindClose(hFind);
798 }
799 }
800 }
801
802 return pathOut;
803 #else
804 return GetFullPath();
805 #endif
806 }
807
808 wxPathFormat wxFileName::GetFormat( wxPathFormat format )
809 {
810 if (format == wxPATH_NATIVE)
811 {
812 #if defined(__WXMSW__) || defined(__WXPM__)
813 format = wxPATH_DOS;
814 #elif defined(__WXMAC__) && !defined(__DARWIN__)
815 format = wxPATH_MAC;
816 #else
817 format = wxPATH_UNIX;
818 #endif
819 }
820 return format;
821 }
822
823 // ----------------------------------------------------------------------------
824 // path splitting function
825 // ----------------------------------------------------------------------------
826
827 void wxFileName::SplitPath(const wxString& fullpath,
828 wxString *pstrPath,
829 wxString *pstrName,
830 wxString *pstrExt,
831 wxPathFormat format)
832 {
833 format = GetFormat(format);
834
835 // find the positions of the last dot and last path separator in the path
836 size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT);
837 size_t posLastSlash = fullpath.find_last_of(GetPathSeparators(format));
838
839 if ( (posLastDot != wxString::npos) && (format == wxPATH_UNIX) )
840 {
841 if ( (posLastDot == 0) ||
842 (fullpath[posLastDot - 1] == wxFILE_SEP_PATH_UNIX) )
843 {
844 // under Unix, dot may be (and commonly is) the first character of
845 // the filename, don't treat the entire filename as extension in
846 // this case
847 posLastDot = wxString::npos;
848 }
849 }
850
851 // if we do have a dot and a slash, check that the dot is in the name part
852 if ( (posLastDot != wxString::npos) &&
853 (posLastSlash != wxString::npos) &&
854 (posLastDot < posLastSlash) )
855 {
856 // the dot is part of the path, not the start of the extension
857 posLastDot = wxString::npos;
858 }
859
860 // now fill in the variables provided by user
861 if ( pstrPath )
862 {
863 if ( posLastSlash == wxString::npos )
864 {
865 // no path at all
866 pstrPath->Empty();
867 }
868 else
869 {
870 // take all until the separator
871 *pstrPath = fullpath.Left(posLastSlash);
872 }
873 }
874
875 if ( pstrName )
876 {
877 // take all characters starting from the one after the last slash and
878 // up to, but excluding, the last dot
879 size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
880 size_t count;
881 if ( posLastDot == wxString::npos )
882 {
883 // take all until the end
884 count = wxString::npos;
885 }
886 else if ( posLastSlash == wxString::npos )
887 {
888 count = posLastDot;
889 }
890 else // have both dot and slash
891 {
892 count = posLastDot - posLastSlash - 1;
893 }
894
895 *pstrName = fullpath.Mid(nStart, count);
896 }
897
898 if ( pstrExt )
899 {
900 if ( posLastDot == wxString::npos )
901 {
902 // no extension
903 pstrExt->Empty();
904 }
905 else
906 {
907 // take everything after the dot
908 *pstrExt = fullpath.Mid(posLastDot + 1);
909 }
910 }
911 }
912
913 // ----------------------------------------------------------------------------
914 // time functions
915 // ----------------------------------------------------------------------------
916
917 bool wxFileName::SetTimes(const wxDateTime *dtCreate,
918 const wxDateTime *dtAccess,
919 const wxDateTime *dtMod)
920 {
921 #if defined(__UNIX_LIKE__)
922 if ( !dtAccess && !dtMod )
923 {
924 // can't modify the creation time anyhow, don't try
925 return TRUE;
926 }
927
928 // if dtAccess or dtMod is not specified, use the other one (which must be
929 // non NULL because of the test above) for both times
930 utimbuf utm;
931 utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
932 utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
933 if ( utime(GetFullPath(), &utm) == 0 )
934 {
935 return TRUE;
936 }
937 #elif defined(__WIN32__)
938 wxFileHandle fh(GetFullPath());
939 if ( fh.IsOk() )
940 {
941 FILETIME ftAccess, ftCreate, ftWrite;
942
943 if ( dtCreate )
944 ConvertWxToFileTime(&ftCreate, *dtCreate);
945 if ( dtAccess )
946 ConvertWxToFileTime(&ftAccess, *dtAccess);
947 if ( dtMod )
948 ConvertWxToFileTime(&ftWrite, *dtMod);
949
950 if ( ::SetFileTime(fh,
951 dtCreate ? &ftCreate : NULL,
952 dtAccess ? &ftAccess : NULL,
953 dtMod ? &ftWrite : NULL) )
954 {
955 return TRUE;
956 }
957 }
958 #else // other platform
959 #endif // platforms
960
961 wxLogSysError(_("Failed to modify file times for '%s'"),
962 GetFullPath().c_str());
963
964 return FALSE;
965 }
966
967 bool wxFileName::Touch()
968 {
969 #if defined(__UNIX_LIKE__)
970 // under Unix touching file is simple: just pass NULL to utime()
971 if ( utime(GetFullPath(), NULL) == 0 )
972 {
973 return TRUE;
974 }
975
976 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
977
978 return FALSE;
979 #else // other platform
980 wxDateTime dtNow = wxDateTime::Now();
981
982 return SetTimes(NULL /* don't change create time */, &dtNow, &dtNow);
983 #endif // platforms
984 }
985
986 bool wxFileName::GetTimes(wxDateTime *dtAccess,
987 wxDateTime *dtMod,
988 wxDateTime *dtChange) const
989 {
990 #if defined(__UNIX_LIKE__)
991 wxStructStat stBuf;
992 if ( wxStat(GetFullPath(), &stBuf) == 0 )
993 {
994 if ( dtAccess )
995 dtAccess->Set(stBuf.st_atime);
996 if ( dtMod )
997 dtMod->Set(stBuf.st_mtime);
998 if ( dtChange )
999 dtChange->Set(stBuf.st_ctime);
1000
1001 return TRUE;
1002 }
1003 #elif defined(__WIN32__)
1004 wxFileHandle fh(GetFullPath());
1005 if ( fh.IsOk() )
1006 {
1007 FILETIME ftAccess, ftCreate, ftWrite;
1008
1009 if ( ::GetFileTime(fh,
1010 dtMod ? &ftCreate : NULL,
1011 dtAccess ? &ftAccess : NULL,
1012 dtChange ? &ftWrite : NULL) )
1013 {
1014 if ( dtMod )
1015 ConvertFileTimeToWx(dtMod, ftCreate);
1016 if ( dtAccess )
1017 ConvertFileTimeToWx(dtAccess, ftAccess);
1018 if ( dtChange )
1019 ConvertFileTimeToWx(dtChange, ftWrite);
1020
1021 return TRUE;
1022 }
1023 }
1024 #else // other platform
1025 #endif // platforms
1026
1027 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1028 GetFullPath().c_str());
1029
1030 return FALSE;
1031 }
1032