From 765f2bc67390209a805fc4db2abd68bbc834a565 Mon Sep 17 00:00:00 2001 From: vaclavt Date: Thu, 17 Feb 2022 20:41:47 +0100 Subject: [PATCH] some TODOs solved --- clib/csvparser.cpp | 30 +++++++++++++++++------------- clib/csvparser.h | 2 +- clib/printf.cpp | 12 ++++++------ clib/sslclient.cpp | 26 +++++++++++--------------- clib/sslclient.h | 1 - stdlib/stdlib.lsp | 25 ++++++++++++++----------- 6 files changed, 49 insertions(+), 47 deletions(-) diff --git a/clib/csvparser.cpp b/clib/csvparser.cpp index 4246001..faa05f9 100644 --- a/clib/csvparser.cpp +++ b/clib/csvparser.cpp @@ -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 parsed_data; - parsed_data.reserve(128); // TODO introduce constant here + std::vector parsed_rows; + parsed_rows.reserve(INITIAL_PARSED_ROWS_SIZE); std::vector 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 &line, std::vector &lines) { +void CsvParser::add_row(const std::vector &columns, std::vector &rows) { if (skip_header && !header_skiped) { header_skiped = true; } else { - if (!line.empty()) - lines.emplace_back(line); + if (!columns.empty()) + rows.emplace_back(columns); } } diff --git a/clib/csvparser.h b/clib/csvparser.h index 6488416..1c72c4e 100644 --- a/clib/csvparser.h +++ b/clib/csvparser.h @@ -26,7 +26,7 @@ public: MlValue parseCSV(const std::string &csvSource); private: - void add_line(const std::vector &line, std::vector &lines); + void add_row(const std::vector &columns, std::vector &rows); static MlValue ivalualize(const std::string &value) ; diff --git a/clib/printf.cpp b/clib/printf.cpp index ad5118e..bfc5bcf 100644 --- a/clib/printf.cpp +++ b/clib/printf.cpp @@ -6,11 +6,11 @@ #include -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 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) { @@ -172,7 +172,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector= format_str.end()) 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, precision, length, *si, parameters[arg_position]); arg_position++; diff --git a/clib/sslclient.cpp b/clib/sslclient.cpp index c5849e4..dc9f58e 100644 --- a/clib/sslclient.cpp +++ b/clib/sslclient.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -16,8 +15,6 @@ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET -// TODO user streams instead of printf's - std::pair HttpClient::doRequest(const std::string &method, const std::string &url, const std::map &headers, const std::string &request_body) { // split url to parts parseURL(url); @@ -39,7 +36,6 @@ std::pair 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; } } \ No newline at end of file diff --git a/clib/sslclient.h b/clib/sslclient.h index 0918aec..19f07b2 100644 --- a/clib/sslclient.h +++ b/clib/sslclient.h @@ -9,7 +9,6 @@ class HttpClient { - // TODO at this moment only https is implemented private: SSL *ssl; diff --git a/stdlib/stdlib.lsp b/stdlib/stdlib.lsp index 243e834..7b000e1 100644 --- a/stdlib/stdlib.lsp +++ b/stdlib/stdlib.lsp @@ -83,19 +83,22 @@ ; return 1 when list contains item otherwise nil (defn member (lst itm) (do - ; TODO check if is empty list - (def found_index -1) - (def i 0) - (def lst_len (len lst)) + (if (list? lst) + (do + (def found_index -1) + (def i 0) + (def lst_len (len lst)) - (while (and (< i lst_len) (= found_index -1)) - (if (= itm (index lst i)) - (set! found_index i) - (set! i (+ i 1)) - )) + (while (and (< i lst_len) (= found_index -1)) + (if (= itm (index lst i)) + (set! found_index i) + (set! i (+ i 1)) + )) - (if (!= -1 found_index) - #t + (if (!= -1 found_index) + #t + nil) + ) nil) ))