27 #include <boost/algorithm/string.hpp> 28 #include <boost/lexical_cast.hpp> 29 #include <boost/tokenizer.hpp> 50 size_t comment_pos = line.
find(comment);
51 if (comment_pos != std::string::npos) {
52 line = line.
substr(0, comment_pos);
73 if (keyword ==
"bool" || keyword ==
"boolean") {
75 }
else if (keyword ==
"int" || keyword ==
"int32") {
76 return typeid(int32_t);
77 }
else if (keyword ==
"long" || keyword ==
"int64") {
78 return typeid(int64_t);
79 }
else if (keyword ==
"float") {
81 }
else if (keyword ==
"double") {
82 return typeid(double);
83 }
else if (keyword ==
"string") {
85 }
else if (keyword ==
"[bool]" || keyword ==
"[boolean]") {
87 }
else if (keyword ==
"[int]" || keyword ==
"[int32]") {
89 }
else if (keyword ==
"[long]" || keyword ==
"[int64]") {
91 }
else if (keyword ==
"[float]") {
93 }
else if (keyword ==
"[double]") {
95 }
else if (keyword ==
"[bool+]" || keyword ==
"[boolean+]") {
97 }
else if (keyword ==
"[int+]" || keyword ==
"[int32+]") {
99 }
else if (keyword ==
"[long+]" || keyword ==
"[int64+]") {
101 }
else if (keyword ==
"[float+]") {
103 }
else if (keyword ==
"[double+]") {
120 if (boost::starts_with(line, comment)) {
123 boost::replace_all(line, comment,
"");
125 if (boost::starts_with(line,
"Column:")) {
133 if (descriptions.
count(name) != 0) {
139 if (!boost::starts_with(token,
"(") && token !=
"-") {
146 if (boost::starts_with(token,
"(")) {
153 if (line_stream && token ==
"-") {
157 while (line_stream) {
158 desc << token <<
' ';
162 boost::trim(desc_str);
163 descriptions.
emplace(std::piecewise_construct,
177 size_t columns_number) {
192 if (boost::starts_with(line, comment)) {
195 boost::replace_all(line, comment,
"");
200 if (boost::starts_with(line,
"Column:")) {
204 auto space_i = temp.
find(
' ');
206 temp = temp.
substr(0, space_i);
208 desc_names.emplace_back(
std::move(temp));
216 if (!last_comment.empty()){
220 while (line_stream) {
221 names.push_back(token);
224 if (names.size() != columns_number) {
231 if (desc_names.size() != 0 && desc_names.size() != columns_number) {
232 logger.
warn() <<
"Number of column descriptions does not matches the number" 233 <<
" of the columns";
238 if (names.size() < columns_number) {
239 for (
size_t i=names.size()+1; i<=columns_number; ++i) {
245 for (
auto name : names) {
246 if (!set.insert(name).second) {
255 template <
typename T>
258 boost::char_separator<char> sep {
","};
259 boost::tokenizer< boost::char_separator<char> > tok {str, sep};
260 for (
auto& s : tok) {
266 template <
typename T>
267 NdArray<T> convertStringToNdArray(
const std::string& str) {
270 }
else if (str[0] !=
'<') {
274 auto closing_char = str.
find(
'>');
275 if (closing_char == std::string::npos) {
279 auto shape_str = str.
substr(1, closing_char - 1);
280 auto shape_i = convertStringToVector<int32_t>(shape_str);
281 auto data = convertStringToVector<T>(str.
substr(closing_char + 1));
285 return NdArray<T>(shape_u, data);
292 if (type ==
typeid(
bool)) {
293 if (value ==
"true" || value ==
"t" || value ==
"yes" || value ==
"y" || value ==
"1") {
296 if (value ==
"false" || value ==
"f" || value ==
"no" || value ==
"n" || value ==
"0") {
299 }
else if (type ==
typeid(int32_t)) {
301 }
else if (type ==
typeid(int64_t)) {
303 }
else if (type ==
typeid(
float)) {
305 }
else if (type ==
typeid(
double)) {
330 }
catch( boost::bad_lexical_cast
const& ) {
341 size_t comment_pos = line.
find(comment);
342 if (comment_pos != std::string::npos) {
343 line = line.
substr(0, comment_pos);
359 size_t comment_pos = line.
find(comment);
360 if (comment_pos != std::string::npos) {
361 line = line.
substr(0, comment_pos);
std::size_t countRemainingRows(std::istream &in, const std::string &comment)
T forward_as_tuple(T... args)
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< bool >, NdArray::NdArray< int32_t >, NdArray::NdArray< int64_t >, NdArray::NdArray< float >, NdArray::NdArray< double > > cell_type
The possible cell types.
static Elements::Logging logger
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::type_index keywordToType(const std::string &keyword)
NdArray(const std::vector< size_t > &shape)
void warn(const std::string &logMessage)
std::map< std::string, ColumnDescription > autoDetectColumnDescriptions(std::istream &in, const std::string &comment)
Reads the column descriptions of the given stream.
This class gets a stream as argument during construction and when it is deleted it sets the position ...
bool hasNextRow(std::istream &in, const std::string &comment)
std::string quoted(const std::string &str)
T back_inserter(T... args)
static Logging getLogger(const std::string &name="")
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.