namespace istead of struct

This commit is contained in:
fluxgen 2002-03-20 11:32:03 +00:00
parent ccf9f5749e
commit 10d6e7a358
2 changed files with 59 additions and 51 deletions

View file

@ -19,7 +19,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // 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" #include "StringUtil.hh"
@ -31,10 +31,13 @@
using namespace std; using namespace std;
namespace StringUtil
{
//------- strdup ------------------------ //------- strdup ------------------------
//TODO: comment this //TODO: comment this
//---------------------------------------- //----------------------------------------
char *StringUtil::strdup(const char *s) { char *strdup(const char *s) {
int l = strlen(s) + 1; int l = strlen(s) + 1;
char *n = new char[l]; char *n = new char[l];
strncpy(n, s, l); strncpy(n, s, l);
@ -47,7 +50,7 @@ char *StringUtil::strdup(const char *s) {
// Returns 0 on success else pointer to str. // Returns 0 on success else pointer to str.
// TODO: comment this // 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; const char *s2, *p2;
for( ; *str; str++) { for( ; *str; str++) {
for(s2=str, p2=ptn; ; s2++,p2++) { for(s2=str, p2=ptn; ; s2++,p2++) {
@ -63,7 +66,7 @@ const char * StringUtil::strcasestr(const char *str, const char *ptn) {
// returns expanded filename // returns expanded filename
// (note: the function creates new memory for the string) // (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]); auto_ptr<char> retval( new char[strlen(filename)+strlen(getenv("HOME"))+2]);
if (filename[0]=='~') { 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 // for the position + 1 in the in-string where the "last"-char value
// was found. // 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) { const char *ok_chars) {
assert(first); assert(first);
assert(last); assert(last);
assert(instr); 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 total_add=0; //used to add extra if there is a \last to skip
string in(instr); std::string in(instr);
// eat leading whitespace // eat leading whitespace
i = in.find_first_not_of(ok_chars); i = in.find_first_not_of(ok_chars);
if (i == string::npos) if (i == std::string::npos)
return -in.size(); // nothing left but whitespace return -in.size(); // nothing left but whitespace
if (in[i]!=first) if (in[i]!=first)
return -i; //return position to error return -i; //return position to error
// find the end of the token // find the end of the token
string::size_type j = i; std::string::size_type j = i;
while (1) { while (1) {
j = in.find_first_of(last, j+1); j = in.find_first_of(last, j+1);
if (j==string::npos) if (j==std::string::npos)
return -in.size(); //send negative size return -in.size(); //send negative size
//we found the last char, check so it doesn't have a '\' before //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 value to last character
return (j+1+total_add); return (j+1+total_add);
} }
}; //end namespace StringUtil

View file

@ -19,60 +19,63 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // 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 #ifndef STRINGUTIL_HH
#define STRINGUTIL_HH #define STRINGUTIL_HH
#include <string> #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 //Similar to `strstr' but this function ignores the case of both strings
static const char *strcasestr(const char *str, const char *ptn); const char *strcasestr(const char *str, const char *ptn);
static char *expandFilename(const char *filename); char *expandFilename(const char *filename);
static int getStringBetween(std::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=" \t\n"); 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 ) { //--------- stringtok ----------------------------------
// eat leading whitespace // Breaks a string into tokens
i = in.find_first_not_of(delimiters, i); // Usage check:
if (i == std::string::npos) // http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3
return; // nothing left but white space // 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;
// find the end of the token while ( i < len ) {
std::string::size_type j = in.find_first_of(delimiters, i); // eat leading whitespace
i = in.find_first_not_of(delimiters, i);
if (i == std::string::npos)
return; // nothing left but white space
// push token // find the end of the token
if (j == std::string::npos) { std::string::size_type j = in.find_first_of(delimiters, i);
container.push_back(in.substr(i));
return;
} else
container.push_back(in.substr(i, j-i));
// set up for next loop // push token
i = j + 1; 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;
}
}
};//end namespace StringUtil
#endif // _STRINGUTIL_HH_ #endif // STRINGUTIL_HH