util.h
/*
Copyright (c) 2007-2023 by Jakob Schröter <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
{
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 )
GLOOX_API unsigned _lookup( const std::string& str, const char* values[],
unsigned size, int def = -1 );
GLOOX_API const std::string _lookup( unsigned code, const char* values[],
unsigned size, const std::string& def = EmptyString );
GLOOX_API unsigned _lookup2( const std::string& str, const char* values[],
unsigned size, int def = -1 );
GLOOX_API const std::string _lookup2( unsigned code, const char* values[],
unsigned size, const std::string& def = EmptyString );
std::string hex( const std::string& input );
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 )();
}
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 );
}
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 );
}
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 );
}
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 );
}
}
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 );
}
}
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 );
}
}
GLOOX_API const std::string escape( std::string what );
GLOOX_API void appendEscaped( std::string& target, const std::string& data );
GLOOX_API bool checkValidXMLChars( const std::string& data );
GLOOX_API int internalLog2( unsigned int n );
GLOOX_API void replaceAll( std::string& target, const std::string& find, const std::string& replace );
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( static_cast<size_t>( 0 ), static_cast<size_t>( 1 ), static_cast<char>( value % base + '0' ) );
value /= base;
}
return sign + output;
}
static inline const std::string int2string( int value )
{
return long2string( value );
}
}
}
#endif // UTIL_H__
gloox::util
A namespace holding a couple utility functions.
Definition atomicrefcount.cpp:36
gloox::util::internalLog2
int internalLog2(unsigned int n)
Definition util.cpp:24
gloox::util::_lookup
unsigned _lookup(const std::string &str, const char *values[], unsigned size, int def)
Definition util.cpp:35
gloox::util::_lookup2
unsigned _lookup2(const std::string &str, const char *values[], unsigned size, int def)
Definition util.cpp:48
gloox::util::clearList
void clearList(std::list< T * > &L)
Definition util.h:152
gloox::util::clearMap
void clearMap(std::map< Key, T * > &M)
Definition util.h:169
gloox::util::appendEscaped
void appendEscaped(std::string &target, const std::string &data)
Definition util.cpp:95
gloox::util::replaceAll
void replaceAll(std::string &target, const std::string &find, const std::string &replace)
Definition util.cpp:177
gloox::util::checkValidXMLChars
bool checkValidXMLChars(const std::string &data)
Definition util.cpp:141
gloox::util::hex
std::string hex(const std::string &input)
Definition util.cpp:60
gloox::util::ForEach
void ForEach(T &t, F f)
Definition util.h:96
gloox::util::escape
const std::string escape(std::string what)
Definition util.cpp:77
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::EmptyString
const std::string EmptyString
Definition gloox.cpp:124