code: сохранение данных из сети в буфер
Этот коммит содержится в:
48
main.cpp
48
main.cpp
@@ -1,6 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
//#include "text.h"
|
//#include "text.h"
|
||||||
#include "svg.h"
|
#include "svg.h"
|
||||||
@@ -36,26 +37,42 @@ input_data(istream& in_stream,bool prompt) {
|
|||||||
|
|
||||||
for (size_t i = 0; i < in.bin_count; i++) {
|
for (size_t i = 0; i < in.bin_count; i++) {
|
||||||
in_stream >> in.colors[i];
|
in_stream >> in.colors[i];
|
||||||
|
//in.colors[i]="red";
|
||||||
bool check = check_color(in.colors[i]);
|
bool check = check_color(in.colors[i]);
|
||||||
while (!check) {
|
while (!check) {
|
||||||
if (prompt) cerr << "Incorrect input. Color doesn't contain # or has spaces." << endl;
|
if (prompt) cerr << "Incorrect input. Color doesn't contain # or has spaces." << endl;
|
||||||
in_stream >> in.colors[i];
|
in_stream >> in.colors[i];
|
||||||
|
//in.colors[i]="red";
|
||||||
check = check_color(in.colors[i]);
|
check = check_color(in.colors[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t write_header(char* buffer, size_t size, size_t nitems, void* userdata)
|
||||||
{
|
{
|
||||||
if (argc > 1)
|
return size * nitems;
|
||||||
{
|
}
|
||||||
//cerr<<argc<<endl;
|
|
||||||
//for (size_t i = 0; argv[i]; i++) cerr<<argv[i]<<endl;
|
Input download(const string& address) {
|
||||||
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
stringstream buffer;
|
||||||
|
|
||||||
CURL* curl = curl_easy_init();
|
CURL* curl = curl_easy_init();
|
||||||
if(curl) {
|
if (curl) {
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
|
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_header);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HEADERDATA, NULL);
|
||||||
res = curl_easy_perform(curl);
|
res = curl_easy_perform(curl);
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
if (res != CURLE_OK) {
|
if (res != CURLE_OK) {
|
||||||
@@ -63,10 +80,21 @@ int main(int argc, char* argv[])
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
|
return input_data(buffer, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
Input in;
|
||||||
|
if (argc > 1)
|
||||||
|
{
|
||||||
|
in = download(argv[1]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
in = input_data(cin,true);
|
||||||
}
|
}
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
|
||||||
auto in = input_data(cin,true);
|
|
||||||
vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
|
vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
|
||||||
//show_histogram_text(bins, in.bin_count);
|
//show_histogram_text(bins, in.bin_count);
|
||||||
show_histogram_svg(bins, in.colors);
|
show_histogram_svg(bins, in.colors);
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user