From f363e05c6d7d29ac152c86b526e25b983d05eea5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 19 Jul 2003 17:06:29 +0000 Subject: [PATCH] added GetForbiddenChars() and TRUE -> true (patch 757777) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@22112 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 3 +- docs/latex/wx/filename.tex | 47 +++++++++------- include/wx/filename.h | 18 +++--- src/common/filename.cpp | 110 ++++++++++++++++++++++++------------- 4 files changed, 113 insertions(+), 65 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 47f7924ecf..00e3ff2a44 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -44,6 +44,7 @@ OTHER CHANGES All: - added wxDateSpan::operator==() and !=() (Lukasz Michalski) +- added wxFileName::GetForbiddenChars() (Dimitri Schoolwerth) - use true/false throughout the library instead of TRUE/FALSE - wxStopWatch::Start() resumes the stop watch if paused, as per the docs - added wxDirTraverser::OnOpenError() to customize the error handling @@ -438,7 +439,7 @@ wxBase: - fixes to the command line parsing error and usage messages - modified wxFileName::CreateTempFileName() to open the file atomically (if possible) and, especially, not to leak the file descriptors under Unix -- memory leak in wxHTTP fixed (Dimitri) +- memory leak in wxHTTP fixed (Dimitri Schoolwerth) - fixes to AM_PATH_WXCONFIG autoconf macro - added wxHashMap class that replaces type-unsafe wxHashTable and is modelled after (non standard) STL hash_map diff --git a/docs/latex/wx/filename.tex b/docs/latex/wx/filename.tex index ef77c01875..4db77cb30e 100644 --- a/docs/latex/wx/filename.tex +++ b/docs/latex/wx/filename.tex @@ -38,13 +38,14 @@ enum wxPathFormat { wxPATH_NATIVE = 0, // the path format for the current platform wxPATH_UNIX, + wxPATH_BEOS = wxPATH_UNIX, wxPATH_MAC, wxPATH_DOS, + wxPATH_WIN = wxPATH_DOS, + wxPATH_OS2 = wxPATH_DOS, wxPATH_VMS, - wxPATH_BEOS = wxPATH_UNIX, - wxPATH_WIN = wxPATH_DOS, - wxPATH_OS2 = wxPATH_DOS + wxPATH_MAX // Not a valid value for specifying path format } \end{verbatim} @@ -189,7 +190,7 @@ Creates the file name from various combinations of data. \membersection{wxFileName::AssignCwd}\label{wxfilenameassigncwd} -\func{void}{AssignCwd}{\param{const wxString\& }{volume = ""}} +\func{static void}{AssignCwd}{\param{const wxString\& }{volume = wxEmptyString}} Makes this object refer to the current working directory on the specified volume (or current volume if {\it volume} is empty). @@ -261,7 +262,7 @@ The full temporary file name or an empty string on error. \func{static bool}{DirExists}{\param{const wxString\& }{dir}} -Returns true if the directory with this name exists. +Returns {\tt true} if the directory with this name exists. \membersection{wxFileName::DirName}\label{wxfilenamedirname} @@ -277,7 +278,7 @@ The {\it dir} parameter may have trailing path separator or not. \func{static bool}{FileExists}{\param{const wxString\& }{file}} -Returns true if the file with this name exists. +Returns {\tt true} if the file with this name exists. \wxheading{See also} @@ -294,7 +295,7 @@ function exists mainly for symmetry with \helpref{DirName}{wxfilenamedirname}. \membersection{wxFileName::GetCwd}\label{wxfilenamegetcwd} -\func{wxString}{GetCwd}{\param{const wxString\& }{volume = ""}} +\func{static wxString}{GetCwd}{\param{const wxString\& }{volume = ""}} Retrieves the value of the current working directory on the specified volume. If the volume is empty, the programs current working directory is returned for the @@ -327,9 +328,15 @@ Returns the directories in string array form. Returns the file name extension. +\membersection{wxFileName::GetForbiddenChars}\label{wxfilenamegetforbiddenchars} + +\func{static wxString}{GetForbiddenChars}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} + +Returns the characters that can't be used in filenames and directory names for the specified format. + \membersection{wxFileName::GetFormat}\label{wxfilenamegetformat} -\func{wxPathFormat}{GetFormat}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} +\func{static wxPathFormat}{GetFormat}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} Returns the canonical path format for this platform. @@ -347,7 +354,7 @@ Returns the full path with name and extension. \membersection{wxFileName::GetHomeDir}\label{wxfilenamegethomedir} -\func{wxString}{GetHomeDir}{\void} +\func{static wxString}{GetHomeDir}{\void} Returns the home directory. @@ -387,7 +394,7 @@ the path.} \membersection{wxFileName::GetPathSeparator}\label{wxfilenamegetpathseparator} -\func{wxChar}{GetPathSeparator}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} +\func{static wxChar}{GetPathSeparator}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} Returns the usually used path separator for this format. For all formats but {\tt wxPATH\_DOS} there is only one path separator anyhow, but for DOS there @@ -400,7 +407,7 @@ method. \membersection{wxFileName::GetPathSeparators}\label{wxfilenamegetpathseparators} -\func{wxString}{GetPathSeparators}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} +\func{static wxString}{GetPathSeparators}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} Returns the string containing all the path separators for this format. For all formats but {\tt wxPATH\_DOS} this string contains only one character but for @@ -446,7 +453,7 @@ example, Unix). \membersection{wxFileName::GetVolumeSeparator}\label{wxfilenamegetvolumeseparator} -\func{wxString}{GetVolumeSeparator}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} +\func{static wxString}{GetVolumeSeparator}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} Returns the string separating the volume from the path for this format. @@ -454,19 +461,19 @@ Returns the string separating the volume from the path for this format. \constfunc{bool}{HasExt}{\void} -Returns true if an extension is present. +Returns {\tt true} if an extension is present. \membersection{wxFileName::HasName}\label{wxfilenamehasname} \constfunc{bool}{HasName}{\void} -Returns true if a name is present. +Returns {\tt true} if a name is present. \membersection{wxFileName::HasVolume}\label{wxfilenamehasvolume} \constfunc{bool}{HasVolume}{\void} -Returns true if a volume specifier is present. +Returns {\tt true} if a volume specifier is present. \membersection{wxFileName::InsertDir}\label{wxfilenameinsertdir} @@ -478,13 +485,13 @@ Inserts a directory before the zero-based position in the directory list. \func{bool}{IsAbsolute}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} -Returns true if this filename is absolute. +Returns {\tt true} if this filename is absolute. \membersection{wxFileName::IsCaseSensitive}\label{wxfilenameiscasesensitive} -\func{bool}{IsCaseSensitive}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} +\func{static bool}{IsCaseSensitive}{\param{wxPathFormat }{format = wxPATH\_NATIVE}} -Returns true if the file names of this type are case-sensitive. +Returns {\tt true} if the file names of this type are case-sensitive. \membersection{wxFileName::IsOk}\label{wxfilenameisok} @@ -497,7 +504,7 @@ invalid state (the former only do it on failure). \membersection{wxFileName::IsPathSeparator}\label{wxfilenameispathseparator} -\func{bool}{IsPathSeparator}{\param{wxChar }{ch}, \param{wxPathFormat }{format = wxPATH\_NATIVE}} +\func{static bool}{IsPathSeparator}{\param{wxChar }{ch}, \param{wxPathFormat }{format = wxPATH\_NATIVE}} Returns {\tt true} if the char is a path separator for this format. @@ -532,7 +539,7 @@ Make the file name absolute. This is a shortcut for \membersection{wxFileName::MakeRelativeTo}\label{wxfilenamemakerelativeto} -\func{bool}{MakeRelativeTo}{\param{const wxString\& }{pathBase = ""}, \param{wxPathFormat }{format = wxPATH\_NATIVE}} +\func{bool}{MakeRelativeTo}{\param{const wxString\& }{pathBase = wxEmptyString}, \param{wxPathFormat }{format = wxPATH\_NATIVE}} This function tries to put this file name in a form relative to {\it pathBase}. In other words, it returns the file name which should be used to access this diff --git a/include/wx/filename.h b/include/wx/filename.h index 0b864b6ab9..401162c336 100644 --- a/include/wx/filename.h +++ b/include/wx/filename.h @@ -52,13 +52,14 @@ enum wxPathFormat { wxPATH_NATIVE = 0, // the path format for the current platform wxPATH_UNIX, + wxPATH_BEOS = wxPATH_UNIX, wxPATH_MAC, wxPATH_DOS, + wxPATH_WIN = wxPATH_DOS, + wxPATH_OS2 = wxPATH_DOS, wxPATH_VMS, - wxPATH_BEOS = wxPATH_UNIX, - wxPATH_WIN = wxPATH_DOS, - wxPATH_OS2 = wxPATH_DOS + wxPATH_MAX // Not a valid value for specifying path format }; // the kind of normalization to do with the file name: these values can be @@ -246,7 +247,6 @@ public: wxFile *fileTemp = NULL); // directory creation and removal. - // if full is TRUE, will try to make each directory in the path. bool Mkdir( int perm = 0777, int flags = 0); static bool Mkdir( const wxString &dir, int perm = 0777, int flags = 0 ); @@ -269,7 +269,7 @@ public: // // pass an empty string to get a path relative to the working directory // - // returns TRUE if the file name was modified, FALSE if we failed to do + // returns true if the file name was modified, false if we failed to do // anything with it (happens when the file is on a different volume, // for example) bool MakeRelativeTo(const wxString& pathBase = wxEmptyString, @@ -311,6 +311,10 @@ public: bool IsRelative(wxPathFormat format = wxPATH_NATIVE) const { return !IsAbsolute(format); } + // Returns the characters that aren't allowed in filenames + // on the specified platform. + static wxString GetForbiddenChars(wxPathFormat format = wxPATH_NATIVE); + // Information about path format // get the string separating the volume from the path for this format, @@ -419,9 +423,9 @@ private: // that our path is '/', i.e. the root directory // // we use m_relative to distinguish between these two cases, it will be - // TRUE in the former and FALSE in the latter + // true in the former and false in the latter // - // NB: the path is not absolute just because m_relative is FALSE, it still + // NB: the path is not absolute just because m_relative is false, it still // needs the drive (i.e. volume) in some formats (Windows) bool m_relative; }; diff --git a/src/common/filename.cpp b/src/common/filename.cpp index a5a5f853a8..125aae19f6 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -188,7 +188,7 @@ public: } } - // return TRUE only if the file could be opened successfully + // return true only if the file could be opened successfully bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; } // get the handle @@ -343,9 +343,13 @@ void wxFileName::SetPath( const wxString &path, wxPathFormat format ) case wxPATH_VMS: // TODO: what is the relative path format here? - m_relative = FALSE; + m_relative = false; break; + default: + wxFAIL_MSG( _T("Unknown path format") ); + // !! Fall through !! + case wxPATH_UNIX: // the paths of the form "~" or "~username" are absolute m_relative = leadingChar != wxT('/') && leadingChar != _T('~'); @@ -355,9 +359,6 @@ void wxFileName::SetPath( const wxString &path, wxPathFormat format ) m_relative = !IsPathSeparator(leadingChar, my_format); break; - default: - wxFAIL_MSG( wxT("error") ); - break; } // 2) Break up the path into its members. If the original path @@ -386,7 +387,7 @@ void wxFileName::SetPath( const wxString &path, wxPathFormat format ) } else // no path at all { - m_relative = TRUE; + m_relative = true; } } @@ -450,7 +451,7 @@ void wxFileName::Clear() m_ext = wxEmptyString; // we don't have any absolute path for now - m_relative = TRUE; + m_relative = true; } /* static */ @@ -816,12 +817,12 @@ bool wxFileName::Mkdir( const wxString& dir, int perm, int flags ) if (!wxMkdir(currPath, perm)) { // no need to try creating further directories - return FALSE; + return false; } } } - return TRUE; + return true; } @@ -930,7 +931,7 @@ bool wxFileName::Normalize(int flags, { wxLogError(_("The path '%s' contains too many \"..\"!"), GetFullPath().c_str()); - return FALSE; + return false; } m_dirs.RemoveAt(m_dirs.GetCount() - 1); @@ -962,7 +963,7 @@ bool wxFileName::Normalize(int flags, // we do have the path now // // NB: need to do this before (maybe) calling Assign() below - m_relative = FALSE; + m_relative = false; #if defined(__WIN32__) if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) ) @@ -971,7 +972,7 @@ bool wxFileName::Normalize(int flags, } #endif // Win32 - return TRUE; + return true; } // ---------------------------------------------------------------------------- @@ -983,17 +984,17 @@ bool wxFileName::IsAbsolute(wxPathFormat format) const // if our path doesn't start with a path separator, it's not an absolute // path if ( m_relative ) - return FALSE; + return false; if ( !GetVolumeSeparator(format).empty() ) { // this format has volumes and an absolute path must have one, it's not // enough to have the full path to bean absolute file under Windows if ( GetVolume().empty() ) - return FALSE; + return false; } - return TRUE; + return true; } bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) @@ -1011,7 +1012,7 @@ bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) ) { // nothing done - return FALSE; + return false; } // same drive, so we don't need our volume @@ -1043,10 +1044,10 @@ bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) } } - m_relative = TRUE; + m_relative = true; // we were modified - return TRUE; + return true; } // ---------------------------------------------------------------------------- @@ -1064,12 +1065,12 @@ bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const fn2.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); if ( fn1.GetFullPath() == fn2.GetFullPath() ) - return TRUE; + return true; // TODO: compare inodes for Unix, this works even when filenames are // different but files are the same (symlinks) (VZ) - return FALSE; + return false; } /* static */ @@ -1079,6 +1080,41 @@ bool wxFileName::IsCaseSensitive( wxPathFormat format ) return GetFormat(format) == wxPATH_UNIX; } +/* static */ +wxString wxFileName::GetForbiddenChars(wxPathFormat format) +{ + // Inits to forbidden characters that are common to (almost) all platforms. + wxString strForbiddenChars = wxT("*?"); + + // If asserts, wxPathFormat has been changed. In case of a new path format + // addition, the following code might have to be updated. + wxCOMPILE_TIME_ASSERT(wxPATH_MAX == 5, wxPathFormatChanged); + switch ( GetFormat(format) ) + { + default : + wxFAIL_MSG( wxT("Unknown path format") ); + // !! Fall through !! + + case wxPATH_UNIX: + break; + + case wxPATH_MAC: + // On a Mac even names with * and ? are allowed (Tested with OS + // 9.2.1 and OS X 10.2.5) + strForbiddenChars = wxEmptyString; + break; + + case wxPATH_DOS: + strForbiddenChars += wxT("\\/:\"<>|"); + break; + + case wxPATH_VMS: + break; + } + + return strForbiddenChars; +} + /* static */ wxString wxFileName::GetVolumeSeparator(wxPathFormat format) { @@ -1107,7 +1143,7 @@ wxString wxFileName::GetPathSeparators(wxPathFormat format) break; default: - wxFAIL_MSG( _T("unknown wxPATH_XXX style") ); + wxFAIL_MSG( _T("Unknown wxPATH_XXX style") ); // fall through case wxPATH_UNIX: @@ -1206,7 +1242,7 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const break; default: - wxFAIL_MSG( _T("unknown path format") ); + wxFAIL_MSG( wxT("Unknown path format") ); // fall through case wxPATH_UNIX: @@ -1256,7 +1292,7 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const break; default: - wxFAIL_MSG( wxT("unexpected path format") ); + wxFAIL_MSG( wxT("Unexpected path format") ); // still fall through case wxPATH_DOS: @@ -1332,19 +1368,19 @@ wxString wxFileName::GetLongPath() const path = GetFullPath(); #if defined(__WIN32__) && !defined(__WXMICROWIN__) - bool success = FALSE; + bool success = false; #if wxUSE_DYNAMIC_LOADER typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); - static bool s_triedToLoad = FALSE; + static bool s_triedToLoad = false; if ( !s_triedToLoad ) { // suppress the errors about missing GetLongPathName[AW] wxLogNull noLog; - s_triedToLoad = TRUE; + s_triedToLoad = true; wxDynamicLibrary dllKernel(_T("kernel32")); if ( dllKernel.IsLoaded() ) { @@ -1375,7 +1411,7 @@ wxString wxFileName::GetLongPath() const ) != 0; pathOut.UngetWriteBuf(); - success = TRUE; + success = true; } } } @@ -1674,7 +1710,7 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess, dtAccess ? &ftAccess : NULL, dtMod ? &ftWrite : NULL) ) { - return TRUE; + return true; } } } @@ -1682,7 +1718,7 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess, if ( !dtAccess && !dtMod ) { // can't modify the creation time anyhow, don't try - return TRUE; + return true; } // if dtAccess or dtMod is not specified, use the other one (which must be @@ -1692,7 +1728,7 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess, utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); if ( utime(GetFullPath().fn_str(), &utm) == 0 ) { - return TRUE; + return true; } #else // other platform #endif // platforms @@ -1700,7 +1736,7 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess, wxLogSysError(_("Failed to modify file times for '%s'"), GetFullPath().c_str()); - return FALSE; + return false; } bool wxFileName::Touch() @@ -1709,12 +1745,12 @@ bool wxFileName::Touch() // under Unix touching file is simple: just pass NULL to utime() if ( utime(GetFullPath().fn_str(), NULL) == 0 ) { - return TRUE; + return true; } wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str()); - return FALSE; + return false; #else // other platform wxDateTime dtNow = wxDateTime::Now(); @@ -1756,7 +1792,7 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, } else { - ok = FALSE; + ok = false; } } @@ -1769,7 +1805,7 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, if ( dtMod ) ConvertFileTimeToWx(dtMod, ftWrite); - return TRUE; + return true; } #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__)) wxStructStat stBuf; @@ -1782,7 +1818,7 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, if ( dtCreate ) dtCreate->Set(stBuf.st_ctime); - return TRUE; + return true; } #else // other platform #endif // platforms @@ -1790,7 +1826,7 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, wxLogSysError(_("Failed to retrieve file times for '%s'"), GetFullPath().c_str()); - return FALSE; + return false; } #endif // wxUSE_DATETIME -- 2.45.2