Commit 2958c56a by Maarten L. Hekkelman

change parser to use streambuf directly

parent 9cff8768
...@@ -248,7 +248,7 @@ class sac_parser ...@@ -248,7 +248,7 @@ class sac_parser
SAVE SAVE
}; };
std::istream &m_source; std::streambuf &m_source;
// Parser state // Parser state
bool m_validate; bool m_validate;
......
...@@ -47,7 +47,7 @@ namespace cif ...@@ -47,7 +47,7 @@ namespace cif
// -------------------------------------------------------------------- // --------------------------------------------------------------------
sac_parser::sac_parser(std::istream &is, bool init) sac_parser::sac_parser(std::istream &is, bool init)
: m_source(is) : m_source(*is.rdbuf())
{ {
m_validate = true; m_validate = true;
m_line_nr = 1; m_line_nr = 1;
...@@ -62,20 +62,20 @@ sac_parser::sac_parser(std::istream &is, bool init) ...@@ -62,20 +62,20 @@ sac_parser::sac_parser(std::istream &is, bool init)
// translation. // translation.
int sac_parser::get_next_char() int sac_parser::get_next_char()
{ {
int result; int result = std::char_traits<char>::eof();
if (m_buffer.empty()) if (not m_buffer.empty())
result = m_source.get();
else
{ {
result = m_buffer.back(); result = m_buffer.back();
m_buffer.pop_back(); m_buffer.pop_back();
} }
else
result = m_source.sbumpc();
// very simple CR/LF translation into LF // very simple CR/LF translation into LF
if (result == '\r') if (result == '\r')
{ {
int lookahead = m_source.get(); int lookahead = m_source.sbumpc();
if (lookahead != '\n') if (lookahead != '\n')
m_buffer.push_back(lookahead); m_buffer.push_back(lookahead);
result = '\n'; result = '\n';
...@@ -453,8 +453,6 @@ void sac_parser::match(CIFToken token) ...@@ -453,8 +453,6 @@ void sac_parser::match(CIFToken token)
bool sac_parser::parse_single_datablock(const std::string &datablock) bool sac_parser::parse_single_datablock(const std::string &datablock)
{ {
// first locate the start, as fast as we can // first locate the start, as fast as we can
auto &sb = *m_source.rdbuf();
enum enum
{ {
start, start,
...@@ -471,7 +469,7 @@ bool sac_parser::parse_single_datablock(const std::string &datablock) ...@@ -471,7 +469,7 @@ bool sac_parser::parse_single_datablock(const std::string &datablock)
std::string::size_type si = 0; std::string::size_type si = 0;
bool found = false; bool found = false;
for (auto ch = sb.sbumpc(); not found and ch != std::streambuf::traits_type::eof(); ch = sb.sbumpc()) for (auto ch = m_source.sbumpc(); not found and ch != std::streambuf::traits_type::eof(); ch = m_source.sbumpc())
{ {
switch (state) switch (state)
{ {
...@@ -544,8 +542,6 @@ sac_parser::datablock_index sac_parser::index_datablocks() ...@@ -544,8 +542,6 @@ sac_parser::datablock_index sac_parser::index_datablocks()
datablock_index index; datablock_index index;
// first locate the start, as fast as we can // first locate the start, as fast as we can
auto &sb = *m_source.rdbuf();
enum enum
{ {
start, start,
...@@ -563,7 +559,7 @@ sac_parser::datablock_index sac_parser::index_datablocks() ...@@ -563,7 +559,7 @@ sac_parser::datablock_index sac_parser::index_datablocks()
std::string::size_type si = 0; std::string::size_type si = 0;
std::string datablock; std::string datablock;
for (auto ch = sb.sbumpc(); ch != std::streambuf::traits_type::eof(); ch = sb.sbumpc()) for (auto ch = m_source.sbumpc(); ch != std::streambuf::traits_type::eof(); ch = m_source.sbumpc())
{ {
switch (state) switch (state)
{ {
...@@ -626,7 +622,7 @@ sac_parser::datablock_index sac_parser::index_datablocks() ...@@ -626,7 +622,7 @@ sac_parser::datablock_index sac_parser::index_datablocks()
else if (isspace(ch)) else if (isspace(ch))
{ {
if (not datablock.empty()) if (not datablock.empty())
index[datablock] = m_source.tellg(); index[datablock] = m_source.pubseekoff(0, std::ios_base::cur, std::ios_base::in);
state = start; state = start;
} }
...@@ -648,7 +644,7 @@ bool sac_parser::parse_single_datablock(const std::string &datablock, const data ...@@ -648,7 +644,7 @@ bool sac_parser::parse_single_datablock(const std::string &datablock, const data
auto i = index.find(datablock); auto i = index.find(datablock);
if (i != index.end()) if (i != index.end())
{ {
m_source.seekg(i->second); m_source.pubseekpos(i->second, std::ios_base::in);
produce_datablock(datablock); produce_datablock(datablock);
m_lookahead = get_next_token(); m_lookahead = get_next_token();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment