added a base class for parsers: BaseParser (convert/baseparser.h|cpp)

there are methods for reading from string/files there
  those methods were moved from SpaceParser and CSVParser
fixed: CSVParser didn't set input_as_utf8 flag
This commit is contained in:
2021-07-17 14:38:22 +02:00
parent 2a3f43c5c3
commit 7ce07c57f5
8 changed files with 329 additions and 363 deletions

View File

@@ -891,122 +891,6 @@ void SpaceParser::read_key()
int SpaceParser::read_utf8_char()
{
int c;
bool correct;
lastc = -1;
do
{
utf8_to_int(file, c, correct);
if( !file )
return lastc;
}
while( !correct );
lastc = c;
if( lastc == '\n' )
++line;
return lastc;
}
int SpaceParser::read_ascii_char()
{
lastc = file.get();
if( lastc == '\n' )
++line;
return lastc;
}
int SpaceParser::read_char_from_wchar_string()
{
if( *pchar_unicode == 0 )
lastc = -1;
else
lastc = *(pchar_unicode++);
if( lastc == '\n' )
++line;
return lastc;
}
int SpaceParser::read_char_from_utf8_string()
{
int c;
bool correct;
lastc = -1;
do
{
size_t len = utf8_to_int(pchar_ascii, c, correct);
pchar_ascii += len;
}
while( *pchar_ascii && !correct );
if( correct )
lastc = c;
if( lastc == '\n' )
++line;
return lastc;
}
int SpaceParser::read_char_from_ascii_string()
{
if( *pchar_ascii == 0 )
lastc = -1;
else
lastc = *(pchar_ascii++);
if( lastc == '\n' )
++line;
return lastc;
}
int SpaceParser::read_char_no_escape()
{
if( reading_from_file )
{
if( input_as_utf8 )
return read_utf8_char();
else
return read_ascii_char();
}
else
{
if( reading_from_wchar_string )
{
return read_char_from_wchar_string();
}
else
{
if( input_as_utf8 )
return read_char_from_utf8_string();
else
return read_char_from_ascii_string();
}
}
}
bool SpaceParser::is_hex_digit(wchar_t c)
{

View File

@@ -40,6 +40,7 @@
#include <fstream>
#include "space.h"
#include "convert/baseparser.h"
@@ -49,7 +50,7 @@ namespace pt
class SpaceParser
class SpaceParser : public BaseParser
{
public:
@@ -154,32 +155,6 @@ private:
Space * root_space;
/*
a number of a line in which there is a syntax_error
*/
int line;
/*
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
*/
@@ -222,13 +197,6 @@ private:
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 "
@@ -236,22 +204,6 @@ private:
bool char_was_escaped;
/*
current file
may it would be better to make a pointer?
if we parse only a string then there is no sense to have such an object
*/
std::ifstream file;
/*
input file is in UTF-8
default: true
*/
bool input_as_utf8;
/*
* if parsing_space is false then it means we are parsing JSON format
*
@@ -287,12 +239,6 @@ private:
void read_token_quoted(std::wstring & token);
void read_multiline_token_quoted(std::wstring & token);
int read_utf8_char();
int read_ascii_char();
int read_char_from_wchar_string();
int read_char_from_utf8_string();
int read_char_from_ascii_string();
int read_char_no_escape();
int read_char();
bool is_white(int c);
void skip_line();