Advertisement
Guest User

getFiles.cpp

a guest
Apr 4th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1.  
  2. #include "getFiles.h"
  3. #include <cstdio>
  4. #include <boost/filesystem.hpp>
  5.  
  6. size_t getFiles::my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) {
  7.     struct FtpFile *out=(struct FtpFile *)stream;
  8.     if(out && !out->stream) {
  9.         /* open file for writing */
  10.          /* if (out->destination.c_str() != NULL) {    // Boost needs fix!!!!! Doesn't create folder as supposed to!
  11.             boost::filesystem::create_directories(out->destination);
  12.         } */
  13.         out->stream=fopen(out->filename.c_str(), "wb");
  14.         if(!out->stream)
  15.             return -1; /* failure, can't open file to write */
  16.     }
  17.     return fwrite(buffer, size, nmemb, out->stream);
  18. }
  19.  
  20. void getFiles::run() {
  21.     if(curl) {
  22.         /*
  23.          * You better replace the URL with one that works!
  24.          */
  25.         curl_easy_setopt(curl, CURLOPT_URL,
  26.                          URL.c_str());
  27.         curl_easy_setopt(curl,CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
  28.         /* Define our callback to get called when there's data to be written */
  29.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
  30.         /* Set a pointer to our struct to pass to the callback */
  31.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
  32.        
  33.         /* Switch on full protocol/debug output */
  34.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  35.        
  36.         res = curl_easy_perform(curl);
  37.        
  38.         /* always cleanup */
  39.         curl_easy_cleanup(curl);
  40.        
  41.         if(CURLE_OK != res) {
  42.             /* we failed */
  43.             fprintf(stderr, "curl told us %d\n", res);
  44.         }
  45.     }
  46.     if(ftpfile.stream)
  47.         fclose(ftpfile.stream); /* close the local file */
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement