diff options
Diffstat (limited to 'src/utility')
-rw-r--r-- | src/utility/path.cpp | 20 | ||||
-rw-r--r-- | src/utility/path.hpp | 10 | ||||
-rw-r--r-- | src/utility/random.cpp | 6 | ||||
-rw-r--r-- | src/utility/random.hpp | 9 | ||||
-rw-r--r-- | src/utility/stringUtils.cpp | 122 | ||||
-rw-r--r-- | src/utility/stringUtils.hpp | 131 |
6 files changed, 277 insertions, 21 deletions
diff --git a/src/utility/path.cpp b/src/utility/path.cpp index 477f29dd..88ad00e5 100644 --- a/src/utility/path.cpp +++ b/src/utility/path.cpp @@ -86,11 +86,11 @@ path& path::operator/=(const component& c) } -path path::parent() const +path path::getParent() const { path p; - if (!empty()) + if (!isEmpty()) { p.m_list.resize(m_list.size() - 1); std::copy(m_list.begin(), m_list.end() - 1, p.m_list.begin()); @@ -130,7 +130,7 @@ const bool path::operator==(const path& p) const for ( ; equal && i != m_list.end() ; ++i, ++j) //equal = (*i == *j); - equal = ((*i).buffer() == (*j).buffer()); + equal = ((*i).getBuffer() == (*j).getBuffer()); return (equal); } @@ -142,25 +142,25 @@ const bool path::operator!=(const path& p) const } -const bool path::empty() const +const bool path::isEmpty() const { return (m_list.empty()); } -const path::component path::last() const +const path::component path::getLastComponent() const { - return (empty() ? component("") : m_list[m_list.size() - 1]); + return (isEmpty() ? component("") : m_list[m_list.size() - 1]); } -path::component& path::last() +path::component& path::getLastComponent() { return (m_list[m_list.size() - 1]); } -const int path::size() const +const int path::getSize() const { return (m_list.size()); } @@ -180,12 +180,12 @@ path::component& path::operator[](const int x) const bool path::isDirectParentOf(const path& p) const { - if (p.size() != size() + 1) + if (p.getSize() != getSize() + 1) return (false); bool equal = true; - for (int i = 0 ; equal && i < size() ; ++i) + for (list::size_type i = 0 ; equal && i < m_list.size() ; ++i) equal = (m_list[i] == p.m_list[i]); return (equal); diff --git a/src/utility/path.hpp b/src/utility/path.hpp index bc980889..8a05f1a2 100644 --- a/src/utility/path.hpp +++ b/src/utility/path.hpp @@ -55,7 +55,7 @@ public: path& operator/=(const component& c); // Return the parent path - path parent() const; + path getParent() const; // Assignment path& operator=(const path& p); @@ -69,25 +69,25 @@ public: * * @return true if the path is empty (no components = root) */ - const bool empty() const; + const bool isEmpty() const; /** Return the last component of this path (const version). * * @return last component */ - const component last() const; + const component getLastComponent() const; /** Return the last component of this path (non-const version). * * @return last component */ - component& last(); + component& getLastComponent(); /** Return the number of components in this path. * * @return number of components */ - const int size() const; + const int getSize() const; /** Return the specified component of the path (const version). * diff --git a/src/utility/random.cpp b/src/utility/random.cpp index 6896d83c..b2a935d8 100644 --- a/src/utility/random.cpp +++ b/src/utility/random.cpp @@ -30,7 +30,7 @@ namespace utility { unsigned int random::m_next(static_cast<unsigned int>(::std::time(NULL))); -const unsigned int random::next() +const unsigned int random::getNext() { // Park and Miller's minimal standard generator: // xn+1 = (a * xn + b) mod c @@ -43,13 +43,13 @@ const unsigned int random::next() } -const unsigned int random::time() +const unsigned int random::getTime() { return (platformDependant::getHandler()->getUnixTime()); } -const unsigned int random::process() +const unsigned int random::getProcess() { return (platformDependant::getHandler()->getProcessId()); } diff --git a/src/utility/random.hpp b/src/utility/random.hpp index 0d9dd92a..70ae6716 100644 --- a/src/utility/random.hpp +++ b/src/utility/random.hpp @@ -25,6 +25,9 @@ namespace vmime { namespace utility { +/** Pseudo-random number generator. + */ + class random { public: @@ -33,21 +36,21 @@ public: * * @return random number */ - static const unsigned int next(); + static const unsigned int getNext(); /** Return the current time as a number (may be used to * build "random" strings). * * @return time as a number */ - static const unsigned int time(); + static const unsigned int getTime(); /** Return the current process number (may be user to * build "random" strings). * * @return process number */ - static const unsigned int process(); + static const unsigned int getProcess(); protected: diff --git a/src/utility/stringUtils.cpp b/src/utility/stringUtils.cpp new file mode 100644 index 00000000..937eb99d --- /dev/null +++ b/src/utility/stringUtils.cpp @@ -0,0 +1,122 @@ +// +// VMime library (http://vmime.sourceforge.net) +// Copyright (C) 2002-2004 Vincent Richard <[email protected]> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include "stringUtils.hpp" + + +namespace vmime +{ + + +const bool stringUtils::isStringEqualNoCase + (const string& s1, const char* s2, const string::size_type n) +{ + // 'n' is the number of characters to compare + // 's2' must be in lowercase letters only + if (s1.length() < n) + return (false); + + bool equal = true; + + for (string::size_type i = 0 ; equal && i < n ; ++i) + equal = (std::tolower(s1[i], std::locale()) == s2[i]); + + return (equal); +} + + +const bool stringUtils::isStringEqualNoCase(const string& s1, const string& s2) +{ + if (s1.length() != s2.length()) + return (false); + + bool equal = true; + const string::const_iterator end = s1.end(); + + for (string::const_iterator i = s1.begin(), j = s2.begin(); i != end ; ++i, ++j) + equal = (std::tolower(*i, std::locale()) == std::tolower(*j, std::locale())); + + return (equal); +} + + +const bool stringUtils::isStringEqualNoCase + (const string::const_iterator begin, const string::const_iterator end, + const char* s, const string::size_type n) +{ + if ((string::size_type)(end - begin) < n) + return (false); + + bool equal = true; + char* c = const_cast<char*>(s); + string::size_type r = n; + + for (string::const_iterator i = begin ; equal && r && *c ; ++i, ++c, --r) + equal = (std::tolower(*i, std::locale()) == *c); + + return (r == 0 && equal); +} + + +const string stringUtils::toLower(const string& str) +{ + string out(str); + const string::iterator end = out.end(); + + for (string::iterator i = out.begin() ; i != end ; ++i) + *i = std::tolower(*i, std::locale()); + + return (out); +} + + +const string stringUtils::trim(const string& str) +{ + string::const_iterator b = str.begin(); + string::const_iterator e = str.end(); + + if (b != e) + { + for ( ; b != e && isspace(*b) ; ++b); + for ( ; e != b && isspace(*(e - 1)) ; --e); + } + + return (string(b, e)); +} + + +const string::size_type stringUtils::countASCIIchars + (const string::const_iterator begin, const string::const_iterator end) +{ + string::size_type count = 0; + + for (string::const_iterator i = begin ; i != end ; ++i) + { + if (isascii(*i)) + { + if (*i != '=' || *(i + 1) != '?') // To avoid bad behaviour... + ++count; + } + } + + return (count); +} + + +} // vmime diff --git a/src/utility/stringUtils.hpp b/src/utility/stringUtils.hpp new file mode 100644 index 00000000..617874f0 --- /dev/null +++ b/src/utility/stringUtils.hpp @@ -0,0 +1,131 @@ +// +// VMime library (http://vmime.sourceforge.net) +// Copyright (C) 2002-2004 Vincent Richard <[email protected]> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_STRINGUTILS_HPP_INCLUDED +#define VMIME_STRINGUTILS_HPP_INCLUDED + + +#include "../types.hpp" +#include "../base.hpp" + +#include <sstream> + + +namespace vmime +{ + + +/** Miscellaneous functions related to strings. + */ + +class stringUtils +{ +public: + + /** Test two strings for equality (case insensitive). + * \warning Use this with ASCII-only strings. + * + * @param s1 first string + * @param s2 second string (must be in lower-case!) + * @param n length of the second string + * @return true if the two strings compare equally, false otherwise + */ + static const bool isStringEqualNoCase(const string& s1, const char* s2, const string::size_type n); + + /** Test two strings for equality (case insensitive). + * \warning Use this with ASCII-only strings. + * + * @param s1 first string + * @param s2 second string + * @return true if the two strings compare equally, false otherwise + */ + static const bool isStringEqualNoCase(const string& s1, const string& s2); + + /** Test two strings for equality (case insensitive). + * \warning Use this with ASCII-only strings. + * + * @param begin start position of the first string + * @param end end position of the first string + * @param s second string (must be in lower-case!) + * @param n length of the second string + * @return true if the two strings compare equally, false otherwise + */ + static const bool isStringEqualNoCase(const string::const_iterator begin, const string::const_iterator end, const char* s, const string::size_type n); + + /** Transform all the characters in a string to lower-case. + * \warning Use this with ASCII-only strings. + * + * @param str the string to transform + * @return a new string in lower-case + */ + static const string toLower(const string& str); + + /** Strip the space characters (SPC, TAB, CR, LF) at the beginning + * and at the end of the specified string. + * + * @param str string in which to strip spaces + * @return a new string with space characters removed + */ + static const string trim(const string& str); + + /** Return the number of 7-bit US-ASCII characters in a string. + * + * @param begin start position + * @param end end position + * @return number of ASCII characters + */ + static const string::size_type countASCIIchars(const string::const_iterator begin, const string::const_iterator end); + + /** Convert the specified value to a string value. + * + * @param value to convert + * @return value converted from type 'TYPE' + */ + template <class TYPE> + static const string toString(const TYPE& value) + { + std::ostringstream oss; + oss << value; + + return (oss.str()); + } + + /** Convert the specified string value to a value of + * the specified type. + * + * @param value value to convert + * @return value converted into type 'TYPE' + */ + template <class TYPE> + static const TYPE fromString(const string& value) + { + TYPE ret; + + std::istringstream iss(value); + iss >> ret; + + return (ret); + } +}; + + +} // vmime + + +#endif // VMIME_STRINGUTILS_HPP_INCLUDED |