diff options
Diffstat (limited to 'src/StringUtil.cc')
-rw-r--r-- | src/StringUtil.cc | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/StringUtil.cc b/src/StringUtil.cc new file mode 100644 index 0000000..a85735a --- /dev/null +++ b/src/StringUtil.cc | |||
@@ -0,0 +1,126 @@ | |||
1 | // StringUtil.cc for fluxbox | ||
2 | // Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id: StringUtil.cc,v 1.7 2002/01/27 12:46:28 fluxgen Exp $ | ||
23 | |||
24 | #include "StringUtil.hh" | ||
25 | |||
26 | #include <string> | ||
27 | #include <cstdio> | ||
28 | #include <cstdlib> | ||
29 | #include <cctype> | ||
30 | #include <memory> | ||
31 | |||
32 | using namespace std; | ||
33 | |||
34 | //------- strdup ------------------------ | ||
35 | //TODO: comment this | ||
36 | //---------------------------------------- | ||
37 | char *StringUtil::strdup(const char *s) { | ||
38 | int l = strlen(s) + 1; | ||
39 | char *n = new char[l]; | ||
40 | strncpy(n, s, l); | ||
41 | return n; | ||
42 | } | ||
43 | |||
44 | //------- strcasestr -------------- | ||
45 | // Tries to find a string in another and | ||
46 | // ignoring the case of the characters | ||
47 | // Returns 0 on success else pointer to str. | ||
48 | // TODO: comment this | ||
49 | //--------------------------------- | ||
50 | const char * StringUtil::strcasestr(const char *str, const char *ptn) { | ||
51 | const char *s2, *p2; | ||
52 | for( ; *str; str++) { | ||
53 | for(s2=str, p2=ptn; ; s2++,p2++) { | ||
54 | if (!*p2) return str; | ||
55 | if (toupper(*s2) != toupper(*p2)) break; | ||
56 | } | ||
57 | } | ||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | //------------- expandFilename ---------------------- | ||
62 | // if ~ then expand it to home of user | ||
63 | // returns expanded filename | ||
64 | // (note: the function creates new memory for the string) | ||
65 | //--------------------------------------------------- | ||
66 | char *StringUtil::expandFilename(const char *filename) { | ||
67 | |||
68 | auto_ptr<char> retval( new char[strlen(filename)+strlen(getenv("HOME"))+2]); | ||
69 | if (filename[0]=='~') { | ||
70 | strcpy(retval.get(), getenv("HOME")); | ||
71 | strcat(retval.get(), &filename[1]); | ||
72 | } else | ||
73 | return StringUtil::strdup(filename); //return unmodified value | ||
74 | |||
75 | return StringUtil::strdup(retval.get()); //return modified value | ||
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 | } | ||