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) {
|
||||
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 newLine(false);
|
||||
std::string field;
|
||||
|
||||
std::vector<MlValue> parsed_data;
|
||||
parsed_data.reserve(128); // TODO introduce constant here
|
||||
std::vector<MlValue> parsed_rows;
|
||||
parsed_rows.reserve(INITIAL_PARSED_ROWS_SIZE);
|
||||
|
||||
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 aEnd = csvSource.end();
|
||||
|
|
@ -45,14 +49,14 @@ MlValue CsvParser::parseCSV(const std::string &csvSource) {
|
|||
} else {
|
||||
if (!newLine) {
|
||||
line.push_back(ivalualize(field));
|
||||
add_line(line, parsed_data);
|
||||
add_row(line, parsed_rows);
|
||||
field.clear();
|
||||
line.clear();
|
||||
linesRead++;
|
||||
if (linesRead == 16) {
|
||||
if (linesRead == ROWS_READ_FOR_SIZE_ESTIMATION) {
|
||||
size_t linesEstimation = csvSource.size() / (std::distance(csvSource.begin(), aChar) / linesRead);
|
||||
if (linesEstimation > parsed_data.capacity())
|
||||
parsed_data.reserve(linesEstimation);
|
||||
if (linesEstimation > parsed_rows.capacity())
|
||||
parsed_rows.reserve(linesEstimation);
|
||||
}
|
||||
newLine = true;
|
||||
}
|
||||
|
|
@ -68,18 +72,18 @@ MlValue CsvParser::parseCSV(const std::string &csvSource) {
|
|||
if (!field.empty())
|
||||
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) {
|
||||
header_skiped = true;
|
||||
} else {
|
||||
if (!line.empty())
|
||||
lines.emplace_back(line);
|
||||
if (!columns.empty())
|
||||
rows.emplace_back(columns);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ public:
|
|||
MlValue parseCSV(const std::string &csvSource);
|
||||
|
||||
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) ;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@
|
|||
#include <vector>
|
||||
|
||||
|
||||
std::string
|
||||
mini_sprintf_format(bool left_align, bool sign, bool space_on_left, bool padding_by_zero, int width, int precision,
|
||||
std::string mini_sprintf_format(bool left_align, bool sign, bool space_on_left, bool padding_by_zero,
|
||||
int width, int precision,
|
||||
int length, char specifier, const MlValue &value) {
|
||||
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;
|
||||
|
||||
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 << dval;
|
||||
s = stream_str.str(); // TODO ??
|
||||
s = stream_str.str();
|
||||
}
|
||||
|
||||
if (width > -1 && s.size() < width) {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
|
|
@ -16,8 +15,6 @@
|
|||
// 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) {
|
||||
// split url to parts
|
||||
parseURL(url);
|
||||
|
|
@ -39,7 +36,6 @@ std::pair<int, std::string> HttpClient::doRequest(const std::string &method, con
|
|||
return std::make_pair(403, "");
|
||||
}
|
||||
|
||||
|
||||
// get headers
|
||||
std::string::size_type position = ssl_read_packet.find("\r\n\r\n");
|
||||
if (position == std::string::npos)
|
||||
|
|
@ -198,7 +194,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
|||
int s;
|
||||
s = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (!s) {
|
||||
printf("sslRequest, error creating socket.\n");
|
||||
std::cerr << "HttpClient::sslRequest, error creating socket" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -213,7 +209,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
|||
|
||||
// connect to server
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -224,7 +220,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
|||
SSL_CTX *ctx = SSL_CTX_new(meth);
|
||||
ssl = SSL_new(ctx);
|
||||
if (!ssl) {
|
||||
printf("sslRequest, error creating SSL.\n");
|
||||
std::cerr << "HttpClient::sslRequest, error creating SSL" << std::endl;
|
||||
logSSL();
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -235,23 +231,23 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
|||
|
||||
int err = SSL_connect(ssl);
|
||||
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();
|
||||
fflush(stdout);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
// 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);
|
||||
if (written_bytes != request.length()) {
|
||||
printf("sslRequest, error sending request\n");
|
||||
std::cerr << "HttpClient::sslRequest, error sending request" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -275,15 +271,15 @@ void HttpClient::showCerts(SSL* ssl) {
|
|||
|
||||
cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
|
||||
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);
|
||||
printf("Subject: %s\n", line);
|
||||
std::cerr << "HttpClient::showCerts, Subject: " << line << std::endl;
|
||||
free(line); /* free the malloc'ed string */
|
||||
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 */
|
||||
X509_free(cert); /* free the malloc'ed certificate copy */
|
||||
} else {
|
||||
printf("No certificates.\n");
|
||||
std::cerr << "HttpClient::showCerts, No certificates." << std::endl;
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
|
||||
class HttpClient {
|
||||
// TODO at this moment only https is implemented
|
||||
|
||||
private:
|
||||
SSL *ssl;
|
||||
|
|
|
|||
|
|
@ -83,7 +83,8 @@
|
|||
; return 1 when list contains item otherwise nil
|
||||
(defn member (lst itm)
|
||||
(do
|
||||
; TODO check if is empty list
|
||||
(if (list? lst)
|
||||
(do
|
||||
(def found_index -1)
|
||||
(def i 0)
|
||||
(def lst_len (len lst))
|
||||
|
|
@ -97,6 +98,8 @@
|
|||
(if (!= -1 found_index)
|
||||
#t
|
||||
nil)
|
||||
)
|
||||
nil)
|
||||
))
|
||||
|
||||
(defn make-list-of (size val)
|
||||
|
|
|
|||
Loading…
Reference in New Issue