#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
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;
+}
typedef std_tchar(string) String;
+String string_makelower(const String &str);
+
void string_replace(String &target, const String &find, const String &replace);
// Comparing
// 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_
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
#ifdef UNICODE
UCS2UTF8_Dealloc(regexp_str);
-#else
- regExp.UnlockBuffer();
#endif
}
}
// 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;
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());
}
}
* @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;
BOOL includeDir(LPCTSTR szDirName);
protected:
- CString ParseExtensions(CString extensions) const;
+ String ParseExtensions(const String &extensions) const;
private:
FilterList * m_pMaskFilter; /*< Filter for filemasks (*.cpp) */
using std::vector;
+static void AddFilterPattern(vector<FileFilterElement*> *filterList, String & str);
+
/**
* @brief Destructor, frees all filters.
*/
* @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;
#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
pfilter->fullpath = szFilepath;
pfilter->name = fileName.c_str(); // Filename is the default name
- CString sLine;
+ String sLine;
bool lossy = false;
bool bLinesLeft = true;
do
// 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);
#ifndef FileFilterMgr_h_included
#define FileFilterMgr_h_included
-// Uses MFC C++ template containers
-#include <afxtempl.h>
#include <vector>
#include "UnicodeString.h"
#include "pcre.h"