StringUtils.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #ifndef _OASYS_STRING_UTILS_H_
00019 #define _OASYS_STRING_UTILS_H_
00020 
00025 #include <ctype.h>
00026 #include <string>
00027 #include <vector>
00028 #include <set>
00029 #include <map>
00030 
00031 // Though hash_set was part of std:: in the 2.9x gcc series, it's been
00032 // moved to ext/__gnu_cxx:: in 3.x
00033 #if (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
00034 #include <hash_set>
00035 #include <hash_map>
00036 #define _std std
00037 #else
00038 #include <ext/hash_set>
00039 #include <ext/hash_map>
00040 #define _std __gnu_cxx
00041 #endif
00042 
00043 namespace oasys {
00044 
00045 //----------------------------------------------------------------------------
00049 struct StringHash {
00050     size_t operator()(const std::string& str) const
00051     {
00052         return _std::__stl_hash_string(str.c_str());
00053     }
00054 };
00055 
00056 //----------------------------------------------------------------------------
00060 struct StringLessThan {
00061     bool operator()(const std::string& str1, const std::string& str2) const
00062     {
00063         return (str1.compare(str2) < 0);
00064     }
00065 };
00066 
00067 //----------------------------------------------------------------------------
00071 struct StringGreaterThan {
00072     bool operator()(const std::string& str1, const std::string& str2) const
00073     {
00074         return (str1.compare(str2) > 0);
00075     }
00076 };
00077 
00078 //----------------------------------------------------------------------------
00082 struct StringEquals {
00083     bool operator()(const std::string& str1, const std::string& str2) const
00084     {
00085         return (str1 == str2);
00086     }
00087 };
00088 
00089 //----------------------------------------------------------------------------
00093 class StringSet : public std::set<std::string, StringLessThan> {
00094 public:
00095     void dump(const char* log) const;
00096 };
00097 
00098 //----------------------------------------------------------------------------
00102 template <class _Type> class StringMap :
00103     public std::map<std::string, _Type, StringLessThan> {
00104 };
00105 
00106 //----------------------------------------------------------------------------
00110 template <class _Type> class StringMultiMap :
00111     public std::multimap<std::string, _Type, StringLessThan> {
00112 };
00113 
00114 //----------------------------------------------------------------------------
00118 class StringHashSet :
00119         public _std::hash_set<std::string, StringHash, StringEquals> {
00120 public:
00121     void dump(const char* log) const;
00122 };
00123 
00124 //----------------------------------------------------------------------------
00128 template <class _Type> class StringHashMap :
00129     public _std::hash_map<std::string, _Type, StringHash, StringEquals> {
00130 };
00131 
00132 //----------------------------------------------------------------------------
00136 class StringVector : public std::vector<std::string> {
00137 };
00138 
00139 //----------------------------------------------------------------------------
00144 int
00145 tokenize(const std::string& str,
00146          const std::string& sep,
00147          std::vector<std::string>* tokens);
00148 
00149 //----------------------------------------------------------------------------
00153 inline void
00154 hex2str(std::string* str, const u_char* bp, size_t len)
00155 {
00156     static const char hex[] = "0123456789abcdef";
00157     str->erase();
00158     for (size_t i = 0; i < len; ++i) {
00159         str->push_back(hex[(bp[i] >> 4) & 0xf]);
00160         str->push_back(hex[bp[i] & 0xf]);
00161     }
00162 }
00163 
00164 //----------------------------------------------------------------------
00168 inline std::string
00169 hex2str(const u_char* bp, size_t len)
00170 {
00171     std::string ret;
00172     hex2str(&ret, bp, len);
00173     return ret;
00174 }
00175 
00176 //----------------------------------------------------------------------
00180 inline void
00181 hex2str(std::string* str, const char* bp, size_t len)
00182 {
00183     return hex2str(str, reinterpret_cast<const u_char*>(bp), len);
00184 }
00185 
00186 //----------------------------------------------------------------------
00190 inline std::string
00191 hex2str(const char* bp, size_t len)
00192 {
00193     return hex2str(reinterpret_cast<const u_char*>(bp), len);
00194 }
00195 
00196 //----------------------------------------------------------------------------
00201 inline void
00202 str2hex(const std::string& str, u_char* bp, size_t len)
00203 {
00204 #define HEXTONUM(x) ((x) < 'a' ? (x) - '0' : x - 'a' + 10)
00205     const char* s = str.data();
00206     for (size_t i = 0; i < len; ++i) {
00207         bp[i] = (HEXTONUM(s[2*i]) << 4) + HEXTONUM(s[2*i + 1]);
00208     }
00209 #undef HEXTONUM
00210 }
00211  
00212 //----------------------------------------------------------------------------
00216 inline bool
00217 str_isascii(const u_char* bp, size_t len)
00218 {
00219     for (size_t i = 0; i < len; ++i) {
00220         if (!isascii(*bp++)) {
00221             return false;
00222         }
00223     }
00224 
00225     return true;
00226 }
00227 
00228 //----------------------------------------------------------------------------
00239 inline size_t
00240 fast_ultoa(unsigned long val, int base, char* endp)
00241 {
00242 #define to_char(n)      ((n) + '0')
00243     char* cp = endp;
00244     long sval;
00245     
00246     static const char xdigs[] = "0123456789abcdef";
00247     
00248     switch(base) {
00249     case 10:
00250         // optimize one-digit numbers
00251         if (val < 10) {
00252             *cp = to_char(val);
00253             return 1;
00254         }
00255 
00256         // according to the FreeBSD folks, signed arithmetic may be
00257         // faster than unsigned, so do at most one unsigned mod/divide
00258         if (val > LONG_MAX) {
00259             *cp-- = to_char(val % 10);
00260             sval = val / 10;
00261         } else {
00262             sval = val;
00263         }
00264         
00265         do {
00266             *cp-- = to_char(sval % 10);
00267             sval /= 10;
00268         } while (sval != 0);
00269 
00270         break;
00271     case 16:
00272         do {
00273             *cp-- = xdigs[val & 15];
00274             val >>= 4;
00275         } while (val != 0);
00276         break;
00277 
00278     default:
00279         return 0; // maybe should do NOTREACHED??
00280     }
00281 
00282     return endp - cp;
00283 
00284 #undef to_char
00285 }
00286 
00287 //----------------------------------------------------------------------------
00288 const char*
00289 bool_to_str(bool b);
00290 
00291 //----------------------------------------------------------------------------
00292 const char*
00293 str_if(bool b, const char* true_str, const char* false_str = "");
00294 
00295 } // namespace oasys
00296 
00297 #endif /* _OASYS_STRING_UTILS_H_ */

Generated on Thu Jun 7 16:56:52 2007 for DTN Reference Implementation by  doxygen 1.5.1