19 #ifndef OST_IO_STAR_PARSER_HH
20 #define OST_IO_STAR_PARSER_HH
26 #include <boost/iostreams/filtering_stream.hpp>
35 namespace ost {
namespace io {
48 category_(category), name_(name), value_(value)
67 std::map<String, int>::const_iterator i=index_map_.find(name);
68 return i==index_map_.end() ? -1 : i->second;
73 category_=category.
str();
80 index_map_.insert(std::make_pair(name.
str(), index_map_.size()));
84 return index_map_.size();
95 std::map<String, int> index_map_;
122 explicit StarParser(std::istream& stream,
bool items_as_row=
false);
140 const std::vector<StringRef>& columns)
178 std::pair<bool, float> TryGetFloat(
const StringRef& data,
180 bool may_fail)
const;
197 std::pair<bool, int> TryGetInt(
const StringRef& data,
199 bool may_fail)
const;
215 filename_ = filename;
227 static bool SplitLine(
const StringRef& line,
228 std::vector<StringRef>& parts,
bool clear=
true);
233 void ParseLastDataItemRow();
238 void ParseEndDataItemRow();
243 if (std::getline(stream_, current_line_)) {
244 str=
StringRef(current_line_.data(), current_line_.length());
246 has_current_line_=
true;
252 bool GetLine(StringRef& ref)
254 if (has_current_line_) {
255 ref=StringRef(current_line_.data(), current_line_.size());
258 return this->NextLine(ref);
263 assert(has_current_line_);
264 has_current_line_=
false;
273 void CallOnDataRow(
const StarLoopDesc& header,
274 const std::vector<String>& columns);
276 void ParseDataItemIdent(
const StringRef ident,
277 StringRef& cat, StringRef& name);
280 void ParseDataItem();
281 void DiagnoseUnknown();
282 bool ParseMultilineValue(
String& value,
bool skip=
false);
283 std::ifstream fstream_;
284 boost::iostreams::filtering_stream<boost::iostreams::input> stream_;
287 bool has_current_line_;
290 StarLoopDesc items_row_header_;
292 std::vector<String> items_row_values_;
convenient datatype for referencing character data
const StringRef & GetCategory() const
const StringRef & GetValue() const
const StringRef & GetName() const
int GetCurrentLinenum() const
retrieve the line, the parser is currently working on
virtual void OnDataRow(const StarLoopDesc &header, const std::vector< StringRef > &columns)
invoked when a data row in a loop is encountered.
virtual bool OnBeginLoop(const StarLoopDesc &header)
called when a loop is encountered
virtual void OnDataItem(const StarDataItem &item)
invoked when a data item is encountered
void SetCategory(const StringRef &category)
parser for the STAR file format
StarDataItem(const StringRef &category, const StringRef &name, const StringRef &value)
virtual bool OnBeginData(const StringRef &data_name)
invoked when a new data control structure is encountered
virtual void OnEndLoop()
invoked when leaving a loop
void SetFilename(const String &filename)
const String & GetCategory() const
int GetIndex(const String &name) const
void Add(const StringRef &name)
virtual void OnEndData()
called when leaving a datasection. Will only be invoked when OnBeginData() returned true...