From e09b94fef5666805510c85b4deff70e6dadf48eb Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Mon, 26 Nov 2012 00:07:10 +0000 Subject: [PATCH] Factor download to a temporary file into a single reused function. --- src/bingmapsource.c | 25 ++----------------------- src/download.c | 34 ++++++++++++++++++++++++++++++++++ src/download.h | 2 ++ src/geonamessearch.c | 28 +--------------------------- src/vikgototool.c | 19 +------------------ 5 files changed, 40 insertions(+), 68 deletions(-) diff --git a/src/bingmapsource.c b/src/bingmapsource.c index 5a797d91..cf72e214 100644 --- a/src/bingmapsource.c +++ b/src/bingmapsource.c @@ -46,7 +46,6 @@ #include #include #include "globals.h" -#include "curl_download.h" #include "bingmapsource.h" #include "bbox.h" #include "background.h" @@ -391,32 +390,12 @@ _parse_file_for_attributions(BingMapSource *self, gchar *filename) static int _load_attributions ( BingMapSource *self ) { - FILE *tmp_file; - int tmp_fd; - gchar *tmpname; - gchar *uri; int ret = 0; /* OK */ BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self); + gchar *uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key); - if ((tmp_fd = g_file_open_tmp ("vik-bing.XXXXXX", &tmpname, NULL)) == -1) { - g_critical(_("couldn't open temp file")); - return -1; - } - - tmp_file = fdopen(tmp_fd, "r+"); - uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key); - - /* TODO: curl may not be available */ - if (curl_download_uri(uri, tmp_file, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)), 0, NULL)) { /* error */ - fclose(tmp_file); - tmp_file = NULL; - ret = -1; - goto done; - } - - fclose(tmp_file); - tmp_file = NULL; + gchar *tmpname = a_download_uri_to_tmp_file ( uri, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)) ); g_debug("%s: %s", __FUNCTION__, tmpname); if (!_parse_file_for_attributions(self, tmpname)) { diff --git a/src/download.c b/src/download.c index 05a525a0..b881dc1e 100644 --- a/src/download.c +++ b/src/download.c @@ -293,3 +293,37 @@ void a_download_handle_cleanup ( void *handle ) { curl_download_handle_cleanup ( handle ); } + +/** + * a_download_url_to_tmp_file: + * @uri: The URI (Uniform Resource Identifier) + * @options: Download options (maybe NULL) + * + * returns name of the temporary file created - NULL if unsuccessful + * this string needs to be freed once used + * the file needs to be removed once used + */ +gchar *a_download_uri_to_tmp_file ( const gchar *uri, DownloadMapOptions *options ) +{ + FILE *tmp_file; + int tmp_fd; + gchar *tmpname; + + if ( (tmp_fd = g_file_open_tmp ("viking-download.XXXXXX", &tmpname, NULL)) == -1 ) { + g_critical (_("couldn't open temp file")); + return NULL; + } + + tmp_file = fdopen(tmp_fd, "r+"); + + if ( curl_download_uri ( uri, tmp_file, options, NULL, NULL ) ) { + // error + fclose ( tmp_file ); + g_remove ( tmpname ); + g_free ( tmpname ); + return NULL; + } + fclose ( tmp_file ); + + return tmpname; +} diff --git a/src/download.h b/src/download.h index e9f69c23..7bbbfbb0 100644 --- a/src/download.h +++ b/src/download.h @@ -88,6 +88,8 @@ int a_ftp_download_get_url ( const char *hostname, const char *uri, const char * void *a_download_handle_init (); void a_download_handle_cleanup ( void *handle ); +gchar *a_download_uri_to_tmp_file ( const gchar *uri, DownloadMapOptions *options ); + /* Error messages returned by download functions */ enum { DOWNLOAD_NO_ERROR = 0, DOWNLOAD_NO_NEWER_FILE, diff --git a/src/geonamessearch.c b/src/geonamessearch.c index 88f7df5e..221fe244 100644 --- a/src/geonamessearch.c +++ b/src/geonamessearch.c @@ -31,7 +31,6 @@ #include "viking.h" #include "util.h" -#include "curl_download.h" #include "geonamessearch.h" /* Compatibility */ @@ -399,31 +398,6 @@ static GList *get_entries_from_file(gchar *file_name) } -static gchar *download_url(gchar *uri) -{ - FILE *tmp_file; - int tmp_fd; - gchar *tmpname; - - if ((tmp_fd = g_file_open_tmp ("vikgsearch.XXXXXX", &tmpname, NULL)) == -1) { - g_critical(_("couldn't open temp file")); - return NULL; - } - tmp_file = fdopen(tmp_fd, "r+"); - - if (curl_download_uri(uri, tmp_file, NULL, 0, NULL)) { - // error - fclose(tmp_file); - tmp_file = NULL; - g_remove(tmpname); - g_free(tmpname); - return(NULL); - } - fclose(tmp_file); - tmp_file = NULL; - return(tmpname); -} - void a_geonames_wikipedia_box ( VikWindow *vw, VikTrwLayer *vtl, struct LatLon maxmin[2] ) { gchar *uri; @@ -444,7 +418,7 @@ void a_geonames_wikipedia_box ( VikWindow *vw, VikTrwLayer *vtl, struct LatLon m g_free(south); south = NULL; g_free(east); east = NULL; g_free(west); west = NULL; - tmpname = download_url(uri); + tmpname = a_download_uri_to_tmp_file ( uri, NULL ); if (!tmpname) { none_found(vw); return; diff --git a/src/vikgototool.c b/src/vikgototool.c index b6516c3f..f784228c 100644 --- a/src/vikgototool.c +++ b/src/vikgototool.c @@ -25,7 +25,6 @@ #include "vikgototool.h" #include "util.h" -#include "curl_download.h" #include @@ -207,8 +206,6 @@ gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord ) { - FILE *tmp_file; - int tmp_fd; gchar *tmpname; gchar *uri; gchar *escaped_srch_str; @@ -221,24 +218,10 @@ int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str); - if ((tmp_fd = g_file_open_tmp ("vikgoto.XXXXXX", &tmpname, NULL)) == -1) { - g_critical(_("couldn't open temp file")); - return -1; - } - - tmp_file = fdopen(tmp_fd, "r+"); uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str); - /* TODO: curl may not be available */ - if (curl_download_uri(uri, tmp_file, vik_goto_tool_get_download_options(self), 0, NULL)) { /* error */ - fclose(tmp_file); - tmp_file = NULL; - ret = -1; - goto done; - } + tmpname = a_download_uri_to_tmp_file ( uri, vik_goto_tool_get_download_options(self) ); - fclose(tmp_file); - tmp_file = NULL; g_debug("%s: %s", __FILE__, tmpname); if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) { ret = -1; -- 2.39.5