Showing
3 changed files
with
62 additions
and
6 deletions
| ... | @@ -3,18 +3,37 @@ | ... | @@ -3,18 +3,37 @@ |
| 3 | 3 | ||
| 4 | 4 | ||
| 5 | ////////////////////////////////////////////////////////////////// | 5 | ////////////////////////////////////////////////////////////////// |
| 6 | +// private func | ||
| 7 | +////////////////////////////////////////////////////////////////// | ||
| 8 | +size_t curl_processor::write_callback(void *contents, size_t size, size_t nmemb, void *userp) { | ||
| 9 | + //copy ptr | ||
| 10 | + std::string* p_str = (std::string*)userp; | ||
| 11 | + | ||
| 12 | + //clear | ||
| 13 | + if (!p_str->empty()) | ||
| 14 | + p_str->clear(); | ||
| 15 | + | ||
| 16 | + //write | ||
| 17 | + p_str->append((char*)contents, size*nmemb); | ||
| 18 | + return size*nmemb; | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +////////////////////////////////////////////////////////////////// | ||
| 6 | // constructor, destructor | 23 | // constructor, destructor |
| 7 | ////////////////////////////////////////////////////////////////// | 24 | ////////////////////////////////////////////////////////////////// |
| 8 | -curl_processor::curl_processor() | 25 | +curl_processor::curl_processor() { |
| 9 | -{ | ||
| 10 | curl = curl_easy_init(); | 26 | curl = curl_easy_init(); |
| 11 | if (!curl) { | 27 | if (!curl) { |
| 12 | throw std::exception("failed to make curl object"); | 28 | throw std::exception("failed to make curl object"); |
| 13 | } | 29 | } |
| 30 | + | ||
| 31 | + //set curl option | ||
| 32 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, this->write_callback); | ||
| 33 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result_buffer); | ||
| 14 | } | 34 | } |
| 15 | 35 | ||
| 16 | -curl_processor::~curl_processor() | 36 | +curl_processor::~curl_processor() { |
| 17 | -{ | ||
| 18 | curl_easy_cleanup(curl); | 37 | curl_easy_cleanup(curl); |
| 19 | } | 38 | } |
| 20 | 39 | ||
| ... | @@ -22,3 +41,11 @@ curl_processor::~curl_processor() | ... | @@ -22,3 +41,11 @@ curl_processor::~curl_processor() |
| 22 | ////////////////////////////////////////////////////////////////// | 41 | ////////////////////////////////////////////////////////////////// |
| 23 | // methods | 42 | // methods |
| 24 | ////////////////////////////////////////////////////////////////// | 43 | ////////////////////////////////////////////////////////////////// |
| 44 | +void curl_processor::set_url(const char *url) { | ||
| 45 | + curl_easy_setopt(curl, CURLOPT_URL, url); | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +bool curl_processor::perform() { | ||
| 49 | + res = curl_easy_perform(curl); | ||
| 50 | + return (res == CURLE_OK); | ||
| 51 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -9,6 +9,11 @@ class curl_processor | ... | @@ -9,6 +9,11 @@ class curl_processor |
| 9 | private: | 9 | private: |
| 10 | CURL *curl; | 10 | CURL *curl; |
| 11 | CURLcode res; | 11 | CURLcode res; |
| 12 | + std::string result_buffer; | ||
| 13 | + | ||
| 14 | + //private func | ||
| 15 | +private: | ||
| 16 | + size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp); | ||
| 12 | 17 | ||
| 13 | //constructor, destructor | 18 | //constructor, destructor |
| 14 | public: | 19 | public: |
| ... | @@ -17,7 +22,9 @@ public: | ... | @@ -17,7 +22,9 @@ public: |
| 17 | 22 | ||
| 18 | //methods | 23 | //methods |
| 19 | public: | 24 | public: |
| 20 | - | 25 | + std::string get_buffer() const { return result_buffer; } |
| 26 | + void set_url(const char *url); | ||
| 27 | + bool perform(); | ||
| 21 | }; | 28 | }; |
| 22 | 29 | ||
| 23 | #endif // CURL_PROCESSOR_H | 30 | #endif // CURL_PROCESSOR_H |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -2,20 +2,42 @@ | ... | @@ -2,20 +2,42 @@ |
| 2 | #include "PaperGraphWidget.h" | 2 | #include "PaperGraphWidget.h" |
| 3 | #include "MainWindow.h" | 3 | #include "MainWindow.h" |
| 4 | 4 | ||
| 5 | +size_t write_callback(void *contents, size_t size, | ||
| 6 | + size_t nmemb, void *userp) { | ||
| 7 | + std::string* p_str = (std::string*)userp; | ||
| 8 | + | ||
| 9 | + //clear | ||
| 10 | + if (!p_str->empty()) | ||
| 11 | + p_str->clear(); | ||
| 12 | + | ||
| 13 | + //write | ||
| 14 | + p_str->append((char*)contents, size*nmemb); | ||
| 15 | + return size*nmemb; | ||
| 16 | +} | ||
| 17 | + | ||
| 5 | int main(int argc, char *argv[]) | 18 | int main(int argc, char *argv[]) |
| 6 | { | 19 | { |
| 7 | if (1) { | 20 | if (1) { |
| 8 | CURL *curl; | 21 | CURL *curl; |
| 9 | CURLcode res; | 22 | CURLcode res; |
| 23 | + std::string read_buffer; | ||
| 10 | 24 | ||
| 11 | curl = curl_easy_init(); | 25 | curl = curl_easy_init(); |
| 12 | if (curl) { | 26 | if (curl) { |
| 13 | curl_easy_setopt(curl, CURLOPT_URL, "http://dblp.uni-trier.de/rec/bib/conf/sbrn/WedemannCD06"); | 27 | curl_easy_setopt(curl, CURLOPT_URL, "http://dblp.uni-trier.de/rec/bib/conf/sbrn/WedemannCD06"); |
| 14 | /* example.com is redirected, so we tell libcurl to follow redirection */ | 28 | /* example.com is redirected, so we tell libcurl to follow redirection */ |
| 15 | - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); | 29 | + |
| 30 | + //write option | ||
| 31 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); | ||
| 32 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &read_buffer); | ||
| 16 | 33 | ||
| 17 | /* Perform the request, res will get the return code */ | 34 | /* Perform the request, res will get the return code */ |
| 18 | res = curl_easy_perform(curl); | 35 | res = curl_easy_perform(curl); |
| 36 | + res = curl_easy_perform(curl); | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + printf("%s\n", read_buffer.c_str()); | ||
| 40 | + | ||
| 19 | /* Check for errors */ | 41 | /* Check for errors */ |
| 20 | if (res != CURLE_OK) | 42 | if (res != CURLE_OK) |
| 21 | fprintf(stderr, "curl_easy_perform() failed: %s\n", | 43 | fprintf(stderr, "curl_easy_perform() failed: %s\n", | ... | ... |
-
Please register or login to post a comment