13 вариант исправленный

main
ShevchukDS 2 месяцев назад
Родитель eb145d7161
Сommit cef7686f9f

@ -1,7 +1,6 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "histogram.h" #include "histogram.h"
#include "text.h"
#include "svg.h" #include "svg.h"
#include <curl/curl.h> #include <curl/curl.h>
#include <sstream> #include <sstream>
@ -36,7 +35,7 @@ size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
return data_size; return data_size;
} }
Input download(const string& address) { void download(const string& address, Input& input) {
stringstream buffer; stringstream buffer;
CURL* curl = curl_easy_init(); CURL* curl = curl_easy_init();
if (curl) { if (curl) {
@ -45,46 +44,52 @@ Input download(const string& address) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
CURLcode res = curl_easy_perform(curl); CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) { if (res != CURLE_OK) {
cerr << curl_easy_strerror(res); cerr << "Error: " << curl_easy_strerror(res) << endl;
exit(1); exit(1);
} }
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
return input_data(buffer, false); Input loaded = input_data(buffer, false);
input.numbers = loaded.numbers;
input.bin_count = loaded.bin_count;
} }
void process_args(int argc, char* argv[], Input& input) { void parse_args(int argc, char* argv[], Input& input, string& url) {
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (string(argv[i]) == "-stroke") { string arg = argv[i];
if (arg == "-stroke") {
if (i + 1 < argc) { if (i + 1 < argc) {
input.stroke_color = argv[i + 1]; input.stroke_color = argv[i+1];
i++; i++;
} else { } else {
cerr << "Error: trebueca one argument.\n"; cerr << "Error: require a color value" << endl;
exit(1); exit(1);
} }
} else if (arg[0] != '-') {
url = arg;
} else {
cerr << "Error: Unknown option " << arg << endl;
exit(1);
} }
} }
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
curl_global_init(CURL_GLOBAL_ALL);
Input input; Input input;
process_args(argc, argv, input); string url;
if (argc > 1) {
bool url_provided = false; parse_args(argc, argv, input, url);
for (int i = 1; i < argc; i++) { if (!url.empty()) {
if (string(argv[i]) != "-stroke" && argv[i][0] != '-') { download(url, input);
input = download(argv[i]); } else {
url_provided = true; input = input_data(cin, true);
break;
}
} }
} else {
if (!url_provided) {
input = input_data(cin, true); input = input_data(cin, true);
} }
const auto bins = make_histogram(input.numbers, input.bin_count);
auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins, input.stroke_color); show_histogram_svg(bins, input.stroke_color);
curl_global_cleanup();
return 0; return 0;
} }

Загрузка…
Отмена
Сохранить