]> git.saurik.com Git - wxWidgets.git/blame - src/common/filename.cpp
fixed wxTaskBarIcon compilation
[wxWidgets.git] / src / common / filename.cpp
CommitLineData
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
097ead30
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
df5ddbca
RR
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"
844f90fb 38#include "wx/config.h" // for wxExpandEnvVars
a35b27b1 39#include "wx/utils.h"
5d978d07
JS
40
41#if wxUSE_DYNLIB_CLASS
951cd180 42 #include "wx/dynlib.h"
5d978d07 43#endif
df5ddbca 44
9e9b65c1
JS
45// For GetShort/LongPathName
46#ifdef __WIN32__
951cd180
VZ
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>
4f2570aa
VZ
56 #include <sys/stat.h>
57 #include <unistd.h>
9e9b65c1
JS
58#endif
59
951cd180
VZ
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
68class wxFileHandle
69{
70public:
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
108private:
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
d56e2b97
VZ
123// the number of milliseconds between the Unix Epoch (January 1, 1970) and the
124// FILETIME reference point (January 1, 1601)
125static const wxLongLong FILETIME_EPOCH_OFFSET = wxLongLong(0xa97, 0x30b66800);
126
127static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft)
951cd180 128{
d56e2b97
VZ
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);
951cd180
VZ
138}
139
d56e2b97 140static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt)
951cd180 141{
d56e2b97
VZ
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();
951cd180
VZ
149}
150
151#endif // __WIN32__
152
097ead30
VZ
153// ============================================================================
154// implementation
155// ============================================================================
156
157// ----------------------------------------------------------------------------
844f90fb 158// wxFileName construction
097ead30 159// ----------------------------------------------------------------------------
df5ddbca 160
a35b27b1
RR
161void wxFileName::Assign( const wxFileName &filepath )
162{
a35b27b1
RR
163 m_ext = filepath.GetExt();
164 m_name = filepath.GetName();
844f90fb 165 m_dirs = filepath.GetDirs();
df5ddbca
RR
166}
167
844f90fb
VZ
168void wxFileName::Assign( const wxString& path,
169 const wxString& name,
170 const wxString& ext,
171 wxPathFormat format )
df5ddbca 172{
844f90fb
VZ
173 wxStringTokenizer tn(path, GetPathSeparators(format),
174 wxTOKEN_RET_EMPTY_ALL);
25db1b74 175 int i = 0;
844f90fb
VZ
176 m_dirs.Clear();
177 while ( tn.HasMoreTokens() )
df5ddbca 178 {
844f90fb
VZ
179 wxString token = tn.GetNextToken();
180
25db1b74
JS
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())
df5ddbca 184 m_dirs.Add( token );
844f90fb 185
25db1b74 186 i ++;
df5ddbca 187 }
844f90fb
VZ
188
189 m_ext = ext;
190 m_name = name;
191}
192
193void wxFileName::Assign(const wxString& fullpath,
194 wxPathFormat format)
195{
196 wxString path, name, ext;
9e8d8607 197 SplitPath(fullpath, &path, &name, &ext, format);
844f90fb
VZ
198
199 Assign(path, name, ext, format);
200}
201
202void wxFileName::Assign(const wxString& path,
203 const wxString& fullname,
204 wxPathFormat format)
205{
206 wxString name, ext;
9e8d8607 207 SplitPath(fullname, NULL /* no path */, &name, &ext, format);
844f90fb
VZ
208
209 Assign(path, name, ext, format);
210}
211
212void wxFileName::Clear()
213{
214 m_dirs.Clear();
215 m_name =
216 m_ext = wxEmptyString;
217}
218
219/* static */
220wxFileName wxFileName::FileName(const wxString& file)
221{
222 return wxFileName(file);
223}
224
225/* static */
226wxFileName wxFileName::DirName(const wxString& dir)
227{
228 wxFileName fn;
229 fn.AssignDir(dir);
230 return fn;
df5ddbca
RR
231}
232
844f90fb
VZ
233// ----------------------------------------------------------------------------
234// existence tests
235// ----------------------------------------------------------------------------
236
df5ddbca
RR
237bool wxFileName::FileExists()
238{
a35b27b1
RR
239 return wxFileName::FileExists( GetFullPath() );
240}
241
242bool wxFileName::FileExists( const wxString &file )
243{
244 return ::wxFileExists( file );
df5ddbca
RR
245}
246
247bool wxFileName::DirExists()
248{
a35b27b1
RR
249 return wxFileName::DirExists( GetFullPath() );
250}
251
252bool wxFileName::DirExists( const wxString &dir )
253{
254 return ::wxDirExists( dir );
df5ddbca
RR
255}
256
844f90fb
VZ
257// ----------------------------------------------------------------------------
258// CWD and HOME stuff
259// ----------------------------------------------------------------------------
260
df5ddbca
RR
261void wxFileName::AssignCwd()
262{
844f90fb 263 AssignDir(wxFileName::GetCwd());
a35b27b1
RR
264}
265
844f90fb 266/* static */
a35b27b1
RR
267wxString wxFileName::GetCwd()
268{
269 return ::wxGetCwd();
270}
271
272bool wxFileName::SetCwd()
273{
274 return wxFileName::SetCwd( GetFullPath() );
df5ddbca
RR
275}
276
a35b27b1 277bool wxFileName::SetCwd( const wxString &cwd )
df5ddbca 278{
a35b27b1 279 return ::wxSetWorkingDirectory( cwd );
df5ddbca
RR
280}
281
a35b27b1
RR
282void wxFileName::AssignHomeDir()
283{
844f90fb 284 AssignDir(wxFileName::GetHomeDir());
a35b27b1 285}
844f90fb 286
a35b27b1
RR
287wxString wxFileName::GetHomeDir()
288{
289 return ::wxGetHomeDir();
290}
844f90fb 291
df5ddbca
RR
292void wxFileName::AssignTempFileName( const wxString &prefix )
293{
844f90fb
VZ
294 wxString fullname;
295 if ( wxGetTempFileName(prefix, fullname) )
296 {
297 Assign(fullname);
298 }
299 else // error
300 {
301 Clear();
302 }
df5ddbca
RR
303}
304
844f90fb
VZ
305// ----------------------------------------------------------------------------
306// directory operations
307// ----------------------------------------------------------------------------
308
f0ce3409 309bool wxFileName::Mkdir( int perm, bool full )
a35b27b1 310{
f0ce3409 311 return wxFileName::Mkdir( GetFullPath(), perm, full );
a35b27b1
RR
312}
313
f0ce3409 314bool wxFileName::Mkdir( const wxString &dir, int perm, bool full )
df5ddbca 315{
f0ce3409
JS
316 if (full)
317 {
318 wxFileName filename(dir);
319 wxArrayString dirs = filename.GetDirs();
77fe02a8 320 dirs.Add(filename.GetName());
f0ce3409
JS
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 );
df5ddbca
RR
350}
351
a35b27b1 352bool wxFileName::Rmdir()
df5ddbca 353{
a35b27b1 354 return wxFileName::Rmdir( GetFullPath() );
df5ddbca
RR
355}
356
a35b27b1 357bool wxFileName::Rmdir( const wxString &dir )
df5ddbca 358{
a35b27b1 359 return ::wxRmdir( dir );
df5ddbca
RR
360}
361
844f90fb
VZ
362// ----------------------------------------------------------------------------
363// path normalization
364// ----------------------------------------------------------------------------
365
366bool wxFileName::Normalize(wxPathNormalize flags,
367 const wxString& cwd,
368 wxPathFormat format)
a35b27b1 369{
844f90fb
VZ
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() )
a35b27b1 380 {
844f90fb
VZ
381 if ( cwd.empty() )
382 curDir.AssignCwd();
a35b27b1 383 else
844f90fb 384 curDir.AssignDir(cwd);
a35b27b1 385 }
844f90fb
VZ
386
387 // handle ~ stuff under Unix only
388 if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
a35b27b1 389 {
844f90fb
VZ
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
da2fd5ac 397 dirs.RemoveAt(0u);
844f90fb
VZ
398 }
399 }
a35b27b1 400 }
844f90fb
VZ
401
402 if ( curDir.IsOk() )
a35b27b1 403 {
844f90fb
VZ
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;
a35b27b1 412 }
844f90fb
VZ
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++ )
a35b27b1 418 {
844f90fb
VZ
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 )
a35b27b1 444 {
844f90fb 445 dir = wxExpandEnvVars(dir);
a35b27b1 446 }
844f90fb
VZ
447
448 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
449 {
450 dir.MakeLower();
451 }
452
453 m_dirs.Add(dir);
a35b27b1 454 }
844f90fb
VZ
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
9e9b65c1
JS
464#if defined(__WXMSW__) && defined(__WIN32__)
465 if (flags & wxPATH_NORM_LONG)
466 {
467 Assign(GetLongPath());
468 }
469#endif
470
a35b27b1
RR
471 return TRUE;
472}
473
844f90fb
VZ
474// ----------------------------------------------------------------------------
475// filename kind tests
476// ----------------------------------------------------------------------------
477
478bool wxFileName::SameAs( const wxFileName &filepath, wxPathFormat format)
df5ddbca 479{
844f90fb
VZ
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;
df5ddbca
RR
495}
496
844f90fb 497/* static */
df5ddbca
RR
498bool wxFileName::IsCaseSensitive( wxPathFormat format )
499{
844f90fb
VZ
500 // only DOS filenames are case-sensitive
501 return GetFormat(format) != wxPATH_DOS;
df5ddbca
RR
502}
503
504bool wxFileName::IsRelative( wxPathFormat format )
505{
844f90fb
VZ
506 return !IsAbsolute(format);
507}
508
509bool wxFileName::IsAbsolute( wxPathFormat format )
510{
511 wxChar ch = m_dirs.IsEmpty() ? _T('\0') : m_dirs[0u][0u];
512
9e9b65c1
JS
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
844f90fb
VZ
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) ||
9e9b65c1 521 driveSep == _T(':') ||
844f90fb
VZ
522 (GetFormat(format) == wxPATH_UNIX && ch == _T('~') );
523}
524
525/* static */
526wxString wxFileName::GetPathSeparators(wxPathFormat format)
527{
528 wxString seps;
529 switch ( GetFormat(format) )
df5ddbca 530 {
844f90fb
VZ
531 case wxPATH_DOS:
532 // accept both as native APIs do
9e8d8607 533 seps << wxFILE_SEP_PATH_UNIX << wxFILE_SEP_PATH_DOS;
844f90fb
VZ
534 break;
535
536 default:
537 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
538 // fall through
539
540 case wxPATH_UNIX:
9e8d8607 541 seps = wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
542 break;
543
544 case wxPATH_MAC:
9e8d8607 545 seps = wxFILE_SEP_PATH_MAC;
844f90fb 546 break;
df5ddbca
RR
547 }
548
844f90fb 549 return seps;
df5ddbca
RR
550}
551
844f90fb
VZ
552/* static */
553bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
df5ddbca 554{
844f90fb 555 return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
df5ddbca
RR
556}
557
558bool wxFileName::IsWild( wxPathFormat format )
559{
844f90fb
VZ
560 // FIXME: this is probably false for Mac and this is surely wrong for most
561 // of Unix shells (think about "[...]")
33ac7e6f 562 (void)format;
844f90fb 563 return m_name.find_first_of(_T("*?")) != wxString::npos;
df5ddbca
RR
564}
565
844f90fb
VZ
566// ----------------------------------------------------------------------------
567// path components manipulation
568// ----------------------------------------------------------------------------
569
df5ddbca
RR
570void wxFileName::AppendDir( const wxString &dir )
571{
572 m_dirs.Add( dir );
573}
574
575void wxFileName::PrependDir( const wxString &dir )
576{
577 m_dirs.Insert( dir, 0 );
578}
579
580void wxFileName::InsertDir( int before, const wxString &dir )
581{
582 m_dirs.Insert( dir, before );
583}
584
585void wxFileName::RemoveDir( int pos )
586{
587 m_dirs.Remove( (size_t)pos );
588}
589
844f90fb
VZ
590// ----------------------------------------------------------------------------
591// accessors
592// ----------------------------------------------------------------------------
593
7124df9b
VZ
594void wxFileName::SetFullName(const wxString& fullname)
595{
596 SplitPath(fullname, NULL /* no path */, &m_name, &m_ext);
597}
598
844f90fb 599wxString wxFileName::GetFullName() const
a35b27b1 600{
844f90fb
VZ
601 wxString fullname = m_name;
602 if ( !m_ext.empty() )
a35b27b1 603 {
9e8d8607 604 fullname << wxFILE_SEP_EXT << m_ext;
a35b27b1 605 }
a35b27b1 606
844f90fb 607 return fullname;
a35b27b1
RR
608}
609
610wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const
df5ddbca
RR
611{
612 format = GetFormat( format );
844f90fb 613
df5ddbca 614 wxString ret;
844f90fb
VZ
615 size_t count = m_dirs.GetCount();
616 for ( size_t i = 0; i < count; i++ )
df5ddbca 617 {
844f90fb
VZ
618 ret += m_dirs[i];
619 if ( add_separator || (i < count) )
620 ret += wxFILE_SEP_PATH;
df5ddbca 621 }
844f90fb 622
df5ddbca
RR
623 return ret;
624}
625
626wxString wxFileName::GetFullPath( wxPathFormat format ) const
627{
dcf0fce4
RR
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;
df5ddbca
RR
666}
667
9e9b65c1
JS
668// Return the short form of the path (returns identity on non-Windows platforms)
669wxString wxFileName::GetShortPath() const
670{
8cb172b4 671#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
9e9b65c1 672 wxString path(GetFullPath());
75ef5722
JS
673 wxString pathOut;
674 DWORD sz = ::GetShortPathName(path, NULL, 0);
675 bool ok = sz != 0;
676 if ( ok )
9e9b65c1 677 {
75ef5722
JS
678 ok = ::GetShortPathName
679 (
680 path,
681 pathOut.GetWriteBuf(sz),
682 sz
683 ) != 0;
684 pathOut.UngetWriteBuf();
9e9b65c1 685 }
75ef5722
JS
686 if (ok)
687 return pathOut;
5716a1ab
VZ
688
689 return path;
9e9b65c1
JS
690#else
691 return GetFullPath();
692#endif
693}
694
695// Return the long form of the path (returns identity on non-Windows platforms)
696wxString wxFileName::GetLongPath() const
697{
8cb172b4 698#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
9e9b65c1 699 wxString path(GetFullPath());
75ef5722 700 wxString pathOut;
05e7001c
JS
701 bool success = FALSE;
702
5716a1ab
VZ
703 // VZ: this code was disabled, why?
704#if 0 // wxUSE_DYNLIB_CLASS
05e7001c
JS
705 typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
706
707 static bool s_triedToLoad = FALSE;
05e7001c
JS
708
709 if ( !s_triedToLoad )
9e9b65c1 710 {
05e7001c 711 s_triedToLoad = TRUE;
05e7001c 712 wxDllType dllKernel = wxDllLoader::LoadLibrary(_T("kernel32"));
33ac7e6f 713 if ( dllKernel )
05e7001c
JS
714 {
715 // may succeed or fail depending on the Windows version
33ac7e6f 716 static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
5d978d07
JS
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
05e7001c
JS
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 }
9e9b65c1 749 }
05e7001c 750 if (success)
75ef5722 751 return pathOut;
5716a1ab 752#endif // wxUSE_DYNLIB_CLASS
05e7001c
JS
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
77fe02a8 763 wxArrayString dirs = GetDirs();
bcbe86e5 764 dirs.Add(GetFullName());
77fe02a8
JS
765
766 size_t count = dirs.GetCount();
05e7001c
JS
767 size_t i;
768 wxString tmpPath;
5d978d07 769
05e7001c
JS
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
77fe02a8 774 tmpPath = pathOut + dirs[i];
05e7001c
JS
775
776 if (tmpPath.Last() == wxT(':'))
5d978d07
JS
777 {
778 // Can't pass a drive and root dir to FindFirstFile,
779 // so continue to next dir
05e7001c 780 tmpPath += wxFILE_SEP_PATH;
5d978d07
JS
781 pathOut = tmpPath;
782 continue;
783 }
05e7001c
JS
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;
5d978d07 794 if ( (i < (count-1)) )
05e7001c
JS
795 pathOut += wxFILE_SEP_PATH;
796
797 ::FindClose(hFind);
798 }
799 }
800 }
5716a1ab 801
05e7001c 802 return pathOut;
9e9b65c1
JS
803#else
804 return GetFullPath();
805#endif
806}
807
df5ddbca
RR
808wxPathFormat wxFileName::GetFormat( wxPathFormat format )
809{
810 if (format == wxPATH_NATIVE)
811 {
812#if defined(__WXMSW__) || defined(__WXPM__)
813 format = wxPATH_DOS;
4d293f8e 814#elif defined(__WXMAC__) && !defined(__DARWIN__)
f72124db 815 format = wxPATH_MAC;
844f90fb 816#else
df5ddbca
RR
817 format = wxPATH_UNIX;
818#endif
819 }
820 return format;
821}
a35b27b1 822
9e8d8607
VZ
823// ----------------------------------------------------------------------------
824// path splitting function
825// ----------------------------------------------------------------------------
826
827void 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
8e7dda21
VZ
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) )
9e8d8607
VZ
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 {
42b1f941
VZ
877 // take all characters starting from the one after the last slash and
878 // up to, but excluding, the last dot
9e8d8607 879 size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
8e7dda21
VZ
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 }
9e8d8607
VZ
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}
951cd180
VZ
912
913// ----------------------------------------------------------------------------
914// time functions
915// ----------------------------------------------------------------------------
916
917bool wxFileName::SetTimes(const wxDateTime *dtCreate,
918 const wxDateTime *dtAccess,
919 const wxDateTime *dtMod)
920{
921#if defined(__UNIX_LIKE__)
246c704f
VZ
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
951cd180 930 utimbuf utm;
246c704f
VZ
931 utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
932 utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
951cd180
VZ
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
967bool 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
986bool 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