From 10d6e7a35822b6ccd5a717581edad254dea20972 Mon Sep 17 00:00:00 2001 From: fluxgen Date: Wed, 20 Mar 2002 11:32:03 +0000 Subject: namespace istead of struct --- src/StringUtil.cc | 25 +++++++++------- src/StringUtil.hh | 85 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 59 insertions(+), 51 deletions(-) diff --git a/src/StringUtil.cc b/src/StringUtil.cc index a85735a..54ef357 100644 --- a/src/StringUtil.cc +++ b/src/StringUtil.cc @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: StringUtil.cc,v 1.7 2002/01/27 12:46:28 fluxgen Exp $ +// $Id: StringUtil.cc,v 1.8 2002/03/20 11:32:03 fluxgen Exp $ #include "StringUtil.hh" @@ -31,10 +31,13 @@ using namespace std; +namespace StringUtil +{ + //------- strdup ------------------------ //TODO: comment this //---------------------------------------- -char *StringUtil::strdup(const char *s) { +char *strdup(const char *s) { int l = strlen(s) + 1; char *n = new char[l]; strncpy(n, s, l); @@ -47,7 +50,7 @@ char *StringUtil::strdup(const char *s) { // Returns 0 on success else pointer to str. // TODO: comment this //--------------------------------- -const char * StringUtil::strcasestr(const char *str, const char *ptn) { +const char *strcasestr(const char *str, const char *ptn) { const char *s2, *p2; for( ; *str; str++) { for(s2=str, p2=ptn; ; s2++,p2++) { @@ -63,7 +66,7 @@ const char * StringUtil::strcasestr(const char *str, const char *ptn) { // returns expanded filename // (note: the function creates new memory for the string) //--------------------------------------------------- -char *StringUtil::expandFilename(const char *filename) { +char *expandFilename(const char *filename) { auto_ptr retval( new char[strlen(filename)+strlen(getenv("HOME"))+2]); if (filename[0]=='~') { @@ -85,29 +88,29 @@ char *StringUtil::expandFilename(const char *filename) { // for the position + 1 in the in-string where the "last"-char value // was found. //------------------------------------------ -int StringUtil::getStringBetween(string& out, const char *instr, const char first, const char last, +int getStringBetween(std::string& out, const char *instr, const char first, const char last, const char *ok_chars) { assert(first); assert(last); assert(instr); - string::size_type i = 0, + std::string::size_type i = 0, total_add=0; //used to add extra if there is a \last to skip - string in(instr); + std::string in(instr); // eat leading whitespace i = in.find_first_not_of(ok_chars); - if (i == string::npos) + if (i == std::string::npos) return -in.size(); // nothing left but whitespace if (in[i]!=first) return -i; //return position to error // find the end of the token - string::size_type j = i; + std::string::size_type j = i; while (1) { j = in.find_first_of(last, j+1); - if (j==string::npos) + if (j==std::string::npos) return -in.size(); //send negative size //we found the last char, check so it doesn't have a '\' before @@ -124,3 +127,5 @@ int StringUtil::getStringBetween(string& out, const char *instr, const char firs //return value to last character return (j+1+total_add); } + +}; //end namespace StringUtil diff --git a/src/StringUtil.hh b/src/StringUtil.hh index a931fa2..91732ab 100644 --- a/src/StringUtil.hh +++ b/src/StringUtil.hh @@ -19,60 +19,63 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -//$Id: StringUtil.hh,v 1.6 2002/02/17 18:52:20 fluxgen Exp $ +//$Id: StringUtil.hh,v 1.7 2002/03/20 11:32:03 fluxgen Exp $ #ifndef STRINGUTIL_HH #define STRINGUTIL_HH #include -struct StringUtil +namespace StringUtil { - static char *strdup(const char *); + +char *strdup(const char *); - //Similar to `strstr' but this function ignores the case of both strings - static const char *strcasestr(const char *str, const char *ptn); +//Similar to `strstr' but this function ignores the case of both strings +const char *strcasestr(const char *str, const char *ptn); - static char *expandFilename(const char *filename); - static int getStringBetween(std::string& out, const char *instr, const char first, const char last, - const char *ok_chars=" \t\n"); - //--------- stringtok ---------------------------------- - // Breaks a string into tokens - // Usage check: - // http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3 - // Taken from an example at: - // http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/stringtok_std_h.txt - //-------------------------------------------------- - template - static void - stringtok (Container &container, std::string const &in, - const char * const delimiters = " \t\n") - { - const std::string::size_type len = in.length(); - std::string::size_type i = 0; +char *expandFilename(const char *filename); +int getStringBetween(std::string& out, const char *instr, const char first, const char last, + const char *ok_chars=" \t\n"); + +//--------- stringtok ---------------------------------- +// Breaks a string into tokens +// Usage check: +// http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3 +// Taken from an example at: +// http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/stringtok_std_h.txt +//-------------------------------------------------- +template +static void +stringtok (Container &container, std::string const &in, + const char * const delimiters = " \t\n") +{ + const std::string::size_type len = in.length(); + std::string::size_type i = 0; + + while ( i < len ) { + // eat leading whitespace + i = in.find_first_not_of(delimiters, i); + if (i == std::string::npos) + return; // nothing left but white space - while ( i < len ) { - // eat leading whitespace - i = in.find_first_not_of(delimiters, i); - if (i == std::string::npos) - return; // nothing left but white space + // find the end of the token + std::string::size_type j = in.find_first_of(delimiters, i); - // find the end of the token - std::string::size_type j = in.find_first_of(delimiters, i); + // push token + if (j == std::string::npos) { + container.push_back(in.substr(i)); + return; + } else + container.push_back(in.substr(i, j-i)); - // push token - if (j == std::string::npos) { - container.push_back(in.substr(i)); - return; - } else - container.push_back(in.substr(i, j-i)); + // set up for next loop + i = j + 1; + } +} - // set up for next loop - i = j + 1; - } - } -}; +};//end namespace StringUtil -#endif // _STRINGUTIL_HH_ +#endif // STRINGUTIL_HH -- cgit v0.11.2