From 08c45600d2c013a30c84369fc5a81e2e04ef2a41 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Wed, 4 Nov 2020 14:06:01 +0100 Subject: [PATCH] simplified data transfer: now directly appended to std::string --- requester.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/requester.cpp b/requester.cpp index 5899762..c5fdacb 100644 --- a/requester.cpp +++ b/requester.cpp @@ -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* dest {reinterpret_cast*>(user_data)}; - size_t oldsize = dest->size(); - dest->resize(oldsize + realsize); - std::memcpy(dest->data() + oldsize, buf, realsize); + std::string* text {reinterpret_cast(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 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; }