diff options
author | fluxgen <fluxgen> | 2002-01-21 01:56:39 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2002-01-21 01:56:39 (GMT) |
commit | fad5bbfdb916b3733b539cda9e49844a78fb0762 (patch) | |
tree | 948876e02bdd9462cc5136f95df045f64b37a6ed | |
parent | 3d20c78714d4bfc95327b16d8af6163d57856463 (diff) | |
download | fluxbox_paul-fad5bbfdb916b3733b539cda9e49844a78fb0762.zip fluxbox_paul-fad5bbfdb916b3733b539cda9e49844a78fb0762.tar.bz2 |
added getStringBetween
-rw-r--r-- | src/StringUtil.cc | 52 | ||||
-rw-r--r-- | 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 @@ | |||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 20 | // DEALINGS IN THE SOFTWARE. |
21 | 21 | ||
22 | // $Id: StringUtil.cc,v 1.5 2002/01/09 14:11:20 fluxgen Exp $ | 22 | // $Id: StringUtil.cc,v 1.6 2002/01/21 01:56:39 fluxgen Exp $ |
23 | 23 | ||
24 | #include "StringUtil.hh" | 24 | #include "StringUtil.hh" |
25 | 25 | ||
@@ -74,3 +74,53 @@ char *StringUtil::expandFilename(const char *filename) { | |||
74 | 74 | ||
75 | return StringUtil::strdup(retval.get()); //return modified value | 75 | return StringUtil::strdup(retval.get()); //return modified value |
76 | } | 76 | } |
77 | |||
78 | //------------- getStringBetween ----------- | ||
79 | // Parses a string between "first" and "last" characters | ||
80 | // and ignoring ok_chars as whitespaces. The value is | ||
81 | // returned in "out". | ||
82 | // Returns negative value on error and this value is the position | ||
83 | // in the in-string where the error occured. | ||
84 | // Returns positive value on success and this value is | ||
85 | // for the position + 1 in the in-string where the "last"-char value | ||
86 | // was found. | ||
87 | //------------------------------------------ | ||
88 | int StringUtil::getStringBetween(string& out, const char *instr, const char first, const char last, | ||
89 | const char *ok_chars) { | ||
90 | assert(first); | ||
91 | assert(last); | ||
92 | assert(instr); | ||
93 | |||
94 | string::size_type i = 0, | ||
95 | total_add=0; //used to add extra if there is a \last to skip | ||
96 | string in(instr); | ||
97 | |||
98 | // eat leading whitespace | ||
99 | i = in.find_first_not_of(ok_chars); | ||
100 | if (i == string::npos) | ||
101 | return -in.size(); // nothing left but whitespace | ||
102 | |||
103 | if (in[i]!=first) | ||
104 | return -i; //return position to error | ||
105 | |||
106 | // find the end of the token | ||
107 | string::size_type j = i; | ||
108 | while (1) { | ||
109 | j = in.find_first_of(last, j+1); | ||
110 | if (j==string::npos) | ||
111 | return -in.size(); //send negative size | ||
112 | |||
113 | //we found the last char, check so it doesn't have a '\' before | ||
114 | if (j>1 && in[j-1] != '\\') | ||
115 | break; | ||
116 | else if (j>1) { | ||
117 | in.erase(j-1, 1); //remove the '\' | ||
118 | j--; | ||
119 | total_add++; //save numchars removed so we can calculate totalpos | ||
120 | } | ||
121 | } | ||
122 | |||
123 | out = in.substr(i+1, j-i-1); //copy the string between first and last | ||
124 | //return value to last character | ||
125 | return (j+1+total_add); | ||
126 | } | ||
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 @@ | |||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 20 | // DEALINGS IN THE SOFTWARE. |
21 | 21 | ||
22 | //$Id: StringUtil.hh,v 1.4 2002/01/09 14:11:20 fluxgen Exp $ | 22 | //$Id: StringUtil.hh,v 1.5 2002/01/21 01:56:39 fluxgen Exp $ |
23 | 23 | ||
24 | #ifndef _STRINGUTIL_HH_ | 24 | #ifndef _STRINGUTIL_HH_ |
25 | #define _STRINGUTIL_HH_ | 25 | #define _STRINGUTIL_HH_ |
@@ -34,7 +34,8 @@ struct StringUtil | |||
34 | static const char *strcasestr(const char *str, const char *ptn); | 34 | static const char *strcasestr(const char *str, const char *ptn); |
35 | 35 | ||
36 | static char *expandFilename(const char *filename); | 36 | static char *expandFilename(const char *filename); |
37 | 37 | static int getStringBetween(std::string& out, const char *instr, const char first, const char last, | |
38 | const char *ok_chars=" \t\n"); | ||
38 | //--------- stringtok ---------------------------------- | 39 | //--------- stringtok ---------------------------------- |
39 | // Breaks a string into tokens | 40 | // Breaks a string into tokens |
40 | // Usage check: | 41 | // Usage check: |