diff --git a/main.cpp b/main.cpp index 5607cd3..f6abb2e 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include "svg.h" #include #include +#include using namespace std; @@ -12,17 +13,20 @@ struct Input size_t bin_count{}; }; -Input input_data(istream& sin, bool prompt) +Input input_data(istream& sin, bool prompt = true) { if (prompt){ Input in; size_t number_count; + cerr << "Input number count" << endl; sin >> number_count; in.numbers.resize(number_count); for (size_t i = 0; i < number_count; i++) { + cerr << "Input number "<< i << endl; sin >> in.numbers[i]; } + cerr << "Input bin count" << endl; sin >> in.bin_count; return in; } @@ -40,19 +44,69 @@ Input input_data(istream& sin, bool prompt) } } -int main(int argc, char* argv[]) { - if (argc > 1) { - CURL* curl = curl_easy_init(); - auto res = curl_easy_perform(curl); - if (res != 0) { +size_t +write_data(void* items, size_t item_size, size_t item_count, void* ctx) { + size_t data_size = item_size * item_count; + stringstream* buffer = reinterpret_cast(ctx); + buffer->write(reinterpret_cast(items), data_size); + return data_size; +} + +Input +download(const string& address) { + stringstream buffer; + CURL* curl = curl_easy_init(); + + if (curl) { + CURLcode res; + curl_easy_setopt(curl, CURLOPT_URL, address.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); + res = curl_easy_perform(curl); + if (res != CURLE_OK) { cout << curl_easy_strerror(res); exit(1); } curl_easy_cleanup(curl); - return 0; } - Input in = input_data(cin, false); + return input_data(buffer, false); +} + +int main(int argc, char* argv[]) { + Input in; + string link, color = ""; + bool flag = false; + if (argc == 2) { + in = download(argv[1]); + } + else if (argc == 3) { + cout << "Wrong argument, use correct input -stroke or leave input empty" << endl; + exit(2); + } + else if (argc > 3) { + for (size_t i = 1; i < argc; i++) { + string cur = argv[i]; + if (cur.find("://") != string::npos) { + link = cur; + } + else if (i != argc - 1){ + string curm = argv[i+1]; + if (cur == "-stroke" && curm.find("://") == string::npos) { + flag = true; + color = curm; + } + } + else if (cur == "-stroke" && i == (argc - 1)) { + cout << "Wrong argument, use correct input -stroke or leave input empty" << endl; + exit(2); + } + } + in = download(link); + } + else { + in = input_data(cin, false); + } auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_svg(bins); + show_histogram_svg(bins, color); return 0; } diff --git a/svg.cpp b/svg.cpp index 17659a7..657995b 100644 --- a/svg.cpp +++ b/svg.cpp @@ -31,16 +31,18 @@ svg_text(double left, double baseline, string text) { cout << "" << text << ""; } -void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black"){ +void +svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black"){ cout << ""; } - void -show_histogram_svg(const vector& bins) { +show_histogram_svg(const vector& bins, string stcolor) { double max = 0; int maxlen = IMAGE_WIDTH - TEXT_WIDTH; + int bnumber = 0; for (auto el: bins) { + bnumber++; if (max < el) { max = el; } @@ -49,6 +51,11 @@ show_histogram_svg(const vector& bins) { svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); string stroke[] = {"red", "green", "blue"}; + if (stcolor != "") { + for (int i = 0; i < bnumber; i++){ + stroke[i] = stcolor; + } + } string fill[] = {"red", "green", "blue"}; int i = 0; double top = 0;