Guest User

getinmemory.nim

a guest
Mar 1st, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 3.21 KB | None | 0 0
  1. #**************************************************************************
  2. #                                   _   _ ____  _
  3. #   Project                     ___| | | |  _ \| |
  4. #                              / __| | | | |_) | |
  5. #                             | (__| |_| |  _ <| |___
  6. #                              \___|\___/|_| \_\_____|
  7. #
  8. #  Copyright (C) 1998 - 2013, Daniel Stenberg, <[email protected]>, 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. type
  27.   MemoryStruct* = object
  28.     memory*: cstring
  29.     size*: csize
  30.  
  31.  
  32. proc WriteMemoryCallback*(contents: pointer; size: csize; nmemb: csize;
  33.                           userp: pointer): csize =
  34.   var realsize: csize = size * nmemb
  35.   var mem: ptr MemoryStruct = cast[ptr MemoryStruct](userp)
  36.   mem.memory = realloc(mem.memory, mem.size + realsize + 1)
  37.   if mem.memory == nil:
  38.     # out of memory!
  39.     printf("not enough memory (realloc returned NULL)\x0A")
  40.     return 0
  41.   memcpy(addr((mem.memory[mem.size])), contents, realsize)
  42.   inc(mem.size, realsize)
  43.   mem.memory[mem.size] = 0
  44.   return realsize
  45.  
  46. proc main*(): cint =
  47.   var curl_handle: ptr CURL
  48.   var res: CURLcode
  49.   var chunk: MemoryStruct
  50.   chunk.memory = malloc(1)
  51.   # will be grown as needed by the realloc above
  52.   chunk.size = 0
  53.   # no data at this point
  54.   curl_global_init(CURL_GLOBAL_ALL)
  55.   # init the curl session
  56.   curl_handle = curl_easy_init()
  57.   # specify URL to get
  58.   curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.example.com/")
  59.   # send all data to this function  
  60.   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback)
  61.   # we pass our 'chunk' struct to the callback function
  62.   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, cast[pointer](addr(chunk)))
  63.   # some servers don't like requests that are made without a user-agent
  64.   #     field, so we provide one
  65.   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0")
  66.   # get it!
  67.   res = curl_easy_perform(curl_handle)
  68.   # check for errors
  69.   if res != CURLE_OK:
  70.     fprintf(stderr, "curl_easy_perform() failed: %s\x0A",
  71.             curl_easy_strerror(res))
  72.   else:
  73.     #
  74.     #      Now, our chunk.memory points to a memory block that is chunk.size
  75.     #      bytes big and contains the remote file.
  76.     #    
  77.     #      Do something nice with it!
  78.     #    
  79.     printf("%lu bytes retrieved\x0A", cast[clong](chunk.size))
  80.   # cleanup curl stuff
  81.   curl_easy_cleanup(curl_handle)
  82.   if chunk.memory:
  83.     free(chunk.memory)
  84.   curl_global_cleanup()
  85.   return 0
Advertisement
Add Comment
Please, Sign In to add comment