]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filename.cpp
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling, Vadim Zeitlin
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "filename.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/filename.h"
37 #include "wx/tokenzr.h"
38 #include "wx/config.h" // for wxExpandEnvVars
41 // For GetShort/LongPathName
44 #include "wx/msw/winundef.h"
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
52 // wxFileName construction
53 // ----------------------------------------------------------------------------
55 void wxFileName::Assign( const wxFileName
&filepath
)
57 m_ext
= filepath
.GetExt();
58 m_name
= filepath
.GetName();
59 m_dirs
= filepath
.GetDirs();
62 void wxFileName::Assign( const wxString
& path
,
67 wxStringTokenizer
tn(path
, GetPathSeparators(format
),
68 wxTOKEN_RET_EMPTY_ALL
);
71 while ( tn
.HasMoreTokens() )
73 wxString token
= tn
.GetNextToken();
75 // If the path starts with a slash, we need the first
76 // dir entry to be an empty for later reassembly.
77 if (first
|| !token
.IsEmpty())
87 void wxFileName::Assign(const wxString
& fullpath
,
90 wxString path
, name
, ext
;
91 SplitPath(fullpath
, &path
, &name
, &ext
, format
);
93 Assign(path
, name
, ext
, format
);
96 void wxFileName::Assign(const wxString
& path
,
97 const wxString
& fullname
,
101 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
103 Assign(path
, name
, ext
, format
);
106 void wxFileName::Clear()
110 m_ext
= wxEmptyString
;
114 wxFileName
wxFileName::FileName(const wxString
& file
)
116 return wxFileName(file
);
120 wxFileName
wxFileName::DirName(const wxString
& dir
)
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 bool wxFileName::FileExists()
133 return wxFileName::FileExists( GetFullPath() );
136 bool wxFileName::FileExists( const wxString
&file
)
138 return ::wxFileExists( file
);
141 bool wxFileName::DirExists()
143 return wxFileName::DirExists( GetFullPath() );
146 bool wxFileName::DirExists( const wxString
&dir
)
148 return ::wxDirExists( dir
);
151 // ----------------------------------------------------------------------------
152 // CWD and HOME stuff
153 // ----------------------------------------------------------------------------
155 void wxFileName::AssignCwd()
157 AssignDir(wxFileName::GetCwd());
161 wxString
wxFileName::GetCwd()
166 bool wxFileName::SetCwd()
168 return wxFileName::SetCwd( GetFullPath() );
171 bool wxFileName::SetCwd( const wxString
&cwd
)
173 return ::wxSetWorkingDirectory( cwd
);
176 void wxFileName::AssignHomeDir()
178 AssignDir(wxFileName::GetHomeDir());
181 wxString
wxFileName::GetHomeDir()
183 return ::wxGetHomeDir();
186 void wxFileName::AssignTempFileName( const wxString
&prefix
)
189 if ( wxGetTempFileName(prefix
, fullname
) )
199 // ----------------------------------------------------------------------------
200 // directory operations
201 // ----------------------------------------------------------------------------
203 bool wxFileName::Mkdir( int perm
)
205 return wxFileName::Mkdir( GetFullPath(), perm
);
208 bool wxFileName::Mkdir( const wxString
&dir
, int perm
)
210 return ::wxMkdir( dir
, perm
);
213 bool wxFileName::Rmdir()
215 return wxFileName::Rmdir( GetFullPath() );
218 bool wxFileName::Rmdir( const wxString
&dir
)
220 return ::wxRmdir( dir
);
223 // ----------------------------------------------------------------------------
224 // path normalization
225 // ----------------------------------------------------------------------------
227 bool wxFileName::Normalize(wxPathNormalize flags
,
231 // the existing path components
232 wxArrayString dirs
= GetDirs();
234 // the path to prepend in front to make the path absolute
237 format
= GetFormat(format
);
239 // make the path absolute
240 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
245 curDir
.AssignDir(cwd
);
248 // handle ~ stuff under Unix only
249 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
251 if ( !dirs
.IsEmpty() )
253 wxString dir
= dirs
[0u];
254 if ( !dir
.empty() && dir
[0u] == _T('~') )
256 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
265 wxArrayString dirsNew
= curDir
.GetDirs();
266 size_t count
= dirs
.GetCount();
267 for ( size_t n
= 0; n
< count
; n
++ )
269 dirsNew
.Add(dirs
[n
]);
275 // now deal with ".", ".." and the rest
277 size_t count
= dirs
.GetCount();
278 for ( size_t n
= 0; n
< count
; n
++ )
280 wxString dir
= dirs
[n
];
282 if ( flags
&& wxPATH_NORM_DOTS
)
284 if ( dir
== wxT(".") )
290 if ( dir
== wxT("..") )
292 if ( m_dirs
.IsEmpty() )
294 wxLogError(_("The path '%s' contains too many \"..\"!"),
295 GetFullPath().c_str());
299 m_dirs
.Remove(m_dirs
.GetCount() - 1);
304 if ( flags
& wxPATH_NORM_ENV_VARS
)
306 dir
= wxExpandEnvVars(dir
);
309 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
317 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
319 // VZ: expand env vars here too?
325 #if defined(__WXMSW__) && defined(__WIN32__)
326 if (flags
& wxPATH_NORM_LONG
)
328 Assign(GetLongPath());
335 // ----------------------------------------------------------------------------
336 // filename kind tests
337 // ----------------------------------------------------------------------------
339 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
341 wxFileName fn1
= *this,
344 // get cwd only once - small time saving
345 wxString cwd
= wxGetCwd();
346 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
347 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
349 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
352 // TODO: compare inodes for Unix, this works even when filenames are
353 // different but files are the same (symlinks) (VZ)
359 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
361 // only DOS filenames are case-sensitive
362 return GetFormat(format
) != wxPATH_DOS
;
365 bool wxFileName::IsRelative( wxPathFormat format
)
367 return !IsAbsolute(format
);
370 bool wxFileName::IsAbsolute( wxPathFormat format
)
372 wxChar ch
= m_dirs
.IsEmpty() ? _T('\0') : m_dirs
[0u][0u];
374 // Hack to cope with e.g. c:\thing - need something better
375 wxChar driveSep
= _T('\0');
376 if (!m_dirs
.IsEmpty() && m_dirs
[0].Length() > 1)
377 driveSep
= m_dirs
[0u][1u];
379 // the path is absolute if it starts with a path separator or, only for
380 // Unix filenames, with "~" or "~user"
381 return IsPathSeparator(ch
, format
) ||
382 driveSep
== _T(':') ||
383 (GetFormat(format
) == wxPATH_UNIX
&& ch
== _T('~') );
387 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
390 switch ( GetFormat(format
) )
393 // accept both as native APIs do
394 seps
<< wxFILE_SEP_PATH_UNIX
<< wxFILE_SEP_PATH_DOS
;
398 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
402 seps
= wxFILE_SEP_PATH_UNIX
;
406 seps
= wxFILE_SEP_PATH_MAC
;
414 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
416 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
419 bool wxFileName::IsWild( wxPathFormat format
)
421 // FIXME: this is probably false for Mac and this is surely wrong for most
422 // of Unix shells (think about "[...]")
423 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
426 // ----------------------------------------------------------------------------
427 // path components manipulation
428 // ----------------------------------------------------------------------------
430 void wxFileName::AppendDir( const wxString
&dir
)
435 void wxFileName::PrependDir( const wxString
&dir
)
437 m_dirs
.Insert( dir
, 0 );
440 void wxFileName::InsertDir( int before
, const wxString
&dir
)
442 m_dirs
.Insert( dir
, before
);
445 void wxFileName::RemoveDir( int pos
)
447 m_dirs
.Remove( (size_t)pos
);
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
454 void wxFileName::SetFullName(const wxString
& fullname
)
456 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
459 wxString
wxFileName::GetFullName() const
461 wxString fullname
= m_name
;
462 if ( !m_ext
.empty() )
464 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
470 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
472 format
= GetFormat( format
);
475 size_t count
= m_dirs
.GetCount();
476 for ( size_t i
= 0; i
< count
; i
++ )
479 if ( add_separator
|| (i
< count
) )
480 ret
+= wxFILE_SEP_PATH
;
486 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
488 return GetPathWithSep() + GetFullName();
491 // Return the short form of the path (returns identity on non-Windows platforms)
492 wxString
wxFileName::GetShortPath() const
494 #if defined(__WXMSW__) && defined(__WIN32__)
495 wxString
path(GetFullPath());
497 wxChar outBuf
[MAX_PATH
];
499 // TODO: can't work out how to determine if the function failed
500 // (positive value if either it succeeded or the buffer was too small)
502 int bufSz
= ::GetShortPathName((const wxChar
*) path
, outBuf
, MAX_PATH
*sizeof(wxChar
));
506 return wxEmptyString
;
510 return wxString(outBuf
);
513 return GetFullPath();
517 // Return the long form of the path (returns identity on non-Windows platforms)
518 wxString
wxFileName::GetLongPath() const
520 #if defined(__WXMSW__) && defined(__WIN32__)
521 wxString
path(GetFullPath());
523 wxChar outBuf
[MAX_PATH
];
525 // TODO: can't work out how to determine if the function failed
526 // (positive value if either it succeeded or the buffer was too small)
528 int bufSz
= ::GetLongPathName((const wxChar
*) path
, outBuf
, MAX_PATH
*sizeof(wxChar
));
532 return wxEmptyString
;
536 return wxString(outBuf
);
539 return GetFullPath();
543 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
545 if (format
== wxPATH_NATIVE
)
547 #if defined(__WXMSW__) || defined(__WXPM__)
549 #elif defined(__WXMAC__)
550 format
= wxPATH_UNIX
; // that's the way the rest of wx' code works right now
552 format
= wxPATH_UNIX
;
558 // ----------------------------------------------------------------------------
559 // path splitting function
560 // ----------------------------------------------------------------------------
562 void wxFileName::SplitPath(const wxString
& fullpath
,
568 format
= GetFormat(format
);
570 // find the positions of the last dot and last path separator in the path
571 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
572 size_t posLastSlash
= fullpath
.find_last_of(GetPathSeparators(format
));
574 if ( (posLastDot
!= wxString::npos
) && (format
== wxPATH_UNIX
) )
576 if ( (posLastDot
== 0) ||
577 (fullpath
[posLastDot
- 1] == wxFILE_SEP_PATH_UNIX
) )
579 // under Unix, dot may be (and commonly is) the first character of
580 // the filename, don't treat the entire filename as extension in
582 posLastDot
= wxString::npos
;
586 // if we do have a dot and a slash, check that the dot is in the name part
587 if ( (posLastDot
!= wxString::npos
) &&
588 (posLastSlash
!= wxString::npos
) &&
589 (posLastDot
< posLastSlash
) )
591 // the dot is part of the path, not the start of the extension
592 posLastDot
= wxString::npos
;
595 // now fill in the variables provided by user
598 if ( posLastSlash
== wxString::npos
)
605 // take all until the separator
606 *pstrPath
= fullpath
.Left(posLastSlash
);
612 // take all characters starting from the one after the last slash and
613 // up to, but excluding, the last dot
614 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
616 if ( posLastDot
== wxString::npos
)
618 // take all until the end
619 count
= wxString::npos
;
621 else if ( posLastSlash
== wxString::npos
)
625 else // have both dot and slash
627 count
= posLastDot
- posLastSlash
- 1;
630 *pstrName
= fullpath
.Mid(nStart
, count
);
635 if ( posLastDot
== wxString::npos
)
642 // take everything after the dot
643 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);