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);
244 return value.
size()==1 && (value[0]==
'?' || value[0]==
'.');
251 void ParseLastDataItemRow();
256 void ParseEndDataItemRow();
261 if (std::getline(stream_, current_line_)) {
262 str=
StringRef(current_line_.data(), current_line_.length());
264 has_current_line_=
true;
270 bool GetLine(StringRef& ref)
272 if (has_current_line_) {
273 ref=StringRef(current_line_.data(), current_line_.size());
276 return this->NextLine(ref);
281 assert(has_current_line_);
282 has_current_line_=
false;
291 void CallOnDataRow(
const StarLoopDesc& header,
292 const std::vector<String>& columns);
294 void ParseDataItemIdent(
const StringRef ident,
295 StringRef& cat, StringRef& name);
298 void ParseDataItem();
299 void DiagnoseUnknown();
300 bool ParseMultilineValue(
String& value,
bool skip=
false);
301 std::ifstream fstream_;
302 boost::iostreams::filtering_stream<boost::iostreams::input> stream_;
305 bool has_current_line_;
308 StarLoopDesc items_row_header_;
310 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
parser for the STAR file format
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)
static bool IsUndefined(StringRef value)
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.