Main Page | Data Structures | Directories | File List | Data Fields | Globals

mrss_download.c

Go to the documentation of this file.
00001 /* mRss - Copyright (C) 2005 bakunin - Andrea Marchesini 
00002  *                                <bakunin@autistici.org>
00003  *
00004  * This source code is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Public License as published 
00006  * by the Free Software Foundation; either version 2 of the License,
00007  * or (at your option) any later version.
00008  *
00009  * This source code is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00012  * Please refer to the GNU Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Public License along with
00015  * this source code; if not, write to:
00016  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #include <config.h>
00021 #else
00022 # error Use configure; make; make install
00023 #endif
00024 
00025 #include "mrss.h"
00026 #include "mrss_internal.h"
00027 
00028 static size_t
00029 __mrss_memorize_file (void *ptr, size_t size, size_t nmemb, void *data)
00030 {
00031   register int realsize = size * nmemb;
00032   __mrss_download_t *mem = (__mrss_download_t *) data;
00033 
00034   if (!mem->mm)
00035     {
00036       if (!(mem->mm = (char *) malloc (realsize + 1)))
00037         return -1;
00038     }
00039   else
00040     {
00041       if (!(mem->mm = (char *) realloc (mem->mm, mem->size + realsize + 1)))
00042         return -1;
00043     }
00044 
00045   memcpy (&(mem->mm[mem->size]), ptr, realsize);
00046   mem->size += realsize;
00047   mem->mm[mem->size] = 0;
00048 
00049   return realsize;
00050 }
00051 
00052 __mrss_download_t *
00053 __mrss_download_file (char *fl)
00054 {
00055   __mrss_download_t *chunk;
00056   CURL *curl;
00057 
00058   if (!(chunk = (__mrss_download_t *) malloc (sizeof (__mrss_download_t))))
00059     return NULL;
00060 
00061   chunk->mm = NULL;
00062   chunk->size = 0;
00063 
00064   curl_global_init (CURL_GLOBAL_DEFAULT);
00065   if (!(curl = curl_easy_init ()))
00066     {
00067       free (chunk);
00068       return NULL;
00069     }
00070 
00071   curl_easy_setopt (curl, CURLOPT_URL, fl);
00072   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, __mrss_memorize_file);
00073   curl_easy_setopt (curl, CURLOPT_FILE, (void *) chunk);
00074   curl_easy_setopt (curl, CURLOPT_TIMEOUT, 10);
00075 
00076   if (curl_easy_perform (curl))
00077     {
00078       if (chunk->mm)
00079         free (chunk->mm);
00080 
00081       free (chunk);
00082 
00083       curl_easy_cleanup (curl);
00084       curl_global_cleanup ();
00085 
00086       return NULL;
00087     }
00088 
00089   curl_easy_cleanup (curl);
00090   curl_global_cleanup ();
00091 
00092   return chunk;
00093 }
00094 
00095 /* EOF */

Generated on Thu Jul 28 12:15:04 2005 for libmrss by  doxygen 1.4.3-20050530