Code

const pointers
[ncmpc.git] / src / easy_download.c
1 /*      by Qball
2  *  
3  *  This program is free software; you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation; either version 2 of the License, or
6  *  (at your option) any later version.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
17  
18 #
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include "ncmpc.h"
26 #ifdef ENABLE_LYRICS_SCREEN
28 #include <curl/curl.h>
29 #include "easy_download.h"
31 static size_t write_data(void *buffer, size_t size,
32         size_t nmemb, easy_download_struct *dld)
33 {
34         if(dld->data == NULL)
35         {
36                 dld->size = 0;
37         }
38         dld->data = g_realloc(dld->data,(gulong)(size*nmemb+dld->size));
40         memset(&(dld->data)[dld->size], '\0', (size*nmemb));
41         memcpy(&(dld->data)[dld->size], buffer, size*nmemb);
43         dld->size += size*nmemb;
44         if(dld->size >= dld->max_size && dld->max_size > 0)
45         {
46                 return 0;
47         }
48         return size*nmemb;
49 }
51 int easy_download(char *url,easy_download_struct *dld,
52                 curl_progress_callback cb)
53 {
54         CURL *curl = NULL;
55         int res;
56         if(!dld) return 0;
57         easy_download_clean(dld);
58         /* initialize curl */
59         curl = curl_easy_init();
60         if(!curl) return 0;
61         /* set uri */
62         curl_easy_setopt(curl, CURLOPT_URL, url);
63         /* set callback data */
64         curl_easy_setopt(curl, CURLOPT_WRITEDATA, dld);
65         /* set callback function */
66         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
67         curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
68         curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, cb);
69         /* run */
70         res = curl_easy_perform(curl);
71         /* cleanup */
72         curl_easy_cleanup(curl);
73         if(!res)
74         {
75                 return 1;
76         }
77         if(dld->data) g_free(dld->data);
78         return 0;
79 }
81 void easy_download_clean(easy_download_struct *dld)
82 {
83         if(dld->data)g_free(dld->data);
84         dld->data = NULL;
85         dld->size = 0;
86         dld->max_size = -1;
87 }
89 #endif