StringUtils.h

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // Though hash_set was part of std:: in the 2.9x gcc series, it's been
00053 // moved to ext/__gnu_cxx:: in 3.x
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         // optimize one-digit numbers
00225         if (val < 10) {
00226             *cp = to_char(val);
00227             return 1;
00228         }
00229 
00230         // according to the FreeBSD folks, signed arithmetic may be
00231         // faster than unsigned, so do at most one unsigned mod/divide
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; // maybe should do NOTREACHED??
00254     }
00255 
00256     return endp - cp;
00257 
00258 #undef to_char
00259 }
00260 
00261 } // namespace oasys
00262 
00263 #endif /* _OASYS_STRING_UTILS_H_ */

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1