OSDN Git Service

More conversion from MFC CString to String in file filter code.
authorKimmo Varis <kimmov@gmail.com>
Thu, 25 Jun 2009 13:24:06 +0000 (13:24 +0000)
committerKimmo Varis <kimmov@gmail.com>
Thu, 25 Jun 2009 13:24:06 +0000 (13:24 +0000)
Src/Common/UnicodeString.cpp
Src/Common/UnicodeString.h
Src/FileFilterHelper.cpp
Src/FileFilterHelper.h
Src/FileFilterMgr.cpp
Src/FileFilterMgr.h

index 9672d0b..c8e554d 100644 (file)
 #include "UnicodeString.h"
 
 /**
+ * @brief Convert a string to lower case string.
+ * @param [in] str String to convert to lower case.
+ * @return Lower case string.
+ */
+String string_makelower(const String &str)
+{
+       String ret(str);
+       String::size_type i = 0;
+       for (i = 0; i < ret.length(); i++)
+               ret[i] = _totlower(ret[i]);
+       return ret;
+}
+
+/**
  * @brief Replace a string inside a string with another string.
  * This function searches for a string inside another string an if found,
  * replaces it with another string. Function can replace several instances
@@ -88,3 +102,37 @@ String string_trim_ws(const String & str)
                result.erase(it + 1, result.end());
        return result;
 }
+
+/**
+ * @brief Trims whitespace chars from begin of the string.
+ * @param [in] str the original string.
+ * @return Trimmed string.
+ */
+String string_trim_ws_begin(const String & str)
+{
+       String result(str);
+       String::iterator it = result.begin();
+       while (_istspace(*it))
+               ++it;
+       
+       if (it != result.begin())
+               result.erase(result.begin(), it);
+       return result;
+}
+
+/**
+ * @brief Trims whitespace chars from end of the string.
+ * @param [in] str the original string.
+ * @return Trimmed string.
+ */
+String string_trim_ws_end(const String & str)
+{
+       String result(str);
+       String::iterator it = result.end() - 1;
+       while (_istspace(*it))
+               --it;
+
+       if (it != result.end() - 1)
+               result.erase(it + 1, result.end());
+       return result;
+}
index 6e9f9da..64be2c8 100644 (file)
@@ -37,6 +37,8 @@
 
 typedef std_tchar(string) String;
 
+String string_makelower(const String &str);
+
 void string_replace(String &target, const String &find, const String &replace);
 
 // Comparing
@@ -44,5 +46,7 @@ int string_compare_nocase(const String &str1, const String &str2);
 
 // Trimming
 String string_trim_ws(const String & str);
+String string_trim_ws_begin(const String & str);
+String string_trim_ws_end(const String & str);
 
 #endif // _UNICODE_STRING_
index beaeae8..3b22f01 100644 (file)
@@ -205,16 +205,16 @@ void FileFilterHelper::SetMask(LPCTSTR strMask)
                return;
        }
        m_sMask = strMask;
-       CString regExp = ParseExtensions(strMask);
+       String regExp = ParseExtensions(strMask);
 
        char * regexp_str;
        FilterList::EncodingType type;
 
 #ifdef UNICODE
-       regexp_str = UCS2UTF8_ConvertToUtf8(regExp);
+       regexp_str = UCS2UTF8_ConvertToUtf8(regExp.c_str());
        type = FilterList::ENC_UTF8;
 #else
-       regexp_str = regExp.LockBuffer();
+       regexp_str = &*regExp.begin();
        type = FilterList::ENC_ANSI;
 #endif
 
@@ -223,8 +223,6 @@ void FileFilterHelper::SetMask(LPCTSTR strMask)
 
 #ifdef UNICODE
        UCS2UTF8_Dealloc(regexp_str);
-#else
-       regExp.UnlockBuffer();
 #endif
 }
 
@@ -245,15 +243,15 @@ BOOL FileFilterHelper::includeFile(LPCTSTR szFileName)
                }
 
                // preprend a backslash if there is none
-               CString strFileName = szFileName;
-               strFileName.MakeLower();
+               String strFileName = szFileName;
+               strFileName = string_makelower(strFileName);
                if (strFileName[0] != _T('\\'))
                        strFileName = _T('\\') + strFileName;
                // append a point if there is no extension
-               if (strFileName.Find(_T('.')) == -1)
+               if (strFileName.find(_T('.')) == -1)
                        strFileName = strFileName + _T('.');
 
-               char * name_utf = UCS2UTF8_ConvertToUtf8(strFileName);
+               char * name_utf = UCS2UTF8_ConvertToUtf8(strFileName.c_str());
                bool match = m_pMaskFilter->Match(name_utf);
                UCS2UTF8_Dealloc(name_utf);
                return match;
@@ -285,10 +283,10 @@ BOOL FileFilterHelper::includeDir(LPCTSTR szDirName)
                        return TRUE;
 
                // Add a backslash
-               CString strDirName(_T("\\"));
+               String strDirName(_T("\\"));
                strDirName += szDirName;
 
-               return m_fileFilterMgr->TestDirNameAgainstFilter(m_currentFilter, strDirName);
+               return m_fileFilterMgr->TestDirNameAgainstFilter(m_currentFilter, strDirName.c_str());
        }
 }
 
@@ -326,43 +324,45 @@ void FileFilterHelper::LoadFileFilterDirPattern(FILEFILTER_FILEMAP & patternsLoa
  * @param [in] Extension list/mask to convert to regular expression.
  * @return Regular expression that matches extension list.
  */
-CString FileFilterHelper::ParseExtensions(CString extensions) const
+String FileFilterHelper::ParseExtensions(const String &extensions) const
 {
-       CString strParsed;
-       CString strPattern;
+       String strParsed;
+       String strPattern;
+       String ext(extensions);
        BOOL bFilterAdded = FALSE;
        static const TCHAR pszSeps[] = _T(" ;|,:");
 
-       extensions += _T(";"); // Add one separator char to end
-       int pos = extensions.FindOneOf(pszSeps);
+       ext += _T(";"); // Add one separator char to end
+       size_t pos = ext.find_first_of(pszSeps);
        
        while (pos >= 0)
        {
-               CString token = extensions.Left(pos); // Get first extension
-               extensions.Delete(0, pos + 1); // Remove extension + separator
+               String token = ext.substr(0, pos); // Get first extension
+               ext = ext.substr(pos + 2); // Remove extension + separator
                
                // Only "*." or "*.something" allowed, other ignored
-               if (token.GetLength() >= 2 && token[0] == '*' && token[1] == '.')
+               if (token.length() >= 2 && token[0] == '*' && token[1] == '.')
                {
                        bFilterAdded = TRUE;
                        strPattern += _T(".*\\.");
-                       strPattern += token.Mid(2);
+                       strPattern += token.substr(2);
                        strPattern += _T("$");
                }
                else
                        bFilterAdded = FALSE;
 
-               pos = extensions.FindOneOf(pszSeps); 
+               pos = ext.find_first_of(pszSeps); 
                if (bFilterAdded && pos >= 0)
                        strPattern += _T("|");
        }
 
-       if (strPattern.IsEmpty())
+       if (strPattern.empty())
                strParsed = _T(".*"); // Match everything
        else
        {
                strParsed = _T("^");
-               strPattern.MakeLower();
+
+               strPattern = string_makelower(strPattern);
                strParsed = strPattern; //+ _T("$");
        }
        return strParsed;
index 857d084..a4dfe25 100644 (file)
@@ -129,7 +129,7 @@ public:
        BOOL includeDir(LPCTSTR szDirName);
 
 protected:
-       CString ParseExtensions(CString extensions) const;
+       String ParseExtensions(const String &extensions) const;
 
 private:
        FilterList * m_pMaskFilter;       /*< Filter for filemasks (*.cpp) */
index c9e24e6..18c1aea 100644 (file)
@@ -39,6 +39,8 @@ static char THIS_FILE[] = __FILE__;
 
 using std::vector;
 
+static void AddFilterPattern(vector<FileFilterElement*> *filterList, String & str);
+
 /**
  * @brief Destructor, frees all filters.
  */
@@ -130,25 +132,25 @@ void FileFilterMgr::DeleteAllFilters()
  * @param [in] filterList List where pattern is added.
  * @param [in] str Temporary variable (ie, it may be altered)
  */
-static void AddFilterPattern(vector<FileFilterElement*> *filterList, CString & str)
+static void AddFilterPattern(vector<FileFilterElement*> *filterList, String & str)
 {
        LPCTSTR commentLeader = _T("##"); // Starts comment
-       str.TrimLeft();
+       str = string_trim_ws_begin(str);
 
        // Ignore lines beginning with '##'
-       int pos = str.Find(commentLeader);
+       size_t pos = str.find(commentLeader);
        if (pos == 0)
                return;
 
        // Find possible comment-separator '<whitespace>##'
        while (pos > 0 && !_istspace(str[pos - 1]))
-               pos = str.Find(commentLeader, pos + 1);
+               pos = str.find(commentLeader, pos + 1);
 
        // Remove comment and whitespaces before it
        if (pos > 0)
-               str = str.Left(pos);
-       str.TrimRight();
-       if (str.IsEmpty())
+               str = str.substr(0, pos);
+       str = string_trim_ws_end(str);
+       if (str.empty())
                return;
 
        const char * errormsg = NULL;
@@ -160,7 +162,7 @@ static void AddFilterPattern(vector<FileFilterElement*> *filterList, CString & s
 #ifdef UNICODE
        // For unicode builds, use UTF-8.
        // Convert pattern to UTF-8 and set option for PCRE to specify UTF-8.
-       regexLen = TransformUcs2ToUtf8((LPCTSTR)str, _tcslen(str),
+       regexLen = TransformUcs2ToUtf8(str.c_str(), str.length(),
                regexString, sizeof(regexString));
        pcre_opts |= PCRE_UTF8;
 #else
@@ -210,7 +212,7 @@ FileFilter * FileFilterMgr::LoadFilterFile(LPCTSTR szFilepath, int & error)
        pfilter->fullpath = szFilepath;
        pfilter->name = fileName.c_str(); // Filename is the default name
 
-       CString sLine;
+       String sLine;
        bool lossy = false;
        bool bLinesLeft = true;
        do
@@ -218,46 +220,45 @@ FileFilter * FileFilterMgr::LoadFilterFile(LPCTSTR szFilepath, int & error)
                // Returns false when last line is read
                String tmpLine;
                bLinesLeft = file.ReadString(tmpLine, &lossy);
-               sLine = tmpLine.c_str();
-               sLine.TrimLeft();
-               sLine.TrimRight();
+               sLine = tmpLine;
+               sLine = string_trim_ws(sLine);
 
-               if (0 == _tcsncmp(sLine, _T("name:"), 5))
+               if (0 == _tcsncmp(sLine.c_str(), _T("name:"), 5))
                {
                        // specifies display name
-                       CString str = sLine.Mid(5);
-                       str.TrimLeft();
-                       if (!str.IsEmpty())
+                       String str = sLine.substr(5);
+                       str = string_trim_ws_begin(str);
+                       if (!str.empty())
                                pfilter->name = str;
                }
-               else if (0 == _tcsncmp(sLine, _T("desc:"), 5))
+               else if (0 == _tcsncmp(sLine.c_str(), _T("desc:"), 5))
                {
                        // specifies display name
-                       CString str = sLine.Mid(5);
-                       str.TrimLeft();
-                       if (!str.IsEmpty())
+                       String str = sLine.substr(5);
+                       str = string_trim_ws_begin(str);
+                       if (!str.empty())
                                pfilter->description = str;
                }
-               else if (0 == _tcsncmp(sLine, _T("def:"), 4))
+               else if (0 == _tcsncmp(sLine.c_str(), _T("def:"), 4))
                {
                        // specifies default
-                       CString str = sLine.Mid(4);
-                       str.TrimLeft();
+                       String str = sLine.substr(4);
+                       str = string_trim_ws_begin(str);
                        if (str == _T("0") || str == _T("no") || str == _T("exclude"))
                                pfilter->default_include = false;
                        else if (str == _T("1") || str == _T("yes") || str == _T("include"))
                                pfilter->default_include = true;
                }
-               else if (0 == _tcsncmp(sLine, _T("f:"), 2))
+               else if (0 == _tcsncmp(sLine.c_str(), _T("f:"), 2))
                {
                        // file filter
-                       CString str = sLine.Mid(2);
+                       String str = sLine.substr(2);
                        AddFilterPattern(&pfilter->filefilters, str);
                }
-               else if (0 == _tcsncmp(sLine, _T("d:"), 2))
+               else if (0 == _tcsncmp(sLine.c_str(), _T("d:"), 2))
                {
                        // directory filter
-                       CString str = sLine.Mid(2);
+                       String str = sLine.substr(2);
                        AddFilterPattern(&pfilter->dirfilters, str);
                }
        } while (bLinesLeft);
index bed0fd2..983bbdb 100644 (file)
@@ -23,8 +23,6 @@
 #ifndef FileFilterMgr_h_included
 #define FileFilterMgr_h_included
 
-// Uses MFC C++ template containers
-#include <afxtempl.h>
 #include <vector>
 #include "UnicodeString.h"
 #include "pcre.h"