Skip to content

Commit

Permalink
add libcurl
Browse files Browse the repository at this point in the history
  • Loading branch information
squawkcpp committed Aug 5, 2018
1 parent 6628a7f commit 1a17cc9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
95 changes: 91 additions & 4 deletions av/discid/discid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
#include "chromaprint.h"

#include "../av.h"
#include "http/httpclient.h"
// #include "http/httpclient.h"
#include <curl/curl.h>

#include "musicbrainz.h"
#include "cddb.h"
Expand All @@ -43,12 +44,98 @@ std::ostream& operator<< ( std::ostream& stream, release_t& r ) {
return stream;
}

struct MemoryStruct {
char *memory;
size_t size;
};

static size_t
WriteMemoryCallback ( void *contents, size_t size, size_t nmemb, void *userp ) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = ( struct MemoryStruct * ) userp;

mem->memory = reinterpret_cast< char* > ( realloc ( mem->memory, mem->size + realsize + 1 ) );

if ( mem->memory == NULL ) {
/* out of memory! */
printf ( "not enough memory (realloc returned NULL)\n" );
return 0;
}

memcpy ( & ( mem->memory[mem->size] ), contents, realsize );
mem->size += realsize;
mem->memory[mem->size] = 0;

return realsize;
}

std::error_code get ( const std::string& uri, std::stringstream& ss ) {

auto _response = http::get ( uri, ss );
CURL *curl_handle;
CURLcode res;

struct MemoryStruct chunk;

chunk.memory = reinterpret_cast< char* > ( malloc ( 1 ) ); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */

curl_global_init ( CURL_GLOBAL_ALL );

/* init the curl session */
curl_handle = curl_easy_init();

/* specify URL to get */
curl_easy_setopt ( curl_handle, CURLOPT_URL, uri.c_str() );

/* send all data to this function */
curl_easy_setopt ( curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback );

/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt ( curl_handle, CURLOPT_WRITEDATA, ( void * ) &chunk );

/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt ( curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0" );

/* get it! */
res = curl_easy_perform ( curl_handle );

/* check for errors */
if ( res != CURLE_OK ) {
fprintf ( stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror ( res ) );
}

else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/

printf ( "%lu bytes retrieved\n", ( unsigned long ) chunk.size );
std::cout << std::string ( chunk.memory, 0, chunk.size ) << std::endl;
ss << std::string ( chunk.memory, 0, chunk.size );
}

/* cleanup curl stuff */
curl_easy_cleanup ( curl_handle );

free ( chunk.memory );

/* we're done with libcurl, so clean it up */
curl_global_cleanup();






// auto _response = http::get ( uri, ss );

if ( _response.status() != http::http_status::OK )
{ return av::make_error_code ( static_cast< int > ( _response.status() ) ); }
// if ( _response.status() != http::http_status::OK )
// { return av::make_error_code ( static_cast< int > ( _response.status() ) ); }

return std::error_code();
}
Expand Down
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AvcppConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
description = "C++ wrapper for ffmpeg libraries"
requires = "ffmpeg/3.4.1@conan-cpp/latest", "chromaprint/1.4.3@conan-cpp/latest", "OpenSSL/1.0.2m@conan/stable", "gtest/1.8.0@bincrafters/stable", "jsonformoderncpp/3.1.2@vthiery/stable", "lightningcpp/0.1.0@conan-cpp/latest"
requires = "ffmpeg/3.4.1@conan-cpp/latest", "chromaprint/1.4.3@conan-cpp/latest", "OpenSSL/1.0.2m@conan/stable", "gtest/1.8.0@bincrafters/stable", "jsonformoderncpp/3.1.2@vthiery/stable", "libcurl/7.60.0@bincrafters/stable"
options = { "shared": [True, False], "build_tests": [True, False], "build_samples": [True, False]}
default_options = "shared=False", "*:shared=False", "build_tests=False", "build_samples=False"
exports_sources = "*", "av/"
Expand Down
2 changes: 1 addition & 1 deletion conanfile.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AvcppConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
description = "C++ wrapper for ffmpeg libraries"
requires = "ffmpeg/3.4.1@conan-cpp/latest", "chromaprint/1.4.3@conan-cpp/latest", "OpenSSL/1.0.2m@conan/stable", "gtest/1.8.0@bincrafters/stable", "jsonformoderncpp/3.1.2@vthiery/stable", "lightningcpp/0.1.0@conan-cpp/latest"
requires = "ffmpeg/3.4.1@conan-cpp/latest", "chromaprint/1.4.3@conan-cpp/latest", "OpenSSL/1.0.2m@conan/stable", "gtest/1.8.0@bincrafters/stable", "jsonformoderncpp/3.1.2@vthiery/stable", "libcurl/7.60.0@bincrafters/stable"
options = { "shared": [True, False], "build_tests": [True, False], "build_samples": [True, False]}
default_options = "shared=False", "*:shared=False", "build_tests=False", "build_samples=False"
exports_sources = "*", "av/"
Expand Down

0 comments on commit 1a17cc9

Please sign in to comment.