29#include <boost/algorithm/string.hpp>
30#include <boost/lexical_cast.hpp>
31#include <boost/spirit/include/qi.hpp>
32#include <boost/tokenizer.hpp>
51 size_t comment_pos = line.
find(comment);
52 if (comment_pos != std::string::npos) {
53 line = line.
substr(0, comment_pos);
77 {
"bool",
typeid(bool)},
78 {
"boolean",
typeid(bool)},
80 {
"int",
typeid(int32_t)},
81 {
"long",
typeid(int64_t)},
82 {
"int32",
typeid(int32_t)},
83 {
"int64",
typeid(int64_t)},
85 {
"float",
typeid(float)},
86 {
"double",
typeid(double)},
99 {
"[int+]",
typeid(NdArray<int32_t>)},
100 {
"[long+]",
typeid(NdArray<int64_t>)},
101 {
"[int32+]",
typeid(NdArray<int32_t>)},
102 {
"[int64+]",
typeid(NdArray<int64_t>)},
103 {
"[float+]",
typeid(NdArray<float>)},
104 {
"[double+]",
typeid(NdArray<double>)},
109 if (p->first == keyword) {
126 if (boost::starts_with(line, comment)) {
129 boost::replace_all(line, comment,
"");
131 if (boost::starts_with(line,
"Column:")) {
139 if (descriptions.
count(name) != 0) {
145 if (!boost::starts_with(token,
"(") && token !=
"-") {
152 if (boost::starts_with(token,
"(")) {
159 if (line_stream && token ==
"-") {
163 while (line_stream) {
164 desc << token <<
' ';
168 boost::trim(desc_str);
195 if (boost::starts_with(line, comment)) {
198 boost::replace_all(line, comment,
"");
203 if (boost::starts_with(line,
"Column:")) {
207 auto space_i = temp.
find(
' ');
209 temp = temp.
substr(0, space_i);
211 desc_names.emplace_back(
std::move(temp));
219 if (!last_comment.empty()) {
223 while (line_stream) {
224 names.push_back(token);
227 if (names.size() != columns_number) {
234 if (desc_names.size() != 0 && desc_names.size() != columns_number) {
235 logger.
warn() <<
"Number of column descriptions does not matches the number"
236 <<
" of the columns";
241 if (names.size() < columns_number) {
242 for (
size_t i = names.size() + 1; i <= columns_number; ++i) {
248 for (
auto name : names) {
249 if (!set.insert(name).second) {
261 boost::char_separator<char> sep{
","};
262 boost::tokenizer<boost::char_separator<char>> tok{str, sep};
263 for (
auto& s : tok) {
270NdArray<T> convertStringToNdArray(
const std::string& str) {
273 }
else if (str[0] !=
'<') {
277 auto closing_char = str.
find(
'>');
278 if (closing_char == std::string::npos) {
282 auto shape_str = str.
substr(1, closing_char - 1);
283 auto shape_i = convertStringToVector<int32_t>(shape_str);
284 auto data = convertStringToVector<T>(str.
substr(closing_char + 1));
288 return NdArray<T>(shape_u, data);
297 if (value ==
"true" || value ==
"t" || value ==
"yes" || value ==
"y" || value ==
"1") {
299 }
else if (value ==
"false" || value ==
"f" || value ==
"no" || value ==
"n" || value ==
"0") {
305 {
typeid(int32_t), boost::lexical_cast<int32_t, const std::string&>},
306 {
typeid(int64_t), boost::lexical_cast<int64_t, const std::string&>},
308 {
typeid(float), boost::lexical_cast<float, const std::string&>},
309 {
typeid(double), boost::lexical_cast<double, const std::string&>},
311 {
typeid(
std::string), boost::lexical_cast<std::string, const std::string&>},
331 return i->second(value);
332 }
catch (boost::bad_lexical_cast
const&) {
342 size_t comment_pos = line.
find(comment);
343 if (comment_pos != std::string::npos) {
344 line = line.
substr(0, comment_pos);
360 size_t comment_pos = line.
find(comment);
361 if (comment_pos != std::string::npos) {
362 line = line.
substr(0, comment_pos);
374 size_t comment_pos = line.
find(comment);
376 if (comment_pos != std::string::npos) {
377 line = line.
substr(0, comment_pos);
385 while (line_stream) {
397 while (in && boost::starts_with(line, comment)) {
404 namespace qi = boost::spirit::qi;
408 auto it1 = token.
begin(), it2 = it1;
409 if (qi::parse(it1, token.
end(), qi::long_, l) && it1 == token.
end()) {
410 return {
typeid(int64_t), 0};
412 if (qi::parse(it2, token.
end(), qi::double_, d) && it2 == token.
end()) {
413 return {
typeid(double), 0};
T back_inserter(T... args)
static Logging getLogger(const std::string &name="")
void warn(const std::string &logMessage)
NdArray(std::vector< size_t > shape_)
boost::variant< bool, int32_t, int64_t, float, double, std::string, std::vector< bool >, std::vector< int32_t >, std::vector< int64_t >, std::vector< float >, std::vector< double >, NdArray::NdArray< int32_t >, NdArray::NdArray< int64_t >, NdArray::NdArray< float >, NdArray::NdArray< double > > cell_type
The possible cell types.
This class gets a stream as argument during construction and when it is deleted it sets the position ...
T emplace_back(T... args)
T forward_as_tuple(T... args)
std::vector< std::string > splitLine(std::string line, const std::string &comment)
std::type_index keywordToType(const std::string &keyword)
bool hasNextRow(std::istream &in, const std::string &comment)
std::map< std::string, ColumnDescription > autoDetectColumnDescriptions(std::istream &in, const std::string &comment)
Reads the column descriptions of the given stream.
size_t countColumns(std::istream &in, const std::string &comment)
Returns the number of whitespace separated tokens of the first non commented line.
std::vector< std::string > autoDetectColumnNames(std::istream &in, const std::string &comment, size_t columns_number)
Reads the column names of the given stream.
std::pair< std::type_index, std::size_t > guessColumnType(const std::string &token)
Row::cell_type convertToCellType(const std::string &value, std::type_index type)
Converts the given value to a Row::cell_type of the given type.
std::vector< std::string > firstDataLine(std::istream &in, const std::string &comment)
const std::vector< std::pair< std::string, std::type_index > > KeywordTypeMap
std::size_t countRemainingRows(std::istream &in, const std::string &comment)
const std::map< std::type_index, std::function< Row::cell_type(const std::string &)> > sCellConverter
static Elements::Logging logger
std::string quoted(const std::string &str)