/usr/include/gloox/util.h is in libgloox-dev 1.0.9-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | /*
Copyright (c) 2007-2013 by Jakob Schroeter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#ifndef UTIL_H__
#define UTIL_H__
#include "gloox.h"
#include <cmath>
#include <algorithm>
#include <string>
#include <list>
#include <map>
#include <cstdlib>
namespace gloox
{
/**
* @brief A namespace holding a couple utility functions.
*/
namespace util
{
#define lookup( a, b ) _lookup( a, b, sizeof(b)/sizeof(char*) )
#define lookup2( a, b ) _lookup2( a, b, sizeof(b)/sizeof(char*) )
#define deflookup( a, b, c ) _lookup( a, b, sizeof(b)/sizeof(char*), c )
#define deflookup2( a, b, c ) _lookup2( a, b, sizeof(b)/sizeof(char*), c )
/**
* Finds the enumerated value associated with a string value.
* @param str String to search for.
* @param values Array of String/Code pairs to look into.
* @param size The array's size.
* @param def Default value returned in case the lookup failed.
* @return The associated enum code.
*/
GLOOX_API unsigned _lookup( const std::string& str, const char* values[],
unsigned size, int def = -1 );
/**
* Finds the string associated with an enumerated type.
* @param code Code of the string to search for.
* @param values Array of String/Code pairs to look into.
* @param size The array's size.
* @param def Default value returned in case the lookup failed.
* @return The associated string (empty in case there's no match).
*/
GLOOX_API const std::string _lookup( unsigned code, const char* values[],
unsigned size, const std::string& def = EmptyString );
/**
* Finds the ORable enumerated value associated with a string value.
* @param str String to search for.
* @param values Array of String/Code pairs to look into.
* @param size The array's size.
* @param def The default value to return if the lookup failed.
* @return The associated enum code.
*/
GLOOX_API unsigned _lookup2( const std::string& str, const char* values[],
unsigned size, int def = -1 );
/**
* Finds the string associated with an ORable enumerated type.
* @param code Code of the string to search for.
* @param values Array of String/Code pairs to look into.
* @param size The array's size.
* @param def The default value to return if the lookup failed.
* @return The associated string (empty in case there's no match).
*/
GLOOX_API const std::string _lookup2( unsigned code, const char* values[],
unsigned size, const std::string& def = EmptyString );
/**
* Returns the input string in hex notation.
* @param input The (binary) input string.
* @return The input string in hex notation.
*/
std::string hex( const std::string& input );
/**
* A convenience function that executes the given function on each object in a given list.
* @param t The object to execute the function on.
* @param f The function to execute.
*/
template< typename T, typename F >
inline void ForEach( T& t, F f )
{
for( typename T::iterator it = t.begin(); it != t.end(); ++it )
( (*it)->*f )();
}
/**
* A convenience function that executes the given function on each object in a given list,
* passing the given argument.
* @param t The object to execute the function on.
* @param f The function to execute.
* @param d An argument to pass to the function.
*/
template< typename T, typename F, typename D >
inline void ForEach( T& t, F f, D& d )
{
for( typename T::iterator it = t.begin(); it != t.end(); ++it )
( (*it)->*f )( d );
}
/**
* A convenience function that executes the given function on each object in a given list,
* passing the given arguments.
* @param t The object to execute the function on.
* @param f The function to execute.
* @param d1 An argument to pass to the function.
* @param d2 An argument to pass to the function.
*/
template< typename T, typename F, typename D1, typename D2 >
inline void ForEach( T& t, F f, D1& d1, D2& d2 )
{
for( typename T::iterator it = t.begin(); it != t.end(); ++it )
( (*it)->*f )( d1, d2 );
}
/**
* A convenience function that executes the given function on each object in a given list,
* passing the given arguments.
* @param t The object to execute the function on.
* @param f The function to execute.
* @param d1 An argument to pass to the function.
* @param d2 An argument to pass to the function.
* @param d3 An argument to pass to the function.
*/
template< typename T, typename F, typename D1, typename D2, typename D3 >
inline void ForEach( T& t, F f, D1& d1, D2& d2, D3& d3 )
{
for( typename T::iterator it = t.begin(); it != t.end(); ++it )
( (*it)->*f )( d1, d2, d3 );
}
/**
* Delete all elements from a list of pointers.
* @param L List of pointers to delete.
*/
template< typename T >
inline void clearList( std::list< T* >& L )
{
typename std::list< T* >::iterator it = L.begin();
typename std::list< T* >::iterator it2;
while( it != L.end() )
{
it2 = it++;
delete (*it2);
L.erase( it2 );
}
}
/**
* Delete all associated values from a map (not the key elements).
* @param M Map of pointer values to delete.
*/
template< typename Key, typename T >
inline void clearMap( std::map< Key, T* >& M )
{
typename std::map< Key, T* >::iterator it = M.begin();
typename std::map< Key, T* >::iterator it2;
while( it != M.end() )
{
it2 = it++;
delete (*it2).second;
M.erase( it2 );
}
}
/**
* Delete all associated values from a map (not the key elements).
* Const key type version.
* @param M Map of pointer values to delete.
*/
template< typename Key, typename T >
inline void clearMap( std::map< const Key, T* >& M )
{
typename std::map< const Key, T* >::iterator it = M.begin();
typename std::map< const Key, T* >::iterator it2;
while( it != M.end() )
{
it2 = it++;
delete (*it2).second;
M.erase( it2 );
}
}
/**
* Does some fancy escaping. (& --> &amp;, etc).
* @note If you intend to append the result of escape
* to another string, use the faster appendEscaped.
* @param what A string to escape.
* @return The escaped string.
*/
GLOOX_API const std::string escape( std::string what );
/**
* Append the data to the target, doing any necessary escaping
* along the way (& --> &amp;, etc).
* This method is faster than calling "escape" and appending the
* return value, especially for source strings that don't need
* any escaping.
* @param target The string to append the data to.
* @param data The string to append that might need escaping.
*/
GLOOX_API void appendEscaped( std::string& target, const std::string& data );
/**
* Checks whether the given input is valid UTF-8.
* @param data The data to check for validity.
* @return @@b True if the input is valid UTF-8, @b false otherwise.
*/
GLOOX_API bool checkValidXMLChars( const std::string& data );
/**
* Custom log2() implementation.
* @param n Figure to take the logarithm from.
* @return The logarithm to the basis of 2.
*/
GLOOX_API int internalLog2( unsigned int n );
/**
* Replace all instances of one substring of arbitrary length
* with another substring of arbitrary length. Replacement happens
* in place (so make a copy first if you don't want the original modified).
* @param target The string to process. Changes are made "in place".
* @param find The sub-string to find within the target string
* @param replace The sub-string to substitute for the find string.
* @todo Look into merging with util::escape() and Parser::decode().
*/
GLOOX_API void replaceAll( std::string& target, const std::string& find, const std::string& replace );
/**
* Converts a long int to its string representation.
* @param value The long integer value.
* @param base The integer's base.
* @return The long int's string represenation.
*/
static inline const std::string long2string( long int value, const int base = 10 )
{
if( base < 2 || base > 16 || value == 0 )
return "0";
std::string output;
std::string sign;
if( value < 0 )
{
sign += "-";
value = -value;
}
while( output.empty() || value > 0 )
{
output.insert( 0, 1, static_cast<char>( value % base + '0' ) );
value /= base;
}
return sign + output;
}
/**
* Converts an int to its string representation.
* @param value The integer value.
* @return The int's string represenation.
*/
static inline const std::string int2string( int value )
{
return long2string( value );
}
}
}
#endif // UTIL_H__
|