10 #include <boost/algorithm/string/predicate.hpp>
11 #include <boost/algorithm/string/trim.hpp>
20 Tokenizer::Tokenizer(
const FilePath &fp)
24 line_counter_(0), position_(0),
25 separator_(
"\\",
" \t",
"\""),
26 line_tokenizer_(line_, separator_)
28 in_ = own_stream_ =
new ifstream;
29 own_stream_->open(
string(fp).c_str() );
31 INPUT_CHECK(! own_stream_->fail(),
"Can not open input file '%s'.\n", f_name_.c_str() );
38 Tokenizer::Tokenizer( std::istream &in)
39 : f_name_(
"__anonymous_stream__"),
43 line_counter_(0), position_(0),
44 separator_(
"\\",
" \t",
"\""),
45 line_tokenizer_(line_, separator_)
49 void Tokenizer::set_comment_pattern(
const std::string &pattern) {
50 comment_pattern_=pattern;
54 bool Tokenizer::skip_to(
const std::string& pattern,
const std::string &end_search_pattern)
56 ASSERT( in_->good(),
"Tokenizer stream (for file: %s) is not ready for i/o operations. Perhaps missing check about correct open.\n", f_name_.c_str());
57 bool end_search= (end_search_pattern.size() > 0);
60 if (line_.find(pattern)!=string::npos ) {
64 if ( end_search && line_.find(end_search_pattern)!=string::npos )
return false;
72 bool Tokenizer::next_line(
bool assert_for_remaining_tokens) {
74 if (assert_for_remaining_tokens && (! eol() )) {
76 xprintf(
Warn,
"Remaining tokens, file '%s', line '%d', after token #%d.\n", f_name_.c_str(), line_num(), position_);
79 if (eof())
return false;
83 while ( ! eof() && line_ ==
"") {
84 std::getline( *in_, line_);
87 if (in_->bad())
xprintf(
Err,
"Can not read from stream, file: '%s', line: '%d'\n", f_name_.c_str(), line_num());
90 if (comment_pattern_.size() && 0==line_.compare(0, comment_pattern_.size(), comment_pattern_) ) line_=
"";
96 DBGMSG(
"Line: '%s'\n", line_.c_str());
106 if ( eol() )
xprintf(
UsrErr,
"Missing token, file: '%s', line: '%d', position: '%d'.\n", f_name_.c_str(), line_num(), position_);
112 void Tokenizer::set_tokenizer()
114 line_tokenizer_.assign(line_);
115 tok_ = line_tokenizer_.begin();
118 while (! eol() && (*tok_).size()==0 ) {position_++; ++tok_;}
124 string Tokenizer::position_msg()
const {
126 ss <<
"token: " << pos() <<
", line: " << line_num() <<
", in file '" << f_name() <<
"'";
131 Tokenizer::~Tokenizer() {
132 if (own_stream_ != NULL)
delete own_stream_;