source: nscp/trunk/include/strEx.h @ 7da80b5

Last change on this file since 7da80b5 was f896cfb, checked in by Michael Medin <michael@…>, 8 years ago

2005-05-15 MickeM

+ Added NRPE support for checkCounter
+ Updated documentation

  • Make check commands ignore case + Added CheckHelpers module to alter the result of various check and similar things

2005-05-14 MickeM

+ Added support for Volumes (CheckDisk)
+ Added support for checking all drives of a certain kind
+ Added support for altering filter (makes it posible to check removale drives)
+ Created webpage and better documentation

  • Property mode set to 100644
File size: 7.7 KB
Line 
1#pragma once
2
3#include <sstream>
4#include <string>
5#include <utility>
6#include <list>
7#ifdef _DEBUG
8#include <iostream>
9#endif
10
11namespace strEx {
12
13        inline void replace(std::string &string, std::string replace, std::string with) {
14                std::string::size_type pos = string.find(replace);
15                std::string::size_type len = replace.length();
16                while (pos != std::string::npos) {
17                        string = string.substr(0,pos)+with+string.substr(pos+len);
18                        pos = string.find(replace, pos+1);
19                }
20        }
21        inline std::string itos(unsigned int i) {
22                std::stringstream ss;
23                ss << i;
24                return ss.str();
25        }
26        inline std::string itos(int i) {
27                std::stringstream ss;
28                ss << i;
29                return ss.str();
30        }
31        inline std::string itos(unsigned long long i) {
32                std::stringstream ss;
33                ss << i;
34                return ss.str();
35        }
36        inline std::string itos(__int64 i) {
37                std::stringstream ss;
38                ss << i;
39                return ss.str();
40        }
41        inline std::string itos(unsigned long i) {
42                std::stringstream ss;
43                ss << i;
44                return ss.str();
45        }
46        inline int stoi(std::string s) {
47                return atoi(s.c_str());
48        }
49        inline long long stoi64(std::string s) {
50                return _atoi64(s.c_str());
51        }
52        inline unsigned stoui_as_time(std::string time, unsigned int smallest_unit = 1000) {
53                std::string::size_type p = time.find_first_of("sSmMhHdDwW");
54                unsigned int value = atoi(time.c_str());
55                if (p == std::string::npos)
56                        return value * smallest_unit;
57                else if ( (time[p] == 's') || (time[p] == 'S') )
58                        return value * 1000;
59                else if ( (time[p] == 'm') || (time[p] == 'M') )
60                        return value * 60 * 1000;
61                else if ( (time[p] == 'h') || (time[p] == 'H') )
62                        return value * 60 * 60 * 1000;
63                else if ( (time[p] == 'd') || (time[p] == 'D') )
64                        return value * 24 * 60 * 60 * 1000;
65                else if ( (time[p] == 'w') || (time[p] == 'W') )
66                        return value * 7 * 24 * 60 * 60 * 1000;
67                return value * smallest_unit;
68        }
69        inline unsigned long long stoi64_as_time(std::string time, unsigned int smallest_unit = 1000) {
70                std::string::size_type p = time.find_first_of("sSmMhHdDwW");
71                unsigned long long value = _atoi64(time.c_str());
72                if (p == std::string::npos)
73                        return value * smallest_unit;
74                else if ( (time[p] == 's') || (time[p] == 'S') )
75                        return value * 1000;
76                else if ( (time[p] == 'm') || (time[p] == 'M') )
77                        return value * 60 * 1000;
78                else if ( (time[p] == 'h') || (time[p] == 'H') )
79                        return value * 60 * 60 * 1000;
80                else if ( (time[p] == 'd') || (time[p] == 'D') )
81                        return value * 24 * 60 * 60 * 1000;
82                else if ( (time[p] == 'w') || (time[p] == 'W') )
83                        return value * 7 * 24 * 60 * 60 * 1000;
84                return value * smallest_unit;
85        }
86        inline std::string itos_as_time(unsigned long long time) {
87                if (time > 7 * 24 * 60 * 60 * 1000)
88                        return itos(static_cast<unsigned int>(time/(7 * 24 * 60 * 60 * 1000))) + "w";
89                else if (time > 24 * 60 * 60 * 1000)
90                        return itos(static_cast<unsigned int>(time/(24 * 60 * 60 * 1000))) + "d";
91                else if (time > 60 * 60 * 1000)
92                        return itos(static_cast<unsigned int>(time/(60 * 60 * 1000))) + "h";
93                else if (time > 60 * 1000)
94                        return itos(static_cast<unsigned int>(time/(60 * 1000))) + "m";
95                else if (time > 1000)
96                        return itos(static_cast<unsigned int>(time/(1000))) + "s";
97                return itos(static_cast<unsigned int>(time));
98        }
99
100        inline long long stoi64_as_BKMG(std::string s) {
101                std::string::size_type p = s.find_first_of("BMKGT");
102                if (p == std::string::npos)
103                        return _atoi64(s.c_str());
104                else if (s[p] == 'B')
105                        return _atoi64(s.c_str());
106                else if (s[p] == 'K')
107                        return _atoi64(s.c_str())*1024;
108                else if (s[p] == 'M')
109                        return _atoi64(s.c_str())*1024*1024;
110                else if (s[p] == 'G')
111                        return _atoi64(s.c_str())*1024*1024*1024;
112                else if (s[p] == 'T')
113                        return _atoi64(s.c_str())*1024*1024*1024*1024;
114                else
115                        return _atoi64(s.c_str());
116        }
117#define BKMG_RANGE "BKMGTP"
118#define BKMG_SIZE 5
119
120        inline std::string itos_as_BKMG(unsigned __int64 i) {
121                unsigned __int64 cpy = i;
122                char postfix[] = BKMG_RANGE;
123                int idx = 0;
124                while ((cpy > 1024)&&(idx<BKMG_SIZE)) {
125                        cpy/=1024;
126                        idx++;
127                }
128                std::string ret = itos(cpy);
129                ret += postfix[idx];
130                if (idx > 0) {
131                        ret += " (" + itos(i) + "B)";
132                }
133                return ret;
134        }
135
136        typedef std::list<std::string> splitList;
137        inline splitList splitEx(std::string str, std::string key) {
138                splitList ret;
139                std::string::size_type pos = 0, lpos = 0;
140                while ((pos = str.find(key, pos)) !=  std::string::npos) {
141                        ret.push_back(str.substr(lpos, pos-lpos));
142                        lpos = ++pos;
143                }
144                if (lpos < str.size())
145                        ret.push_back(str.substr(lpos));
146                return ret;
147        }
148
149        inline std::pair<std::string,std::string> split(std::string str, std::string key) {
150                std::string::size_type pos = str.find(key);
151                if (pos == std::string::npos)
152                        return std::pair<std::string,std::string>(str, "");
153                return std::pair<std::string,std::string>(str.substr(0, pos), str.substr(pos+key.length()));
154        }
155        typedef std::pair<std::string,std::string> token;
156        inline token getToken(std::string buffer, char split) {
157                std::string::size_type pos = buffer.find(split);
158                if (pos == std::string::npos)
159                        return token(buffer, "");
160                if (pos == buffer.length()-1)
161                        return token(buffer.substr(0, pos), "");
162                return token(buffer.substr(0, pos-1), buffer.substr(++pos));
163        }
164
165
166        template<class _E>
167        struct blind_traits : public std::char_traits<_E>
168        {
169                static bool eq(const _E& x, const _E& y) {
170                        return tolower( x ) == tolower( y );
171                }
172                static bool lt(const _E& x, const _E& y) {
173                        return tolower( x ) < tolower( y );
174                }
175
176                static int compare(const _E *x, const _E *y, size_t n) {
177                        return strnicmp( x, y, n );
178                }
179
180                //  There's no memichr(), so we roll our own.  It ain't rocket science.
181                static const _E * __cdecl find(const _E *buf, size_t len, const _E& ch) {
182                        //  Jerry says that x86s have special mojo for memchr(), so the
183                        //  memchr() calls end up being reasonably efficient in practice.
184                        const _E *pu = (const _E *)memchr(buf, ch, len);
185                        const _E *pl = (const _E *)memchr(buf, tolower( ch ), len);
186                        if ( ! pu )
187                                return pl;  //  Might be NULL; if so, NULL's the word.
188                        else if ( ! pl )
189                                return pu;
190                        else
191                                //  If either one was NULL, we return the other; if neither is
192                                //  NULL, we return the lesser of the two.
193                                return ( pu < pl ) ? pu : pl;
194                }
195
196                //  I'm reasonably sure that this is eq() for wide characters.  Maybe.
197                static bool eq_int_type(const int_type& ch1, const int_type& ch2) {
198                        return char_traits<_E>::eq_int_type( tolower( ch1 ), tolower( ch2 ) );
199                }
200        };
201
202        //  And here's our case-blind string class.
203        typedef std::basic_string<char, blind_traits<char>, std::allocator<char> >  blindstr;
204
205#ifdef _DEBUG
206        inline void test_getToken(std::string in1, char in2, std::string out1, std::string out2) {
207                token t = getToken(in1, in2);
208                std::cout << "strEx::test_getToken(" << in1 << ", " << in2 << ") : ";
209                if ((t.first == out1) && (t.second == out2))
210                        std::cout << "Succeeded" << std::endl;
211                else
212                        std::cout << "Failed [" << out1 << "=" << t.first << ", " << out2 << "=" << t.second << "]" << std::endl;
213        }
214        inline void run_test_getToken() {
215                test_getToken("", '&', "", "");
216                test_getToken("&", '&', "", "");
217                test_getToken("&&", '&', "", "&");
218                test_getToken("foo", '&', "foo", "");
219                test_getToken("foo&", '&', "foo", "");
220                test_getToken("foo&bar", '&', "foo", "bar");
221                test_getToken("foo&bar&test", '&', "foo", "bar&test");
222        }
223
224        inline void test_replace(std::string source, std::string replace, std::string with, std::string out) {
225                std::cout << "strEx::test_replace(" << source << ", " << replace << ", " << with << ") : ";
226                std::string s = source;
227                strEx::replace(s, replace, with);
228                if (s == out)
229                        std::cout << "Succeeded" << std::endl;
230                else
231                        std::cout << "Failed [" << s << "=" << out << "]" << std::endl;
232        }
233        inline void run_test_replace() {
234                test_replace("", "", "", "");
235                test_replace("foo", "", "", "foo");
236                test_replace("foobar", "foo", "", "bar");
237                test_replace("foobar", "foo", "bar", "barbar");
238        }
239
240#endif
241}
Note: See TracBrowser for help on using the repository browser.