work with CURL
Этот коммит содержится в:
70
main.cpp
70
main.cpp
@@ -3,6 +3,7 @@
|
|||||||
#include "svg.h"
|
#include "svg.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -12,17 +13,20 @@ struct Input
|
|||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input input_data(istream& sin, bool prompt)
|
Input input_data(istream& sin, bool prompt = true)
|
||||||
{
|
{
|
||||||
if (prompt){
|
if (prompt){
|
||||||
Input in;
|
Input in;
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
|
cerr << "Input number count" << endl;
|
||||||
sin >> number_count;
|
sin >> number_count;
|
||||||
in.numbers.resize(number_count);
|
in.numbers.resize(number_count);
|
||||||
for (size_t i = 0; i < number_count; i++)
|
for (size_t i = 0; i < number_count; i++)
|
||||||
{
|
{
|
||||||
|
cerr << "Input number "<< i << endl;
|
||||||
sin >> in.numbers[i];
|
sin >> in.numbers[i];
|
||||||
}
|
}
|
||||||
|
cerr << "Input bin count" << endl;
|
||||||
sin >> in.bin_count;
|
sin >> in.bin_count;
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
@@ -40,19 +44,69 @@ Input input_data(istream& sin, bool prompt)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
size_t
|
||||||
if (argc > 1) {
|
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
|
||||||
|
download(const string& address) {
|
||||||
|
stringstream buffer;
|
||||||
CURL* curl = curl_easy_init();
|
CURL* curl = curl_easy_init();
|
||||||
auto res = curl_easy_perform(curl);
|
|
||||||
if (res != 0) {
|
if (curl) {
|
||||||
|
CURLcode res;
|
||||||
|
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);
|
||||||
|
if (res != CURLE_OK) {
|
||||||
cout << curl_easy_strerror(res);
|
cout << curl_easy_strerror(res);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
Input in = input_data(cin, false);
|
return input_data(buffer, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
Input in;
|
||||||
|
string link, color = "";
|
||||||
|
bool flag = false;
|
||||||
|
if (argc == 2) {
|
||||||
|
in = download(argv[1]);
|
||||||
|
}
|
||||||
|
else if (argc == 3) {
|
||||||
|
cout << "Wrong argument, use correct input -stroke <color> or leave input empty" << endl;
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
else if (argc > 3) {
|
||||||
|
for (size_t i = 1; i < argc; i++) {
|
||||||
|
string cur = argv[i];
|
||||||
|
if (cur.find("://") != string::npos) {
|
||||||
|
link = cur;
|
||||||
|
}
|
||||||
|
else if (i != argc - 1){
|
||||||
|
string curm = argv[i+1];
|
||||||
|
if (cur == "-stroke" && curm.find("://") == string::npos) {
|
||||||
|
flag = true;
|
||||||
|
color = curm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (cur == "-stroke" && i == (argc - 1)) {
|
||||||
|
cout << "Wrong argument, use correct input -stroke <color> or leave input empty" << endl;
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
in = download(link);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
in = input_data(cin, false);
|
||||||
|
}
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
show_histogram_svg(bins);
|
show_histogram_svg(bins, color);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
13
svg.cpp
13
svg.cpp
@@ -31,16 +31,18 @@ svg_text(double left, double baseline, string text) {
|
|||||||
cout << "<text x='" << left << "' y='" << baseline<< "'>" << text << "</text>";
|
cout << "<text x='" << left << "' y='" << baseline<< "'>" << text << "</text>";
|
||||||
}
|
}
|
||||||
|
|
||||||
void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black"){
|
void
|
||||||
|
svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black"){
|
||||||
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />";
|
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
show_histogram_svg(const vector<size_t>& bins) {
|
show_histogram_svg(const vector<size_t>& bins, string stcolor) {
|
||||||
double max = 0;
|
double max = 0;
|
||||||
int maxlen = IMAGE_WIDTH - TEXT_WIDTH;
|
int maxlen = IMAGE_WIDTH - TEXT_WIDTH;
|
||||||
|
int bnumber = 0;
|
||||||
for (auto el: bins) {
|
for (auto el: bins) {
|
||||||
|
bnumber++;
|
||||||
if (max < el) {
|
if (max < el) {
|
||||||
max = el;
|
max = el;
|
||||||
}
|
}
|
||||||
@@ -49,6 +51,11 @@ show_histogram_svg(const vector<size_t>& bins) {
|
|||||||
|
|
||||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||||
string stroke[] = {"red", "green", "blue"};
|
string stroke[] = {"red", "green", "blue"};
|
||||||
|
if (stcolor != "") {
|
||||||
|
for (int i = 0; i < bnumber; i++){
|
||||||
|
stroke[i] = stcolor;
|
||||||
|
}
|
||||||
|
}
|
||||||
string fill[] = {"red", "green", "blue"};
|
string fill[] = {"red", "green", "blue"};
|
||||||
int i = 0;
|
int i = 0;
|
||||||
double top = 0;
|
double top = 0;
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user