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 ==
"") {
128 bool needs_quotes =
false;
131 for(
char c: string_value) {
140 switch(string_value[0]) {
177 if(!needs_quotes && string_value.size() >= 5 && string_value[4] ==
'_') {
179 if((string_value[0] ==
'd' || string_value[0] ==
'D') &&
180 (string_value[1] ==
'a' || string_value[1] ==
'A') &&
181 (string_value[2] ==
't' || string_value[2] ==
'T') &&
182 (string_value[3] ==
'a' || string_value[3] ==
'A')) {
185 if((string_value[0] ==
's' || string_value[0] ==
'S') &&
186 (string_value[1] ==
'a' || string_value[1] ==
'A') &&
187 (string_value[2] ==
'v' || string_value[2] ==
'V') &&
188 (string_value[3] ==
'e' || string_value[3] ==
'E')) {
194 if(!needs_quotes && string_value.size() == 5 && string_value[4] ==
'_') {
196 if((string_value[0] ==
'l' || string_value[0] ==
'L') &&
197 (string_value[1] ==
'o' || string_value[1] ==
'O') &&
198 (string_value[2] ==
'o' || string_value[2] ==
'O') &&
199 (string_value[3] ==
'p' || string_value[3] ==
'P')) {
202 if((string_value[0] ==
's' || string_value[0] ==
'S') &&
203 (string_value[1] ==
't' || string_value[1] ==
'T') &&
204 (string_value[2] ==
'o' || string_value[2] ==
'O') &&
205 (string_value[3] ==
'p' || string_value[3] ==
'P')) {
210 if(!needs_quotes && string_value.size() == 7 && string_value[6] ==
'_') {
212 if((string_value[0] ==
'g' || string_value[0] ==
'G') &&
213 (string_value[1] ==
'l' || string_value[1] ==
'L') &&
214 (string_value[2] ==
'o' || string_value[2] ==
'O') &&
215 (string_value[3] ==
'b' || string_value[3] ==
'B') &&
216 (string_value[4] ==
'a' || string_value[4] ==
'A') &&
217 (string_value[5] ==
'l' || string_value[5] ==
'L')) {
223 value.value_ =
"\"" + string_value +
"\"";
225 value.value_ = string_value;
240 attribute_(attribute),
243 s << category_ <<
'.' << attribute_ <<
' ' << value_.GetValue() << std::endl;
260 std::map<String, int>::const_iterator i=index_map_.find(attribute);
261 return i==index_map_.end() ? -1 : i->second;
265 index_map_.insert(std::make_pair(attribute, index_map_.size()));
269 return index_map_.size();
273 std::vector<std::pair<int, String> > tmp;
274 for(
auto it = index_map_.begin(); it != index_map_.end(); ++it) {
275 tmp.push_back(std::make_pair(it->second, it->first));
277 std::sort(tmp.begin(), tmp.end());
278 for(
auto it = tmp.begin(); it != tmp.end(); ++it) {
279 s << category_ <<
"." << it->second << std::endl;
286 std::map<String, int> index_map_;
297 void AddData(
const std::vector<StarWriterValue>& data) {
298 if(data.size() != desc_.GetSize()) {
301 data_.insert(data_.end(), data.begin(), data.end());
304 const std::vector<StarWriterValue>&
GetData() {
return data_; }
307 return data_.size() / desc_.GetSize();
314 s <<
"loop_" << std::endl;
316 int desc_size = desc_.GetSize();
317 for(
size_t i = 0; i < data_.size(); ++i) {
318 s << data_[i].GetValue();
319 if((i+1) % desc_size == 0) {
329 std::vector<StarWriterValue> data_;
344 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)
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