some TODOs solved
This commit is contained in:
parent
2d26c59df6
commit
765f2bc673
|
|
@ -14,16 +14,20 @@ CsvParser::CsvParser(bool skip_hdr, char field_sep, char quote_ch, char line_sep
|
||||||
}
|
}
|
||||||
|
|
||||||
MlValue CsvParser::parseCSV(const std::string &csvSource) {
|
MlValue CsvParser::parseCSV(const std::string &csvSource) {
|
||||||
int linesRead = 0;
|
constexpr size_t INITIAL_PARSED_ROWS_SIZE = 128;
|
||||||
|
constexpr size_t INITIAL_COLUMNS_SIZE = 32;
|
||||||
|
constexpr size_t ROWS_READ_FOR_SIZE_ESTIMATION = 16;
|
||||||
|
|
||||||
|
size_t linesRead = 0;
|
||||||
bool inQuote(false);
|
bool inQuote(false);
|
||||||
bool newLine(false);
|
bool newLine(false);
|
||||||
std::string field;
|
std::string field;
|
||||||
|
|
||||||
std::vector<MlValue> parsed_data;
|
std::vector<MlValue> parsed_rows;
|
||||||
parsed_data.reserve(128); // TODO introduce constant here
|
parsed_rows.reserve(INITIAL_PARSED_ROWS_SIZE);
|
||||||
|
|
||||||
std::vector<MlValue> line;
|
std::vector<MlValue> line;
|
||||||
line.reserve(32); // TODO introduce constant here
|
line.reserve(INITIAL_COLUMNS_SIZE);
|
||||||
|
|
||||||
std::string::const_iterator aChar = csvSource.begin();
|
std::string::const_iterator aChar = csvSource.begin();
|
||||||
std::string::const_iterator aEnd = csvSource.end();
|
std::string::const_iterator aEnd = csvSource.end();
|
||||||
|
|
@ -45,14 +49,14 @@ MlValue CsvParser::parseCSV(const std::string &csvSource) {
|
||||||
} else {
|
} else {
|
||||||
if (!newLine) {
|
if (!newLine) {
|
||||||
line.push_back(ivalualize(field));
|
line.push_back(ivalualize(field));
|
||||||
add_line(line, parsed_data);
|
add_row(line, parsed_rows);
|
||||||
field.clear();
|
field.clear();
|
||||||
line.clear();
|
line.clear();
|
||||||
linesRead++;
|
linesRead++;
|
||||||
if (linesRead == 16) {
|
if (linesRead == ROWS_READ_FOR_SIZE_ESTIMATION) {
|
||||||
size_t linesEstimation = csvSource.size() / (std::distance(csvSource.begin(), aChar) / linesRead);
|
size_t linesEstimation = csvSource.size() / (std::distance(csvSource.begin(), aChar) / linesRead);
|
||||||
if (linesEstimation > parsed_data.capacity())
|
if (linesEstimation > parsed_rows.capacity())
|
||||||
parsed_data.reserve(linesEstimation);
|
parsed_rows.reserve(linesEstimation);
|
||||||
}
|
}
|
||||||
newLine = true;
|
newLine = true;
|
||||||
}
|
}
|
||||||
|
|
@ -68,18 +72,18 @@ MlValue CsvParser::parseCSV(const std::string &csvSource) {
|
||||||
if (!field.empty())
|
if (!field.empty())
|
||||||
line.push_back(ivalualize(field));
|
line.push_back(ivalualize(field));
|
||||||
|
|
||||||
add_line(line, parsed_data);
|
add_row(line, parsed_rows);
|
||||||
|
|
||||||
return parsed_data;
|
return parsed_rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void CsvParser::add_line(const std::vector<MlValue> &line, std::vector<MlValue> &lines) {
|
void CsvParser::add_row(const std::vector<MlValue> &columns, std::vector<MlValue> &rows) {
|
||||||
if (skip_header && !header_skiped) {
|
if (skip_header && !header_skiped) {
|
||||||
header_skiped = true;
|
header_skiped = true;
|
||||||
} else {
|
} else {
|
||||||
if (!line.empty())
|
if (!columns.empty())
|
||||||
lines.emplace_back(line);
|
rows.emplace_back(columns);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ public:
|
||||||
MlValue parseCSV(const std::string &csvSource);
|
MlValue parseCSV(const std::string &csvSource);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void add_line(const std::vector<MlValue> &line, std::vector<MlValue> &lines);
|
void add_row(const std::vector<MlValue> &columns, std::vector<MlValue> &rows);
|
||||||
|
|
||||||
static MlValue ivalualize(const std::string &value) ;
|
static MlValue ivalualize(const std::string &value) ;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,11 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
std::string
|
std::string mini_sprintf_format(bool left_align, bool sign, bool space_on_left, bool padding_by_zero,
|
||||||
mini_sprintf_format(bool left_align, bool sign, bool space_on_left, bool padding_by_zero, int width, int precision,
|
int width, int precision,
|
||||||
int length, char specifier, const MlValue &value) {
|
int length, char specifier, const MlValue &value) {
|
||||||
std::string s;
|
std::string s;
|
||||||
std::ostringstream stream_str; // PERF simpler solution..without string stream
|
std::ostringstream stream_str; // PERF string append should be faster
|
||||||
bool is_positive = false;
|
bool is_positive = false;
|
||||||
|
|
||||||
if (specifier == 's') {
|
if (specifier == 's') {
|
||||||
|
|
@ -47,7 +47,7 @@ mini_sprintf_format(bool left_align, bool sign, bool space_on_left, bool padding
|
||||||
stream_str << std::setprecision(precision);
|
stream_str << std::setprecision(precision);
|
||||||
|
|
||||||
stream_str << dval;
|
stream_str << dval;
|
||||||
s = stream_str.str(); // TODO ??
|
s = stream_str.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (width > -1 && s.size() < width) {
|
if (width > -1 && s.size() < width) {
|
||||||
|
|
@ -172,7 +172,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
||||||
// specifier
|
// specifier
|
||||||
if (si >= format_str.end())
|
if (si >= format_str.end())
|
||||||
return output_str; // invalid end of string
|
return output_str; // invalid end of string
|
||||||
if (*si == 'i' || *si == 'd' || *si == 'f' || *si == 's' || *si == 'c') { // TODO more specifiers
|
if (*si == 'i' || *si == 'd' || *si == 'f' || *si == 's' || *si == 'c') { // TODO more specifiers
|
||||||
std::string s = mini_sprintf_format(left_align, sign, space_on_left, padding_by_zero, width,
|
std::string s = mini_sprintf_format(left_align, sign, space_on_left, padding_by_zero, width,
|
||||||
precision, length, *si, parameters[arg_position]);
|
precision, length, *si, parameters[arg_position]);
|
||||||
arg_position++;
|
arg_position++;
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
||||||
#include <cstdio>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
@ -16,8 +15,6 @@
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
|
||||||
|
|
||||||
|
|
||||||
// TODO user streams instead of printf's
|
|
||||||
|
|
||||||
std::pair<int, std::string> HttpClient::doRequest(const std::string &method, const std::string &url, const std::map<std::string, std::string> &headers, const std::string &request_body) {
|
std::pair<int, std::string> HttpClient::doRequest(const std::string &method, const std::string &url, const std::map<std::string, std::string> &headers, const std::string &request_body) {
|
||||||
// split url to parts
|
// split url to parts
|
||||||
parseURL(url);
|
parseURL(url);
|
||||||
|
|
@ -39,7 +36,6 @@ std::pair<int, std::string> HttpClient::doRequest(const std::string &method, con
|
||||||
return std::make_pair(403, "");
|
return std::make_pair(403, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// get headers
|
// get headers
|
||||||
std::string::size_type position = ssl_read_packet.find("\r\n\r\n");
|
std::string::size_type position = ssl_read_packet.find("\r\n\r\n");
|
||||||
if (position == std::string::npos)
|
if (position == std::string::npos)
|
||||||
|
|
@ -198,7 +194,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
||||||
int s;
|
int s;
|
||||||
s = socket(AF_INET, SOCK_STREAM, 0);
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (!s) {
|
if (!s) {
|
||||||
printf("sslRequest, error creating socket.\n");
|
std::cerr << "HttpClient::sslRequest, error creating socket" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -213,7 +209,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
||||||
|
|
||||||
// connect to server
|
// connect to server
|
||||||
if (connect(s, (struct sockaddr *) &sa, socklen)) {
|
if (connect(s, (struct sockaddr *) &sa, socklen)) {
|
||||||
printf("sslRequest, error connecting to server.\n");
|
std::cerr << "HttpClient::sslRequest, error connecting to server" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -224,7 +220,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
||||||
SSL_CTX *ctx = SSL_CTX_new(meth);
|
SSL_CTX *ctx = SSL_CTX_new(meth);
|
||||||
ssl = SSL_new(ctx);
|
ssl = SSL_new(ctx);
|
||||||
if (!ssl) {
|
if (!ssl) {
|
||||||
printf("sslRequest, error creating SSL.\n");
|
std::cerr << "HttpClient::sslRequest, error creating SSL" << std::endl;
|
||||||
logSSL();
|
logSSL();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
@ -235,23 +231,23 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
||||||
|
|
||||||
int err = SSL_connect(ssl);
|
int err = SSL_connect(ssl);
|
||||||
if (err <= 0) {
|
if (err <= 0) {
|
||||||
printf("sslRequest, error creating SSL connection. err=%x\n", err);
|
std::cerr << "HttpClient::sslRequest, error creating SSL connection. " << err << std::endl;
|
||||||
logSSL();
|
logSSL();
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// log cipher
|
// log cipher
|
||||||
// printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
|
// std::cerr << "HttpClient::sslRequest, SSL connection using" << SSL_get_cipher (ssl) << std::endl;
|
||||||
// showCerts(ssl);
|
// showCerts(ssl);
|
||||||
|
|
||||||
|
|
||||||
// send request
|
// send request
|
||||||
// printf ("SSL sending request %s\n", request.c_str());
|
// std::cerr << "HttpClient::sslRequest, SSL sending request: " << request << std::endl;
|
||||||
|
|
||||||
int written_bytes = sslSendPacket(request);
|
int written_bytes = sslSendPacket(request);
|
||||||
if (written_bytes != request.length()) {
|
if (written_bytes != request.length()) {
|
||||||
printf("sslRequest, error sending request\n");
|
std::cerr << "HttpClient::sslRequest, error sending request" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -275,15 +271,15 @@ void HttpClient::showCerts(SSL* ssl) {
|
||||||
|
|
||||||
cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
|
cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
|
||||||
if ( cert != NULL ) {
|
if ( cert != NULL ) {
|
||||||
printf("Server certificates:\n");
|
std::cerr << "HttpClient::showCerts, Server certificates: " << std::endl;
|
||||||
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
|
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
|
||||||
printf("Subject: %s\n", line);
|
std::cerr << "HttpClient::showCerts, Subject: " << line << std::endl;
|
||||||
free(line); /* free the malloc'ed string */
|
free(line); /* free the malloc'ed string */
|
||||||
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
|
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
|
||||||
printf("Issuer: %s\n", line);
|
std::cerr << "HttpClient::showCerts, Issuer: " << line << std::endl;
|
||||||
free(line); /* free the malloc'ed string */
|
free(line); /* free the malloc'ed string */
|
||||||
X509_free(cert); /* free the malloc'ed certificate copy */
|
X509_free(cert); /* free the malloc'ed certificate copy */
|
||||||
} else {
|
} else {
|
||||||
printf("No certificates.\n");
|
std::cerr << "HttpClient::showCerts, No certificates." << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -9,7 +9,6 @@
|
||||||
|
|
||||||
|
|
||||||
class HttpClient {
|
class HttpClient {
|
||||||
// TODO at this moment only https is implemented
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SSL *ssl;
|
SSL *ssl;
|
||||||
|
|
|
||||||
|
|
@ -83,19 +83,22 @@
|
||||||
; return 1 when list contains item otherwise nil
|
; return 1 when list contains item otherwise nil
|
||||||
(defn member (lst itm)
|
(defn member (lst itm)
|
||||||
(do
|
(do
|
||||||
; TODO check if is empty list
|
(if (list? lst)
|
||||||
(def found_index -1)
|
(do
|
||||||
(def i 0)
|
(def found_index -1)
|
||||||
(def lst_len (len lst))
|
(def i 0)
|
||||||
|
(def lst_len (len lst))
|
||||||
|
|
||||||
(while (and (< i lst_len) (= found_index -1))
|
(while (and (< i lst_len) (= found_index -1))
|
||||||
(if (= itm (index lst i))
|
(if (= itm (index lst i))
|
||||||
(set! found_index i)
|
(set! found_index i)
|
||||||
(set! i (+ i 1))
|
(set! i (+ i 1))
|
||||||
))
|
))
|
||||||
|
|
||||||
(if (!= -1 found_index)
|
(if (!= -1 found_index)
|
||||||
#t
|
#t
|
||||||
|
nil)
|
||||||
|
)
|
||||||
nil)
|
nil)
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue