From ff58c6a0ef78568a90e1986b8daa752ff1661828 Mon Sep 17 00:00:00 2001 From: EfimovaLA Date: Sun, 2 Jun 2024 20:41:46 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= =?UTF-8?q?=20=D0=B2=20=D0=B1=D1=83=D1=84=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 55 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index 988fbbf..ed61cc6 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,9 @@ #include "histogram_internal.h" #include "svg.h" #include +#include +#include + using namespace std; const size_t SCREEN_WIDTH = 80; @@ -40,25 +43,47 @@ Input input_data(istream& stream, bool prompt) } -int main(int argc, char* argv[]) -{ +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); + auto text = reinterpret_cast(items); + buffer->write(text, data_size); + return data_size; +} + +Input download(const string& address) { + stringstream buffer; + curl_global_init(CURL_GLOBAL_ALL); - if (argc > 1) + CURL * curl = curl_easy_init(); + + if(curl) { - CURL * curl = curl_easy_init(); - if(curl) - { - CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, argv[1]); - res = curl_easy_perform(curl); - if(res != CURLE_OK){ - curl_easy_strerror(res); - exit(1); - } - curl_easy_cleanup(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){ + curl_easy_strerror(res); + exit(1); } - return 0; + curl_easy_cleanup(curl); } + return input_data(buffer, false); +} + + +int main(int argc, char* argv[]) +{ + Input input; + if (argc > 1) { + input = download(argv[1]); + } else { + input = input_data(cin, true); + } + bool prompt = true; auto in = input_data(cin, prompt);