@ -24,20 +24,12 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
# pragma once
# include <type_traits>
# include "misc_language.h"
# include "portable_storage_base.h"
# include "portable_storage_from_bin.h"
# include "portable_storage_to_json.h"
# include "portable_storage_from_json.h"
# include "portable_storage_val_converters.h"
# include "misc_log_ex.h"
# include "span.h"
# include "int-util.h"
namespace epee
{
@ -92,7 +84,7 @@ namespace epee
//-------------------------------------------------------------------------------
bool store_to_binary ( byte_slice & target , std : : size_t initial_buffer_size = 8192 ) ;
bool load_from_binary ( const epee : : span < const uint8_t > target , const limits_t * limits = NULL ) ;
bool load_from_binary ( const std : : string & target , const limits_t * limits = NULL ) { return load_from_binary ( epee : : strspan < uint8_t > ( target ) , limits ) ; }
bool load_from_binary ( const std : : string & target , const limits_t * limits = NULL ) ;
template < class trace_policy >
bool dump_as_xml ( std : : string & targetObj , const std : : string & root_name = " " ) ;
bool dump_as_json ( std : : string & targetObj , size_t indent = 0 , bool insert_newlines = true ) ;
@ -117,85 +109,13 @@ namespace epee
} ;
# pragma pack(pop)
} ;
inline
bool portable_storage : : dump_as_json ( std : : string & buff , size_t indent , bool insert_newlines )
{
TRY_ENTRY ( ) ;
std : : stringstream ss ;
epee : : serialization : : dump_as_json ( ss , m_root , indent , insert_newlines ) ;
buff = ss . str ( ) ;
return true ;
CATCH_ENTRY ( " portable_storage::dump_as_json " , false )
}
inline
bool portable_storage : : load_from_json ( const std : : string & source )
{
TRY_ENTRY ( ) ;
return json : : load_from_json ( source , * this ) ;
CATCH_ENTRY ( " portable_storage::load_from_json " , false )
}
template < class trace_policy >
bool portable_storage : : dump_as_xml ( std : : string & targetObj , const std : : string & root_name )
{
return false ; //TODO: don't think i ever again will use xml - ambiguous and "overtagged" format
}
inline
bool portable_storage : : load_from_binary ( const epee : : span < const uint8_t > source , const limits_t * limits )
{
m_root . m_entries . clear ( ) ;
if ( source . size ( ) < sizeof ( storage_block_header ) )
{
LOG_ERROR ( " portable_storage: wrong binary format, packet size = " < < source . size ( ) < < " less than expected sizeof(storage_block_header)= " < < sizeof ( storage_block_header ) ) ;
return false ;
}
storage_block_header * pbuff = ( storage_block_header * ) source . data ( ) ;
if ( pbuff - > m_signature_a ! = SWAP32LE ( PORTABLE_STORAGE_SIGNATUREA ) | |
pbuff - > m_signature_b ! = SWAP32LE ( PORTABLE_STORAGE_SIGNATUREB )
)
{
LOG_ERROR ( " portable_storage: wrong binary format - signature mismatch " ) ;
return false ;
}
if ( pbuff - > m_ver ! = PORTABLE_STORAGE_FORMAT_VER )
{
LOG_ERROR ( " portable_storage: wrong binary format - unknown format ver = " < < pbuff - > m_ver ) ;
return false ;
}
TRY_ENTRY ( ) ;
throwable_buffer_reader buf_reader ( source . data ( ) + sizeof ( storage_block_header ) , source . size ( ) - sizeof ( storage_block_header ) ) ;
if ( limits )
buf_reader . set_limits ( limits - > n_objects , limits - > n_fields , limits - > n_strings ) ;
buf_reader . read ( m_root ) ;
return true ; //TODO:
CATCH_ENTRY ( " portable_storage::load_from_binary " , false ) ;
}
//---------------------------------------------------------------------------------------------------------------
inline
hsection portable_storage : : open_section ( const std : : string & section_name , hsection hparent_section , bool create_if_notexist )
{
TRY_ENTRY ( ) ;
hparent_section = hparent_section ? hparent_section : & m_root ;
storage_entry * pentry = find_storage_entry ( section_name , hparent_section ) ;
if ( ! pentry )
{
if ( ! create_if_notexist )
return nullptr ;
return insert_new_section ( section_name , hparent_section ) ;
}
CHECK_AND_ASSERT ( pentry , nullptr ) ;
//check that section_entry we find is real "CSSection"
if ( pentry - > type ( ) ! = typeid ( section ) )
{
if ( create_if_notexist )
* pentry = storage_entry ( section ( ) ) ; //replace
else
return nullptr ;
}
return & boost : : get < section > ( * pentry ) ;
CATCH_ENTRY ( " portable_storage::open_section " , nullptr ) ;
}
//---------------------------------------------------------------------------------------------------------------
}
template < class to_type >
struct get_value_visitor : boost : : static_visitor < void >
{
@ -221,20 +141,6 @@ namespace epee
//CATCH_ENTRY("portable_storage::template<>get_value", false);
}
//---------------------------------------------------------------------------------------------------------------
inline
bool portable_storage : : get_value ( const std : : string & value_name , storage_entry & val , hsection hparent_section )
{
//TRY_ENTRY();
if ( ! hparent_section ) hparent_section = & m_root ;
storage_entry * pentry = find_storage_entry ( value_name , hparent_section ) ;
if ( ! pentry )
return false ;
val = * pentry ;
return true ;
//CATCH_ENTRY("portable_storage::template<>get_value", false);
}
//---------------------------------------------------------------------------------------------------------------
template < class t_value >
bool portable_storage : : set_value ( const std : : string & value_name , t_value & & v , hsection hparent_section )
{
@ -256,19 +162,6 @@ namespace epee
CATCH_ENTRY ( " portable_storage::template<>set_value " , false ) ;
}
//---------------------------------------------------------------------------------------------------------------
inline
storage_entry * portable_storage : : find_storage_entry ( const std : : string & pentry_name , hsection psection )
{
TRY_ENTRY ( ) ;
CHECK_AND_ASSERT ( psection , nullptr ) ;
auto it = psection - > m_entries . find ( pentry_name ) ;
if ( it = = psection - > m_entries . end ( ) )
return nullptr ;
return & it - > second ;
CATCH_ENTRY ( " portable_storage::find_storage_entry " , nullptr ) ;
}
//---------------------------------------------------------------------------------------------------------------
template < class entry_type >
storage_entry * portable_storage : : insert_new_entry_get_storage_entry ( const std : : string & pentry_name , hsection psection , entry_type & & entry )
{
@ -281,16 +174,6 @@ namespace epee
CATCH_ENTRY ( " portable_storage::insert_new_entry_get_storage_entry " , nullptr ) ;
}
//---------------------------------------------------------------------------------------------------------------
inline
hsection portable_storage : : insert_new_section ( const std : : string & pentry_name , hsection psection )
{
TRY_ENTRY ( ) ;
storage_entry * pse = insert_new_entry_get_storage_entry ( pentry_name , psection , section ( ) ) ;
if ( ! pse ) return nullptr ;
return & boost : : get < section > ( * pse ) ;
CATCH_ENTRY ( " portable_storage::insert_new_section " , nullptr ) ;
}
//---------------------------------------------------------------------------------------------------------------
template < class to_type >
struct get_first_value_visitor : boost : : static_visitor < bool >
{
@ -344,7 +227,6 @@ namespace epee
}
} ;
template < class t_value >
bool portable_storage : : get_next_value ( harray hval_array , t_value & target )
{
@ -402,83 +284,5 @@ namespace epee
return true ;
CATCH_ENTRY ( " portable_storage::insert_next_value " , false ) ;
}
//---------------------------------------------------------------------------------------------------------------
//sections
inline
harray portable_storage : : get_first_section ( const std : : string & sec_name , hsection & h_child_section , hsection hparent_section )
{
TRY_ENTRY ( ) ;
if ( ! hparent_section ) hparent_section = & m_root ;
storage_entry * pentry = find_storage_entry ( sec_name , hparent_section ) ;
if ( ! pentry )
return nullptr ;
if ( pentry - > type ( ) ! = typeid ( array_entry ) )
return nullptr ;
array_entry & ar_entry = boost : : get < array_entry > ( * pentry ) ;
if ( ar_entry . type ( ) ! = typeid ( array_entry_t < section > ) )
return nullptr ;
array_entry_t < section > & sec_array = boost : : get < array_entry_t < section > > ( ar_entry ) ;
section * psec = sec_array . get_first_val ( ) ;
if ( ! psec )
return nullptr ;
h_child_section = psec ;
return & ar_entry ;
CATCH_ENTRY ( " portable_storage::get_first_section " , nullptr ) ;
}
//---------------------------------------------------------------------------------------------------------------
inline
bool portable_storage : : get_next_section ( harray hsec_array , hsection & h_child_section )
{
TRY_ENTRY ( ) ;
CHECK_AND_ASSERT ( hsec_array , false ) ;
if ( hsec_array - > type ( ) ! = typeid ( array_entry_t < section > ) )
return false ;
array_entry_t < section > & sec_array = boost : : get < array_entry_t < section > > ( * hsec_array ) ;
h_child_section = sec_array . get_next_val ( ) ;
if ( ! h_child_section )
return false ;
return true ;
CATCH_ENTRY ( " portable_storage::get_next_section " , false ) ;
}
//---------------------------------------------------------------------------------------------------------------
inline
harray portable_storage : : insert_first_section ( const std : : string & sec_name , hsection & hinserted_childsection , hsection hparent_section )
{
TRY_ENTRY ( ) ;
if ( ! hparent_section ) hparent_section = & m_root ;
storage_entry * pentry = find_storage_entry ( sec_name , hparent_section ) ;
if ( ! pentry )
{
pentry = insert_new_entry_get_storage_entry ( sec_name , hparent_section , array_entry ( array_entry_t < section > ( ) ) ) ;
if ( ! pentry )
return nullptr ;
}
if ( pentry - > type ( ) ! = typeid ( array_entry ) )
* pentry = storage_entry ( array_entry ( array_entry_t < section > ( ) ) ) ;
array_entry & ar_entry = boost : : get < array_entry > ( * pentry ) ;
if ( ar_entry . type ( ) ! = typeid ( array_entry_t < section > ) )
ar_entry = array_entry ( array_entry_t < section > ( ) ) ;
array_entry_t < section > & sec_array = boost : : get < array_entry_t < section > > ( ar_entry ) ;
hinserted_childsection = & sec_array . insert_first_val ( section ( ) ) ;
return & ar_entry ;
CATCH_ENTRY ( " portable_storage::insert_first_section " , nullptr ) ;
}
//---------------------------------------------------------------------------------------------------------------
inline
bool portable_storage : : insert_next_section ( harray hsec_array , hsection & hinserted_childsection )
{
TRY_ENTRY ( ) ;
CHECK_AND_ASSERT ( hsec_array , false ) ;
CHECK_AND_ASSERT_MES ( hsec_array - > type ( ) = = typeid ( array_entry_t < section > ) ,
false , " unexpected type(not 'section') in insert_next_section, type: " < < hsec_array - > type ( ) . name ( ) ) ;
array_entry_t < section > & sec_array = boost : : get < array_entry_t < section > > ( * hsec_array ) ;
hinserted_childsection = & sec_array . insert_next_value ( section ( ) ) ;
return true ;
CATCH_ENTRY ( " portable_storage::insert_next_section " , false ) ;
}
//---------------------------------------------------------------------------------------------------------------
}
}