조성현

중간저장3

......@@ -3,18 +3,37 @@
//////////////////////////////////////////////////////////////////
// private func
//////////////////////////////////////////////////////////////////
size_t curl_processor::write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
//copy ptr
std::string* p_str = (std::string*)userp;
//clear
if (!p_str->empty())
p_str->clear();
//write
p_str->append((char*)contents, size*nmemb);
return size*nmemb;
}
//////////////////////////////////////////////////////////////////
// constructor, destructor
//////////////////////////////////////////////////////////////////
curl_processor::curl_processor()
{
curl_processor::curl_processor() {
curl = curl_easy_init();
if (!curl) {
throw std::exception("failed to make curl object");
}
//set curl option
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, this->write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result_buffer);
}
curl_processor::~curl_processor()
{
curl_processor::~curl_processor() {
curl_easy_cleanup(curl);
}
......@@ -22,3 +41,11 @@ curl_processor::~curl_processor()
//////////////////////////////////////////////////////////////////
// methods
//////////////////////////////////////////////////////////////////
void curl_processor::set_url(const char *url) {
curl_easy_setopt(curl, CURLOPT_URL, url);
}
bool curl_processor::perform() {
res = curl_easy_perform(curl);
return (res == CURLE_OK);
}
\ No newline at end of file
......
......@@ -9,6 +9,11 @@ class curl_processor
private:
CURL *curl;
CURLcode res;
std::string result_buffer;
//private func
private:
size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp);
//constructor, destructor
public:
......@@ -17,7 +22,9 @@ public:
//methods
public:
std::string get_buffer() const { return result_buffer; }
void set_url(const char *url);
bool perform();
};
#endif // CURL_PROCESSOR_H
\ No newline at end of file
......
......@@ -2,20 +2,42 @@
#include "PaperGraphWidget.h"
#include "MainWindow.h"
size_t write_callback(void *contents, size_t size,
size_t nmemb, void *userp) {
std::string* p_str = (std::string*)userp;
//clear
if (!p_str->empty())
p_str->clear();
//write
p_str->append((char*)contents, size*nmemb);
return size*nmemb;
}
int main(int argc, char *argv[])
{
if (1) {
CURL *curl;
CURLcode res;
std::string read_buffer;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://dblp.uni-trier.de/rec/bib/conf/sbrn/WedemannCD06");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
//write option
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &read_buffer);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
res = curl_easy_perform(curl);
printf("%s\n", read_buffer.c_str());
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
......