4 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
8 * Alexander Aksenov <a.aksenov@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
23 * - Samsung RnD Institute Russia
29 #include <boost/algorithm/string.hpp>
30 #include <data_writer.h>
31 #include "writer_csv.h"
34 static bool first_element = true;
36 static void prepare_output(std::ostream &ios)
41 first_element = false;
44 static inline void start_output()
49 static inline void finish_output()
51 first_element = false;
57 static void data_out(const std::string &name, const T val, std::ostream &ios)
65 static void data_hex_out(const std::string &name, const T val,
70 ios << "0x" << std::hex << val << std::dec;
74 static void args_out(const std::string &mod, const T val, std::ostream &ios)
78 ios << "\"" << mod << "\"," << val;
82 static void args_vector_out(const std::string &mod, const std::vector<T> &val,
83 const uint64_t p, std::ostream &ios)
87 ios << "\"" << mod << "\"," << val.size() << ",0x" << std::hex << p
90 for (auto const &item : val)
98 void DataWriterCSV::start_msg()
103 void DataWriterCSV::finish_msg()
108 void DataWriterCSV::output_val(const std::string &name, const uint8_t val)
110 data_out(name, val, ios_);
113 void DataWriterCSV::output_val(const std::string &name, const uint32_t val)
115 data_out(name, val, ios_);
118 void DataWriterCSV::output_val(const std::string &name, const uint64_t val)
120 data_out(name, val, ios_);
123 void DataWriterCSV::output_val(const std::string &name, const float val)
125 data_out(name, val, ios_);
128 void DataWriterCSV::output_val(const std::string &name, const double val)
130 data_out(name, val, ios_);
133 void DataWriterCSV::output_val(const std::string &name, const std::string &val)
135 prepare_output(ios_);
139 if ((val.find("\n") == std::string::npos) && (val.find("\r") == std::string::npos)) {
142 std::string new_val(val);
144 boost::replace_all(new_val, "\n", "\\n");
145 boost::replace_all(new_val, "\r", "\\r");
152 void DataWriterCSV::output_val(const std::string &name, const char val)
154 data_out(name, val, ios_);
157 void DataWriterCSV::output_val(const std::string &name, const int val)
159 data_out(name, val, ios_);
162 void DataWriterCSV::output_hex(const std::string &name, const uint32_t val)
164 data_hex_out(name, val, ios_);
167 void DataWriterCSV::output_hex(const std::string &name, const uint64_t val)
169 data_hex_out(name, val, ios_);
172 void DataWriterCSV::output_args(const std::string &name, const bool val)
174 args_out("b", val, ios_);
177 void DataWriterCSV::output_args(const std::string &name, const uint8_t val)
179 args_out("c", val, ios_);
182 void DataWriterCSV::output_args(const std::string &name, const uint32_t val)
184 args_out("d", val, ios_);
187 void DataWriterCSV::output_args(const std::string &name, const uint64_t val)
189 args_out("x", val, ios_);
192 void DataWriterCSV::output_args(const std::string &name, const float val)
194 args_out("f", val, ios_);
197 void DataWriterCSV::output_args(const std::string &name, const double val)
199 args_out("w", val, ios_);
202 void DataWriterCSV::output_args(const std::string &name, const std::string &val)
204 prepare_output(ios_);
208 if ((val.find("\n") == std::string::npos) && (val.find("\r") == std::string::npos)) {
211 std::string new_val(val);
213 boost::replace_all(new_val, "\n", "\\n");
214 boost::replace_all(new_val, "\r", "\\r");
221 void DataWriterCSV::output_args(const std::string &name,
222 const std::vector<float> &val, uint64_t p)
224 args_vector_out("F", val, p, ios_);
227 void DataWriterCSV::output_args(const std::string &name,
228 const std::vector<uint32_t> &val, uint64_t p)
230 args_vector_out("D", val, p, ios_);
233 void DataWriterCSV::output_time(const std::string &name, const uint64_t val)
235 prepare_output(ios_);
240 void DataWriterCSV::output_args_hex(const std::string &name, const uint64_t val)
242 prepare_output(ios_);
244 ios_ << "0x" << std::hex << val << std::dec;
247 void DataWriterCSV::output_buf(const std::vector<char> &buf)
249 // TODO No place with implemnetation