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)
193 bool may_fail)
const;
212 bool may_fail)
const;
228 filename_ = filename;
241 std::vector<StringRef>& parts,
bool clear=
true);
246 void ParseLastDataItemRow();
251 void ParseEndDataItemRow();
256 if (std::getline(stream_, current_line_)) {
257 str=
StringRef(current_line_.data(), current_line_.length());
259 has_current_line_=
true;
267 if (has_current_line_) {
268 ref=
StringRef(current_line_.data(), current_line_.size());
271 return this->NextLine(ref);
276 assert(has_current_line_);
277 has_current_line_=
false;
286 void CallOnDataRow(
const StarLoopDesc& header,
287 const std::vector<String>& columns);
289 void ParseDataItemIdent(
const StringRef ident,
290 StringRef& cat, StringRef& name);
293 void ParseDataItem();
294 void DiagnoseUnknown();
295 bool ParseMultilineValue(
String& value,
bool skip=
false);
296 std::ifstream fstream_;
297 boost::iostreams::filtering_stream<boost::iostreams::input> stream_;
300 bool has_current_line_;
303 StarLoopDesc items_row_header_;
305 std::vector<String> items_row_values_;
convenient datatype for referencing character data
const StringRef & GetName() const
const StringRef & GetValue() const
const StringRef & GetCategory() const
StarDataItem(const StringRef &category, const StringRef &name, const StringRef &value)
void Add(const StringRef &name)
void SetCategory(const StringRef &category)
int GetIndex(const String &name) const
const String & GetCategory() const
std::pair< bool, float > TryGetFloat(const StringRef &data, const String &name, bool may_fail) const
try to convert a value to float, on failure raise an exception.
virtual bool OnBeginData(const StringRef &data_name)
invoked when a new data control structure is encountered
int GetCurrentLinenum() const
retrieve the line, the parser is currently working on
float TryGetFloat(const StringRef &data, const String &name) const
try to convert a value to float, on failure raise an exception.
static bool SplitLine(const StringRef &line, std::vector< StringRef > &parts, bool clear=true)
void SetFilename(const String &filename)
virtual void OnEndData()
called when leaving a datasection. Will only be invoked when OnBeginData() returned true.
String FormatDiagnostic(StarDiagType type, const String &message, int line=-1) const
format diagnostic and returns it as a string.
virtual void OnEndLoop()
invoked when leaving a loop
virtual void OnDataItem(const StarDataItem &item)
invoked when a data item is encountered
virtual void OnDataRow(const StarLoopDesc &header, const std::vector< StringRef > &columns)
invoked when a data row in a loop is encountered.
Real TryGetReal(const StringRef &data, const String &name) const
try to convert a value to Real, on failure raise an exception.
int TryGetInt(const StringRef &data, const String &name) const
try to convert a value to integer, on failure raise an exception.
bool TryGetBool(const StringRef &data, const String &name) const
try to convert a value to bool, on failure raise an exception.
StarParser(std::istream &stream, bool items_as_row=false)
create a StarParser
StarParser(const String &filename, bool items_as_row=false)
std::pair< bool, int > TryGetInt(const StringRef &data, const String &name, bool may_fail) const
try to convert a value to integer, exception can be turned off.
virtual bool OnBeginLoop(const StarLoopDesc &header)
called when a loop is encountered
Real GetRealOrDefault(const StringRef &data, const String &name, Real alt, bool(*is_default)(StringRef)) const
try to convert a value to Real, on failure return default value.