From 877e45e7f0c11ed210e5128711e794a3ffc8af1a Mon Sep 17 00:00:00 2001 From: BiriukovaAlS Date: Sun, 4 Jun 2023 02:55:17 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D1=81?= =?UTF-8?q?=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 | 56 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/main.cpp b/main.cpp index 203eccb..342def5 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,8 @@ #include "text.h" #include "svg.h" #include +#include +#include using namespace std; @@ -16,6 +18,14 @@ struct Input size_t number_count; }; +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 input_data(istream& in, bool promt) { @@ -41,29 +51,43 @@ input_data(istream& in, bool promt) return ik; } -int main(int argc, char* argv[]) + +Input +download(const string& address) { - 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); - if (res != 0) - { - cout << curl_easy_strerror(res); - exit(1); - } - curl_easy_cleanup(curl); + 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 != 0) + { + cout << curl_easy_strerror(res); + exit(1); } - return 0; + curl_easy_cleanup(curl); } + return input_data(buffer, false); +} - curl_global_init(CURL_GLOBAL_ALL); +int main(int argc, char* argv[]) +{ + Input in; + if (argc > 1) + { + in = download(argv[1]); + } + else + { + in = input_data(cin, true); + } size_t number_count; - auto in = input_data(cin,true); auto B = make_histogram(in.numbers, in.kol_kor); show_histogram_svg(B, in.number_count);