gloox
1.0
|
00001 /* 00002 Copyright (c) 2007-2009 by Jakob Schroeter <js@camaya.net> 00003 This file is part of the gloox library. http://camaya.net/gloox 00004 00005 This software is distributed under a license. The full license 00006 agreement can be found in the file LICENSE in this distribution. 00007 This software may not be copied, modified, sold or distributed 00008 other than expressed in the named license agreement. 00009 00010 This software is distributed without any warranty. 00011 */ 00012 00013 #ifndef UTIL_H__ 00014 #define UTIL_H__ 00015 00016 #include "gloox.h" 00017 00018 #include <cmath> 00019 #include <algorithm> 00020 #include <string> 00021 #include <list> 00022 #include <map> 00023 00024 namespace gloox 00025 { 00026 00030 namespace util 00031 { 00032 00033 #define lookup( a, b ) _lookup( a, b, sizeof(b)/sizeof(char*) ) 00034 #define lookup2( a, b ) _lookup2( a, b, sizeof(b)/sizeof(char*) ) 00035 #define deflookup( a, b, c ) _lookup( a, b, sizeof(b)/sizeof(char*), c ) 00036 #define deflookup2( a, b, c ) _lookup2( a, b, sizeof(b)/sizeof(char*), c ) 00037 00046 GLOOX_API unsigned _lookup( const std::string& str, const char* values[], 00047 unsigned size, int def = -1 ); 00048 00057 GLOOX_API const std::string _lookup( unsigned code, const char* values[], 00058 unsigned size, const std::string& def = EmptyString ); 00059 00068 GLOOX_API unsigned _lookup2( const std::string& str, const char* values[], 00069 unsigned size, int def = -1 ); 00070 00079 GLOOX_API const std::string _lookup2( unsigned code, const char* values[], 00080 unsigned size, const std::string& def = EmptyString ); 00081 00087 template< typename T, typename F > 00088 inline void ForEach( T& t, F f ) 00089 { 00090 for( typename T::iterator it = t.begin(); it != t.end(); ++it ) 00091 ( (*it)->*f )(); 00092 } 00093 00101 template< typename T, typename F, typename D > 00102 inline void ForEach( T& t, F f, D& d ) 00103 { 00104 for( typename T::iterator it = t.begin(); it != t.end(); ++it ) 00105 ( (*it)->*f )( d ); 00106 } 00107 00116 template< typename T, typename F, typename D1, typename D2 > 00117 inline void ForEach( T& t, F f, D1& d1, D2& d2 ) 00118 { 00119 for( typename T::iterator it = t.begin(); it != t.end(); ++it ) 00120 ( (*it)->*f )( d1, d2 ); 00121 } 00122 00132 template< typename T, typename F, typename D1, typename D2, typename D3 > 00133 inline void ForEach( T& t, F f, D1& d1, D2& d2, D3& d3 ) 00134 { 00135 for( typename T::iterator it = t.begin(); it != t.end(); ++it ) 00136 ( (*it)->*f )( d1, d2, d3 ); 00137 } 00138 00143 template< typename T > 00144 inline void clearList( std::list< T* >& L ) 00145 { 00146 typename std::list< T* >::iterator it = L.begin(); 00147 typename std::list< T* >::iterator it2; 00148 while( it != L.end() ) 00149 { 00150 it2 = it++; 00151 delete (*it2); 00152 L.erase( it2 ); 00153 } 00154 } 00155 00160 template< typename Key, typename T > 00161 inline void clearMap( std::map< Key, T* >& M ) 00162 { 00163 typename std::map< Key, T* >::iterator it = M.begin(); 00164 typename std::map< Key, T* >::iterator it2; 00165 while( it != M.end() ) 00166 { 00167 it2 = it++; 00168 delete (*it2).second; 00169 M.erase( it2 ); 00170 } 00171 } 00172 00178 template< typename Key, typename T > 00179 inline void clearMap( std::map< const Key, T* >& M ) 00180 { 00181 typename std::map< const Key, T* >::iterator it = M.begin(); 00182 typename std::map< const Key, T* >::iterator it2; 00183 while( it != M.end() ) 00184 { 00185 it2 = it++; 00186 delete (*it2).second; 00187 M.erase( it2 ); 00188 } 00189 } 00190 00196 GLOOX_API const std::string escape( std::string what ); 00197 00203 GLOOX_API bool checkValidXMLChars( const std::string& data ); 00204 00210 GLOOX_API int internalLog2( unsigned int n ); 00211 00221 GLOOX_API void replaceAll( std::string& target, const std::string& find, const std::string& replace ); 00222 00229 static inline const std::string long2string( long int value, const int base = 10 ) 00230 { 00231 int add = 0; 00232 if( base < 2 || base > 16 || value == 0 ) 00233 return "0"; 00234 else if( value < 0 ) 00235 { 00236 ++add; 00237 value = -value; 00238 } 00239 int len = (int)( log( (double)( value ? value : 1 ) ) / log( (double)base ) ) + 1; 00240 const char digits[] = "0123456789ABCDEF"; 00241 char* num = (char*)calloc( len + 1 + add, sizeof( char ) ); 00242 num[len--] = '\0'; 00243 if( add ) 00244 num[0] = '-'; 00245 while( value && len > -1 ) 00246 { 00247 num[len-- + add] = digits[(int)( value % base )]; 00248 value /= base; 00249 } 00250 const std::string result( num ); 00251 free( num ); 00252 return result; 00253 } 00254 00260 static inline const std::string int2string( int value ) 00261 { 00262 return long2string( value ); 00263 } 00264 00265 } 00266 00267 } 00268 00269 #endif // UTIL_H__