]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/thirdparty/curl/docs/examples/getinmemory.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / thirdparty / curl / docs / examples / getinmemory.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 /* Example source code to show how the callback function can be used to
23  * download data into a chunk of memory instead of storing it in a file.
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <curl/curl.h>
31
32 struct MemoryStruct {
33   char *memory;
34   size_t size;
35 };
36
37
38 static size_t
39 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
40 {
41   size_t realsize = size * nmemb;
42   struct MemoryStruct *mem = (struct MemoryStruct *)userp;
43
44   mem->memory = realloc(mem->memory, mem->size + realsize + 1);
45   if(mem->memory == NULL) {
46     /* out of memory! */
47     printf("not enough memory (realloc returned NULL)\n");
48     return 0;
49   }
50
51   memcpy(&(mem->memory[mem->size]), contents, realsize);
52   mem->size += realsize;
53   mem->memory[mem->size] = 0;
54
55   return realsize;
56 }
57
58
59 int main(void)
60 {
61   CURL *curl_handle;
62   CURLcode res;
63
64   struct MemoryStruct chunk;
65
66   chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */
67   chunk.size = 0;    /* no data at this point */
68
69   curl_global_init(CURL_GLOBAL_ALL);
70
71   /* init the curl session */
72   curl_handle = curl_easy_init();
73
74   /* specify URL to get */
75   curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.example.com/");
76
77   /* send all data to this function  */
78   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
79
80   /* we pass our 'chunk' struct to the callback function */
81   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
82
83   /* some servers don't like requests that are made without a user-agent
84      field, so we provide one */
85   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
86
87   /* get it! */
88   res = curl_easy_perform(curl_handle);
89
90   /* check for errors */
91   if(res != CURLE_OK) {
92     fprintf(stderr, "curl_easy_perform() failed: %s\n",
93             curl_easy_strerror(res));
94   }
95   else {
96     /*
97      * Now, our chunk.memory points to a memory block that is chunk.size
98      * bytes big and contains the remote file.
99      *
100      * Do something nice with it!
101      *
102      * You should be aware of the fact that at this point we might have an
103      * allocated data block, and nothing has yet deallocated that data. So when
104      * you're done with it, you should free() it as a nice application.
105      */
106
107     printf("%lu bytes retrieved\n", (long)chunk.size);
108   }
109
110   /* cleanup curl stuff */
111   curl_easy_cleanup(curl_handle);
112
113   if(chunk.memory)
114     free(chunk.memory);
115
116   /* we're done with libcurl, so clean it up */
117   curl_global_cleanup();
118
119   return 0;
120 }