gloox  1.0.9
util.h
1 /*
2  Copyright (c) 2007-2013 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 #include <cstdlib>
24 
25 namespace gloox
26 {
27 
31  namespace util
32  {
33 
34  #define lookup( a, b ) _lookup( a, b, sizeof(b)/sizeof(char*) )
35  #define lookup2( a, b ) _lookup2( a, b, sizeof(b)/sizeof(char*) )
36  #define deflookup( a, b, c ) _lookup( a, b, sizeof(b)/sizeof(char*), c )
37  #define deflookup2( a, b, c ) _lookup2( a, b, sizeof(b)/sizeof(char*), c )
38 
47  GLOOX_API unsigned _lookup( const std::string& str, const char* values[],
48  unsigned size, int def = -1 );
49 
58  GLOOX_API const std::string _lookup( unsigned code, const char* values[],
59  unsigned size, const std::string& def = EmptyString );
60 
69  GLOOX_API unsigned _lookup2( const std::string& str, const char* values[],
70  unsigned size, int def = -1 );
71 
80  GLOOX_API const std::string _lookup2( unsigned code, const char* values[],
81  unsigned size, const std::string& def = EmptyString );
82 
88  std::string hex( const std::string& input );
89 
95  template< typename T, typename F >
96  inline void ForEach( T& t, F f )
97  {
98  for( typename T::iterator it = t.begin(); it != t.end(); ++it )
99  ( (*it)->*f )();
100  }
101 
109  template< typename T, typename F, typename D >
110  inline void ForEach( T& t, F f, D& d )
111  {
112  for( typename T::iterator it = t.begin(); it != t.end(); ++it )
113  ( (*it)->*f )( d );
114  }
115 
124  template< typename T, typename F, typename D1, typename D2 >
125  inline void ForEach( T& t, F f, D1& d1, D2& d2 )
126  {
127  for( typename T::iterator it = t.begin(); it != t.end(); ++it )
128  ( (*it)->*f )( d1, d2 );
129  }
130 
140  template< typename T, typename F, typename D1, typename D2, typename D3 >
141  inline void ForEach( T& t, F f, D1& d1, D2& d2, D3& d3 )
142  {
143  for( typename T::iterator it = t.begin(); it != t.end(); ++it )
144  ( (*it)->*f )( d1, d2, d3 );
145  }
146 
151  template< typename T >
152  inline void clearList( std::list< T* >& L )
153  {
154  typename std::list< T* >::iterator it = L.begin();
155  typename std::list< T* >::iterator it2;
156  while( it != L.end() )
157  {
158  it2 = it++;
159  delete (*it2);
160  L.erase( it2 );
161  }
162  }
163 
168  template< typename Key, typename T >
169  inline void clearMap( std::map< Key, T* >& M )
170  {
171  typename std::map< Key, T* >::iterator it = M.begin();
172  typename std::map< Key, T* >::iterator it2;
173  while( it != M.end() )
174  {
175  it2 = it++;
176  delete (*it2).second;
177  M.erase( it2 );
178  }
179  }
180 
186  template< typename Key, typename T >
187  inline void clearMap( std::map< const Key, T* >& M )
188  {
189  typename std::map< const Key, T* >::iterator it = M.begin();
190  typename std::map< const Key, T* >::iterator it2;
191  while( it != M.end() )
192  {
193  it2 = it++;
194  delete (*it2).second;
195  M.erase( it2 );
196  }
197  }
198 
206  GLOOX_API const std::string escape( std::string what );
207 
217  GLOOX_API void appendEscaped( std::string& target, const std::string& data );
218 
224  GLOOX_API bool checkValidXMLChars( const std::string& data );
225 
231  GLOOX_API int internalLog2( unsigned int n );
232 
242  GLOOX_API void replaceAll( std::string& target, const std::string& find, const std::string& replace );
243 
250  static inline const std::string long2string( long int value, const int base = 10 )
251  {
252  if( base < 2 || base > 16 || value == 0 )
253  return "0";
254 
255  std::string output;
256  std::string sign;
257 
258  if( value < 0 )
259  {
260  sign += "-";
261  value = -value;
262  }
263 
264  while( output.empty() || value > 0 )
265  {
266  output.insert( 0, 1, static_cast<char>( value % base + '0' ) );
267  value /= base;
268  }
269 
270  return sign + output;
271  }
272 
278  static inline const std::string int2string( int value )
279  {
280  return long2string( value );
281  }
282 
283  }
284 
285 }
286 
287 #endif // UTIL_H__