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