code: Сохранение данных из сети в буфер

main
BushmanovAS 11 месяцев назад
Родитель 90b76949e4
Сommit 13c7d45255

@ -31,6 +31,13 @@ Input input_data(istream& in, bool prompt = true)
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
in >> input.bin_count; in >> input.bin_count;
return input; return input;
}
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) {
@ -39,6 +46,8 @@ download(const string& address) {
if(curl) { if(curl) {
CURLcode res; CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, address.c_str()); 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){
cerr << curl_easy_strerror(res); cerr << curl_easy_strerror(res);
@ -51,15 +60,15 @@ download(const string& address) {
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
bool show_prompts;
cout << "Show prompts? (1 for yes, 0 for no): ";
cin >> show_prompts;
Input input; Input input;
if (argc > 1) { if (argc > 1) {
input = download(argv[1]); input = download(argv[1]);
} else { } else {
input = input_data(cin, true); input = input_data(cin, true);
} }
bool show_prompts;
cout << "Show prompts? (1 for yes, 0 for no): ";
cin >> show_prompts;
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);
} }

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