small rectorings
This commit is contained in:
parent
10bd95da65
commit
796f6a5785
|
|
@ -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<MlValue> &line, std::vector<MlValue>
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -28,9 +28,9 @@ public:
|
|||
private:
|
||||
void add_line(const std::vector<MlValue> &line, std::vector<MlValue> &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) ;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
#include "printf.h"
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
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
|
||||
bool is_positive = false;
|
||||
|
|
@ -21,12 +22,12 @@ 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) {
|
||||
if (!value.as_string().empty()) {
|
||||
stream_str << (int) value.as_string()[0];
|
||||
return stream_str.str();
|
||||
} else {
|
||||
|
|
@ -124,7 +125,8 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
|||
// %[flags][width][.precision][length]specifier
|
||||
|
||||
// flags: - + \_ # 0
|
||||
while ((*si == '-' || *si == '+' || *si == ' ' || *si == '#' || *si == '0') && si < format_str.end()) {
|
||||
while ((*si == '-' || *si == '+' || *si == ' ' || *si == '#' || *si == '0') &&
|
||||
si < format_str.end()) {
|
||||
switch (*si) {
|
||||
case '-':
|
||||
left_align = true;
|
||||
|
|
@ -141,7 +143,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
|||
padding_by_zero = true;
|
||||
left_align = true;
|
||||
break;
|
||||
};
|
||||
}
|
||||
si++;
|
||||
}
|
||||
// width
|
||||
|
|
@ -171,13 +173,14 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
|||
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]);
|
||||
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 == '\\') {
|
||||
|
|
@ -201,7 +204,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
|||
buf.clear();
|
||||
buf.push_back(*(si + 1));
|
||||
buf.push_back(*(si + 2));
|
||||
output_str.push_back((char)std::strtol( &buf[0], 0, 16));
|
||||
output_str.push_back((char) std::strtol(&buf[0], nullptr, 16));
|
||||
si += 2;
|
||||
break;
|
||||
case '0': // octal ie "\033"
|
||||
|
|
@ -209,7 +212,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
|||
buf.clear();
|
||||
buf.push_back(*(si + 1));
|
||||
buf.push_back(*(si + 2));
|
||||
output_str.push_back((char)std::strtol( &buf[0], 0, 8));
|
||||
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));
|
||||
|
|
@ -218,7 +221,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
|||
break;
|
||||
default:
|
||||
output_str += c1;
|
||||
};
|
||||
}
|
||||
|
||||
// normal characters
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ private:
|
|||
|
||||
[[nodiscard]] static std::string createRequestHeaders(const std::map<std::string, std::string> &headers) ;
|
||||
|
||||
int responseStatusCode(const std::string &status_str) const;
|
||||
static int responseStatusCode(const std::string &status_str) ;
|
||||
|
||||
void responseHeaders(const std::string &hdr);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue