gloox  1.0.1
util.h
1 /*
2  Copyright (c) 2007-2012 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 
198  GLOOX_API const std::string escape( std::string what );
199 
209  GLOOX_API void appendEscaped( std::string& target, const std::string& data );
210 
216  GLOOX_API bool checkValidXMLChars( const std::string& data );
217 
223  GLOOX_API int internalLog2( unsigned int n );
224 
234  GLOOX_API void replaceAll( std::string& target, const std::string& find, const std::string& replace );
235 
242  static inline const std::string long2string( long int value, const int base = 10 )
243  {
244  if( base < 2 || base > 16 || value == 0 )
245  return "0";
246 
247  std::string output;
248  std::string sign;
249 
250  if( value < 0 )
251  {
252  sign += "-";
253  value = -value;
254  }
255 
256  while( output.empty() || value > 0 )
257  {
258  output.insert( 0, 1, static_cast<char>( value % base + '0' ) );
259  value /= base;
260  }
261 
262  return sign + output;
263  }
264 
270  static inline const std::string int2string( int value )
271  {
272  return long2string( value );
273  }
274 
275  }
276 
277 }
278 
279 #endif // UTIL_H__