api2021 part I #4
|
@ -55,18 +55,6 @@ SpaceParser::SpaceParser()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void SpaceParser::SetSpace(Space * pspace)
|
|
||||||
{
|
|
||||||
root_space = pspace;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void SpaceParser::SetSpace(Space & pspace)
|
|
||||||
{
|
|
||||||
root_space = &pspace;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void SpaceParser::SetDefault()
|
void SpaceParser::SetDefault()
|
||||||
{
|
{
|
||||||
// you can change this separators to what you want
|
// you can change this separators to what you want
|
||||||
|
@ -110,17 +98,18 @@ int SpaceParser::get_last_parsed_line()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseJSONFile(const char * file_name)
|
SpaceParser::Status SpaceParser::ParseJSONFile(const char * file_name, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
reading_from_file = true;
|
reading_from_file = true;
|
||||||
parsing_space = false;
|
parsing_space = false;
|
||||||
|
root_space = &out_space;
|
||||||
|
|
||||||
file.clear();
|
file.clear();
|
||||||
file.open(file_name, std::ios_base::binary | std::ios_base::in);
|
file.open(file_name, std::ios_base::binary | std::ios_base::in);
|
||||||
|
|
||||||
if( file )
|
if( file )
|
||||||
{
|
{
|
||||||
ParseRootSpace();
|
ParseRootSpace(clear_space);
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -133,44 +122,45 @@ return status;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseJSONFile(const std::string & file_name)
|
SpaceParser::Status SpaceParser::ParseJSONFile(const std::string & file_name, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
return ParseJSONFile(file_name.c_str());
|
return ParseJSONFile(file_name.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseJSONFile(const wchar_t * file_name)
|
SpaceParser::Status SpaceParser::ParseJSONFile(const wchar_t * file_name, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
std::string file_name_utf8;
|
std::string file_name_utf8;
|
||||||
|
|
||||||
wide_to_utf8(file_name, file_name_utf8);
|
wide_to_utf8(file_name, file_name_utf8);
|
||||||
return ParseJSONFile(file_name_utf8.c_str());
|
return ParseJSONFile(file_name_utf8.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseJSONFile(const std::wstring & file_name)
|
SpaceParser::Status SpaceParser::ParseJSONFile(const std::wstring & file_name, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
return ParseJSONFile(file_name.c_str());
|
return ParseJSONFile(file_name.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseSpaceFile(const char * file_name)
|
SpaceParser::Status SpaceParser::ParseSpaceFile(const char * file_name, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
reading_from_file = true;
|
reading_from_file = true;
|
||||||
parsing_space = true;
|
parsing_space = true;
|
||||||
|
root_space = &out_space;
|
||||||
|
|
||||||
file.clear();
|
file.clear();
|
||||||
file.open(file_name, std::ios_base::binary | std::ios_base::in);
|
file.open(file_name, std::ios_base::binary | std::ios_base::in);
|
||||||
|
|
||||||
if( file )
|
if( file )
|
||||||
{
|
{
|
||||||
ParseRootSpace();
|
ParseRootSpace(clear_space);
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -183,126 +173,129 @@ return status;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseSpaceFile(const std::string & file_name)
|
SpaceParser::Status SpaceParser::ParseSpaceFile(const std::string & file_name, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
return ParseSpaceFile(file_name.c_str());
|
return ParseSpaceFile(file_name.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseSpaceFile(const wchar_t * file_name)
|
SpaceParser::Status SpaceParser::ParseSpaceFile(const wchar_t * file_name, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
std::string file_name_utf8;
|
std::string file_name_utf8;
|
||||||
|
|
||||||
wide_to_utf8(file_name, file_name_utf8);
|
wide_to_utf8(file_name, file_name_utf8);
|
||||||
return ParseSpaceFile(file_name_utf8.c_str());
|
return ParseSpaceFile(file_name_utf8.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseSpaceFile(const std::wstring & file_name)
|
SpaceParser::Status SpaceParser::ParseSpaceFile(const std::wstring & file_name, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
return ParseSpaceFile(file_name.c_str());
|
return ParseSpaceFile(file_name.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseJSON(const char * str)
|
SpaceParser::Status SpaceParser::ParseJSON(const char * str, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
reading_from_file = false;
|
reading_from_file = false;
|
||||||
reading_from_wchar_string = false;
|
reading_from_wchar_string = false;
|
||||||
pchar_ascii = str;
|
pchar_ascii = str;
|
||||||
pchar_unicode = 0;
|
pchar_unicode = 0;
|
||||||
parsing_space = false;
|
parsing_space = false;
|
||||||
|
root_space = &out_space;
|
||||||
|
|
||||||
ParseRootSpace();
|
ParseRootSpace(clear_space);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseJSON(const std::string & str)
|
SpaceParser::Status SpaceParser::ParseJSON(const std::string & str, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
return ParseJSON(str.c_str());
|
return ParseJSON(str.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseJSON(const wchar_t * str)
|
SpaceParser::Status SpaceParser::ParseJSON(const wchar_t * str, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
reading_from_file = false;
|
reading_from_file = false;
|
||||||
reading_from_wchar_string = true;
|
reading_from_wchar_string = true;
|
||||||
pchar_unicode = str;
|
pchar_unicode = str;
|
||||||
pchar_ascii = 0;
|
pchar_ascii = 0;
|
||||||
parsing_space = false;
|
parsing_space = false;
|
||||||
|
root_space = &out_space;
|
||||||
|
|
||||||
ParseRootSpace();
|
ParseRootSpace(clear_space);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseJSON(const std::wstring & str)
|
SpaceParser::Status SpaceParser::ParseJSON(const std::wstring & str, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
return ParseJSON(str.c_str());
|
return ParseJSON(str.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseSpace(const char * str)
|
SpaceParser::Status SpaceParser::ParseSpace(const char * str, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
reading_from_file = false;
|
reading_from_file = false;
|
||||||
reading_from_wchar_string = false;
|
reading_from_wchar_string = false;
|
||||||
pchar_ascii = str;
|
pchar_ascii = str;
|
||||||
pchar_unicode = 0;
|
pchar_unicode = 0;
|
||||||
parsing_space = true;
|
parsing_space = true;
|
||||||
|
root_space = &out_space;
|
||||||
|
|
||||||
ParseRootSpace();
|
ParseRootSpace(clear_space);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseSpace(const std::string & str)
|
SpaceParser::Status SpaceParser::ParseSpace(const std::string & str, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
return ParseSpace(str.c_str());
|
return ParseSpace(str.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseSpace(const wchar_t * str)
|
SpaceParser::Status SpaceParser::ParseSpace(const wchar_t * str, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
reading_from_file = false;
|
reading_from_file = false;
|
||||||
reading_from_wchar_string = true;
|
reading_from_wchar_string = true;
|
||||||
pchar_unicode = str;
|
pchar_unicode = str;
|
||||||
pchar_ascii = 0;
|
pchar_ascii = 0;
|
||||||
parsing_space = true;
|
parsing_space = true;
|
||||||
|
root_space = &out_space;
|
||||||
|
|
||||||
ParseRootSpace();
|
ParseRootSpace(clear_space);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SpaceParser::Status SpaceParser::ParseSpace(const std::wstring & str)
|
SpaceParser::Status SpaceParser::ParseSpace(const std::wstring & str, Space & out_space, bool clear_space)
|
||||||
{
|
{
|
||||||
return ParseSpace(str.c_str());
|
return ParseSpace(str.c_str(), out_space, clear_space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void SpaceParser::ParseRootSpace()
|
void SpaceParser::ParseRootSpace(bool clear_root_space)
|
||||||
{
|
{
|
||||||
line = 1;
|
line = 1;
|
||||||
status = ok;
|
status = ok;
|
||||||
|
|
||||||
if( !root_space )
|
if( clear_root_space )
|
||||||
{
|
{
|
||||||
status = no_space;
|
root_space->set_empty_object();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ReadChar(); // put first character to lastc
|
ReadChar(); // put first character to lastc
|
||||||
|
@ -784,37 +777,6 @@ void SpaceParser::TrimLastWhite(std::wstring & s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
void SpaceParser::Trim(std::wstring & s)
|
|
||||||
{
|
|
||||||
std::wstring::size_type i;
|
|
||||||
|
|
||||||
if( s.empty() )
|
|
||||||
return;
|
|
||||||
|
|
||||||
// looking for white characters at the end
|
|
||||||
for(i=s.size()-1 ; i>0 && IsWhite(s[i]) ; --i);
|
|
||||||
|
|
||||||
if( i==0 && IsWhite(s[i]) )
|
|
||||||
{
|
|
||||||
// the whole string consists of white characters
|
|
||||||
s.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// deleting white characters at the end
|
|
||||||
if( i != s.size() - 1 )
|
|
||||||
s.erase(i+1, std::wstring::npos);
|
|
||||||
|
|
||||||
// looking for white characters at the beginning
|
|
||||||
for(i=0 ; i<s.size() && IsWhite(s[i]) ; ++i);
|
|
||||||
|
|
||||||
// deleting white characters at the beginning
|
|
||||||
if( i != 0 )
|
|
||||||
s.erase(0, i);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
void SpaceParser::ReadTokenUntilDelimiter(std::wstring & token, int delimiter1, int delimiter2)
|
void SpaceParser::ReadTokenUntilDelimiter(std::wstring & token, int delimiter1, int delimiter2)
|
||||||
{
|
{
|
||||||
|
@ -867,7 +829,6 @@ void SpaceParser::ReadStringValue(std::wstring & token, bool is_object_value, bo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// rename to something like ReadSpaceFieldToken???
|
|
||||||
void SpaceParser::ReadSpaceFieldToken(std::wstring & token)
|
void SpaceParser::ReadSpaceFieldToken(std::wstring & token)
|
||||||
{
|
{
|
||||||
token.clear();
|
token.clear();
|
||||||
|
|
|
@ -60,11 +60,6 @@ public:
|
||||||
SpaceParser();
|
SpaceParser();
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
setting the root space
|
|
||||||
*/
|
|
||||||
void SetSpace(Space * pspace);
|
|
||||||
void SetSpace(Space & pspace);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -77,7 +72,7 @@ public:
|
||||||
/*
|
/*
|
||||||
status of parsing
|
status of parsing
|
||||||
*/
|
*/
|
||||||
enum Status { ok, cant_open_file, syntax_error, no_space };
|
enum Status { ok, cant_open_file, syntax_error };
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -92,42 +87,42 @@ public:
|
||||||
main methods used to parse a JSON file
|
main methods used to parse a JSON file
|
||||||
file_name is the path to a file
|
file_name is the path to a file
|
||||||
*/
|
*/
|
||||||
Status ParseJSONFile(const char * file_name);
|
Status ParseJSONFile(const char * file_name, Space & out_space, bool clear_space = true);
|
||||||
Status ParseJSONFile(const std::string & file_name);
|
Status ParseJSONFile(const std::string & file_name, Space & out_space, bool clear_space = true);
|
||||||
Status ParseJSONFile(const wchar_t * file_name);
|
Status ParseJSONFile(const wchar_t * file_name, Space & out_space, bool clear_space = true);
|
||||||
Status ParseJSONFile(const std::wstring & file_name);
|
Status ParseJSONFile(const std::wstring & file_name, Space & out_space, bool clear_space = true);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
main methods used to parse a Space file
|
main methods used to parse a Space file
|
||||||
file_name is the path to a file
|
file_name is the path to a file
|
||||||
*/
|
*/
|
||||||
Status ParseSpaceFile(const char * file_name);
|
Status ParseSpaceFile(const char * file_name, Space & out_space, bool clear_space = true);
|
||||||
Status ParseSpaceFile(const std::string & file_name);
|
Status ParseSpaceFile(const std::string & file_name, Space & out_space, bool clear_space = true);
|
||||||
Status ParseSpaceFile(const wchar_t * file_name);
|
Status ParseSpaceFile(const wchar_t * file_name, Space & out_space, bool clear_space = true);
|
||||||
Status ParseSpaceFile(const std::wstring & file_name);
|
Status ParseSpaceFile(const std::wstring & file_name, Space & out_space, bool clear_space = true);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
main methods used to parse
|
main methods used to parse
|
||||||
str - input string (either 8bit ascii or UTF-8 -- see UTF8() method)
|
str - input string (either 8bit ascii or UTF-8 -- see UTF8() method)
|
||||||
*/
|
*/
|
||||||
Status ParseJSON(const char * str);
|
Status ParseJSON(const char * str, Space & out_space, bool clear_space = true);
|
||||||
Status ParseJSON(const std::string & str);
|
Status ParseJSON(const std::string & str, Space & out_space, bool clear_space = true);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
main methods used to parse
|
main methods used to parse
|
||||||
here input string is always in unicode (wide characters)
|
here input string is always in unicode (wide characters)
|
||||||
*/
|
*/
|
||||||
Status ParseJSON(const wchar_t * str);
|
Status ParseJSON(const wchar_t * str, Space & out_space, bool clear_space = true);
|
||||||
Status ParseJSON(const std::wstring & str);
|
Status ParseJSON(const std::wstring & str, Space & out_space, bool clear_space = true);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Status ParseSpace(const char * str);
|
Status ParseSpace(const char * str, Space & out_space, bool clear_space = true);
|
||||||
Status ParseSpace(const std::string & str);
|
Status ParseSpace(const std::string & str, Space & out_space, bool clear_space = true);
|
||||||
Status ParseSpace(const wchar_t * str);
|
Status ParseSpace(const wchar_t * str, Space & out_space, bool clear_space = true);
|
||||||
Status ParseSpace(const std::wstring & str);
|
Status ParseSpace(const std::wstring & str, Space & out_space, bool clear_space = true);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -310,8 +305,7 @@ private:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// new
|
void ParseRootSpace(bool clear_root_space);
|
||||||
void ParseRootSpace();
|
|
||||||
void Parse(Space * space, bool is_object_value, bool is_table_value);
|
void Parse(Space * space, bool is_object_value, bool is_table_value);
|
||||||
void ParseSpace(Space * space);
|
void ParseSpace(Space * space);
|
||||||
void ParseTable(Space * space);
|
void ParseTable(Space * space);
|
||||||
|
|
Loading…
Reference in New Issue