fixed: in Space: pointers 'name' and 'child_spaces' were not correctly initialized in cctors
added in Space: - some methods for adding values to an object, such as: Space & Space::add(const std::wstring & field, bool val) (bool, short, int, long, long long etc.) - methods for creating lists: void Space::to_list(std::list<std::string> & output_list, bool clear_list) const bool Space::to_list(const wchar_t * field, std::list<std::string> & output_list, bool clear_list) const - methods for converting a value from an object field: bool Space::to_bool(const wchar_t * field, bool default_value) const - methods for testing strings: bool Space::is_equal(const char * val) const bool Space::is_equal(const std::string & val) const bool Space::is_equal(const wchar_t * val) const bool Space::is_equal(const std::wstring & val) const - methods to get the raw pointer to a value from an object, such as: bool * Space::get_bool(const wchar_t * field) float * Space::get_float(const wchar_t * field) - methods for finding a child space (used in Space format only) Space * Space::find_child_space(const wchar_t * name) Space & Space::find_add_child_space(const wchar_t * name)
This commit is contained in:
349
space/space.h
349
space/space.h
@@ -39,13 +39,14 @@
|
||||
#define headerfile_picotools_space_space
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <cstdio>
|
||||
#include <cwchar>
|
||||
#include "textstream/types.h"
|
||||
#include "convert/inttostr.h"
|
||||
//#include "utf8/utf8.h"
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -251,6 +252,9 @@ public:
|
||||
Space & add(const std::string & val);
|
||||
Space & add(const std::wstring & val);
|
||||
Space & add(const Space * space);
|
||||
Space & add_empty_space(); // IMPROVEME rename me to something better
|
||||
|
||||
|
||||
|
||||
// add a value to the object, change to object if needed, return the reference to the new inserted item
|
||||
Space & add(const wchar_t * field, bool val);
|
||||
@@ -271,10 +275,51 @@ public:
|
||||
Space & add(const wchar_t * field, const Space * space);
|
||||
Space & add_empty_space(const wchar_t * field); // IMPROVEME rename me to something better
|
||||
|
||||
Space & add(const std::wstring & field, bool val);
|
||||
Space & add(const std::wstring & field, short val);
|
||||
Space & add(const std::wstring & field, int val);
|
||||
Space & add(const std::wstring & field, long val);
|
||||
Space & add(const std::wstring & field, long long val);
|
||||
Space & add(const std::wstring & field, unsigned short val);
|
||||
Space & add(const std::wstring & field, unsigned int val);
|
||||
Space & add(const std::wstring & field, unsigned long val);
|
||||
Space & add(const std::wstring & field, unsigned long long val);
|
||||
Space & add(const std::wstring & field, float val);
|
||||
Space & add(const std::wstring & field, double val);
|
||||
Space & add(const std::wstring & field, const char * val);
|
||||
Space & add(const std::wstring & field, const wchar_t * val);
|
||||
Space & add(const std::wstring & field, const std::string & val);
|
||||
Space & add(const std::wstring & field, const std::wstring & val);
|
||||
Space & add(const std::wstring & field, const Space * space);
|
||||
Space & add_empty_space(const std::wstring & field); // IMPROVEME rename me to something better
|
||||
|
||||
|
||||
// for child spaces (used only in Space format)
|
||||
// rename to something better
|
||||
Space & add_child_space(const wchar_t * space_name);
|
||||
|
||||
|
||||
// IMPROVEME add a similar 'set' method and cctor
|
||||
template<typename StreamType>
|
||||
Space & add_stream(const wchar_t * field, StreamType & str)
|
||||
{
|
||||
std::wstring temp;
|
||||
str.to_string(temp);
|
||||
|
||||
return add(field, temp);
|
||||
}
|
||||
|
||||
template<typename StreamType>
|
||||
Space & add_stream(const std::wstring & field, StreamType & str)
|
||||
{
|
||||
std::wstring temp;
|
||||
str.to_string(temp);
|
||||
|
||||
return add(field, temp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool is_null() const;
|
||||
bool is_bool() const;
|
||||
bool is_llong() const;
|
||||
@@ -304,6 +349,59 @@ public:
|
||||
std::string to_str() const;
|
||||
std::wstring to_wstr() const;
|
||||
|
||||
void to_list(std::list<std::string> & output_list, bool clear_list = true) const;
|
||||
void to_list(std::list<std::wstring> & output_list, bool clear_list = true) const;
|
||||
void to_list(std::vector<std::string> & output_list, bool clear_list = true) const;
|
||||
void to_list(std::vector<std::wstring> & output_list, bool clear_list = true) const;
|
||||
|
||||
|
||||
// returns value from object, field is a key
|
||||
bool to_bool(const wchar_t * field, bool default_value = false) const;
|
||||
short to_short(const wchar_t * field, short default_value = 0) const;
|
||||
int to_int(const wchar_t * field, int default_value = 0) const;
|
||||
long to_long(const wchar_t * field, long default_value = 0) const;
|
||||
long long to_llong(const wchar_t * field, long long default_value = 0) const;
|
||||
long long to_long_long(const wchar_t * field, long long default_value = 0) const;
|
||||
unsigned short to_ushort(const wchar_t * field, unsigned short default_value = 0) const;
|
||||
unsigned int to_uint(const wchar_t * field, unsigned int default_value = 0) const;
|
||||
unsigned long to_ulong(const wchar_t * field, unsigned long default_value = 0) const;
|
||||
unsigned long long to_ullong(const wchar_t * field, unsigned long long default_value = 0) const;
|
||||
unsigned long long to_ulong_long(const wchar_t * field, unsigned long long default_value = 0) const;
|
||||
std::string to_str(const wchar_t * field, const char * default_value = nullptr) const;
|
||||
std::string to_str(const wchar_t * field, const std::string & default_value) const;
|
||||
std::wstring to_wstr(const wchar_t * field, const wchar_t * default_value = nullptr) const;
|
||||
std::wstring to_wstr(const wchar_t * field, const std::wstring & default_value) const;
|
||||
|
||||
bool to_list(const wchar_t * field, std::list<std::string> & output_list, bool clear_list = true) const;
|
||||
bool to_list(const wchar_t * field, std::list<std::wstring> & output_list, bool clear_list = true) const;
|
||||
bool to_list(const std::wstring & field, std::list<std::string> & output_list, bool clear_list = true) const;
|
||||
bool to_list(const std::wstring & field, std::list<std::wstring> & output_list, bool clear_list = true) const;
|
||||
|
||||
bool to_list(const wchar_t * field, std::vector<std::string> & output_list, bool clear_list = true) const;
|
||||
bool to_list(const wchar_t * field, std::vector<std::wstring> & output_list, bool clear_list = true) const;
|
||||
bool to_list(const std::wstring & field, std::vector<std::string> & output_list, bool clear_list = true) const;
|
||||
bool to_list(const std::wstring & field, std::vector<std::wstring> & output_list, bool clear_list = true) const;
|
||||
|
||||
// returns value from object, field is a key
|
||||
bool to_bool(const std::wstring & field, bool default_value = false) const;
|
||||
short to_short(const std::wstring & field, short default_value = 0) const;
|
||||
int to_int(const std::wstring & field, int default_value = 0) const;
|
||||
long to_long(const std::wstring & field, long default_value = 0) const;
|
||||
long long to_llong(const std::wstring & field, long long default_value = 0) const;
|
||||
long long to_long_long(const std::wstring & field, long long default_value = 0) const;
|
||||
unsigned short to_ushort(const std::wstring & field, unsigned short default_value = 0) const;
|
||||
unsigned int to_uint(const std::wstring & field, unsigned int default_value = 0) const;
|
||||
unsigned long to_ulong(const std::wstring & field, unsigned long default_value = 0) const;
|
||||
unsigned long long to_ullong(const std::wstring & field, unsigned long long default_value = 0) const;
|
||||
unsigned long long to_ulong_long(const std::wstring & field, unsigned long long default_value = 0) const;
|
||||
std::string to_str(const std::wstring & field, const char * default_value = nullptr) const;
|
||||
std::string to_str(const std::wstring & field, const std::string & default_value) const;
|
||||
std::wstring to_wstr(const std::wstring & field, const wchar_t * default_value = nullptr) const;
|
||||
std::wstring to_wstr(const std::wstring & field, const std::wstring & default_value) const;
|
||||
|
||||
|
||||
bool to_str_list(std::list<std::string> & output_list) const;
|
||||
bool to_wstr_list(std::list<std::wstring> & output_list) const;
|
||||
|
||||
|
||||
bool * get_bool();
|
||||
@@ -311,11 +409,36 @@ public:
|
||||
long long * get_long_long();
|
||||
float * get_float();
|
||||
double * get_double();
|
||||
std::string * get_string();
|
||||
std::wstring * get_wstring();
|
||||
std::string * get_str();
|
||||
std::wstring * get_wstr();
|
||||
ObjectType * get_object();
|
||||
TableType * get_table();
|
||||
|
||||
bool is_equal(const char * val) const;
|
||||
bool is_equal(const std::string & val) const;
|
||||
bool is_equal(const wchar_t * val) const;
|
||||
bool is_equal(const std::wstring & val) const;
|
||||
// add these is_equal with std::wstring
|
||||
|
||||
|
||||
// what about getters from tables?
|
||||
// may something like: Space * get_table_item(size_t index)?
|
||||
// and get_table_bool(size_t index)
|
||||
// or just only get_bool(size_t index)?
|
||||
// size_t argument will be only for tables, wchar_t* or std::wstring for objects?
|
||||
|
||||
// getters from object
|
||||
Space * get_object_field(const wchar_t * field); // may a better name?
|
||||
bool * get_bool(const wchar_t * field);
|
||||
long long * get_llong(const wchar_t * field);
|
||||
long long * get_long_long(const wchar_t * field);
|
||||
float * get_float(const wchar_t * field);
|
||||
double * get_double(const wchar_t * field);
|
||||
std::string * get_str(const wchar_t * field);
|
||||
std::wstring * get_wstr(const wchar_t * field);
|
||||
ObjectType * get_object(const wchar_t * field);
|
||||
TableType * get_table(const wchar_t * field);
|
||||
// add these getters with std::wstring
|
||||
|
||||
|
||||
const bool * get_bool() const;
|
||||
@@ -323,11 +446,32 @@ public:
|
||||
const long long * get_long_long() const;
|
||||
const float * get_float() const;
|
||||
const double * get_double() const;
|
||||
const std::string * get_string() const;
|
||||
const std::wstring * get_wstring() const;
|
||||
const std::string * get_str() const;
|
||||
const std::wstring * get_wstr() const;
|
||||
const ObjectType * get_object() const;
|
||||
const TableType * get_table() const;
|
||||
|
||||
bool has_key(const wchar_t * field) const; // may has_key() would be a better name?
|
||||
bool has_key(const std::wstring & field) const;
|
||||
|
||||
const Space * get_object_field(const wchar_t * field) const; // may a better name?
|
||||
const Space * get_object_field(const std::wstring & field) const; // may a better name?
|
||||
|
||||
const bool * get_bool(const wchar_t * field) const;
|
||||
const long long * get_llong(const wchar_t * field) const;
|
||||
const long long * get_long_long(const wchar_t * field) const;
|
||||
const float * get_float(const wchar_t * field) const;
|
||||
const double * get_double(const wchar_t * field) const;
|
||||
const std::string * get_str(const wchar_t * field) const;
|
||||
const std::wstring * get_wstr(const wchar_t * field) const;
|
||||
const ObjectType * get_object(const wchar_t * field) const;
|
||||
const TableType * get_table(const wchar_t * field) const;
|
||||
// add these getters with std::wstring
|
||||
|
||||
|
||||
// remove a field from an object
|
||||
void remove(const wchar_t * field);
|
||||
void remove(const std::wstring & field);
|
||||
|
||||
|
||||
std::string serialize_to_space_str(bool pretty_print = false) const;
|
||||
@@ -353,7 +497,7 @@ public:
|
||||
|
||||
|
||||
template<typename StreamType>
|
||||
void serialize_to_json_stream(StreamType & str) const
|
||||
void serialize_to_json_stream(StreamType & str, bool pretty_print = false) const
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
@@ -396,11 +540,26 @@ public:
|
||||
}
|
||||
|
||||
|
||||
bool is_equal(const wchar_t * field, const char * val) const;
|
||||
bool is_equal(const wchar_t * field, const std::string & val) const;
|
||||
bool is_equal(const wchar_t * field, const wchar_t * val) const;
|
||||
bool is_equal(const wchar_t * field, const std::wstring & val) const;
|
||||
// add these is_equal with std::wstring
|
||||
|
||||
|
||||
Space * find_child_space(const wchar_t * name);
|
||||
Space * find_child_space(const std::wstring & name);
|
||||
const Space * find_child_space(const wchar_t * name) const;
|
||||
const Space * find_child_space(const std::wstring & name) const;
|
||||
|
||||
Space & find_add_child_space(const wchar_t * name);
|
||||
Space & find_add_child_space(const std::wstring & name);
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
template<typename ArgType>
|
||||
Space & add_generic(ArgType val)
|
||||
Space & add_generic(const ArgType & val)
|
||||
{
|
||||
initialize_value_table_if_needed();
|
||||
|
||||
@@ -412,7 +571,7 @@ protected:
|
||||
|
||||
|
||||
template<typename ArgType>
|
||||
Space & add_generic(const wchar_t * field, ArgType val)
|
||||
Space & add_generic(const wchar_t * field, const ArgType & val)
|
||||
{
|
||||
initialize_value_object_if_needed();
|
||||
|
||||
@@ -423,6 +582,13 @@ protected:
|
||||
}
|
||||
|
||||
|
||||
template<typename ArgType>
|
||||
Space & add_generic(const std::wstring & field, const ArgType & val)
|
||||
{
|
||||
return add_generic(field.c_str(), val);
|
||||
}
|
||||
|
||||
|
||||
template<typename ArgType>
|
||||
ArgType to_generic_numeric_signed_value() const
|
||||
{
|
||||
@@ -452,6 +618,129 @@ protected:
|
||||
unsigned long long convert_wstring_to_ulong_long() const;
|
||||
|
||||
|
||||
template<typename ListType>
|
||||
void to_list_str_generic(ListType & output_list, bool clear_list) const
|
||||
{
|
||||
if( clear_list )
|
||||
output_list.clear();
|
||||
|
||||
if( type == type_string )
|
||||
{
|
||||
output_list.push_back(value.value_string);
|
||||
}
|
||||
else
|
||||
if( type == type_table )
|
||||
{
|
||||
for(size_t i = 0 ; i < value.value_table.size() ; ++i)
|
||||
{
|
||||
output_list.push_back(value.value_table[i]->to_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
output_list.push_back(to_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ListType>
|
||||
void to_list_wstr_generic(ListType & output_list, bool clear_list) const
|
||||
{
|
||||
if( clear_list )
|
||||
output_list.clear();
|
||||
|
||||
if( type == type_wstring )
|
||||
{
|
||||
output_list.push_back(value.value_wstring);
|
||||
}
|
||||
else
|
||||
if( type == type_table )
|
||||
{
|
||||
for(size_t i = 0 ; i < value.value_table.size() ; ++i)
|
||||
{
|
||||
output_list.push_back(value.value_table[i]->to_wstr());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
output_list.push_back(to_wstr());
|
||||
}
|
||||
}
|
||||
|
||||
template<typename ListType>
|
||||
bool to_list_generic(const wchar_t * field, ListType & output_list, bool clear_list) const
|
||||
{
|
||||
if( clear_list )
|
||||
output_list.clear();
|
||||
|
||||
const Space * space = get_object_field(field);
|
||||
|
||||
if( space )
|
||||
{
|
||||
space->to_list(output_list, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<typename ListType>
|
||||
bool to_list_generic(const std::wstring & field, ListType & output_list, bool clear_list) const
|
||||
{
|
||||
if( clear_list )
|
||||
output_list.clear();
|
||||
|
||||
const Space * space = get_object_field(field);
|
||||
|
||||
if( space )
|
||||
{
|
||||
space->to_list(output_list, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<typename ArgType>
|
||||
Space * find_child_space_generic(const ArgType & name)
|
||||
{
|
||||
if( child_spaces )
|
||||
{
|
||||
for(size_t i = 0 ; i < child_spaces->size() ; ++i)
|
||||
{
|
||||
Space * space = (*child_spaces)[i];
|
||||
|
||||
if( space->name && *space->name == name )
|
||||
{
|
||||
return space;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
template<typename ArgType>
|
||||
Space * find_child_space_generic(const ArgType & name) const
|
||||
{
|
||||
if( child_spaces )
|
||||
{
|
||||
for(size_t i = 0 ; i < child_spaces->size() ; ++i)
|
||||
{
|
||||
Space * space = (*child_spaces)[i];
|
||||
|
||||
if( space->name && *space->name == name )
|
||||
{
|
||||
return space;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
template<typename StreamType>
|
||||
@@ -531,11 +820,14 @@ protected:
|
||||
}
|
||||
else
|
||||
{
|
||||
StreamType temp_stream;
|
||||
|
||||
// input is utf8 but output is wide
|
||||
UTF8ToWide(input_str, temp_stream, false);
|
||||
copy_input_stream_to_output(temp_stream, out_str, escape);
|
||||
copy_input_string_to_output(input_str, out_str, escape); // temporarily
|
||||
|
||||
// !!!!!!!!!!!!!!!!!!! FIXME
|
||||
// StreamType temp_stream;
|
||||
// UTF8ToWide(input_str, temp_stream, false);
|
||||
//
|
||||
// copy_input_stream_to_output(temp_stream, out_str, escape);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -862,38 +1154,6 @@ protected:
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
///////////////////////////// remove me
|
||||
|
||||
template<class Stream>
|
||||
void Serialize(Stream & out, bool use_indents = false, bool use_comments = false, int level = 0) const
|
||||
{
|
||||
}
|
||||
|
||||
template<class Stream>
|
||||
void SerializeTableMulti(Stream & out, bool use_indents, int level) const
|
||||
{
|
||||
}
|
||||
|
||||
template<class Stream, class StringType>
|
||||
static void PrintValue(Stream & out, const StringType & str, bool use_quote = true)
|
||||
{
|
||||
}
|
||||
|
||||
template<class Stream>
|
||||
static void PrintKey(Stream & out, const std::wstring & str)
|
||||
{
|
||||
}
|
||||
|
||||
template<class Stream>
|
||||
static void PrintLevel(Stream & out, bool use_indents, int level)
|
||||
{
|
||||
}
|
||||
/////////////////////////////
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
template<typename StreamType>
|
||||
@@ -998,6 +1258,7 @@ protected:
|
||||
void copy_value_object(const Value & value_from);
|
||||
void copy_value_table(const Value & value_from);
|
||||
|
||||
void initialize();
|
||||
void initialize_value_null_if_needed();
|
||||
void initialize_value_bool_if_needed();
|
||||
void initialize_value_long_if_needed();
|
||||
|
Reference in New Issue
Block a user