1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-01-28 01:28:30 +00:00

C++ify some curl stuff.

This commit is contained in:
Brenden Matthews 2010-10-14 17:28:57 -07:00
parent 93c6baebb6
commit ca9acc13c0
5 changed files with 28 additions and 31 deletions

View File

@ -54,18 +54,17 @@ typedef struct _ccurl_headers_t {
} ccurl_headers_t; } ccurl_headers_t;
/* finds a location based on uri in the list provided */ /* 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(); for (ccurl_location_list::iterator i = locations.begin();
i != locations.end(); i++) { i != locations.end(); i++) {
if ((*i)->uri && if ((*i)->uri == std::string(uri)) {
strcmp((*i)->uri, uri) == EQUAL) {
return *i; return *i;
} }
} }
ccurl_location_ptr next = ccurl_location_ptr(new ccurl_location_t); ccurl_location_ptr next = ccurl_location_ptr(new ccurl_location_t);
DBGP("new curl location: '%s'", uri); DBGP("new curl location: '%s'", uri.c_str());
next->uri = strndup(uri, text_buffer_size); next->uri = std::string(uri, text_buffer_size);
locations.push_back(next); locations.push_back(next);
return next; return next;
} }
@ -75,9 +74,6 @@ void ccurl_free_locations(ccurl_location_list &locations)
{ {
for (ccurl_location_list::iterator i = locations.begin(); for (ccurl_location_list::iterator i = locations.begin();
i != locations.end(); i++) { 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); free_and_zero((*i)->result);
(*i)->p_timed_thread.reset(); (*i)->p_timed_thread.reset();
} }
@ -130,7 +126,7 @@ void ccurl_fetch_data(thread_handle &handle, const ccurl_location_ptr &curloc)
CURLcode res; CURLcode res;
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
// curl temps /* curl temps */
ccurl_memory_t chunk; ccurl_memory_t chunk;
ccurl_headers_t response_headers; 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(); curl = curl_easy_init();
if (curl) { if (curl) {
DBGP("reading curl data from '%s'", curloc->uri); DBGP("reading curl data from '%s'", curloc->uri.c_str());
curl_easy_setopt(curl, CURLOPT_URL, curloc->uri); curl_easy_setopt(curl, CURLOPT_URL, curloc->uri.c_str());
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, ccurl_parse_header_callback); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, ccurl_parse_header_callback);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *) &response_headers); 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_LIMIT, 1000);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60);
if (curloc->last_modified) { if (!curloc->last_modified.empty()) {
const char *header = "If-Modified-Since: "; 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); 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); headers = curl_slist_append(headers, str);
free(str); free(str);
} }
if (curloc->etag) { if (!curloc->etag.empty()) {
const char *header = "If-None-Match: "; 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); 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); headers = curl_slist_append(headers, str);
free(str); free(str);
} }
@ -182,14 +178,14 @@ void ccurl_fetch_data(thread_handle &handle, const ccurl_location_ptr &curloc)
case 200: case 200:
{ {
std::lock_guard<std::mutex> lock(handle.mutex()); std::lock_guard<std::mutex> lock(handle.mutex());
free_and_zero(curloc->last_modified); curloc->last_modified.clear();
free_and_zero(curloc->etag); curloc->etag.clear();
if (response_headers.last_modified) { if (response_headers.last_modified) {
curloc->last_modified = curloc->last_modified =
strdup(response_headers.last_modified); std::string(response_headers.last_modified);
} }
if (response_headers.etag) { if (response_headers.etag) {
curloc->etag = strdup(response_headers.etag); curloc->etag = std::string(response_headers.etag);
} }
curloc->process_function(curloc->result, chunk.memory); 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 */ /* 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); ccurl_location_ptr curloc = ccurl_find_location(ccurl_locations, uri);
if (!curloc->p_timed_thread) { if (!curloc->p_timed_thread) {

View File

@ -34,9 +34,9 @@
struct ccurl_location_t { struct ccurl_location_t {
ccurl_location_t() : uri(0), last_modified(0), etag(0), result(0) {} ccurl_location_t() : uri(0), last_modified(0), etag(0), result(0) {}
/* uri of location */ /* uri of location */
char *uri; std::string uri;
char *last_modified; std::string last_modified;
char *etag; std::string etag;
/* a pointer to some arbitrary data, will be freed by ccurl_free_info() if /* a pointer to some arbitrary data, will be freed by ccurl_free_info() if
* non-null */ * non-null */
char *result; char *result;
@ -52,7 +52,7 @@ typedef std::shared_ptr<ccurl_location_t> ccurl_location_ptr;
typedef std::list<ccurl_location_ptr> ccurl_location_list; typedef std::list<ccurl_location_ptr> ccurl_location_list;
/* find an existing location for the uri specified */ /* 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 /* free all locations (as well as location->uri and location->result if
* non-null) */ * non-null) */
void ccurl_free_locations(ccurl_location_list &locations); 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 */ /* for $curl, free internal list pointer */
void ccurl_free_info(void); void ccurl_free_info(void);
/* runs instance of $curl */ /* 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_parse_arg(struct text_object *, const char *);
void curl_print(struct text_object *, char *, int); void curl_print(struct text_object *, char *, int);

View File

@ -52,7 +52,7 @@ void rss_free_info(void)
ccurl_free_locations(locations_rss); 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) act_par, int interval, unsigned int nrspaces)
{ {
PRSS *data; PRSS *data;

View File

@ -234,7 +234,7 @@ char *scan_graph(struct text_object *obj, const char *args, double defscale)
if (*buf == '"') { if (*buf == '"') {
char *_ptr; char *_ptr;
size_t _size; size_t _size;
if (_ptr = const_cast<char*>(strrchr(args, '"'))) { if ((_ptr = const_cast<char*>(strrchr(args, '"')))) {
_size = _ptr - args - 1; _size = _ptr - args - 1;
} }
_size = _size < 1024 ? _size : 1023; _size = _size < 1024 ? _size : 1023;

View File

@ -709,7 +709,8 @@ void wind_deg_to_dir(char *p, int p_max_size, int wind_deg) {
} }
#ifdef BUILD_WEATHER_XOAP #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; 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 */ #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[] = { static const char *wc[] = {
"", "drizzle", "rain", "hail", "soft hail", "", "drizzle", "rain", "hail", "soft hail",