From ca9acc13c06234875059c7b5b97af35205978976 Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Thu, 14 Oct 2010 17:28:57 -0700 Subject: [PATCH] C++ify some curl stuff. --- src/ccurl_thread.cc | 40 ++++++++++++++++++---------------------- src/ccurl_thread.h | 10 +++++----- src/rss.cc | 2 +- src/specials.cc | 2 +- src/weather.cc | 5 +++-- 5 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/ccurl_thread.cc b/src/ccurl_thread.cc index 48528559..49b6040c 100644 --- a/src/ccurl_thread.cc +++ b/src/ccurl_thread.cc @@ -54,18 +54,17 @@ typedef struct _ccurl_headers_t { } ccurl_headers_t; /* finds a location based on uri in the list provided */ -ccurl_location_ptr ccurl_find_location(ccurl_location_list &locations, char *uri) +ccurl_location_ptr ccurl_find_location(ccurl_location_list &locations, const std::string &uri) { for (ccurl_location_list::iterator i = locations.begin(); i != locations.end(); i++) { - if ((*i)->uri && - strcmp((*i)->uri, uri) == EQUAL) { + if ((*i)->uri == std::string(uri)) { return *i; } } ccurl_location_ptr next = ccurl_location_ptr(new ccurl_location_t); - DBGP("new curl location: '%s'", uri); - next->uri = strndup(uri, text_buffer_size); + DBGP("new curl location: '%s'", uri.c_str()); + next->uri = std::string(uri, text_buffer_size); locations.push_back(next); return next; } @@ -75,9 +74,6 @@ void ccurl_free_locations(ccurl_location_list &locations) { for (ccurl_location_list::iterator i = locations.begin(); i != locations.end(); i++) { - free_and_zero((*i)->uri); - free_and_zero((*i)->last_modified); - free_and_zero((*i)->etag); free_and_zero((*i)->result); (*i)->p_timed_thread.reset(); } @@ -130,7 +126,7 @@ void ccurl_fetch_data(thread_handle &handle, const ccurl_location_ptr &curloc) CURLcode res; struct curl_slist *headers = NULL; - // curl temps + /* curl temps */ ccurl_memory_t chunk; ccurl_headers_t response_headers; @@ -140,8 +136,8 @@ void ccurl_fetch_data(thread_handle &handle, const ccurl_location_ptr &curloc) curl = curl_easy_init(); if (curl) { - DBGP("reading curl data from '%s'", curloc->uri); - curl_easy_setopt(curl, CURLOPT_URL, curloc->uri); + DBGP("reading curl data from '%s'", curloc->uri.c_str()); + curl_easy_setopt(curl, CURLOPT_URL, curloc->uri.c_str()); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, ccurl_parse_header_callback); curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *) &response_headers); @@ -152,19 +148,19 @@ void ccurl_fetch_data(thread_handle &handle, const ccurl_location_ptr &curloc) curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1000); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60); - if (curloc->last_modified) { + if (!curloc->last_modified.empty()) { const char *header = "If-Modified-Since: "; - int len = strlen(header) + strlen(curloc->last_modified) + 1; + int len = strlen(header) + curloc->last_modified.size() + 1; char *str = (char*) malloc(len); - snprintf(str, len, "%s%s", header, curloc->last_modified); + snprintf(str, len, "%s%s", header, curloc->last_modified.c_str()); headers = curl_slist_append(headers, str); free(str); } - if (curloc->etag) { + if (!curloc->etag.empty()) { const char *header = "If-None-Match: "; - int len = strlen(header) + strlen(curloc->etag) + 1; + int len = strlen(header) + curloc->etag.size() + 1; char *str = (char*) malloc(len); - snprintf(str, len, "%s%s", header, curloc->etag); + snprintf(str, len, "%s%s", header, curloc->etag.c_str()); headers = curl_slist_append(headers, str); free(str); } @@ -182,14 +178,14 @@ void ccurl_fetch_data(thread_handle &handle, const ccurl_location_ptr &curloc) case 200: { std::lock_guard lock(handle.mutex()); - free_and_zero(curloc->last_modified); - free_and_zero(curloc->etag); + curloc->last_modified.clear(); + curloc->etag.clear(); if (response_headers.last_modified) { curloc->last_modified = - strdup(response_headers.last_modified); + std::string(response_headers.last_modified); } if (response_headers.etag) { - curloc->etag = strdup(response_headers.etag); + curloc->etag = std::string(response_headers.etag); } curloc->process_function(curloc->result, chunk.memory); } @@ -269,7 +265,7 @@ static void ccurl_parse_data(char *result, const char *data) } /* prints result data to text buffer, used by $curl */ -void ccurl_process_info(char *p, int p_max_size, char *uri, int interval) +void ccurl_process_info(char *p, int p_max_size, const std::string &uri, int interval) { ccurl_location_ptr curloc = ccurl_find_location(ccurl_locations, uri); if (!curloc->p_timed_thread) { diff --git a/src/ccurl_thread.h b/src/ccurl_thread.h index 06bb0372..442d6b0c 100644 --- a/src/ccurl_thread.h +++ b/src/ccurl_thread.h @@ -34,9 +34,9 @@ struct ccurl_location_t { ccurl_location_t() : uri(0), last_modified(0), etag(0), result(0) {} /* uri of location */ - char *uri; - char *last_modified; - char *etag; + std::string uri; + std::string last_modified; + std::string etag; /* a pointer to some arbitrary data, will be freed by ccurl_free_info() if * non-null */ char *result; @@ -52,7 +52,7 @@ typedef std::shared_ptr ccurl_location_ptr; typedef std::list ccurl_location_list; /* find an existing location for the uri specified */ -ccurl_location_ptr ccurl_find_location(ccurl_location_list &locations, char *uri); +ccurl_location_ptr ccurl_find_location(ccurl_location_list &locations, const std::string &uri); /* free all locations (as well as location->uri and location->result if * non-null) */ void ccurl_free_locations(ccurl_location_list &locations); @@ -68,7 +68,7 @@ void ccurl_init_thread(const ccurl_location_ptr &curloc, int interval); /* for $curl, free internal list pointer */ void ccurl_free_info(void); /* runs instance of $curl */ -void ccurl_process_info(char *p, int p_max_size, char *uri, int interval); +void ccurl_process_info(char *p, int p_max_size, const std::string &uri, int interval); void curl_parse_arg(struct text_object *, const char *); void curl_print(struct text_object *, char *, int); diff --git a/src/rss.cc b/src/rss.cc index 79b73b2c..e8605846 100644 --- a/src/rss.cc +++ b/src/rss.cc @@ -52,7 +52,7 @@ void rss_free_info(void) ccurl_free_locations(locations_rss); } -static void rss_process_info(char *p, int p_max_size, char *uri, char *action, int +static void rss_process_info(char *p, int p_max_size, const std::string &uri, char *action, int act_par, int interval, unsigned int nrspaces) { PRSS *data; diff --git a/src/specials.cc b/src/specials.cc index afad3668..081d9ded 100644 --- a/src/specials.cc +++ b/src/specials.cc @@ -234,7 +234,7 @@ char *scan_graph(struct text_object *obj, const char *args, double defscale) if (*buf == '"') { char *_ptr; size_t _size; - if (_ptr = const_cast(strrchr(args, '"'))) { + if ((_ptr = const_cast(strrchr(args, '"')))) { _size = _ptr - args - 1; } _size = _size < 1024 ? _size : 1023; diff --git a/src/weather.cc b/src/weather.cc index c56bbc4f..0f55d0dc 100644 --- a/src/weather.cc +++ b/src/weather.cc @@ -709,7 +709,8 @@ void wind_deg_to_dir(char *p, int p_max_size, int wind_deg) { } #ifdef BUILD_WEATHER_XOAP -static void weather_forecast_process_info(char *p, int p_max_size, char *uri, unsigned int day, char *data_type, int interval) +static void weather_forecast_process_info(char *p, int p_max_size, const + std::string &uri, unsigned int day, char *data_type, int interval) { PWEATHER_FORECAST *data; @@ -754,7 +755,7 @@ static void weather_forecast_process_info(char *p, int p_max_size, char *uri, un } #endif /* BUILD_WEATHER_XOAP */ -static void weather_process_info(char *p, int p_max_size, char *uri, char *data_type, int interval) +static void weather_process_info(char *p, int p_max_size, const std::string &uri, char *data_type, int interval) { static const char *wc[] = { "", "drizzle", "rain", "hail", "soft hail",