simplified data transfer: now directly appended to std::string

This commit is contained in:
Bob Polis 2020-11-04 14:06:01 +01:00
parent 9ef1ffc1c9
commit 08c45600d2

View File

@ -17,10 +17,8 @@ extern sc::logger logger;
size_t requester::write_data(char *buf, size_t sz, size_t nmemb, void *user_data) {
size_t realsize = sz * nmemb;
SCInfo(logger, "received ", realsize, " bytes");
std::vector<char>* dest {reinterpret_cast<std::vector<char>*>(user_data)};
size_t oldsize = dest->size();
dest->resize(oldsize + realsize);
std::memcpy(dest->data() + oldsize, buf, realsize);
std::string* text {reinterpret_cast<std::string*>(user_data)};
text->append(buf, realsize);
return realsize;
}
@ -35,11 +33,10 @@ requester::~requester() {
}
std::string requester::get(const std::string &url) {
std::string text;
curl_easy_setopt(_h.get(), CURLOPT_URL, url.c_str());
std::vector<char> buf;
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &buf);
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &text);
auto success = curl_easy_perform(_h.get());
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
std::string text {buf.data(), buf.size()};
return text;
}