csv parser initial version

This commit is contained in:
2021-02-09 17:19:46 +01:00
parent d64570bf21
commit 5315e58823
5 changed files with 62 additions and 5 deletions

38
ml.cpp
View File

@@ -51,6 +51,8 @@ std::string read_file_contents(std::string filename) {
#include <sstream>
#include <exception>
#include "csvparser.h"
////////////////////////////////////////////////////////////////////////////////
/// ERROR MESSAGES /////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@@ -89,7 +91,7 @@ std::string read_file_contents(std::string filename) {
////////////////////////////////////////////////////////////////////////////////
// Convert an object to a string using a stringstream conveniently
#define to_string( x ) static_cast<std::ostringstream&>((std::ostringstream() << std::dec << x )).str()
#define to_string( x ) static_cast<std::ostringstream>((std::ostringstream() << std::dec << x )).str()
// Replace a substring with a replacement string in a source string
void replace_substring(std::string &src, std::string substr, std::string replacement) {
@@ -1189,6 +1191,38 @@ namespace builtin {
return Value(rand()%(high-low+1) + low);
}
// Get the contents of a file
Value parse_csv(std::vector<Value> args, Environment &env) {
eval_args(args, env);
// TODO add support for more params specifying options
if (args.size() != 1)
throw Error(Value("read-csv", parse_csv), env, args.size() > 1? TOO_MANY_ARGS : TOO_FEW_ARGS);
// PERF optimize it for memory usage and performance
CsvParser csv(true);
std::vector< std::vector<std::string> > parsed_data; // TODO some default size here
csv.parseCSV(args[0].as_string(), parsed_data);
int rows = parsed_data.size();
int cols = rows > 0 ? parsed_data[0].size() : 0;
std::vector<Value> result;
if (rows > 0 && cols > 0) {
for (int r = 0; r < rows; r++) {
std::vector<Value> row;
for (int c = 0; c < cols; c++) {
std::string value = parsed_data[r][c];
row.push_back(Value::string(value));
}
result.push_back(row);
}
}
return Value(result);
}
// Get the contents of a file
Value read_file(std::vector<Value> args, Environment &env) {
// Is not a special form, so we can evaluate our args.
@@ -1197,7 +1231,6 @@ namespace builtin {
if (args.size() != 1)
throw Error(Value("read-file", read_file), env, args.size() > 1? TOO_MANY_ARGS : TOO_FEW_ARGS);
// return Value::string(content);
return Value::string(read_file_contents(args[0].as_string()));
}
@@ -1736,6 +1769,7 @@ Value Environment::get(std::string name) const {
if (name == "input") return Value("input", builtin::input);
if (name == "random") return Value("random", builtin::random);
if (name == "include") return Value("include", builtin::include);
if (name == "parse-csv") return Value("parse-csv", builtin::parse_csv);
if (name == "read-file") return Value("read-file", builtin::read_file);
if (name == "write-file") return Value("write-file", builtin::write_file);
#endif