remove the CVSish $Id$ lines
[platform/upstream/curl.git] / docs / examples / getinmemory.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  *
9  * Example source code to show how the callback function can be used to
10  * download data into a chunk of memory instead of storing it in a file.
11  *
12  * This exact source code has not been verified to work.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <curl/curl.h>
20 #include <curl/types.h>
21 #include <curl/easy.h>
22
23 struct MemoryStruct {
24   char *memory;
25   size_t size;
26 };
27
28 static void *myrealloc(void *ptr, size_t size);
29
30 static void *myrealloc(void *ptr, size_t size)
31 {
32   /* There might be a realloc() out there that doesn't like reallocing
33      NULL pointers, so we take care of it here */
34   if(ptr)
35     return realloc(ptr, size);
36   else
37     return malloc(size);
38 }
39
40 static size_t
41 WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
42 {
43   size_t realsize = size * nmemb;
44   struct MemoryStruct *mem = (struct MemoryStruct *)data;
45
46   mem->memory = myrealloc(mem->memory, mem->size + realsize + 1);
47   if (mem->memory) {
48     memcpy(&(mem->memory[mem->size]), ptr, realsize);
49     mem->size += realsize;
50     mem->memory[mem->size] = 0;
51   }
52   return realsize;
53 }
54
55 int main(int argc, char **argv)
56 {
57   CURL *curl_handle;
58
59   struct MemoryStruct chunk;
60
61   chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
62   chunk.size = 0;    /* no data at this point */
63
64   curl_global_init(CURL_GLOBAL_ALL);
65
66   /* init the curl session */
67   curl_handle = curl_easy_init();
68
69   /* specify URL to get */
70   curl_easy_setopt(curl_handle, CURLOPT_URL, "http://cool.haxx.se/");
71
72   /* send all data to this function  */
73   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
74
75   /* we pass our 'chunk' struct to the callback function */
76   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
77
78   /* some servers don't like requests that are made without a user-agent
79      field, so we provide one */
80   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
81
82   /* get it! */
83   curl_easy_perform(curl_handle);
84
85   /* cleanup curl stuff */
86   curl_easy_cleanup(curl_handle);
87
88   /*
89    * Now, our chunk.memory points to a memory block that is chunk.size
90    * bytes big and contains the remote file.
91    *
92    * Do something nice with it!
93    *
94    * You should be aware of the fact that at this point we might have an
95    * allocated data block, and nothing has yet deallocated that data. So when
96    * you're done with it, you should free() it as a nice application.
97    */
98
99   if(chunk.memory)
100     free(chunk.memory);
101
102   /* we're done with libcurl, so clean it up */
103   curl_global_cleanup();
104
105   return 0;
106 }