diff options
author | Erich Eckner <git@eckner.net> | 2018-11-02 12:33:19 +0100 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2018-11-02 12:33:19 +0100 |
commit | 33792df4c9cfbf7c6336b88da88ead3a7a78af7e (patch) | |
tree | 2f0612005ddf0f2378d188cc8d5f7ee8c9c516a5 | |
parent | 56fda64a831adf6418f55bd46a3c81c20684da60 (diff) | |
download | anzeige-33792df4c9cfbf7c6336b88da88ead3a7a78af7e.tar.xz |
input_gadgets.c: fix some memory leaks
-rw-r--r-- | input_gadgets.c | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/input_gadgets.c b/input_gadgets.c index 613727f..2e79681 100644 --- a/input_gadgets.c +++ b/input_gadgets.c @@ -212,6 +212,22 @@ xmlChar *xml_extract_string(xmlDoc *doc, xmlNode *cur_node, char *name) return xmlNodeListGetString(doc, sub_node -> xmlChildrenNode, 0); } +int xml_strcmp_property_value(xmlNode *node, char *key, char *check_value) +{ + xmlChar *is_value = xmlGetProp(node,(xmlChar *)key); + int res = strcmp((char *)is_value, check_value); + xmlFree(is_value); + return res; +} + +double xml_extract_float(xmlNode *node, char *key) +{ + xmlChar *value = xmlGetProp(node, (xmlChar *)key); + double res = atof((char *)value); + xmlFree(value); + return res; +} + char *gadgets_retrieve_weather_forecast(char *output, int max_len) { CURL *curl_handle; @@ -258,16 +274,19 @@ char *gadgets_retrieve_weather_forecast(char *output, int max_len) cur_node = xmlDocGetRootElement(doc); if (!cur_node) { fprintf(stderr, "Failed to reach root node of xml\n"); + xmlFreeDoc(doc); return NULL; } cur_node = xml_find_node_by_name(cur_node, "weatherdata"); if (!cur_node) { fprintf(stderr, "Failed to enter <weatherdata>\n"); + xmlFreeDoc(doc); return NULL; } cur_node = xml_find_node_by_name(cur_node -> children, "product"); if (!cur_node) { fprintf(stderr, "Failed to enter <product>\n"); + xmlFreeDoc(doc); return NULL; } @@ -279,38 +298,38 @@ char *gadgets_retrieve_weather_forecast(char *output, int max_len) now = ((int)(now/60/60))*60*60; for (cur_node = cur_node -> children; cur_node; cur_node = cur_node->next) { - if ((cur_node->type == XML_ELEMENT_NODE) && (strcmp((char *)cur_node->name,"time")==0) && (strcmp((char *)xmlGetProp(cur_node,(xmlChar *)"datatype"),"forecast")==0)) { + if ((cur_node->type == XML_ELEMENT_NODE) && (strcmp((char *)cur_node->name,"time")==0) && (xml_strcmp_property_value(cur_node, "datatype", "forecast")==0)) { for (int i=0; i<=14; i++) { now += i*60*60; strftime(time_str[0],21,"%Y-%m-%dT%H:%M:%SZ",gmtime(&now)); now += 60*60; strftime(time_str[1],21,"%Y-%m-%dT%H:%M:%SZ",gmtime(&now)); now -= (i+1)*60*60; - if ((strcmp(time_str[1],(char *)xmlGetProp(cur_node,(xmlChar *)"from"))==0) && (strcmp(time_str[1],(char *)xmlGetProp(cur_node,(xmlChar *)"to"))==0)) { + if ((xml_strcmp_property_value(cur_node, "from", time_str[1])==0) && (xml_strcmp_property_value(cur_node, "to", time_str[1])==0)) { sub_node = xml_find_node_by_name(cur_node -> children,"location"); - cur_val = atof((char *)xmlGetProp(xml_find_node_by_name(sub_node -> children, "temperature"), (xmlChar *)"value")); + cur_val = xml_extract_float(xml_find_node_by_name(sub_node -> children, "temperature"), "value"); if (isnan(temp_max) || (cur_val > temp_max)) temp_max = cur_val; if (isnan(temp_min) || (cur_val < temp_min)) temp_min = cur_val; - cur_val = atof((char *)xmlGetProp(xml_find_node_by_name(sub_node -> children, "windSpeed"), (xmlChar *)"mps")); + cur_val = xml_extract_float(xml_find_node_by_name(sub_node -> children, "windSpeed"), "mps"); if (isnan(wind_max) || (cur_val > wind_max)) wind_max = cur_val; } - if ((strcmp(time_str[0],(char *)xmlGetProp(cur_node,(xmlChar *)"from"))==0) && (strcmp(time_str[1],(char *)xmlGetProp(cur_node,(xmlChar *)"to"))==0)) { + if ((xml_strcmp_property_value(cur_node, "from", time_str[0])==0) && (xml_strcmp_property_value(cur_node, "to", time_str[1])==0)) { sub_node = xml_find_node_by_name(cur_node -> children,"location"); if (!sub_node) continue; sub_node = xml_find_node_by_name(sub_node -> children, "precipitation"); if (!sub_node) continue; - rain_total += atof((char *)xmlGetProp(sub_node,(xmlChar *)"value")); + rain_total += xml_extract_float(sub_node, "value"); } } } } + xmlFreeDoc(doc); - xmlCleanupParser(); if (max_len > 0) { int i = snprintf(output, max_len, "%0.1f ,, %0.1f °C; %0.1f mm; %0.1f m/s", temp_min, temp_max, rain_total, wind_max); |