pikotools/space/jsontospaceparser.h

364 lines
6.8 KiB
C++

/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2017, Tomasz Sowa
* 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.
*/
#ifndef headerfile_picotools_space_jsonspaceparser
#define headerfile_picotools_space_jsonspaceparser
#include <fstream>
#include "space.h"
namespace PT
{
#ifdef nonexisting_value
class JSONToSpaceParser
{
public:
/*
ctor -- setting default values (SetDefault() method)
*/
JSONToSpaceParser();
/*
setting the root space
*/
void SetSpace(Space * pspace);
void SetSpace(Space & pspace);
/*
setting options of the parser to the default values
utf8 etc.
*/
void SetDefault();
/*
status of parsing
*/
enum Status { ok, cant_open_file, syntax_error, max_nested_spaces_exceeded, no_space };
/*
the last status of parsing, set by Parse() methods
*/
Status status;
/*
a number of a line in which there is a syntax_error
*/
int line;
/*
how many objects were skipped
used in parsing tables when create_table_as_space is false
*/
size_t skipped;
/*
main methods used to parse
file_name is the path to a file
*/
Status Parse(const char * file_name);
Status Parse(const std::string & file_name);
Status Parse(const wchar_t * file_name);
Status Parse(const std::wstring & file_name);
/*
main methods used to parse
str - input string (either 8bit ascii or UTF-8 -- see UTF8() method)
*/
Status ParseString(const char * str);
Status ParseString(const std::string & str);
/*
main methods used to parse
here input string is always in unicode (wide characters)
*/
Status ParseString(const wchar_t * str);
Status ParseString(const std::wstring & str);
/*
if true then empty values and lists, e.g:
option =
option2 = ()
will be omitted (not inserted to 'table')
default: false
*/
void SkipEmpty(bool skip);
/*
'\' character is used to escape other characters in a quoted string
so "some \t t\"ext" will produce "some t t"ext"
default: true
*/
void UseEscapeChar(bool escape);
/*
if true then the input file or string (char* or std::string) is treated as UTF-8
*/
void UTF8(bool utf);
/*
default: true
*/
void CreateTableAsSpace(bool create_table_as_space_);
private:
/*
current space set by SetSpace();
*/
Space * root_space;
/*
a space in which we are now
*/
Space * space;
/*
true if Parse() method was called
false if ParseString() was called
*/
bool reading_from_file;
/*
pointers to the current character
if ParseString() is in used
*/
const char * pchar_ascii;
const wchar_t * pchar_unicode;
/*
true if ParseString(wchar_t *) or ParseString(std::wstring&) was called
*/
bool reading_from_wchar_string;
/*
last read token
*/
std::wstring token;
/*
last read key
*/
std::wstring key;
/*
last read list
*/
Space::Value value;
/*
separator between a variable and a value, default: '='
*/
int separator;
/*
space starting character, default: '{'
*/
int space_start;
/*
space ending character, default: '}'
*/
int space_end;
/*
table starting character, default: '['
*/
int table_start;
/*
table ending character, default: ']'
*/
int table_end;
/*
option delimiter, default: ','
*/
int option_delimiter;
/*
last read char
or -1 if the end
*/
int lastc;
/*
true if the lastc was escaped (with a backslash)
we have to know if the last sequence was \" or just "
*/
bool char_was_escaped;
/*
current file
*/
std::ifstream file;
/*
if true then empty lists, e.g:
option =
option2 = ()
will be omitted (not inserted to 'table')
default: false
*/
bool skip_empty;
/*
input file is in UTF-8
default: true
*/
bool input_as_utf8;
/*
if true you can use an escape character '\' in quoted values
*/
bool use_escape_char;
/*
if false we only allow the tables to consists of text items (numeric, boolean too)
objects are not allowed then
default: true
*/
bool create_table_as_space;
/*
*/
size_t current_nested_level;
/*
default: 1000;
*/
size_t max_nested_level;
std::string afile_name;
void Parse();
void ParseSpace(bool has_space_name, bool insert_new_space = true);
void ParseTextTable();
void ParseObjectsTable(bool has_key);
void ParseTable(bool has_key);
void ParseKeyValuePairs();
void SkipText();
void SkipObjectOrTable(int start_char, int end_char);
void SkipTable();
void SkipObject();
void SpaceEnds(bool skip_space_char = true);
void SpaceStarts(bool has_space_name, bool skip_space_char = true);
void DeleteFromTable(const std::wstring & var);
void ReadTokenQuoted();
void ReadTokenSingle(bool white_delimit, bool new_line_delimit, int delimit1, int delimit2);
void ReadToken(bool white_delimit, bool new_line_delimit, int delimit1, int delimit2);
void ReadKey();
void ReadValue(bool skip_object_or_table = false,
bool add_space_for_text_value = false,
bool has_key = false,
bool auto_add_text_value = false);
void AddKeyValuePair();
int ReadUTF8Char();
int ReadASCIIChar();
int ReadCharFromWcharString();
int ReadCharFromUTF8String();
int ReadCharFromAsciiString();
int ReadCharNoEscape();
int ReadChar();
bool IsWhite(int c);
void SkipWhite();
void Trim(std::wstring & s);
bool IsHexDigit(wchar_t c);
int HexToInt(wchar_t c);
void ReadUnicodeCodePoint();
};
#endif
} // namespace
#endif