16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-22 13:59:19 +02:00

added quotation marks handling to the parser

This commit is contained in:
tmj-fstate
2017-01-28 04:40:54 +01:00
parent fe1c52027e
commit c3d79e29e2
2 changed files with 94 additions and 35 deletions

View File

@@ -21,7 +21,7 @@ http://mozilla.org/MPL/2.0/.
// cParser -- generic class for parsing text data. // cParser -- generic class for parsing text data.
// constructors // constructors
cParser::cParser(std::string Stream, buffertype Type, std::string Path, bool tr) cParser::cParser(std::string const Stream, buffertype Type, std::string Path, bool tr)
{ {
LoadTraction = tr; LoadTraction = tr;
// build comments map // build comments map
@@ -73,28 +73,33 @@ bool cParser::getTokens(int Count, bool ToLower, const char *Break)
else else
trtest="x"; //nie wczytywać trtest="x"; //nie wczytywać
*/ */
/*
int i; int i;
this->str(""); this->str("");
this->clear(); this->clear();
for (i = 0; i < Count; ++i) */
for (int i = 0; i < Count; ++i)
{ {
std::string string = readToken(ToLower, Break); std::string token = readToken(ToLower, Break);
if( true == string.empty() ) { if( true == token.empty() ) {
// no more tokens // no more tokens
break; break;
} }
// collect parameters // collect parameters
tokens.emplace_back( token );
/*
if (i == 0) if (i == 0)
this->str(string); this->str(token);
else else
{ {
std::string temp = this->str(); std::string temp = this->str();
temp.append("\n"); temp.append("\n");
temp.append(string); temp.append(token);
this->str(temp); this->str(temp);
} }
*/
} }
if (i < Count) if (tokens.size() < Count)
return false; return false;
else else
return true; return true;
@@ -146,6 +151,8 @@ std::string cParser::readToken(bool ToLower, const char *Break)
if (ToLower) if (ToLower)
c = tolower(c); c = tolower(c);
token += c; token += c;
if (findQuotes(token)) // do glue together words enclosed in quotes
break;
if (trimComments(token)) // don't glue together words separated with comment if (trimComments(token)) // don't glue together words separated with comment
break; break;
} }
@@ -181,32 +188,50 @@ std::string cParser::readToken(bool ToLower, const char *Break)
return token; return token;
} }
std::string cParser::readQuotes(char const Quote) { // read the stream until specified char or stream end
std::string token = "";
char c;
while( mStream->peek() != EOF && Quote != (c = mStream->get()) ) { // get all chars until the quote mark
token += c;
}
return token;
}
std::string cParser::readComment( std::string const &Break ) { // pobieranie znaków aż do znalezienia znacznika końca
std::string token = "";
while( mStream->peek() != EOF ) { // o ile nie koniec pliku
token += mStream->get(); // pobranie znaku
if( token.rfind( Break ) != std::string::npos ) // szukanie znacznika końca
break;
}
return token;
}
bool cParser::findQuotes( std::string &String ) {
if( String.rfind( '\"' ) != std::string::npos ) {
String.erase( String.rfind( '\"' ), 1 );
String += readQuotes();
return true;
}
return false;
}
bool cParser::trimComments(std::string &String) bool cParser::trimComments(std::string &String)
{ {
for (commentmap::iterator cmIt = mComments.begin(); cmIt != mComments.end(); ++cmIt) for (commentmap::iterator cmIt = mComments.begin(); cmIt != mComments.end(); ++cmIt)
{ {
if (String.find((*cmIt).first) != std::string::npos) if (String.rfind((*cmIt).first) != std::string::npos)
{ {
readComment((*cmIt).second); readComment((*cmIt).second);
String.resize(String.find((*cmIt).first)); String.resize(String.rfind((*cmIt).first));
return true; return true;
} }
} }
return false; return false;
} }
std::string cParser::readComment(std::string const &Break)
{ // pobieranie znaków aż do znalezienia znacznika końca
std::string token = "";
while (mStream->peek() != EOF)
{ // o ile nie koniec pliku
token += mStream->get(); // pobranie znaku
if (token.find(Break) != std::string::npos) // szukanie znacznika końca
break;
}
return token;
}
int cParser::getProgress() const int cParser::getProgress() const
{ {
return static_cast<int>( mStream->rdbuf()->pubseekoff(0, std::ios_base::cur) * 100 / mSize ); return static_cast<int>( mStream->rdbuf()->pubseekoff(0, std::ios_base::cur) * 100 / mSize );

View File

@@ -18,7 +18,7 @@ http://mozilla.org/MPL/2.0/.
///////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////
// cParser -- generic class for parsing text data, either from file or provided string // cParser -- generic class for parsing text data, either from file or provided string
class cParser : public std::stringstream class cParser //: public std::stringstream
{ {
public: public:
// parameters: // parameters:
@@ -33,6 +33,15 @@ class cParser : public std::stringstream
// destructor: // destructor:
virtual ~cParser(); virtual ~cParser();
// methods: // methods:
template <typename _Type>
cParser&
operator>>( _Type &Right );
template <>
cParser&
operator>>( std::string &Right );
template <>
cParser&
operator>>( bool &Right );
template <typename _Output> template <typename _Output>
_Output _Output
getToken( bool const ToLower = true ) getToken( bool const ToLower = true )
@@ -74,11 +83,13 @@ class cParser : public std::stringstream
// load traction? // load traction?
bool LoadTraction; bool LoadTraction;
protected: private:
// methods: // methods:
std::string readToken(bool ToLower = true, const char *Break = "\n\t ;"); std::string readToken(bool ToLower = true, const char *Break = "\n\t ;");
std::string readQuotes( char const Quote = '\"' );
std::string readComment( std::string const &Break = "\n\t ;" ); std::string readComment( std::string const &Break = "\n\t ;" );
std::string trtest; // std::string trtest;
bool findQuotes( std::string &String );
bool trimComments( std::string &String ); bool trimComments( std::string &String );
// members: // members:
std::istream *mStream; // relevant kind of buffer is attached on creation. std::istream *mStream; // relevant kind of buffer is attached on creation.
@@ -88,19 +99,42 @@ class cParser : public std::stringstream
commentmap mComments; commentmap mComments;
cParser *mIncludeParser; // child class to handle include directives. cParser *mIncludeParser; // child class to handle include directives.
std::vector<std::string> parameters; // parameter list for included file. std::vector<std::string> parameters; // parameter list for included file.
std::deque<std::string> tokens;
}; };
inline template<typename _Type>
cParser& cParser&
operator>>( cParser &Parser, bool &Right ) { cParser::operator>>( _Type &Right ) {
std::istream::sentry const streamokay( Parser ); if( true == this->tokens.empty() ) { return *this; }
if( streamokay ) {
std::string value; std::stringstream converter( this->tokens.front() );
Parser >> value; converter >> Right;
Right = ( value == "true" ); this->tokens.pop_front();
return *this;
} }
return Parser; template<>
cParser&
cParser::operator>>( std::string &Right ) {
if( true == this->tokens.empty() ) { return *this; }
Right = this->tokens.front();
this->tokens.pop_front();
return *this;
}
template<>
cParser&
cParser::operator>>( bool &Right ) {
if( true == this->tokens.empty() ) { return *this; }
Right = ( this->tokens.front() == "true" );
this->tokens.pop_front();
return *this;
} }