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
|
line.reserve(32); // TODO introduce constant here
|
||||||
|
|
||||||
std::string::const_iterator aChar = csvSource.begin();
|
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) {
|
if (*aChar == quote_character) {
|
||||||
newLine = false;
|
newLine = false;
|
||||||
inQuote = !inQuote;
|
inQuote = !inQuote;
|
||||||
} else if (*aChar == field_separator) {
|
} else if (*aChar == field_separator) {
|
||||||
newLine = false;
|
newLine = false;
|
||||||
if (inQuote == true) {
|
if (inQuote) {
|
||||||
field += *aChar;
|
field += *aChar;
|
||||||
} else {
|
} else {
|
||||||
line.push_back(ivalualize(field));
|
line.push_back(ivalualize(field));
|
||||||
field.clear();
|
field.clear();
|
||||||
}
|
}
|
||||||
} else if (*aChar == line_separator || *aChar == line_separator2) {
|
} else if (*aChar == line_separator || *aChar == line_separator2) {
|
||||||
if (inQuote == true) {
|
if (inQuote) {
|
||||||
field += *aChar;
|
field += *aChar;
|
||||||
} else {
|
} else {
|
||||||
if (newLine == false) {
|
if (!newLine) {
|
||||||
line.push_back(ivalualize(field));
|
line.push_back(ivalualize(field));
|
||||||
add_line(line, parsed_data);
|
add_line(line, parsed_data);
|
||||||
field.clear();
|
field.clear();
|
||||||
line.clear();
|
line.clear();
|
||||||
linesRead++;
|
linesRead++;
|
||||||
if (linesRead == 16) {
|
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())
|
if (linesEstimation > parsed_data.capacity())
|
||||||
parsed_data.reserve(linesEstimation);
|
parsed_data.reserve(linesEstimation);
|
||||||
}
|
}
|
||||||
|
|
@ -64,7 +65,7 @@ MlValue CsvParser::parseCSV(const std::string &csvSource) {
|
||||||
aChar++;
|
aChar++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field.size())
|
if (!field.empty())
|
||||||
line.push_back(ivalualize(field));
|
line.push_back(ivalualize(field));
|
||||||
|
|
||||||
add_line(line, parsed_data);
|
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) {
|
if (skip_header && !header_skiped) {
|
||||||
header_skiped = true;
|
header_skiped = true;
|
||||||
} else {
|
} else {
|
||||||
if (line.size())
|
if (!line.empty())
|
||||||
lines.push_back(line);
|
lines.emplace_back(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MlValue CsvParser::ivalualize(const std::string &value) const {
|
MlValue CsvParser::ivalualize(const std::string &value) {
|
||||||
long int_val;
|
long int_val;
|
||||||
double float_val;
|
double float_val;
|
||||||
if (value.empty() || ((!isdigit(value[0])) && (value[0] != '-') && (value[0] != '+'))) {
|
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
|
// 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;
|
char *end_ptr;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
// if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false;
|
// 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
|
// 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;
|
char *end_ptr;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
// if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false;
|
// if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false;
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,9 @@ public:
|
||||||
private:
|
private:
|
||||||
void add_line(const std::vector<MlValue> &line, std::vector<MlValue> &lines);
|
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 "printf.h"
|
||||||
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#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::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;
|
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 (specifier == 'i' || specifier == 'd') {
|
||||||
if (value.is_number()) {
|
if (value.is_number()) {
|
||||||
int ival = value.as_int();
|
auto ival = value.as_int();
|
||||||
is_positive = ival >= 0;
|
is_positive = ival >= 0;
|
||||||
|
|
||||||
s = std::to_string(ival);
|
s = std::to_string(ival);
|
||||||
} else if (value.is_string()) { // print ascii code of character
|
} 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];
|
stream_str << (int) value.as_string()[0];
|
||||||
return stream_str.str();
|
return stream_str.str();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -124,7 +125,8 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
||||||
// %[flags][width][.precision][length]specifier
|
// %[flags][width][.precision][length]specifier
|
||||||
|
|
||||||
// flags: - + \_ # 0
|
// 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) {
|
switch (*si) {
|
||||||
case '-':
|
case '-':
|
||||||
left_align = true;
|
left_align = true;
|
||||||
|
|
@ -141,7 +143,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
||||||
padding_by_zero = true;
|
padding_by_zero = true;
|
||||||
left_align = true;
|
left_align = true;
|
||||||
break;
|
break;
|
||||||
};
|
}
|
||||||
si++;
|
si++;
|
||||||
}
|
}
|
||||||
// width
|
// width
|
||||||
|
|
@ -171,13 +173,14 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
||||||
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, 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++;
|
arg_position++;
|
||||||
output_str += s;
|
output_str += s;
|
||||||
} else {
|
} else {
|
||||||
output_str += "UNKNOWN FORMAT SPECIFIER";
|
output_str += "UNKNOWN FORMAT SPECIFIER";
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
// escaping sequences
|
// escaping sequences
|
||||||
} else if (c == '\\') {
|
} else if (c == '\\') {
|
||||||
|
|
@ -201,7 +204,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
||||||
buf.clear();
|
buf.clear();
|
||||||
buf.push_back(*(si + 1));
|
buf.push_back(*(si + 1));
|
||||||
buf.push_back(*(si + 2));
|
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;
|
si += 2;
|
||||||
break;
|
break;
|
||||||
case '0': // octal ie "\033"
|
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.clear();
|
||||||
buf.push_back(*(si + 1));
|
buf.push_back(*(si + 1));
|
||||||
buf.push_back(*(si + 2));
|
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
|
// TODO maybe octal constant has 3 bytes
|
||||||
// if (si + 2 >= format_str.end()) return output_str; // end of string, invalid octal or hex constant
|
// if (si + 2 >= format_str.end()) return output_str; // end of string, invalid octal or hex constant
|
||||||
// buf.push_back(*(si + 3));
|
// buf.push_back(*(si + 3));
|
||||||
|
|
@ -218,7 +221,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
output_str += c1;
|
output_str += c1;
|
||||||
};
|
}
|
||||||
|
|
||||||
// normal characters
|
// normal characters
|
||||||
} else {
|
} 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
|
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;
|
std::smatch status_matches;
|
||||||
if (std::regex_search(status_str, status_matches, status_rgx)) {
|
if (std::regex_search(status_str, status_matches, status_rgx)) {
|
||||||
if (status_matches.size() > 1)
|
if (status_matches.size() > 1)
|
||||||
|
|
@ -125,17 +125,19 @@ void HttpClient::parseURL(const std::string &url) {
|
||||||
case 6:
|
case 6:
|
||||||
href = matches[i].str();
|
href = matches[i].str();
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
std::cerr << "Unexpected part of url: " << url << std::endl;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
// std::cout << i << ": '" << matches[i].str() << "'\n";
|
|
||||||
}
|
}
|
||||||
} else {
|
} 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) {
|
std::string HttpClient::inetAddress(const std::string &hostname) {
|
||||||
hostent *record = gethostbyname(hostname.c_str());
|
hostent *record = gethostbyname(hostname.c_str());
|
||||||
if (record == NULL) {
|
if (record == nullptr) {
|
||||||
throw std::runtime_error(hostname + " network unavailable.");
|
throw std::runtime_error(hostname + " network unavailable.");
|
||||||
}
|
}
|
||||||
auto *address = (in_addr *) record->h_addr;
|
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
|
// socket address
|
||||||
std::string server_ip = inetAddress(server_name);
|
std::string server_ip = inetAddress(server_name);
|
||||||
struct sockaddr_in sa;
|
struct sockaddr_in sa{};
|
||||||
memset(&sa, 0, sizeof(sa));
|
memset(&sa, 0, sizeof(sa));
|
||||||
sa.sin_family = AF_INET;
|
sa.sin_family = AF_INET;
|
||||||
sa.sin_addr.s_addr = inet_addr(server_ip.c_str());
|
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() {
|
void HttpClient::logSSL() {
|
||||||
unsigned long err;
|
unsigned long err;
|
||||||
while ((err = ERR_get_error())) {
|
while ((err = ERR_get_error())) {
|
||||||
char *str = ERR_error_string(err, 0);
|
char *str = ERR_error_string(err, nullptr);
|
||||||
if (!str)
|
if (!str)
|
||||||
return;
|
return;
|
||||||
std::cerr << str << std::endl;
|
std::cerr << str << std::endl;
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ private:
|
||||||
|
|
||||||
[[nodiscard]] static std::string createRequestHeaders(const std::map<std::string, std::string> &headers) ;
|
[[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);
|
void responseHeaders(const std::string &hdr);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue