diff --git a/clib/csvparser.cpp b/clib/csvparser.cpp index 66aa466..fcbeb48 100644 --- a/clib/csvparser.cpp +++ b/clib/csvparser.cpp @@ -58,6 +58,33 @@ void CsvParser::parseCSV(const std::string &csvSource, std::vector< std::vector< addLine(line, lines); } +MlValue CsvParser::ivalualize(std::vector< std::vector > &parsed_data) const { + int rows = parsed_data.size(); + int cols = rows > 0 ? parsed_data[0].size() : 0; + + std::vector result; + + if (rows > 0 && cols > 0) { + for (int r = 0; r < rows; r++) { + std::vector 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 &line, std::vector< std::vector > &lines) { if (skip_header && !header_skiped) { header_skiped = true; @@ -66,3 +93,18 @@ void CsvParser::addLine(const std::vector &line, std::vector< std:: 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); +} diff --git a/clib/csvparser.h b/clib/csvparser.h index 212c4d8..2548906 100644 --- a/clib/csvparser.h +++ b/clib/csvparser.h @@ -1,6 +1,9 @@ +#include "../ml.h" + #include #include +#include class CsvParser { @@ -18,6 +21,10 @@ public: void parseCSV(const std::string &csvSource, std::vector< std::vector > &lines); + MlValue ivalualize(std::vector< std::vector > &parsed_data) const; private: void addLine(const std::vector &line, std::vector< std::vector > &lines); + + bool is_string_int(const std::string &str) const; + bool is_string_float(const std::string &str) const; }; diff --git a/clib/sslclient.cpp b/clib/sslclient.cpp index 2f91eab..0d3c621 100644 --- a/clib/sslclient.cpp +++ b/clib/sslclient.cpp @@ -20,9 +20,6 @@ HttpClient::HttpClient(){}; std::pair HttpClient::doGetRequest(const std::string &url, const std::unordered_map &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 std::regex rgx{R"(^(?:((?:https?|s?ftp):)//)([^:/\s]+)(?::(\d*))?(?:/([^\s?#]+)?([?][^?#]*)?(#.*)?)?)"}; @@ -56,15 +53,17 @@ std::pair HttpClient::doGetRequest(const std::string &url, con // std::cout << i << ": '" << matches[i].str() << "'\n"; } } else { - std::cerr << "Match not found\n"; // TODO better message + std::cerr << "Match not found" << std::endl; // TODO better message } std::string headers_string = ""; for (auto it = headers.begin(); it != headers.end(); ++it) { 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 ??? int bytes_read = sslRequest(server, request); @@ -107,10 +106,6 @@ std::pair 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 return std::make_pair(200, body); }; @@ -135,8 +130,10 @@ int HttpClient::sslRecvPacket() { char buf[len + 1]; do { len = SSL_read(ssl, buf, len); - buf[len] = 0; - ssl_read_packet.append((const char *)buf); + if (len >= 0) { + buf[len] = 0; + ssl_read_packet.append((const char *)buf); + } } while (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)); // send request + //std::err << request << std::endl; sslSendPacket(request); // read response and return its length