00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef _OASYS_STRING_UTILS_H_
00040 #define _OASYS_STRING_UTILS_H_
00041
00046 #include <ctype.h>
00047 #include <string>
00048 #include <vector>
00049 #include <set>
00050 #include <map>
00051
00052
00053
00054 #if (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
00055 #include <hash_set>
00056 #include <hash_map>
00057 #define _std std
00058 #else
00059 #include <ext/hash_set>
00060 #include <ext/hash_map>
00061 #define _std __gnu_cxx
00062 #endif
00063
00064 namespace oasys {
00065
00069 struct StringHash {
00070 size_t operator()(const std::string& str) const
00071 {
00072 return _std::__stl_hash_string(str.c_str());
00073 }
00074 };
00075
00079 struct StringLessThan {
00080 bool operator()(const std::string& str1, const std::string& str2) const
00081 {
00082 return (str1.compare(str2) < 0);
00083 }
00084 };
00085
00089 struct StringGreaterThan {
00090 bool operator()(const std::string& str1, const std::string& str2) const
00091 {
00092 return (str1.compare(str2) > 0);
00093 }
00094 };
00095
00099 struct StringEquals {
00100 bool operator()(const std::string& str1, const std::string& str2) const
00101 {
00102 return (str1 == str2);
00103 }
00104 };
00105
00109 class StringSet : public std::set<std::string, StringLessThan> {
00110 public:
00111 void dump(const char* log) const;
00112 };
00113
00117 template <class _Type> class StringMap :
00118 public std::map<std::string, _Type, StringLessThan> {
00119 };
00120
00124 template <class _Type> class StringMultiMap :
00125 public std::multimap<std::string, _Type, StringLessThan> {
00126 };
00127
00131 class StringHashSet :
00132 public _std::hash_set<std::string, StringHash, StringEquals> {
00133 public:
00134 void dump(const char* log) const;
00135 };
00136
00140 template <class _Type> class StringHashMap :
00141 public _std::hash_map<std::string, _Type, StringHash, StringEquals> {
00142 };
00143
00147 class StringVector : public std::vector<std::string> {
00148 };
00149
00154 int
00155 tokenize(const std::string& str,
00156 const std::string& sep,
00157 std::vector<std::string>* tokens);
00158
00162 inline void
00163 hex2str(std::string* str, const u_char* bp, size_t len)
00164 {
00165 static const char hex[] = "0123456789abcdef";
00166 str->erase();
00167 for (size_t i = 0; i < len; ++i) {
00168 str->push_back(hex[(bp[i] >> 4) & 0xf]);
00169 str->push_back(hex[bp[i] & 0xf]);
00170 }
00171 }
00172
00177 inline void
00178 str2hex(const std::string& str, u_char* bp, size_t len)
00179 {
00180 #define HEXTONUM(x) ((x) < 'a' ? (x) - '0' : x - 'a' + 10)
00181 const char* s = str.data();
00182 for (size_t i = 0; i < len; ++i) {
00183 bp[i] = (HEXTONUM(s[2*i]) << 4) + HEXTONUM(s[2*i + 1]);
00184 }
00185 #undef HEXTONUM
00186 }
00187
00191 inline bool
00192 str_isascii(const u_char* bp, size_t len)
00193 {
00194 for (size_t i = 0; i < len; ++i) {
00195 if (!isascii(*bp++)) {
00196 return false;
00197 }
00198 }
00199
00200 return true;
00201 }
00202
00213 inline size_t
00214 fast_ultoa(unsigned long val, int base, char* endp)
00215 {
00216 #define to_char(n) ((n) + '0')
00217 char* cp = endp;
00218 long sval;
00219
00220 static const char xdigs[] = "0123456789abcdef";
00221
00222 switch(base) {
00223 case 10:
00224
00225 if (val < 10) {
00226 *cp = to_char(val);
00227 return 1;
00228 }
00229
00230
00231
00232 if (val > LONG_MAX) {
00233 *cp-- = to_char(val % 10);
00234 sval = val / 10;
00235 } else {
00236 sval = val;
00237 }
00238
00239 do {
00240 *cp-- = to_char(sval % 10);
00241 sval /= 10;
00242 } while (sval != 0);
00243
00244 break;
00245 case 16:
00246 do {
00247 *cp-- = xdigs[val & 15];
00248 val >>= 4;
00249 } while (val != 0);
00250 break;
00251
00252 default:
00253 return 0;
00254 }
00255
00256 return endp - cp;
00257
00258 #undef to_char
00259 }
00260
00261 }
00262
00263 #endif