From fad5bbfdb916b3733b539cda9e49844a78fb0762 Mon Sep 17 00:00:00 2001 From: fluxgen Date: Mon, 21 Jan 2002 01:56:39 +0000 Subject: added getStringBetween --- src/StringUtil.cc | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/StringUtil.hh | 5 +++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/StringUtil.cc b/src/StringUtil.cc index 036a4f4..ba5f58d 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.5 2002/01/09 14:11:20 fluxgen Exp $ +// $Id: StringUtil.cc,v 1.6 2002/01/21 01:56:39 fluxgen Exp $ #include "StringUtil.hh" @@ -74,3 +74,53 @@ char *StringUtil::expandFilename(const char *filename) { return StringUtil::strdup(retval.get()); //return modified value } + +//------------- getStringBetween ----------- +// Parses a string between "first" and "last" characters +// and ignoring ok_chars as whitespaces. The value is +// returned in "out". +// Returns negative value on error and this value is the position +// in the in-string where the error occured. +// 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, + const char *ok_chars) { + assert(first); + assert(last); + assert(instr); + + string::size_type i = 0, + total_add=0; //used to add extra if there is a \last to skip + string in(instr); + + // eat leading whitespace + i = in.find_first_not_of(ok_chars); + if (i == 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; + while (1) { + j = in.find_first_of(last, j+1); + if (j==string::npos) + return -in.size(); //send negative size + + //we found the last char, check so it doesn't have a '\' before + if (j>1 && in[j-1] != '\\') + break; + else if (j>1) { + in.erase(j-1, 1); //remove the '\' + j--; + total_add++; //save numchars removed so we can calculate totalpos + } + } + + 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); +} diff --git a/src/StringUtil.hh b/src/StringUtil.hh index 59ce57c..4ac3bff 100644 --- a/src/StringUtil.hh +++ b/src/StringUtil.hh @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -//$Id: StringUtil.hh,v 1.4 2002/01/09 14:11:20 fluxgen Exp $ +//$Id: StringUtil.hh,v 1.5 2002/01/21 01:56:39 fluxgen Exp $ #ifndef _STRINGUTIL_HH_ #define _STRINGUTIL_HH_ @@ -34,7 +34,8 @@ struct StringUtil static 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: -- cgit v0.11.2