getinmemory.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * Shows how the write callback function can be used to download data into a
00024  * chunk of memory instead of storing it in a file.
00025  * </DESC>
00026  */
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 
00032 #include <curl/curl.h>
00033 
00034 struct MemoryStruct {
00035   char *memory;
00036   size_t size;
00037 };
00038 
00039 static size_t
00040 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
00041 {
00042   size_t realsize = size * nmemb;
00043   struct MemoryStruct *mem = (struct MemoryStruct *)userp;
00044 
00045   mem->memory = realloc(mem->memory, mem->size + realsize + 1);
00046   if(mem->memory == NULL) {
00047     /* out of memory! */
00048     printf("not enough memory (realloc returned NULL)\n");
00049     return 0;
00050   }
00051 
00052   memcpy(&(mem->memory[mem->size]), contents, realsize);
00053   mem->size += realsize;
00054   mem->memory[mem->size] = 0;
00055 
00056   return realsize;
00057 }
00058 
00059 int main(void)
00060 {
00061   CURL *curl_handle;
00062   CURLcode res;
00063 
00064   struct MemoryStruct chunk;
00065 
00066   chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */
00067   chunk.size = 0;    /* no data at this point */
00068 
00069   curl_global_init(CURL_GLOBAL_ALL);
00070 
00071   /* init the curl session */
00072   curl_handle = curl_easy_init();
00073 
00074   /* specify URL to get */
00075   curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.example.com/");
00076 
00077   /* send all data to this function  */
00078   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00079 
00080   /* we pass our 'chunk' struct to the callback function */
00081   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
00082 
00083   /* some servers don't like requests that are made without a user-agent
00084      field, so we provide one */
00085   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
00086 
00087   /* get it! */
00088   res = curl_easy_perform(curl_handle);
00089 
00090   /* check for errors */
00091   if(res != CURLE_OK) {
00092     fprintf(stderr, "curl_easy_perform() failed: %s\n",
00093             curl_easy_strerror(res));
00094   }
00095   else {
00096     /*
00097      * Now, our chunk.memory points to a memory block that is chunk.size
00098      * bytes big and contains the remote file.
00099      *
00100      * Do something nice with it!
00101      */
00102 
00103     printf("%lu bytes retrieved\n", (long)chunk.size);
00104   }
00105 
00106   /* cleanup curl stuff */
00107   curl_easy_cleanup(curl_handle);
00108 
00109   free(chunk.memory);
00110 
00111   /* we're done with libcurl, so clean it up */
00112   curl_global_cleanup();
00113 
00114   return 0;
00115 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02