main: исправление ошибок с cURL

main
TarasovEE 5 дней назад
Родитель 4bfc5445de
Сommit f4b7b6e0fa

@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h"
using namespace std; using namespace std;
@ -18,13 +19,13 @@ using namespace std;
// Ââîä îöåíêîê ñ êëàâèàòóðû. // Ââîä îöåíêîê ñ êëàâèàòóðû.
Input Input input_data(istream& in, bool prompt)
input_data(istream& in, bool prompt)
{ {
Input local; Input local;
size_t number_count; size_t number_count;
cerr << "Enter number count: "; if (prompt)
cerr << "Enter number count: ";
in >> number_count; in >> number_count;
local.numbers.resize(number_count); local.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++) for (size_t i = 0; i < number_count; i++)
@ -35,10 +36,20 @@ input_data(istream& in, bool prompt)
return local; return local;
} }
// Ñîõðàíåíèå ôàéëà èç ñåòè â áóôåð.
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<stringstream*>(ctx);
(*buffer).write(reinterpret_cast<const char*>(items), data_size);
return data_size;
}
// Çàãðóçêà ôàéëà èç ñåòè â áóôåð. // Çàãðóçêà ôàéëà èç ñåòè â áóôåð.
Input Input download(const string& address)
download(const string& address)
{ {
stringstream buffer; stringstream buffer;
@ -46,7 +57,9 @@ download(const string& address)
if(curl) if(curl)
{ {
CURLcode res; CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, address); 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); res = curl_easy_perform(curl);
if (res != CURLE_OK) if (res != CURLE_OK)
{ {
@ -58,20 +71,9 @@ download(const string& address)
return input_data(buffer, false); return input_data(buffer, false);
} }
// Ñîõðàíåíèå ôàéëà èç ñåòè â áóôåð.
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<stringstream*>(ctx);
buffer->write(reinterpret_cast<const char*>(items), data_size);
return data_size;
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
curl_global_init(CURL_GLOBAL_ALL);
Input input; Input input;
if (argc > 1) if (argc > 1)
input = download(argv[1]); input = download(argv[1]);
@ -80,5 +82,7 @@ int main(int argc, char* argv[])
const auto bins = make_histogram(input.numbers, input.bin_count); const auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins); show_histogram_svg(bins);
return 0;
} }

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