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;
85 virtual void ToStream(std::ostream& s) = 0;
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_;
340 void Write(
const String& data_name,
const String& filename);
341 void Write(
const String& data_name, std::ostream& stream);
344 std::vector<StarWriterObjectPtr> categories_to_write_;
StarWriterLoopDesc(const String &category)
virtual void ToStream(std::ostream &s)
const String & GetValue() const
int GetIndex(const String &attribute) const
const String & GetCategory() const
virtual void ToStream(std::ostream &s)
const String & GetCategory() const
virtual void ToStream(std::ostream &s)
boost::shared_ptr< StarWriterLoop > StarWriterLoopPtr
StarWriterDataItem(const String &category, const String &attribute, const StarWriterValue &value)
void Push(StarWriterObjectPtr obj)
static StarWriterValue FromInt(int int_value)
boost::shared_ptr< StarWriterObject > StarWriterObjectPtr
StarWriterLoop(const StarWriterLoopDesc &desc)
boost::shared_ptr< StarWriterDataItem > StarWriterDataItemPtr
const std::vector< StarWriterValue > & GetData()
static StarWriterValue FromString(const String &string_value)
void AddData(const std::vector< StarWriterValue > &data)
const StarWriterValue & GetValue() const
boost::shared_ptr< StarWriterLoopDesc > StarWriterLoopDescPtr
virtual ~StarWriterObject()
const StarWriterLoopDesc & GetDesc()
void Add(const String &attribute)
const String & GetAttribute() const
boost::shared_ptr< StarWriterValue > StarWriterValuePtr
static StarWriterValue FromFloat(Real float_value, int decimals)