[Xfce4-commits] <xfce4-weather-plugin:master> Cleanup weather-parsers.
Harald Judt
noreply at xfce.org
Tue Jul 31 18:14:03 CEST 2012
Updating branch refs/heads/master
to 0e07ddaa53bbde785039dab22c055ae28feb2f76 (commit)
from 92a743a80ca7c7b0e543f42ffaf402a703543730 (commit)
commit 0e07ddaa53bbde785039dab22c055ae28feb2f76
Author: Harald Judt <h.judt at gmx.at>
Date: Tue Jul 31 18:11:38 2012 +0200
Cleanup weather-parsers.
Make parse_time use parse_xml_timestring function, cleanup and
add optimization hints.
panel-plugin/weather-parsers.c | 59 +++++++++++++++------------------------
panel-plugin/weather-parsers.h | 2 +
2 files changed, 25 insertions(+), 36 deletions(-)
diff --git a/panel-plugin/weather-parsers.c b/panel-plugin/weather-parsers.c
index 2b42f5e..c94ad96 100644
--- a/panel-plugin/weather-parsers.c
+++ b/panel-plugin/weather-parsers.c
@@ -62,7 +62,7 @@ parse_weather(xmlNode *cur_node)
xml_weather *ret;
xmlNode *child_node;
- if (!NODE_IS_TYPE(cur_node, "weatherdata")) {
+ if (G_UNLIKELY(!NODE_IS_TYPE(cur_node, "weatherdata"))) {
return NULL;
}
@@ -97,17 +97,19 @@ parse_xml_timestring(gchar *ts, gchar *format) {
struct tm tm;
memset(&t, 0, sizeof(time_t));
- if (ts == NULL)
+ if (G_UNLIKELY(ts == NULL))
return t;
/* standard format */
if (format == NULL)
format = "%Y-%m-%dT%H:%M:%SZ";
+ /* strptime needs an initialized struct, or unpredictable
+ * behaviour might occur */
memset(&tm, 0, sizeof(struct tm));
tm.tm_isdst = -1;
- if (strptime(ts, format, &tm) == NULL)
+ if (G_UNLIKELY(strptime(ts, format, &tm) == NULL))
return t;
t = my_timegm(&tm);
@@ -195,8 +197,12 @@ xml_astro *
parse_astro(xmlNode *cur_node)
{
xmlNode *child_node, *time_node = NULL, *location_node = NULL;
- xml_astro *astro = g_slice_new0(xml_astro);
+ xml_astro *astro;
+
+ if (cur_node == NULL || !NODE_IS_TYPE(cur_node, "astrodata"))
+ return NULL;
+ astro = g_slice_new0(xml_astro);
if (G_UNLIKELY(astro == NULL))
return NULL;
@@ -220,57 +226,38 @@ void
parse_time(xmlNode *cur_node,
xml_weather *data)
{
- gchar *datatype = xmlGetProp(cur_node, (const xmlChar *) "datatype");
- gchar *start = xmlGetProp(cur_node, (const xmlChar *) "from");
- gchar *end = xmlGetProp(cur_node, (const xmlChar *) "to");
- struct tm start_tm, end_tm;
+ gchar *datatype, *from, *to;
time_t start_t, end_t;
xml_time *timeslice;
xmlNode *child_node;
- /* strptime needs an initialized struct, or unpredictable
- * behaviour might occur */
- memset(&start_tm, 0, sizeof(struct tm));
- memset(&end_tm, 0, sizeof(struct tm));
- start_tm.tm_isdst = -1;
- end_tm.tm_isdst = -1;
-
+ datatype = PROP(cur_node, "datatype");
if (xmlStrcasecmp(datatype, "forecast")) {
xmlFree(datatype);
- xmlFree(start);
- xmlFree(end);
return;
}
-
xmlFree(datatype);
- if (strptime(start, "%Y-%m-%dT%H:%M:%SZ", &start_tm) == NULL) {
- xmlFree(start);
- xmlFree(end);
- return;
- }
+ from = PROP(cur_node, "from");
+ start_t = parse_xml_timestring(from, NULL);
+ xmlFree(from);
- if (strptime(end, "%Y-%m-%dT%H:%M:%SZ", &end_tm) == NULL) {
- xmlFree(start);
- xmlFree(end);
- return;
- }
-
- xmlFree(start);
- xmlFree(end);
+ to = PROP(cur_node, "to");
+ end_t = parse_xml_timestring(to, NULL);
+ xmlFree(from);
- start_t = my_timegm(&start_tm);
- end_t = my_timegm(&end_tm);
+ if (G_UNLIKELY(!start_t || !end_t))
+ return;
timeslice = get_timeslice(data, start_t, end_t);
- if (!timeslice) {
+ if (G_UNLIKELY(!timeslice)) {
g_warning("no timeslice");
return;
}
for (child_node = cur_node->children; child_node;
child_node = child_node->next)
- if (NODE_IS_TYPE(child_node, "location")) {
+ if (G_LIKELY(NODE_IS_TYPE(child_node, "location"))) {
if (timeslice->location == NULL)
timeslice->location = g_slice_new0(xml_location);
parse_location(child_node, timeslice->location);
@@ -290,7 +277,7 @@ get_timeslice(xml_weather *data,
data->timeslice[i]->end == end_t)
return data->timeslice[i];
}
- if (data->num_timeslices == MAX_TIMESLICE -1)
+ if (data->num_timeslices == MAX_TIMESLICE - 1)
return NULL;
data->timeslice[data->num_timeslices] = g_slice_new0(xml_time);
diff --git a/panel-plugin/weather-parsers.h b/panel-plugin/weather-parsers.h
index 2e50a7a..8ec3873 100644
--- a/panel-plugin/weather-parsers.h
+++ b/panel-plugin/weather-parsers.h
@@ -26,8 +26,10 @@ G_BEGIN_DECLS
#define DATA(node) \
((gchar *) xmlNodeListGetString(node->doc, node->children, 1))
+
#define PROP(node, prop) \
((gchar *) xmlGetProp((node), (const xmlChar *) (prop)))
+
#define NODE_IS_TYPE(node, type) \
(xmlStrEqual(node->name, (const xmlChar *) type))
More information about the Xfce4-commits
mailing list