diff options
author | fluxgen <fluxgen> | 2003-04-26 18:12:47 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2003-04-26 18:12:47 (GMT) |
commit | ae47696324e489355d19ea45c17442a3515a8845 (patch) | |
tree | 3124daba84d8ba6dda9201310f9a494a02ec640d /src/FbTk | |
parent | 3e92ad1010694d789dcceb383059587641928f9f (diff) | |
download | fluxbox-ae47696324e489355d19ea45c17442a3515a8845.zip fluxbox-ae47696324e489355d19ea45c17442a3515a8845.tar.bz2 |
moved from fluxbox source
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/StringUtil.cc | 148 | ||||
-rw-r--r-- | src/FbTk/StringUtil.hh | 85 |
2 files changed, 233 insertions, 0 deletions
diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc new file mode 100644 index 0000000..bd9a2df --- /dev/null +++ b/src/FbTk/StringUtil.cc | |||
@@ -0,0 +1,148 @@ | |||
1 | // StringUtil.cc for fluxbox | ||
2 | // Copyright (c) 2001 - 2003 Henrik Kinnunen (fluxgen<at>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.1 2003/04/26 18:12:47 fluxgen Exp $ | ||
23 | |||
24 | #include "StringUtil.hh" | ||
25 | |||
26 | #include <string> | ||
27 | #include <cstdio> | ||
28 | #include <cstdlib> | ||
29 | #include <cctype> | ||
30 | #include <cassert> | ||
31 | #include <memory> | ||
32 | |||
33 | using namespace std; | ||
34 | |||
35 | namespace FbTk { | ||
36 | |||
37 | namespace StringUtil { | ||
38 | |||
39 | /** | ||
40 | Takes a pointer to string *s as an argument, | ||
41 | creates a new string n, copies s to n and | ||
42 | returns a pointer to n. | ||
43 | */ | ||
44 | char *strdup(const char *s) { | ||
45 | int l = strlen(s) + 1; | ||
46 | char *n = new char[l]; | ||
47 | strncpy(n, s, l); | ||
48 | return n; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | Tries to find a string in another and | ||
53 | ignoring the case of the characters | ||
54 | Returns 0 on success else pointer to str. | ||
55 | */ | ||
56 | const char *strcasestr(const char *str, const char *ptn) { | ||
57 | const char *s2, *p2; | ||
58 | for( ; *str; str++) { | ||
59 | for(s2=str, p2=ptn; ; s2++,p2++) { | ||
60 | // check if we reached the end of ptn, if so, return str | ||
61 | if (!*p2) | ||
62 | return str; | ||
63 | // check if the chars match(ignoring case) | ||
64 | if (toupper(*s2) != toupper(*p2)) | ||
65 | break; | ||
66 | } | ||
67 | } | ||
68 | return 0; | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | if ~ then expand it to home of user | ||
73 | returns expanded filename | ||
74 | */ | ||
75 | string expandFilename(const std::string &filename) { | ||
76 | |||
77 | string retval; | ||
78 | size_t pos = filename.find_first_not_of(" \t"); | ||
79 | if (pos != std::string::npos && filename[pos] == '~') { | ||
80 | retval = getenv("HOME"); | ||
81 | if (pos != filename.size()) { | ||
82 | // copy from the character after '~' | ||
83 | retval += static_cast<const char *>(filename.c_str() + pos + 1); | ||
84 | } | ||
85 | } else | ||
86 | return filename; //return unmodified value | ||
87 | |||
88 | return retval; | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | Parses a string between "first" and "last" characters | ||
93 | and ignoring ok_chars as whitespaces. The value is | ||
94 | returned in "out". | ||
95 | Returns negative value on error and this value is the position | ||
96 | in the in-string where the error occured. | ||
97 | Returns positive value on success and this value is | ||
98 | for the position + 1 in the in-string where the "last"-char value | ||
99 | was found. | ||
100 | */ | ||
101 | int getStringBetween(std::string& out, const char *instr, const char first, const char last, | ||
102 | const char *ok_chars) { | ||
103 | assert(first); | ||
104 | assert(last); | ||
105 | assert(instr); | ||
106 | |||
107 | std::string::size_type i = 0, | ||
108 | total_add=0; //used to add extra if there is a \last to skip | ||
109 | std::string in(instr); | ||
110 | |||
111 | // eat leading whitespace | ||
112 | i = in.find_first_not_of(ok_chars); | ||
113 | if (i == std::string::npos) | ||
114 | return -in.size(); // nothing left but whitespace | ||
115 | |||
116 | if (in[i]!=first) | ||
117 | return -i; //return position to error | ||
118 | |||
119 | // find the end of the token | ||
120 | std::string::size_type j = i; | ||
121 | while (1) { | ||
122 | j = in.find_first_of(last, j+1); | ||
123 | if (j==std::string::npos) | ||
124 | return -in.size(); //send negative size | ||
125 | |||
126 | //we found the last char, check so it doesn't have a '\' before | ||
127 | if (j>1 && in[j-1] != '\\') | ||
128 | break; | ||
129 | else if (j>1) { | ||
130 | in.erase(j-1, 1); //remove the '\' | ||
131 | j--; | ||
132 | total_add++; //save numchars removed so we can calculate totalpos | ||
133 | } | ||
134 | } | ||
135 | |||
136 | out = in.substr(i+1, j-i-1); //copy the string between first and last | ||
137 | //return value to last character | ||
138 | return (j+1+total_add); | ||
139 | } | ||
140 | |||
141 | void toLower(char * const conv) { | ||
142 | for (size_t byte_pos = 0; byte_pos < strlen(conv); ++byte_pos) | ||
143 | conv[byte_pos] = tolower(conv[byte_pos]); | ||
144 | } | ||
145 | |||
146 | }; // end namespace StringUtil | ||
147 | |||
148 | }; // end namespace FbTk | ||
diff --git a/src/FbTk/StringUtil.hh b/src/FbTk/StringUtil.hh new file mode 100644 index 0000000..c1f568e --- /dev/null +++ b/src/FbTk/StringUtil.hh | |||
@@ -0,0 +1,85 @@ | |||
1 | // StringUtil.hh for fluxbox | ||
2 | // Copyright (c) 2001 - 2003 Henrik Kinnunen (fluxgen(at)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.hh,v 1.1 2003/04/26 18:12:46 fluxgen Exp $ | ||
23 | |||
24 | #ifndef FBTK_STRINGUTIL_HH | ||
25 | #define FBTK_STRINGUTIL_HH | ||
26 | |||
27 | #include <string> | ||
28 | |||
29 | namespace FbTk { | ||
30 | |||
31 | namespace StringUtil { | ||
32 | |||
33 | char *strdup(const char *); | ||
34 | |||
35 | /// Similar to `strstr' but this function ignores the case of both strings | ||
36 | const char *strcasestr(const char *str, const char *ptn); | ||
37 | |||
38 | /// expands ~ to value of ${HOME} enviroment variable | ||
39 | std::string expandFilename(const std::string &filename); | ||
40 | |||
41 | /// returns string between character first and last | ||
42 | int getStringBetween(std::string& out, const char *instr, | ||
43 | char first, char last, | ||
44 | const char *ok_chars=" \t\n"); | ||
45 | |||
46 | /// converts a string to lover case | ||
47 | void toLower(char * const conv); | ||
48 | |||
49 | |||
50 | /// Breaks a string into tokens | ||
51 | template <typename Container> | ||
52 | static void | ||
53 | stringtok (Container &container, std::string const &in, | ||
54 | const char * const delimiters = " \t\n") { | ||
55 | |||
56 | const std::string::size_type len = in.length(); | ||
57 | std::string::size_type i = 0; | ||
58 | |||
59 | while ( i < len ) { | ||
60 | // eat leading whitespace | ||
61 | i = in.find_first_not_of(delimiters, i); | ||
62 | if (i == std::string::npos) | ||
63 | return; // nothing left but white space | ||
64 | |||
65 | // find the end of the token | ||
66 | std::string::size_type j = in.find_first_of(delimiters, i); | ||
67 | |||
68 | // push token | ||
69 | if (j == std::string::npos) { | ||
70 | container.push_back(in.substr(i)); | ||
71 | return; | ||
72 | } else | ||
73 | container.push_back(in.substr(i, j-i)); | ||
74 | |||
75 | // set up for next loop | ||
76 | i = j + 1; | ||
77 | } | ||
78 | } | ||
79 | |||
80 | }; // end namespace StringUtil | ||
81 | |||
82 | }; // end namespace FbTk | ||
83 | |||
84 | |||
85 | #endif // FBTK_STRINGUTIL_HH | ||