You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __GP_CURL_PROTOTYPE____
  2. #define __GP_CURL_PROTOTYPE____
  3. #include "core.h"
  4. class _GP_CURL{
  5. public:
  6. char *memory;
  7. CURL *curl;
  8. size_t size;
  9. private:
  10. static size_t WriteMemoryCallback( void *ptr, size_t nsize, size_t nmemb, void *data ){
  11. size_t realsize = nsize * nmemb;
  12. _GP_CURL *me = ( _GP_CURL * )data;
  13. me->memory = ( char * ) realloc( me->memory, me->size + realsize + 1 );
  14. if ( me->memory == NULL) {
  15. /* out of memory! */
  16. return -1;
  17. };
  18. memcpy( &( me->memory[ me->size ] ), ptr, realsize );
  19. me->size += realsize;
  20. me->memory[ me->size ] = 0;
  21. return realsize;
  22. };
  23. public:
  24. _GP_CURL(){
  25. memory = NULL;
  26. size = 0;
  27. curl = curl_easy_init();
  28. };
  29. ~_GP_CURL(){
  30. if( memory ) free( memory );
  31. };
  32. _GP_CURL( CSTR url ){
  33. CURLcode res;
  34. curl = curl_easy_init();
  35. memory = NULL;
  36. size = 0;
  37. if( curl ){
  38. curl_easy_setopt( curl, CURLOPT_URL, url );
  39. curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback );
  40. curl_easy_setopt( curl, CURLOPT_WRITEDATA, ( void *) this );
  41. curl_easy_setopt( curl, CURLOPT_USERAGENT, "libcurl-agent/1.0" );
  42. res = curl_easy_perform( curl );
  43. /* always cleanup */
  44. curl_easy_cleanup( curl );
  45. };
  46. };
  47. void Query( CSTR url ){
  48. CURLcode res;
  49. if( curl ){
  50. curl_easy_setopt( curl, CURLOPT_URL, url );
  51. curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback );
  52. curl_easy_setopt( curl, CURLOPT_WRITEDATA, ( void *) this );
  53. //curl_easy_setopt( curl, CURLOPT_USERAGENT, "libcurl-agent/1.0" );
  54. res = curl_easy_perform( curl );
  55. /* always cleanup */
  56. curl_easy_cleanup( curl );
  57. };
  58. };
  59. size_t getSize( void ){
  60. return size;
  61. };
  62. CSTR getResult( void ){
  63. return ( CSTR ) memory;
  64. };
  65. };
  66. #endif