2012-07-12 17:01:15 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of PikoTools
|
|
|
|
* and is distributed under the (new) BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-03-16 18:40:14 +01:00
|
|
|
* Copyright (c) 2012-2021, Tomasz Sowa
|
2012-07-12 17:01:15 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
|
|
|
* project may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <wchar.h>
|
2021-03-17 18:33:41 +01:00
|
|
|
#include "spaceparser.h"
|
2012-07-12 17:01:15 +02:00
|
|
|
#include "utf8/utf8.h"
|
2021-03-16 18:40:14 +01:00
|
|
|
#include "convert/strtoint.h"
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
2021-05-20 16:11:12 +02:00
|
|
|
namespace pt
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
2021-03-17 18:33:41 +01:00
|
|
|
SpaceParser::SpaceParser()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:28:31 +02:00
|
|
|
root_space = nullptr;
|
2012-07-12 17:01:15 +02:00
|
|
|
space_start = '{';
|
|
|
|
space_end = '}';
|
|
|
|
option_delimiter = ',';
|
|
|
|
input_as_utf8 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:28:31 +02:00
|
|
|
void SpaceParser::use_utf8(bool utf)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
input_as_utf8 = utf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-17 18:33:41 +01:00
|
|
|
int SpaceParser::get_last_parsed_line()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
return line;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_json_file(const char * file_name, Space & out_space, bool clear_space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
reading_from_file = true;
|
2021-03-17 18:24:50 +01:00
|
|
|
parsing_space = false;
|
2021-05-21 01:33:01 +02:00
|
|
|
root_space = &out_space;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
file.clear();
|
|
|
|
file.open(file_name, std::ios_base::binary | std::ios_base::in);
|
|
|
|
|
|
|
|
if( file )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_root_space(clear_space);
|
2012-07-12 17:01:15 +02:00
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = cant_open_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_json_file(const std::string & file_name, Space & out_space, bool clear_space)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_json_file(file_name.c_str(), out_space, clear_space);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_json_file(const wchar_t * file_name, Space & out_space, bool clear_space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-17 18:24:50 +01:00
|
|
|
std::string file_name_utf8;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
wide_to_utf8(file_name, file_name_utf8);
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_json_file(file_name_utf8.c_str(), out_space, clear_space);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_json_file(const std::wstring & file_name, Space & out_space, bool clear_space)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_json_file(file_name.c_str(), out_space, clear_space);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_space_file(const char * file_name, Space & out_space, bool clear_space)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
reading_from_file = true;
|
|
|
|
parsing_space = true;
|
2021-05-21 01:33:01 +02:00
|
|
|
root_space = &out_space;
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
file.clear();
|
|
|
|
file.open(file_name, std::ios_base::binary | std::ios_base::in);
|
|
|
|
|
|
|
|
if( file )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_root_space(clear_space);
|
2021-03-17 18:24:50 +01:00
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = cant_open_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_space_file(const std::string & file_name, Space & out_space, bool clear_space)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_space_file(file_name.c_str(), out_space, clear_space);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_space_file(const wchar_t * file_name, Space & out_space, bool clear_space)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
std::string file_name_utf8;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
wide_to_utf8(file_name, file_name_utf8);
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_space_file(file_name_utf8.c_str(), out_space, clear_space);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_space_file(const std::wstring & file_name, Space & out_space, bool clear_space)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_space_file(file_name.c_str(), out_space, clear_space);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_json(const char * str, Space & out_space, bool clear_space)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
reading_from_file = false;
|
|
|
|
reading_from_wchar_string = false;
|
|
|
|
pchar_ascii = str;
|
|
|
|
pchar_unicode = 0;
|
|
|
|
parsing_space = false;
|
2021-05-21 01:33:01 +02:00
|
|
|
root_space = &out_space;
|
2021-03-17 18:24:50 +01:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_root_space(clear_space);
|
2021-03-17 18:24:50 +01:00
|
|
|
|
2021-05-21 01:33:01 +02:00
|
|
|
return status;
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_json(const std::string & str, Space & out_space, bool clear_space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_json(str.c_str(), out_space, clear_space);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_json(const wchar_t * str, Space & out_space, bool clear_space)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
reading_from_file = false;
|
|
|
|
reading_from_wchar_string = true;
|
|
|
|
pchar_unicode = str;
|
|
|
|
pchar_ascii = 0;
|
|
|
|
parsing_space = false;
|
2021-05-21 01:33:01 +02:00
|
|
|
root_space = &out_space;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_root_space(clear_space);
|
2021-03-17 18:24:50 +01:00
|
|
|
|
2021-05-21 01:33:01 +02:00
|
|
|
return status;
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_json(const std::wstring & str, Space & out_space, bool clear_space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_json(str.c_str(), out_space, clear_space);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_space(const char * str, Space & out_space, bool clear_space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
reading_from_file = false;
|
|
|
|
reading_from_wchar_string = false;
|
|
|
|
pchar_ascii = str;
|
|
|
|
pchar_unicode = 0;
|
2021-03-17 18:24:50 +01:00
|
|
|
parsing_space = true;
|
2021-05-21 01:33:01 +02:00
|
|
|
root_space = &out_space;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_root_space(clear_space);
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 01:33:01 +02:00
|
|
|
return status;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_space(const std::string & str, Space & out_space, bool clear_space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_space(str.c_str(), out_space, clear_space);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_space(const wchar_t * str, Space & out_space, bool clear_space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
reading_from_file = false;
|
|
|
|
reading_from_wchar_string = true;
|
|
|
|
pchar_unicode = str;
|
|
|
|
pchar_ascii = 0;
|
2021-03-17 18:24:50 +01:00
|
|
|
parsing_space = true;
|
2021-05-21 01:33:01 +02:00
|
|
|
root_space = &out_space;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_root_space(clear_space);
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 01:33:01 +02:00
|
|
|
return status;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
SpaceParser::Status SpaceParser::parse_space(const std::wstring & str, Space & out_space, bool clear_space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
return parse_space(str.c_str(), out_space, clear_space);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::parse_root_space(bool clear_root_space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
line = 1;
|
|
|
|
status = ok;
|
|
|
|
|
2021-05-21 01:33:01 +02:00
|
|
|
if( clear_root_space )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 01:33:01 +02:00
|
|
|
root_space->set_empty_object();
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // put first character to lastc
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
if( parsing_space )
|
|
|
|
{
|
|
|
|
separator = '=';
|
|
|
|
table_start = '(';
|
|
|
|
table_end = ')';
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_space(root_space);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
separator = ':';
|
|
|
|
table_start = '[';
|
|
|
|
table_end = ']';
|
2021-05-21 04:42:55 +02:00
|
|
|
parse(root_space, false, false);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2021-03-18 17:55:30 +01:00
|
|
|
|
|
|
|
if( lastc != -1 )
|
|
|
|
status = syntax_error;
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
token.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::parse(Space * space, bool is_object_value, bool is_table_value)
|
2021-03-16 18:40:14 +01:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
if( lastc == space_start )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_space(space);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( lastc == table_start )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_table(space);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
else
|
2021-03-16 18:40:14 +01:00
|
|
|
if( lastc == '"' ) // IMPROVEME define a variable
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_text_value(space);
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_string_value(token, is_object_value, is_table_value);
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
if( token == L"null" )
|
|
|
|
{
|
|
|
|
space->set_null();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( token == L"true" )
|
|
|
|
{
|
|
|
|
space->set(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( token == L"false" )
|
|
|
|
{
|
|
|
|
space->set(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( is_integer_token() )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_integer_value(space);
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( is_floating_point_token() )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_floating_point_value(space);
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-17 18:24:50 +01:00
|
|
|
if( parsing_space )
|
|
|
|
{
|
|
|
|
space->set(token);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::parse_space(Space * space)
|
2021-03-16 18:40:14 +01:00
|
|
|
{
|
2021-03-17 18:24:50 +01:00
|
|
|
/*
|
|
|
|
* in Space format in global namespace the space start character is not required
|
|
|
|
*/
|
|
|
|
bool need_space_start_character = !parsing_space || space != root_space;
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-03-17 18:24:50 +01:00
|
|
|
if( need_space_start_character )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // inserting a next character after the space_start char to lastc
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-05-21 00:26:19 +02:00
|
|
|
if( !space->is_object() )
|
|
|
|
space->set_empty_object();
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_key_value_pairs(space);
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-03-17 18:24:50 +01:00
|
|
|
if( need_space_start_character )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-17 18:24:50 +01:00
|
|
|
if( lastc == space_end )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::parse_text_value(Space * space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
space->set_empty_wstring();
|
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)
2021-04-08 17:16:37 +02:00
|
|
|
std::wstring * str = space->get_wstr();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-17 18:24:50 +01:00
|
|
|
if( parsing_space )
|
2021-05-21 04:42:55 +02:00
|
|
|
read_multiline_token_quoted(*str);
|
2012-07-12 17:01:15 +02:00
|
|
|
else
|
2021-05-21 04:42:55 +02:00
|
|
|
read_token_quoted(*str);
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::parse_integer_value(Space * space)
|
2021-03-16 18:40:14 +01:00
|
|
|
{
|
|
|
|
const wchar_t * after_str = nullptr;
|
|
|
|
bool was_overflow = false;
|
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)
2021-04-08 17:16:37 +02:00
|
|
|
int base = 10;
|
2021-03-16 18:40:14 +01:00
|
|
|
|
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)
2021-04-08 17:16:37 +02:00
|
|
|
if( parsing_space )
|
|
|
|
{
|
|
|
|
// in Space format when the integer value begins with a zero it means
|
|
|
|
// this is an octal number
|
|
|
|
if( !token.empty() && token[0] == '0' )
|
|
|
|
base = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long val = Toll(token.c_str(), base, &after_str, &was_overflow, false);
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
if( was_overflow )
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( size_t(after_str - token.c_str()) != token.size() )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
status = syntax_error;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
space->set(val);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 00:51:14 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::parse_floating_point_value(Space * space)
|
2021-03-16 18:40:14 +01:00
|
|
|
{
|
|
|
|
wchar_t * after_str = nullptr;
|
|
|
|
double val = wcstod(token.c_str(), &after_str);
|
|
|
|
|
|
|
|
if( errno == ERANGE )
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( size_t(after_str - token.c_str()) != token.size() )
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
space->set(val);
|
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::parse_table(Space * space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // inserting a next character after the table_start char to lastc
|
2021-03-17 18:24:50 +01:00
|
|
|
space->set_empty_table();
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_values_list(space);
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
if( lastc == table_end )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::parse_key_value_pairs(Space * space)
|
2021-03-16 18:40:14 +01:00
|
|
|
{
|
|
|
|
bool is_first = true;
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
while( status == ok && lastc != space_end && lastc != -1 )
|
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
if( !is_first )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
if( lastc == option_delimiter )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // inserting a next character after the option_delimiter to lastc
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
if( parsing_space )
|
|
|
|
{
|
|
|
|
// in space format a space_end character is allowed to be after the last table item
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
if( lastc == space_end )
|
|
|
|
break;
|
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
else
|
2021-03-17 18:24:50 +01:00
|
|
|
if( !parsing_space )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-17 18:24:50 +01:00
|
|
|
// in json format the option_delimiter is required
|
2012-07-12 17:01:15 +02:00
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2012-07-12 17:01:15 +02:00
|
|
|
if( status == ok )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_key();
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-03-17 18:24:50 +01:00
|
|
|
if( status == ok )
|
2021-03-16 18:40:14 +01:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
if( lastc == separator )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // inserting a next character after the separator to lastc
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-03-17 18:24:50 +01:00
|
|
|
Space & new_space = space->add(token.c_str(), new Space());
|
2021-05-21 04:42:55 +02:00
|
|
|
parse(&new_space, true, false);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( parsing_space && lastc == space_start )
|
|
|
|
{
|
|
|
|
Space & new_space = space->add_child_space(token.c_str());
|
2021-05-21 04:42:55 +02:00
|
|
|
parse_space(&new_space);
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
is_first = false;
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::parse_values_list(Space * space)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
bool is_first = true;
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
while( status == ok && lastc != table_end && lastc != -1 )
|
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
if( !is_first )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
if( lastc == option_delimiter ) // may add a new delimiter for tables? default the same as for objects...
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // inserting a next character after the delimiter
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
if( parsing_space )
|
|
|
|
{
|
|
|
|
// in space format a table_end character is allowed to be after the last table item
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
if( lastc == table_end )
|
|
|
|
break;
|
|
|
|
}
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
|
|
|
else
|
2021-03-17 18:24:50 +01:00
|
|
|
if( !parsing_space )
|
2021-03-16 18:40:14 +01:00
|
|
|
{
|
2021-03-17 18:24:50 +01:00
|
|
|
// in json format the option_delimiter is required
|
2021-03-16 18:40:14 +01:00
|
|
|
status = syntax_error;
|
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
if( status == ok )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
Space * new_space = &space->add(new Space());
|
2021-05-21 04:42:55 +02:00
|
|
|
parse(new_space, false, true);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
is_first = false;
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-03-17 18:33:41 +01:00
|
|
|
bool SpaceParser::is_integer_token()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
if( token.empty() )
|
|
|
|
return false;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if( token[i] == '-' )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
i += 1;
|
|
|
|
|
|
|
|
if( token.size() == 1 )
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
for( ; i < token.size() ; ++i)
|
|
|
|
{
|
|
|
|
if( token[i] < '0' || token[i] > '9' )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
return false;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
return true;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-17 18:33:41 +01:00
|
|
|
bool SpaceParser::is_floating_point_token()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
bool was_dot = false;
|
|
|
|
bool was_exponential = false;
|
|
|
|
bool was_plus_minus_sign = false;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
if( token.empty() )
|
|
|
|
return false;
|
2018-04-17 00:51:14 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
size_t i = 0;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
if( token[i] == '-' )
|
|
|
|
{
|
|
|
|
i += 1;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
if( token.size() == 1 )
|
|
|
|
return false;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
for( ; i < token.size() ; ++i)
|
|
|
|
{
|
|
|
|
if( token[i] == '.' )
|
|
|
|
{
|
|
|
|
if( was_dot || was_exponential )
|
|
|
|
return false;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
was_dot = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( token[i] == 'e' || token[i]=='E' )
|
|
|
|
{
|
|
|
|
if( was_exponential )
|
|
|
|
return false;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
was_exponential = true;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
// the exponential character cannot be the last character
|
|
|
|
if( i + 1 == token.size() )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( token[i] == '+' || token[i] == '-' )
|
|
|
|
{
|
|
|
|
if( was_plus_minus_sign || !was_exponential )
|
|
|
|
return false;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
// the plus or minus should be after the exponential character
|
|
|
|
if( i > 0 && (token[i-1] != 'e' && token[i-1] != 'E') )
|
|
|
|
return false;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
was_plus_minus_sign = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( token[i] < '0' || token[i] > '9' )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
return true;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
bool SpaceParser::is_white(int c)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
// 13 (\r) is at the end of a line in a dos file \r\n
|
|
|
|
// 160 is an unbreakable space
|
|
|
|
if( c==' ' || c=='\t' || c==13 || c==160 || c==10 )
|
|
|
|
return true;
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
return false;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-17 18:33:41 +01:00
|
|
|
bool SpaceParser::is_alfa_numeric_char(int c)
|
2021-03-16 18:40:14 +01:00
|
|
|
{
|
|
|
|
return (c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= '0' && c <= '9') ||
|
2021-04-12 18:49:34 +02:00
|
|
|
c == '.' || c=='-' || c=='+';
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::skip_line()
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
while( lastc != -1 && (char_was_escaped || lastc != '\n') )
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::skip_white()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-17 18:24:50 +01:00
|
|
|
if( parsing_space )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
while( is_white(lastc) || (!char_was_escaped && lastc == '#') )
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
if( lastc == '#' )
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_line();
|
2021-03-17 18:24:50 +01:00
|
|
|
else
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
while( is_white(lastc) )
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::trim_last_white(std::wstring & s)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
std::wstring::size_type i;
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
for(i=s.size() ; i>0 && is_white(s[i-1]) ; --i)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( i < s.size() )
|
|
|
|
{
|
|
|
|
s.erase(i, std::wstring::npos);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::read_token_until_delimiter(std::wstring & token, int delimiter1, int delimiter2)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
token.clear();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-18 16:26:56 +01:00
|
|
|
while( lastc != -1 && (char_was_escaped || (lastc != '\n' && lastc != '#' && lastc != delimiter1 && lastc != delimiter2)) )
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
token += static_cast<wchar_t>(lastc);
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
trim_last_white(token);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::read_alfa_numeric_token(std::wstring & token)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
token.clear();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
while( is_alfa_numeric_char(lastc) )
|
|
|
|
{
|
|
|
|
token += static_cast<wchar_t>(lastc);
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2021-03-16 18:40:14 +01:00
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-17 18:24:50 +01:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::read_string_value(std::wstring & token, bool is_object_value, bool is_table_value)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
if( parsing_space )
|
|
|
|
{
|
|
|
|
if( is_object_value )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_token_until_delimiter(token, space_end, -1);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( is_table_value )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_token_until_delimiter(token, table_end, option_delimiter);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_token_until_delimiter(token, -1, -1);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_alfa_numeric_token(token);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::read_space_field_token(std::wstring & token)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
token.clear();
|
|
|
|
|
2021-03-18 16:26:56 +01:00
|
|
|
while( lastc != -1 && (char_was_escaped || (lastc != separator && lastc != 10 && lastc != space_start && lastc != '#' )) )
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
token += static_cast<wchar_t>(lastc);
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
trim_last_white(token);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
// IMPROVEME in JSON we should not allow non-escaped a new line character
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::read_token_quoted(std::wstring & token)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
token.clear();
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // skipping the first quotation mark
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-17 18:24:50 +01:00
|
|
|
while( lastc != -1 && (char_was_escaped || (lastc != '"' && lastc != 10)) )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
token += static_cast<wchar_t>(lastc);
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !char_was_escaped && lastc == '"' )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // skipping the last quotation mark
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
status = syntax_error;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::read_multiline_token_quoted(std::wstring & token)
|
2021-03-17 18:24:50 +01:00
|
|
|
{
|
|
|
|
token.clear();
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // skipping the first quotation mark
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
while( lastc != -1 && (char_was_escaped || lastc != '"') )
|
|
|
|
{
|
|
|
|
token += static_cast<wchar_t>(lastc);
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char();
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !char_was_escaped && lastc == '"' )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char(); // skipping the last quotation mark
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
2021-03-18 16:26:56 +01:00
|
|
|
/*
|
|
|
|
* this method is used to read the field name (key) in an object
|
|
|
|
* or to read the space child name (used in Space format)
|
|
|
|
*/
|
2021-05-21 04:42:55 +02:00
|
|
|
void SpaceParser::read_key()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
skip_white();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
if( parsing_space )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-03-17 18:24:50 +01:00
|
|
|
if( lastc == '"' )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_multiline_token_quoted(token);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_space_field_token(token);
|
2021-03-17 18:24:50 +01:00
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
if( lastc == '"' )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
read_token_quoted(token);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-16 18:40:14 +01:00
|
|
|
status = syntax_error;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
int SpaceParser::read_utf8_char()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
bool correct;
|
|
|
|
|
|
|
|
lastc = -1;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
utf8_to_int(file, c, correct);
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
if( !file )
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
while( !correct );
|
|
|
|
|
|
|
|
lastc = c;
|
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
int SpaceParser::read_ascii_char()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
lastc = file.get();
|
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
int SpaceParser::read_char_from_wchar_string()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
if( *pchar_unicode == 0 )
|
|
|
|
lastc = -1;
|
|
|
|
else
|
|
|
|
lastc = *(pchar_unicode++);
|
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
int SpaceParser::read_char_from_utf8_string()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
bool correct;
|
|
|
|
|
|
|
|
lastc = -1;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
size_t len = utf8_to_int(pchar_ascii, c, correct);
|
2012-07-12 17:01:15 +02:00
|
|
|
pchar_ascii += len;
|
|
|
|
}
|
2018-04-26 18:36:15 +02:00
|
|
|
while( *pchar_ascii && !correct );
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2018-04-26 18:36:15 +02:00
|
|
|
if( correct )
|
|
|
|
lastc = c;
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
int SpaceParser::read_char_from_ascii_string()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
if( *pchar_ascii == 0 )
|
|
|
|
lastc = -1;
|
|
|
|
else
|
|
|
|
lastc = *(pchar_ascii++);
|
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
int SpaceParser::read_char_no_escape()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
if( reading_from_file )
|
|
|
|
{
|
|
|
|
if( input_as_utf8 )
|
2021-05-21 04:42:55 +02:00
|
|
|
return read_utf8_char();
|
2012-07-12 17:01:15 +02:00
|
|
|
else
|
2021-05-21 04:42:55 +02:00
|
|
|
return read_ascii_char();
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( reading_from_wchar_string )
|
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
return read_char_from_wchar_string();
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( input_as_utf8 )
|
2021-05-21 04:42:55 +02:00
|
|
|
return read_char_from_utf8_string();
|
2012-07-12 17:01:15 +02:00
|
|
|
else
|
2021-05-21 04:42:55 +02:00
|
|
|
return read_char_from_ascii_string();
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
bool SpaceParser::is_hex_digit(wchar_t c)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
return ((c>='0' && c<='9') ||
|
|
|
|
(c>='a' && c<='f') ||
|
|
|
|
(c>='A' && c<='F') );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
int SpaceParser::hex_to_int(wchar_t c)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
if( c>='0' && c<='9' )
|
|
|
|
return c - '0';
|
|
|
|
|
|
|
|
if( c>='a' && c<='f' )
|
|
|
|
return c - 'a' + 10;
|
|
|
|
|
|
|
|
if( c>='A' && c<='F' )
|
|
|
|
return c - 'A' + 10;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-14 13:48:32 +02:00
|
|
|
/*
|
|
|
|
* format: \uHHHH where H is a hex digit 0-F
|
|
|
|
*/
|
|
|
|
bool SpaceParser::read_unicode_four_digit_format(bool has_first_byte, int first_byte)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-06-14 13:48:32 +02:00
|
|
|
int c;
|
2012-07-12 17:01:15 +02:00
|
|
|
int value = 0;
|
|
|
|
|
|
|
|
for(int i=0 ; i<4 ; ++i)
|
2021-06-14 13:48:32 +02:00
|
|
|
{
|
|
|
|
if( i == 0 && has_first_byte )
|
|
|
|
{
|
|
|
|
c = first_byte;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c = read_char_no_escape();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !is_hex_digit(c) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = (value << 4) | hex_to_int(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
lastc = static_cast<wchar_t>(value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* format: \uHHHH and optionally following by \uHHHH
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void SpaceParser::read_unicode_json_format(bool has_first_byte, int first_byte)
|
|
|
|
{
|
|
|
|
bool ok = read_unicode_four_digit_format(has_first_byte, first_byte);
|
|
|
|
|
|
|
|
if( ok && pt::is_first_surrogate_char(lastc) )
|
|
|
|
{
|
|
|
|
int c1 = lastc;
|
|
|
|
int c = read_char_no_escape();
|
|
|
|
|
|
|
|
ok = ok && (c == '\\');
|
|
|
|
|
|
|
|
if( ok )
|
|
|
|
{
|
|
|
|
c = read_char_no_escape();
|
|
|
|
ok = ok && (c == 'u');
|
|
|
|
ok = ok && read_unicode_four_digit_format(false, 0);
|
|
|
|
|
|
|
|
if( ok && pt::is_second_surrogate_char(lastc) )
|
|
|
|
{
|
|
|
|
int c2 = lastc;
|
|
|
|
ok = ok && pt::surrogate_pair_to_int(c1, c2, lastc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !ok || !pt::utf8_check_range(lastc) )
|
|
|
|
{
|
|
|
|
lastc = 0xFFFD; // U+FFFD "replacement character";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* format: \u{H...} where H is a hex digit 0-F, minimum digits: 1, maximum digits: 6
|
|
|
|
*/
|
|
|
|
void SpaceParser::read_unicode_floating_format()
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int value = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// max 6 hex digits + '}'
|
|
|
|
for(i=0 ; i<7 ; ++i)
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-05-21 04:42:55 +02:00
|
|
|
c = read_char_no_escape();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
if( !is_hex_digit(c) )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
2021-06-14 13:48:32 +02:00
|
|
|
break;
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
value = (value << 4) | hex_to_int(c);
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 13:48:32 +02:00
|
|
|
if( i > 0 && c == '}' && pt::utf8_check_range(value) )
|
|
|
|
{
|
|
|
|
lastc = static_cast<wchar_t>(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastc = 0xFFFD; // U+FFFD "replacement character";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SpaceParser::read_unicode_code_point()
|
|
|
|
{
|
|
|
|
if( parsing_space )
|
|
|
|
{
|
|
|
|
int c = read_char_no_escape();
|
|
|
|
|
|
|
|
if( c == '{' )
|
|
|
|
{
|
|
|
|
read_unicode_floating_format();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read_unicode_json_format(true, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read_unicode_json_format(false, 0);
|
|
|
|
}
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
int SpaceParser::read_char()
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
char_was_escaped = false;
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char_no_escape();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:28:31 +02:00
|
|
|
if( lastc == '\\' )
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
char_was_escaped = true;
|
2021-05-21 04:42:55 +02:00
|
|
|
read_char_no_escape();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
switch(lastc)
|
|
|
|
{
|
|
|
|
case '0': lastc = 0; break;
|
|
|
|
case 't': lastc = '\t'; break;
|
|
|
|
case 'r': lastc = '\r'; break;
|
|
|
|
case 'n': lastc = '\n'; break;
|
|
|
|
case 'b': lastc = 0x08; break;
|
|
|
|
case 'f': lastc = 0x0c; break;
|
2021-05-21 04:42:55 +02:00
|
|
|
case 'u': read_unicode_code_point(); break;
|
2021-03-18 16:26:56 +01:00
|
|
|
// "in other cases we return the last character, so two \\ returns one \ "
|
2012-07-12 17:01:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-06-14 13:48:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-12 17:01:15 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|