19 #ifndef OST_IO_STAR_WRITER_HH
20 #define OST_IO_STAR_WRITER_HH
24 #include <boost/iostreams/filtering_stream.hpp>
25 #include <boost/iostreams/filter/gzip.hpp>
26 #include <boost/shared_ptr.hpp>
37 len = std::snprintf(data,
sizeof(data),
"%.0f", f);
40 len = std::snprintf(data,
sizeof(data),
"%.1f", f);
43 len = std::snprintf(data,
sizeof(data),
"%.2f", f);
46 len = std::snprintf(data,
sizeof(data),
"%.3f", f);
49 len = std::snprintf(data,
sizeof(data),
"%.4f", f);
52 len = std::snprintf(data,
sizeof(data),
"%.5f", f);
55 len = std::snprintf(data,
sizeof(data),
"%.6f", f);
61 if(len < 0 || len > 20) {
68 namespace ost {
namespace io {
70 class StarWriterObject;
71 class StarWriterValue;
72 class StarWriterDataItem;
73 class StarWriterLoopDesc;
96 value.value_ = std::to_string(int_value);
101 fts(float_value, decimals, value.value_);
107 if(string_value ==
"") {
111 value.value_ =
"\n;" + string_value +
"\n;\n";
114 value.value_ =
"\"" + string_value +
"\"";
117 value.value_ = string_value;
146 if(isspace(c) && c !=
'\n') {
185 if(v.size() >= 5 && v[4] ==
'_') {
187 if((v[0] ==
'd' || v[0] ==
'D') &&
188 (v[1] ==
'a' || v[1] ==
'A') &&
189 (v[2] ==
't' || v[2] ==
'T') &&
190 (v[3] ==
'a' || v[3] ==
'A')) {
193 if((v[0] ==
's' || v[0] ==
'S') &&
194 (v[1] ==
'a' || v[1] ==
'A') &&
195 (v[2] ==
'v' || v[2] ==
'V') &&
196 (v[3] ==
'e' || v[3] ==
'E')) {
202 if(v.size() == 5 && v[4] ==
'_') {
204 if((v[0] ==
'l' || v[0] ==
'L') &&
205 (v[1] ==
'o' || v[1] ==
'O') &&
206 (v[2] ==
'o' || v[2] ==
'O') &&
207 (v[3] ==
'p' || v[3] ==
'P')) {
210 if((v[0] ==
's' || v[0] ==
'S') &&
211 (v[1] ==
't' || v[1] ==
'T') &&
212 (v[2] ==
'o' || v[2] ==
'O') &&
213 (v[3] ==
'p' || v[3] ==
'P')) {
218 if(v.size() == 7 && v[6] ==
'_') {
220 if((v[0] ==
'g' || v[0] ==
'G') &&
221 (v[1] ==
'l' || v[1] ==
'L') &&
222 (v[2] ==
'o' || v[2] ==
'O') &&
223 (v[3] ==
'b' || v[3] ==
'B') &&
224 (v[4] ==
'a' || v[4] ==
'A') &&
225 (v[5] ==
'l' || v[5] ==
'L')) {
251 attribute_(attribute),
254 s << category_ <<
'.' << attribute_ <<
' ' << value_.GetValue() << std::endl;
271 std::map<String, int>::const_iterator i=index_map_.find(attribute);
272 return i==index_map_.end() ? -1 : i->second;
276 index_map_.insert(std::make_pair(attribute, index_map_.size()));
280 return index_map_.size();
284 std::vector<std::pair<int, String> > tmp;
285 for(
auto it = index_map_.begin(); it != index_map_.end(); ++it) {
286 tmp.push_back(std::make_pair(it->second, it->first));
288 std::sort(tmp.begin(), tmp.end());
289 for(
auto it = tmp.begin(); it != tmp.end(); ++it) {
290 s << category_ <<
"." << it->second << std::endl;
297 std::map<String, int> index_map_;
308 void AddData(
const std::vector<StarWriterValue>& data) {
309 if(data.size() != desc_.GetSize()) {
312 data_.insert(data_.end(), data.begin(), data.end());
315 const std::vector<StarWriterValue>&
GetData() {
return data_; }
318 return data_.size() / desc_.GetSize();
325 s <<
"loop_" << std::endl;
327 int desc_size = desc_.GetSize();
328 for(
size_t i = 0; i < data_.size(); ++i) {
329 const String& v = data_[i].GetValue();
331 bool ends_with_newline = v.back() ==
'\n';
332 if((i+1) % desc_size == 0) {
334 if(!ends_with_newline) {
345 std::vector<StarWriterValue> data_;
360 std::vector<StarWriterObjectPtr> categories_to_write_;
const String & GetAttribute() const
virtual void ToStream(std::ostream &s)
StarWriterDataItem(const String &category, const String &attribute, const StarWriterValue &value)
const StarWriterValue & GetValue() const
const String & GetCategory() const
void Write(const String &data_name, std::ostream &stream)
void Push(StarWriterObjectPtr obj)
void Write(const String &data_name, const String &filename)
StarWriterLoopDesc(const String &category)
void Add(const String &attribute)
int GetIndex(const String &attribute) const
virtual void ToStream(std::ostream &s)
const String & GetCategory() const
StarWriterLoop(const StarWriterLoopDesc &desc)
const std::vector< StarWriterValue > & GetData()
const StarWriterLoopDesc & GetDesc()
void AddData(const std::vector< StarWriterValue > &data)
virtual void ToStream(std::ostream &s)
virtual void ToStream(std::ostream &s)=0
virtual ~StarWriterObject()
const String & GetValue() const
static StarWriterValue FromInt(int int_value)
static StarWriterValue FromFloat(Real float_value, int decimals)
static StarWriterValue FromString(const String &string_value)
static bool HasNewline(const String &v)
static bool NeedsQuotes(const String &v)
boost::shared_ptr< StarWriterLoop > StarWriterLoopPtr
boost::shared_ptr< StarWriterDataItem > StarWriterDataItemPtr
boost::shared_ptr< StarWriterLoopDesc > StarWriterLoopDescPtr
boost::shared_ptr< StarWriterObject > StarWriterObjectPtr
boost::shared_ptr< StarWriterValue > StarWriterValuePtr