move constructing MlValues to clib from interpreter

This commit is contained in:
VaclavT 2021-02-14 08:56:14 +01:00
parent 2e58a31266
commit 8f0b19798f
3 changed files with 58 additions and 11 deletions

View File

@ -58,6 +58,33 @@ void CsvParser::parseCSV(const std::string &csvSource, std::vector< std::vector<
addLine(line, lines); addLine(line, lines);
} }
MlValue CsvParser::ivalualize(std::vector< std::vector<std::string> > &parsed_data) const {
int rows = parsed_data.size();
int cols = rows > 0 ? parsed_data[0].size() : 0;
std::vector<MlValue> result;
if (rows > 0 && cols > 0) {
for (int r = 0; r < rows; r++) {
std::vector<MlValue> row;
for (int c = 0; c < cols; c++) {
std::string value = parsed_data[r][c];
if (is_string_int(value)) {
row.push_back(MlValue(stoi(value)));
}
if (is_string_float(value)) {
row.push_back(MlValue(std::stod(value)));
} else {
row.push_back(MlValue::string(value));
}
}
result.push_back(row);
}
}
return result;
}
void CsvParser::addLine(const std::vector<std::string> &line, std::vector< std::vector<std::string> > &lines) { void CsvParser::addLine(const std::vector<std::string> &line, std::vector< std::vector<std::string> > &lines) {
if (skip_header && !header_skiped) { if (skip_header && !header_skiped) {
header_skiped = true; header_skiped = true;
@ -66,3 +93,18 @@ void CsvParser::addLine(const std::vector<std::string> &line, std::vector< std::
lines.push_back(line); lines.push_back(line);
} }
} }
// std::regex int_underscored_regex("[0-9][0-9_]+[0-9]");
std::regex int_regex("[0-9]+");
std::regex double_regex("[0-9]+\\.[0-9]+");
// Is string representing int value
bool CsvParser::is_string_int(const std::string &str) const {
return std::regex_match(str, int_regex);
}
// Is string representing float value
bool CsvParser::is_string_float(const std::string &str) const {
return std::regex_match(str, double_regex);
}

View File

@ -1,6 +1,9 @@
#include "../ml.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <regex>
class CsvParser { class CsvParser {
@ -18,6 +21,10 @@ public:
void parseCSV(const std::string &csvSource, std::vector< std::vector<std::string> > &lines); void parseCSV(const std::string &csvSource, std::vector< std::vector<std::string> > &lines);
MlValue ivalualize(std::vector< std::vector<std::string> > &parsed_data) const;
private: private:
void addLine(const std::vector<std::string> &line, std::vector< std::vector<std::string> > &lines); void addLine(const std::vector<std::string> &line, std::vector< std::vector<std::string> > &lines);
bool is_string_int(const std::string &str) const;
bool is_string_float(const std::string &str) const;
}; };

View File

@ -20,9 +20,6 @@
HttpClient::HttpClient(){}; HttpClient::HttpClient(){};
std::pair<int, std::string> HttpClient::doGetRequest(const std::string &url, const std::unordered_map<std::string, std::string> &headers) { std::pair<int, std::string> HttpClient::doGetRequest(const std::string &url, const std::unordered_map<std::string, std::string> &headers) {
// ^(?:((?:https?|s?ftp):)\/\/)([^:\/\s]+)(?::(\d*))?(?:\/([^\s?#]+)?([?][^?#]*)?(#.*)?)?
// viz test
// https://api.iextrading.com:443/1.0/stock/market/batch?symbols=cah,khc,syf,jnj&types=quote#muhehe
// https://stackoverflow.com/questions/25896916/parse-http-headers-in-c // https://stackoverflow.com/questions/25896916/parse-http-headers-in-c
std::regex rgx{R"(^(?:((?:https?|s?ftp):)//)([^:/\s]+)(?::(\d*))?(?:/([^\s?#]+)?([?][^?#]*)?(#.*)?)?)"}; std::regex rgx{R"(^(?:((?:https?|s?ftp):)//)([^:/\s]+)(?::(\d*))?(?:/([^\s?#]+)?([?][^?#]*)?(#.*)?)?)"};
@ -56,15 +53,17 @@ std::pair<int, std::string> HttpClient::doGetRequest(const std::string &url, con
// std::cout << i << ": '" << matches[i].str() << "'\n"; // std::cout << i << ": '" << matches[i].str() << "'\n";
} }
} else { } else {
std::cerr << "Match not found\n"; // TODO better message std::cerr << "Match not found" << std::endl; // TODO better message
} }
std::string headers_string = ""; std::string headers_string = "";
for (auto it = headers.begin(); it != headers.end(); ++it) { for (auto it = headers.begin(); it != headers.end(); ++it) {
headers_string.append("\r\n" + it->first + ": " + it->second); headers_string.append("\r\n" + it->first + ": " + it->second);
// std::cerr << "KEY: `" << it->first << "`, VALUE: `" << it->second << '`' << std::endl;
} }
std::string request = "GET " + full_url + " HTTP/1.0\r\nHost: " + server + headers_string + "\r\n\r\n";
std::string request = "GET " + full_url + " HTTP/1.0\r\nHost: " + server + headers_string + "\r\n\r\n";
// TODO memory leaks ??? // TODO memory leaks ???
int bytes_read = sslRequest(server, request); int bytes_read = sslRequest(server, request);
@ -107,10 +106,6 @@ std::pair<int, std::string> HttpClient::doGetRequest(const std::string &url, con
} }
} }
// for(auto& kv: headers_map) {
// std::cout << "KEY: `" << kv.first << "`, VALUE: `" << kv.second << '`' << std::endl;
// }
// TODO if error return error desc in string // TODO if error return error desc in string
return std::make_pair(200, body); return std::make_pair(200, body);
}; };
@ -135,8 +130,10 @@ int HttpClient::sslRecvPacket() {
char buf[len + 1]; char buf[len + 1];
do { do {
len = SSL_read(ssl, buf, len); len = SSL_read(ssl, buf, len);
buf[len] = 0; if (len >= 0) {
ssl_read_packet.append((const char *)buf); buf[len] = 0;
ssl_read_packet.append((const char *)buf);
}
} while (len > 0); } while (len > 0);
if (len < 0) { if (len < 0) {
@ -224,6 +221,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
// printf ("SSL connection using %s\n", SSL_get_cipher (ssl)); // printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
// send request // send request
//std::err << request << std::endl;
sslSendPacket(request); sslSendPacket(request);
// read response and return its length // read response and return its length