namespace istead of struct
fluxgen fluxgen
2 files changed,
59 insertions(+),
51 deletions(-)
M
src/StringUtil.cc
→
src/StringUtil.cc
@@ -19,7 +19,7 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// 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 @@ #include <memory>
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 @@ // ignoring the case of the characters
// 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 @@ // if ~ then expand it to home of user
// 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<char> retval( new char[strlen(filename)+strlen(getenv("HOME"))+2]); if (filename[0]=='~') {@@ -85,29 +88,29 @@ // Returns positive value on success and this value is
// 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 @@ out = in.substr(i+1, j-i-1); //copy the string between first and last
//return value to last character return (j+1+total_add); } + +}; //end namespace StringUtil
M
src/StringUtil.hh
→
src/StringUtil.hh
@@ -19,60 +19,63 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// 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 <string> -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 <typename Container> - 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 <typename Container> +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