gloox  1.0
util.h
1 /*
2  Copyright (c) 2007-2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 #ifndef UTIL_H__
14 #define UTIL_H__
15 
16 #include "gloox.h"
17 
18 #include <cmath>
19 #include <algorithm>
20 #include <string>
21 #include <list>
22 #include <map>
23 
24 namespace gloox
25 {
26 
30  namespace util
31  {
32 
33  #define lookup( a, b ) _lookup( a, b, sizeof(b)/sizeof(char*) )
34  #define lookup2( a, b ) _lookup2( a, b, sizeof(b)/sizeof(char*) )
35  #define deflookup( a, b, c ) _lookup( a, b, sizeof(b)/sizeof(char*), c )
36  #define deflookup2( a, b, c ) _lookup2( a, b, sizeof(b)/sizeof(char*), c )
37 
46  GLOOX_API unsigned _lookup( const std::string& str, const char* values[],
47  unsigned size, int def = -1 );
48 
57  GLOOX_API const std::string _lookup( unsigned code, const char* values[],
58  unsigned size, const std::string& def = EmptyString );
59 
68  GLOOX_API unsigned _lookup2( const std::string& str, const char* values[],
69  unsigned size, int def = -1 );
70 
79  GLOOX_API const std::string _lookup2( unsigned code, const char* values[],
80  unsigned size, const std::string& def = EmptyString );
81 
87  template< typename T, typename F >
88  inline void ForEach( T& t, F f )
89  {
90  for( typename T::iterator it = t.begin(); it != t.end(); ++it )
91  ( (*it)->*f )();
92  }
93 
101  template< typename T, typename F, typename D >
102  inline void ForEach( T& t, F f, D& d )
103  {
104  for( typename T::iterator it = t.begin(); it != t.end(); ++it )
105  ( (*it)->*f )( d );
106  }
107 
116  template< typename T, typename F, typename D1, typename D2 >
117  inline void ForEach( T& t, F f, D1& d1, D2& d2 )
118  {
119  for( typename T::iterator it = t.begin(); it != t.end(); ++it )
120  ( (*it)->*f )( d1, d2 );
121  }
122 
132  template< typename T, typename F, typename D1, typename D2, typename D3 >
133  inline void ForEach( T& t, F f, D1& d1, D2& d2, D3& d3 )
134  {
135  for( typename T::iterator it = t.begin(); it != t.end(); ++it )
136  ( (*it)->*f )( d1, d2, d3 );
137  }
138 
143  template< typename T >
144  inline void clearList( std::list< T* >& L )
145  {
146  typename std::list< T* >::iterator it = L.begin();
147  typename std::list< T* >::iterator it2;
148  while( it != L.end() )
149  {
150  it2 = it++;
151  delete (*it2);
152  L.erase( it2 );
153  }
154  }
155 
160  template< typename Key, typename T >
161  inline void clearMap( std::map< Key, T* >& M )
162  {
163  typename std::map< Key, T* >::iterator it = M.begin();
164  typename std::map< Key, T* >::iterator it2;
165  while( it != M.end() )
166  {
167  it2 = it++;
168  delete (*it2).second;
169  M.erase( it2 );
170  }
171  }
172 
178  template< typename Key, typename T >
179  inline void clearMap( std::map< const Key, T* >& M )
180  {
181  typename std::map< const Key, T* >::iterator it = M.begin();
182  typename std::map< const Key, T* >::iterator it2;
183  while( it != M.end() )
184  {
185  it2 = it++;
186  delete (*it2).second;
187  M.erase( it2 );
188  }
189  }
190 
196  GLOOX_API const std::string escape( std::string what );
197 
203  GLOOX_API bool checkValidXMLChars( const std::string& data );
204 
210  GLOOX_API int internalLog2( unsigned int n );
211 
221  GLOOX_API void replaceAll( std::string& target, const std::string& find, const std::string& replace );
222 
229  static inline const std::string long2string( long int value, const int base = 10 )
230  {
231  int add = 0;
232  if( base < 2 || base > 16 || value == 0 )
233  return "0";
234  else if( value < 0 )
235  {
236  ++add;
237  value = -value;
238  }
239  int len = (int)( log( (double)( value ? value : 1 ) ) / log( (double)base ) ) + 1;
240  const char digits[] = "0123456789ABCDEF";
241  char* num = (char*)calloc( len + 1 + add, sizeof( char ) );
242  num[len--] = '\0';
243  if( add )
244  num[0] = '-';
245  while( value && len > -1 )
246  {
247  num[len-- + add] = digits[(int)( value % base )];
248  value /= base;
249  }
250  const std::string result( num );
251  free( num );
252  return result;
253  }
254 
260  static inline const std::string int2string( int value )
261  {
262  return long2string( value );
263  }
264 
265  }
266 
267 }
268 
269 #endif // UTIL_H__