aboutsummaryrefslogtreecommitdiffstats
path: root/src/easy_download.c
diff options
context:
space:
mode:
authorAndreas Obergrusberger <tradiaz@yahoo.de>2006-08-04 08:57:51 +0000
committerAndreas Obergrusberger <tradiaz@yahoo.de>2006-08-04 08:57:51 +0000
commit25ad69ee7c66b623ac0e4f9cab114bd20afe52d6 (patch)
treef1d41648ef4aa216e023f34b908f02a0c819c36b /src/easy_download.c
parente2a6f80bc6c71cfe6a68f419b2561d6d32252a88 (diff)
downloadmpd-25ad69ee7c66b623ac0e4f9cab114bd20afe52d6.tar.gz
mpd-25ad69ee7c66b623ac0e4f9cab114bd20afe52d6.tar.xz
mpd-25ad69ee7c66b623ac0e4f9cab114bd20afe52d6.zip
added qball's easy_download
git-svn-id: https://svn.musicpd.org/ncmpc/branches/tradiaz@4544 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'src/easy_download.c')
-rw-r--r--src/easy_download.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/easy_download.c b/src/easy_download.c
new file mode 100644
index 000000000..e28541747
--- /dev/null
+++ b/src/easy_download.c
@@ -0,0 +1,82 @@
+/* by Qball
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+#include <curl/curl.h>
+#include "easy_download.h"
+
+static size_t write_data(void *buffer, size_t size,
+ size_t nmemb, easy_download_struct *dld)
+{
+ if(dld->data == NULL)
+ {
+ dld->size = 0;
+ }
+ dld->data = g_realloc(dld->data,(gulong)(size*nmemb+dld->size));
+
+ memset(&(dld->data)[dld->size], '\0', (size*nmemb));
+ memcpy(&(dld->data)[dld->size], buffer, size*nmemb);
+
+ dld->size += size*nmemb;
+ if(dld->size >= dld->max_size && dld->max_size > 0)
+ {
+ return 0;
+ }
+ return size*nmemb;
+}
+
+int easy_download(char *url,easy_download_struct *dld,
+ curl_progress_callback cb)
+{
+ CURL *curl = NULL;
+ int res;
+ if(!dld) return 0;
+ easy_download_clean(dld);
+ /* initialize curl */
+ curl = curl_easy_init();
+ if(!curl) return 0;
+ /* set uri */
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ /* set callback data */
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, dld);
+ /* set callback function */
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
+ curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
+ curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, cb);
+ /* run */
+ res = curl_easy_perform(curl);
+ /* cleanup */
+ curl_easy_cleanup(curl);
+ if(!res)
+ {
+ return 1;
+ }
+ if(dld->data) g_free(dld->data);
+ return 0;
+}
+
+void easy_download_clean(easy_download_struct *dld)
+{
+ if(dld->data)g_free(dld->data);
+ dld->data = NULL;
+ dld->size = 0;
+ dld->max_size = -1;
+}