|
|
|
@ -1,8 +1,11 @@
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
#include "histogram.h"
|
|
|
|
|
#include "svg.h"
|
|
|
|
|
#include "text.h"
|
|
|
|
|
#include "svg.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
struct Input {
|
|
|
|
@ -10,29 +13,65 @@ struct Input {
|
|
|
|
|
size_t bin_count{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Input input_data() {
|
|
|
|
|
Input in;
|
|
|
|
|
Input input_data(istream& in, bool prompt = false) {
|
|
|
|
|
Input in_data;
|
|
|
|
|
size_t number_count;
|
|
|
|
|
|
|
|
|
|
cerr << "Enter number count: ";
|
|
|
|
|
cin >> number_count;
|
|
|
|
|
|
|
|
|
|
in.numbers.resize(number_count);
|
|
|
|
|
cerr << "Enter numbers: ";
|
|
|
|
|
if (prompt) cerr << "Enter number count: ";
|
|
|
|
|
in >> number_count;
|
|
|
|
|
in_data.numbers.resize(number_count);
|
|
|
|
|
if (prompt && number_count > 0) cerr << "Enter numbers: ";
|
|
|
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
|
|
|
cin >> in.numbers[i];
|
|
|
|
|
in >> in_data.numbers[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cerr << "Enter bin count: ";
|
|
|
|
|
cin >> in.bin_count;
|
|
|
|
|
|
|
|
|
|
return in;
|
|
|
|
|
if (prompt) cerr << "Enter bin count: ";
|
|
|
|
|
in >> in_data.bin_count;
|
|
|
|
|
return in_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Колбэк-функция для cURL: записывает данные в буфер
|
|
|
|
|
size_t write_data(void* ptr, size_t size, size_t nmemb, void* userdata) {
|
|
|
|
|
size_t total = size * nmemb;
|
|
|
|
|
stringstream* stream = reinterpret_cast<stringstream*>(userdata);
|
|
|
|
|
stream->write(reinterpret_cast<const char*>(ptr), total);
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
auto in = input_data();
|
|
|
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
|
|
|
Input download(const string& url) {
|
|
|
|
|
CURL* curl = curl_easy_init();
|
|
|
|
|
if (!curl) {
|
|
|
|
|
cerr << "Failed to init curl" << endl;
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stringstream buffer;
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.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 != CURLE_OK) {
|
|
|
|
|
cerr << "cURL error: " << curl_easy_strerror(res) << endl;
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return input_data(buffer, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
|
|
|
|
|
Input input;
|
|
|
|
|
if (argc > 1) {
|
|
|
|
|
input = download(argv[1]);
|
|
|
|
|
} else {
|
|
|
|
|
input = input_data(cin, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto bins = make_histogram(input.numbers, input.bin_count);
|
|
|
|
|
show_histogram_svg(bins);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|