namespace istead of struct
This commit is contained in:
parent
ccf9f5749e
commit
10d6e7a358
2 changed files with 59 additions and 51 deletions
|
@ -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<char> 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
|
||||
|
|
|
@ -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 <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");
|
||||
|
||||
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
|
||||
//--------- 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;
|
||||
|
||||
// find the end of the token
|
||||
std::string::size_type j = in.find_first_of(delimiters, i);
|
||||
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
|
||||
|
||||
// push token
|
||||
if (j == std::string::npos) {
|
||||
container.push_back(in.substr(i));
|
||||
return;
|
||||
} else
|
||||
container.push_back(in.substr(i, j-i));
|
||||
// find the end of the token
|
||||
std::string::size_type j = in.find_first_of(delimiters, i);
|
||||
|
||||
// set up for next loop
|
||||
i = j + 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
};//end namespace StringUtil
|
||||
|
||||
|
||||
|
||||
#endif // _STRINGUTIL_HH_
|
||||
#endif // STRINGUTIL_HH
|
||||
|
|
Loading…
Reference in a new issue