From d514d8763339e0c6de473aab459df6818aef85aa Mon Sep 17 00:00:00 2001 From: MokeevNV Date: Wed, 21 May 2025 21:48:19 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20?= =?UTF-8?q?=D1=81=20=D0=B1=D1=83=D1=84=D0=B5=D1=80=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index 683d6c9..0512971 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include using namespace std; @@ -40,26 +41,41 @@ Input input_data(istream& in, bool promt = false) { return qin; } +size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx){ + stringstream* buffer = reinterpret_cast(ctx); + size_t data_size = item_size * item_count; + buffer->write(reinterpret_cast(items), data_size); + return data_size; +} + +Input download(const string& address) { + stringstream buffer; + CURL *curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_URL, address.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); + CURLcode res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + if(res != 0){ + cerr << curl_easy_strerror(res); + exit(1); + } + } + return input_data(buffer, false); +} + int main(int argc, char* argv[]) { curl_global_init(CURL_GLOBAL_ALL); - if(argc > 1) { - CURL *curl = curl_easy_init(); - if(curl) { - CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, argv[1]); - res = curl_easy_perform(curl); - curl_easy_cleanup(curl); - if(res != 0){ - cerr << curl_easy_strerror(res); - exit(1); - } - } - return 0; + Input input; + if (argc > 1) { + input = download(argv[1]); + } else { + input = input_data(cin, true); } - auto in = input_data(cin); - auto bins = make_histogram(in.numbers, in.bin_count); + auto bins = make_histogram(input.numbers, input.bin_count); show_histogram_svg(bins); }