2016-01-18 15:33:29 +00:00
// Copyright (c) 2011-2016 The Cryptonote developers
2015-09-18 11:55:31 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-05-27 12:08:46 +00:00
# pragma once
# include <cstdint>
# include <sstream>
# include <stdexcept>
# include <vector>
namespace Common {
2015-07-30 15:22:07 +00:00
std : : string asString ( const void * data , size_t size ) ; // Does not throw
2015-05-27 12:08:46 +00:00
std : : string asString ( const std : : vector < uint8_t > & data ) ; // Does not throw
2015-07-30 15:22:07 +00:00
std : : vector < uint8_t > asBinaryArray ( const std : : string & data ) ;
2015-05-27 12:08:46 +00:00
uint8_t fromHex ( char character ) ; // Returns value of hex 'character', throws on error
bool fromHex ( char character , uint8_t & value ) ; // Assigns value of hex 'character' to 'value', returns false on error, does not throw
2015-07-30 15:22:07 +00:00
size_t fromHex ( const std : : string & text , void * data , size_t bufferSize ) ; // Assigns values of hex 'text' to buffer 'data' up to 'bufferSize', returns actual data size, throws on error
bool fromHex ( const std : : string & text , void * data , size_t bufferSize , size_t & size ) ; // Assigns values of hex 'text' to buffer 'data' up to 'bufferSize', assigns actual data size to 'size', returns false on error, does not throw
2015-05-27 12:08:46 +00:00
std : : vector < uint8_t > fromHex ( const std : : string & text ) ; // Returns values of hex 'text', throws on error
bool fromHex ( const std : : string & text , std : : vector < uint8_t > & data ) ; // Appends values of hex 'text' to 'data', returns false on error, does not throw
template < typename T >
bool podFromHex ( const std : : string & text , T & val ) {
2015-07-30 15:22:07 +00:00
size_t outSize ;
2015-05-27 12:08:46 +00:00
return fromHex ( text , & val , sizeof ( val ) , outSize ) & & outSize = = sizeof ( val ) ;
}
2015-07-30 15:22:07 +00:00
std : : string toHex ( const void * data , size_t size ) ; // Returns hex representation of ('data', 'size'), does not throw
void toHex ( const void * data , size_t size , std : : string & text ) ; // Appends hex representation of ('data', 'size') to 'text', does not throw
2015-05-27 12:08:46 +00:00
std : : string toHex ( const std : : vector < uint8_t > & data ) ; // Returns hex representation of 'data', does not throw
void toHex ( const std : : vector < uint8_t > & data , std : : string & text ) ; // Appends hex representation of 'data' to 'text', does not throw
template < class T >
std : : string podToHex ( const T & s ) {
return toHex ( & s , sizeof ( s ) ) ;
}
std : : string extract ( std : : string & text , char delimiter ) ; // Does not throw
2015-07-30 15:22:07 +00:00
std : : string extract ( const std : : string & text , char delimiter , size_t & offset ) ; // Does not throw
2015-05-27 12:08:46 +00:00
template < typename T > T fromString ( const std : : string & text ) { // Throws on error
T value ;
std : : istringstream stream ( text ) ;
stream > > value ;
if ( stream . fail ( ) ) {
throw std : : runtime_error ( " fromString: unable to parse value " ) ;
}
return value ;
}
template < typename T > bool fromString ( const std : : string & text , T & value ) { // Does not throw
std : : istringstream stream ( text ) ;
stream > > value ;
return ! stream . fail ( ) ;
}
template < typename T > std : : vector < T > fromDelimitedString ( const std : : string & source , char delimiter ) { // Throws on error
std : : vector < T > data ;
2015-07-30 15:22:07 +00:00
for ( size_t offset = 0 ; offset ! = source . size ( ) ; ) {
2015-05-27 12:08:46 +00:00
data . emplace_back ( fromString < T > ( extract ( source , delimiter , offset ) ) ) ;
}
return data ;
}
template < typename T > bool fromDelimitedString ( const std : : string & source , char delimiter , std : : vector < T > & data ) { // Does not throw
2015-07-30 15:22:07 +00:00
for ( size_t offset = 0 ; offset ! = source . size ( ) ; ) {
2015-05-27 12:08:46 +00:00
T value ;
if ( ! fromString < T > ( extract ( source , delimiter , offset ) , value ) ) {
return false ;
}
data . emplace_back ( value ) ;
}
return true ;
}
template < typename T > std : : string toString ( const T & value ) { // Does not throw
std : : ostringstream stream ;
stream < < value ;
return stream . str ( ) ;
}
template < typename T > void toString ( const T & value , std : : string & text ) { // Does not throw
std : : ostringstream stream ;
stream < < value ;
text + = stream . str ( ) ;
}
bool loadFileToString ( const std : : string & filepath , std : : string & buf ) ;
bool saveStringToFile ( const std : : string & filepath , const std : : string & buf ) ;
std : : string base64Decode ( std : : string const & encoded_string ) ;
std : : string ipAddressToString ( uint32_t ip ) ;
bool parseIpAddressAndPort ( uint32_t & ip , uint32_t & port , const std : : string & addr ) ;
std : : string timeIntervalToString ( uint64_t intervalInSeconds ) ;
}