code:вывод curl
Этот коммит содержится в:
83
main.cpp
83
main.cpp
@@ -1,9 +1,10 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <curl/curl.h>
|
||||
#include "histogram.h"
|
||||
#include "svg.h"
|
||||
#include "text.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -12,39 +13,65 @@ struct Input {
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input input_data(istream& in_stream, bool prompt) {
|
||||
Input in;
|
||||
Input input_data(istream& in, bool prompt = false) {
|
||||
Input in_data;
|
||||
size_t number_count;
|
||||
|
||||
if (prompt) {
|
||||
cerr << "Enter number count: ";
|
||||
}
|
||||
in_stream >> number_count;
|
||||
|
||||
in.numbers.resize(number_count);
|
||||
if (prompt && number_count > 0) {
|
||||
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++) {
|
||||
in_stream >> in.numbers[i];
|
||||
in >> in_data.numbers[i];
|
||||
}
|
||||
|
||||
if (prompt) {
|
||||
cerr << "Enter bin count: ";
|
||||
}
|
||||
in_stream >> in.bin_count;
|
||||
|
||||
return in;
|
||||
if (prompt) cerr << "Enter bin count: ";
|
||||
in >> in_data.bin_count;
|
||||
return in_data;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Колбэк-функция для 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;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
auto in = input_data(cin, true);
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
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;
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user