From 9bd2b446f756dd10560ac70a88563b0af0212ffe Mon Sep 17 00:00:00 2001 From: "Nikita (PodolskyNK)" Date: Mon, 27 May 2024 14:09:05 +0300 Subject: [PATCH] code: data stream --- main.cpp | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/main.cpp b/main.cpp index 04f9f7a..952cd9c 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,8 @@ #include "histogram.h" #include "text.h" #include "svg.h" +#include +#include #include using namespace std; @@ -32,23 +34,30 @@ Input input_data(istream& stream, bool prompt) { stream >> in.bin_count; return in; } +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()); + res = curl_easy_perform(curl); + if (res!=CURLE_OK){ + cerr << curl_easy_strerror(res); + exit(1); + } + curl_easy_cleanup(curl); + } + return input_data(buffer, false); +} int main(int argc, char* argv[]) { + Input input; 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!=CURLE_OK){ - cerr << curl_easy_strerror(res); - exit(1); - } - curl_easy_cleanup(curl); - } - return 0; + input = download(argv[1]); + } else { + input = input_data(cin, true); } - Input in = input_data(cin, false); - vector bins = make_histogram(in.numbers, in.bin_count); + const auto bins = make_histogram(input.numbers, input.bin_count); show_histogram_svg(bins); }