From 796f6a5785711440926507d20a8064ba9c3f88a0 Mon Sep 17 00:00:00 2001 From: VaclavT Date: Sun, 16 Jan 2022 20:40:09 +0100 Subject: [PATCH] small rectorings --- clib/csvparser.cpp | 23 ++-- clib/csvparser.h | 6 +- clib/printf.cpp | 255 +++++++++++++++++++++++---------------------- clib/sslclient.cpp | 16 +-- clib/sslclient.h | 2 +- usql/parser.cpp | 2 +- 6 files changed, 155 insertions(+), 149 deletions(-) diff --git a/clib/csvparser.cpp b/clib/csvparser.cpp index 4cdb826..4246001 100644 --- a/clib/csvparser.cpp +++ b/clib/csvparser.cpp @@ -26,30 +26,31 @@ MlValue CsvParser::parseCSV(const std::string &csvSource) { line.reserve(32); // TODO introduce constant here std::string::const_iterator aChar = csvSource.begin(); - while (aChar != csvSource.end()) { + std::string::const_iterator aEnd = csvSource.end(); + while (aChar != aEnd) { if (*aChar == quote_character) { newLine = false; inQuote = !inQuote; } else if (*aChar == field_separator) { newLine = false; - if (inQuote == true) { + if (inQuote) { field += *aChar; } else { line.push_back(ivalualize(field)); field.clear(); } } else if (*aChar == line_separator || *aChar == line_separator2) { - if (inQuote == true) { + if (inQuote) { field += *aChar; } else { - if (newLine == false) { + if (!newLine) { line.push_back(ivalualize(field)); add_line(line, parsed_data); field.clear(); line.clear(); linesRead++; if (linesRead == 16) { - int 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()) parsed_data.reserve(linesEstimation); } @@ -64,7 +65,7 @@ MlValue CsvParser::parseCSV(const std::string &csvSource) { aChar++; } - if (field.size()) + if (!field.empty()) line.push_back(ivalualize(field)); add_line(line, parsed_data); @@ -77,12 +78,12 @@ void CsvParser::add_line(const std::vector &line, std::vector if (skip_header && !header_skiped) { header_skiped = true; } else { - if (line.size()) - lines.push_back(line); + if (!line.empty()) + lines.emplace_back(line); } } -MlValue CsvParser::ivalualize(const std::string &value) const { +MlValue CsvParser::ivalualize(const std::string &value) { long int_val; double float_val; if (value.empty() || ((!isdigit(value[0])) && (value[0] != '-') && (value[0] != '+'))) { @@ -97,7 +98,7 @@ MlValue CsvParser::ivalualize(const std::string &value) const { } // Is string representing int value -bool CsvParser::is_string_int(const std::string &s, long &val) const { +bool CsvParser::is_string_int(const std::string &s, long &val) { char *end_ptr; errno = 0; // if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false; @@ -110,7 +111,7 @@ bool CsvParser::is_string_int(const std::string &s, long &val) const { } // Is string representing float value -bool CsvParser::is_string_float(const std::string &s, double &val) const { +bool CsvParser::is_string_float(const std::string &s, double &val) { char *end_ptr; errno = 0; // if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false; diff --git a/clib/csvparser.h b/clib/csvparser.h index d1f780f..6488416 100644 --- a/clib/csvparser.h +++ b/clib/csvparser.h @@ -28,9 +28,9 @@ public: private: void add_line(const std::vector &line, std::vector &lines); - MlValue ivalualize(const std::string &value) const; + static MlValue ivalualize(const std::string &value) ; - bool is_string_int(const std::string &s, long &val) const; + static bool is_string_int(const std::string &s, long &val) ; - bool is_string_float(const std::string &s, double &val) const; + static bool is_string_float(const std::string &s, double &val) ; }; diff --git a/clib/printf.cpp b/clib/printf.cpp index e626b6a..ad5118e 100644 --- a/clib/printf.cpp +++ b/clib/printf.cpp @@ -1,15 +1,16 @@ #include "printf.h" #include -#include #include #include #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 simpler solution..without string stream bool is_positive = false; if (specifier == 's') { @@ -21,13 +22,13 @@ std::string mini_sprintf_format(bool left_align, bool sign, bool space_on_left, } if (specifier == 'i' || specifier == 'd') { if (value.is_number()) { - int ival = value.as_int(); + auto ival = value.as_int(); is_positive = ival >= 0; s = std::to_string(ival); - } else if (value.is_string()) { // print ascii code of character - if (value.as_string().size() > 0) { - stream_str << (int)value.as_string()[0]; + } else if (value.is_string()) { // print ascii code of character + if (!value.as_string().empty()) { + stream_str << (int) value.as_string()[0]; return stream_str.str(); } else { // TODO handle empty string - error? @@ -88,96 +89,98 @@ std::string mini_sprintf(const std::string &format_str, const std::vector= format_str.end()) - return output_str; // invalid end of string - while (*si >= '0' && *si <= '9' && si < format_str.end()) { - if (width == -1) { - width = 0; - } - width = width * 10 + (*si - '0'); - si++; - } - // precision - if (si >= format_str.end()) - return output_str; // invalid end of string - if (*si == '.') { - precision = 0; - if (++si >= format_str.end()) - return output_str; // invalid end of string - while (*si >= '0' && *si <= '9' && si < format_str.end()) { - precision = precision * 10 + (*si - '0'); + // flags: - + \_ # 0 + while ((*si == '-' || *si == '+' || *si == ' ' || *si == '#' || *si == '0') && + si < format_str.end()) { + switch (*si) { + case '-': + left_align = true; + break; + case '+': + sign = true; + break; + case ' ': + space_on_left = true; + break; + case '#': + break; + case '0': + padding_by_zero = true; + left_align = true; + break; + } si++; } - } - // length - // specifier - if (si >= format_str.end()) - return output_str; // invalid end of string - 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++; - output_str += s; - } else { - output_str += "UNKNOWN FORMAT SPECIFIER"; - } - }; + // width + if (si >= format_str.end()) + return output_str; // invalid end of string + while (*si >= '0' && *si <= '9' && si < format_str.end()) { + if (width == -1) { + width = 0; + } + width = width * 10 + (*si - '0'); + si++; + } + // precision + if (si >= format_str.end()) + return output_str; // invalid end of string + if (*si == '.') { + precision = 0; + if (++si >= format_str.end()) + return output_str; // invalid end of string + while (*si >= '0' && *si <= '9' && si < format_str.end()) { + precision = precision * 10 + (*si - '0'); + si++; + } + } + // length + // specifier + if (si >= format_str.end()) + return output_str; // invalid end of string + 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++; + output_str += s; + } else { + output_str += "UNKNOWN FORMAT SPECIFIER"; + } + } // escaping sequences } else if (c == '\\') { @@ -187,38 +190,38 @@ std::string mini_sprintf(const std::string &format_str, const std::vector= format_str.end()) return output_str; // end of string, invalid hex constant - buf.clear(); - buf.push_back(*(si + 1)); - buf.push_back(*(si + 2)); - output_str.push_back((char)std::strtol( &buf[0], 0, 16)); - si += 2; - break; - case '0': // octal ie "\033" - if (si + 2 >= format_str.end()) return output_str; // end of string, invalid octal constant - buf.clear(); - buf.push_back(*(si + 1)); - buf.push_back(*(si + 2)); - output_str.push_back((char)std::strtol( &buf[0], 0, 8)); - // TODO maybe octal constant has 3 bytes - // if (si + 2 >= format_str.end()) return output_str; // end of string, invalid octal or hex constant - // buf.push_back(*(si + 3)); - // si += 3; - si += 2; - break; - default: - output_str += c1; - }; + case 't': + output_str += '\t'; + break; + case 'r': + output_str += '\r'; + break; + case 'n': + output_str += '\n'; + break; + case 'x': // hex ie \x1b + if (si + 2 >= format_str.end()) return output_str; // end of string, invalid hex constant + buf.clear(); + buf.push_back(*(si + 1)); + buf.push_back(*(si + 2)); + output_str.push_back((char) std::strtol(&buf[0], nullptr, 16)); + si += 2; + break; + case '0': // octal ie "\033" + if (si + 2 >= format_str.end()) return output_str; // end of string, invalid octal constant + buf.clear(); + buf.push_back(*(si + 1)); + buf.push_back(*(si + 2)); + output_str.push_back((char) std::strtol(&buf[0], nullptr, 8)); + // TODO maybe octal constant has 3 bytes + // if (si + 2 >= format_str.end()) return output_str; // end of string, invalid octal or hex constant + // buf.push_back(*(si + 3)); + // si += 3; + si += 2; + break; + default: + output_str += c1; + } // normal characters } else { diff --git a/clib/sslclient.cpp b/clib/sslclient.cpp index c721edd..c5849e4 100644 --- a/clib/sslclient.cpp +++ b/clib/sslclient.cpp @@ -76,10 +76,10 @@ void HttpClient::responseHeaders(const std::string &headers) { } } -int HttpClient::responseStatusCode(const std::string &status_str) const { +int HttpClient::responseStatusCode(const std::string &status_str) { auto resp_status = 200; // default is OK - std::regex status_rgx{"^HTTP/\\d\\.\\d (\\d{3}) .+$"}; + std::regex status_rgx{R"(^HTTP/\d\.\d (\d{3}) .+$)"}; std::smatch status_matches; if (std::regex_search(status_str, status_matches, status_rgx)) { if (status_matches.size() > 1) @@ -125,17 +125,19 @@ void HttpClient::parseURL(const std::string &url) { case 6: href = matches[i].str(); break; + default: + std::cerr << "Unexpected part of url: " << url << std::endl; + break; } - // std::cout << i << ": '" << matches[i].str() << "'\n"; } } else { - std::cerr << "Match not found" << std::endl; // TODO better message + std::cerr << "Cannot parse url: " << url << std::endl; } } std::string HttpClient::inetAddress(const std::string &hostname) { hostent *record = gethostbyname(hostname.c_str()); - if (record == NULL) { + if (record == nullptr) { throw std::runtime_error(hostname + " network unavailable."); } auto *address = (in_addr *) record->h_addr; @@ -202,7 +204,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re // socket address std::string server_ip = inetAddress(server_name); - struct sockaddr_in sa; + struct sockaddr_in sa{}; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(server_ip.c_str()); @@ -260,7 +262,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re void HttpClient::logSSL() { unsigned long err; while ((err = ERR_get_error())) { - char *str = ERR_error_string(err, 0); + char *str = ERR_error_string(err, nullptr); if (!str) return; std::cerr << str << std::endl; diff --git a/clib/sslclient.h b/clib/sslclient.h index f20100c..0918aec 100644 --- a/clib/sslclient.h +++ b/clib/sslclient.h @@ -40,7 +40,7 @@ private: [[nodiscard]] static std::string createRequestHeaders(const std::map &headers) ; - int responseStatusCode(const std::string &status_str) const; + static int responseStatusCode(const std::string &status_str) ; void responseHeaders(const std::string &hdr); }; diff --git a/usql/parser.cpp b/usql/parser.cpp index 7d63d4b..d0428dd 100644 --- a/usql/parser.cpp +++ b/usql/parser.cpp @@ -109,7 +109,7 @@ namespace usql { m_lexer.skipTokenOptional(TokenType::comma); //constraints - //defaults + //defaults } while (m_lexer.tokenType() != TokenType::close_paren); return std::make_unique(table_name, cols_def);