[Xfce4-commits] <xfce4-weather-plugin:master> Replace timeslice array with GArray.
Harald Judt
noreply at xfce.org
Sun Dec 9 23:44:06 CET 2012
Updating branch refs/heads/master
to b9fcca223ae6922deb7919060d471565e0d69062 (commit)
from 5ffddb58d7a7f2fa4bf110f30f00b92d46481c53 (commit)
commit b9fcca223ae6922deb7919060d471565e0d69062
Author: Harald Judt <h.judt at gmx.at>
Date: Sun Dec 9 19:24:17 2012 +0100
Replace timeslice array with GArray.
This makes inserting, removing and updating much easier, and the GArray
can grow dynamically. The get_timeslice rewrite makes has_timeslice
redundant, which is a small performance improvement.
panel-plugin/weather-data.c | 30 ++++-------------
panel-plugin/weather-debug.c | 22 ++++++++----
panel-plugin/weather-parsers.c | 72 ++++++++++++++++++++-------------------
panel-plugin/weather-parsers.h | 5 +--
4 files changed, 59 insertions(+), 70 deletions(-)
diff --git a/panel-plugin/weather-data.c b/panel-plugin/weather-data.c
index b12d6c5..217c19f 100644
--- a/panel-plugin/weather-data.c
+++ b/panel-plugin/weather-data.c
@@ -54,21 +54,6 @@
comb->location->var = g_strdup(end->location->var);
-static gboolean
-has_timeslice(xml_weather *data,
- const time_t start_t,
- const time_t end_t)
-{
- guint i = 0;
-
- for (i = 0; i < data->num_timeslices; i++)
- if (data->timeslice[i]->start == start_t &&
- data->timeslice[i]->end == end_t)
- return TRUE;
- return FALSE;
-}
-
-
/* convert string to a double value, returning backup value on error */
gdouble
string_to_double(const gchar *str,
@@ -459,12 +444,10 @@ make_combined_timeslice(xml_weather *data,
guint i;
/* find point data at start of interval (may not be available) */
- if (has_timeslice(data, interval->start, interval->start))
- start = get_timeslice(data, interval->start, interval->start);
+ start = get_timeslice(data, interval->start, interval->start);
/* find point interval at end of interval */
- if (has_timeslice(data, interval->end, interval->end))
- end = get_timeslice(data, interval->end, interval->end);
+ end = get_timeslice(data, interval->end, interval->end);
if (start == NULL && end == NULL)
return NULL;
@@ -602,6 +585,7 @@ find_timeslice(xml_weather *data,
const gint prev_hours_limit,
const gint next_hours_limit)
{
+ xml_time *timeslice;
time_t start_t, end_t;
gint hours = 0;
@@ -615,8 +599,8 @@ find_timeslice(xml_weather *data,
start_t = time_calc_hour(start_tm, 0 - hours);
end_t = time_calc_hour(end_tm, 0 - hours);
- if (has_timeslice(data, start_t, end_t))
- return get_timeslice(data, start_t, end_t);
+ if ((timeslice = get_timeslice(data, start_t, end_t)))
+ return timeslice;
}
/* check later hours */
@@ -624,8 +608,8 @@ find_timeslice(xml_weather *data,
start_t = time_calc_hour(start_tm, hours);
end_t = time_calc_hour(end_tm, hours);
- if (has_timeslice(data, start_t, end_t))
- return get_timeslice(data, start_t, end_t);
+ if ((timeslice = get_timeslice(data, start_t, end_t)))
+ return timeslice;
}
hours++;
}
diff --git a/panel-plugin/weather-debug.c b/panel-plugin/weather-debug.c
index f3fc823..a950199 100644
--- a/panel-plugin/weather-debug.c
+++ b/panel-plugin/weather-debug.c
@@ -340,23 +340,29 @@ weather_dump_units_config(const units_config *units)
gchar *
-weather_dump_weatherdata(const xml_weather *weatherdata)
+weather_dump_weatherdata(const xml_weather *wd)
{
GString *out;
+ xml_time *timeslice;
gchar *start, *end, *loc, *result;
gboolean is_interval;
guint i;
out = g_string_sized_new(20480);
g_string_assign(out, "Timeslices (local time): ");
- g_string_append_printf(out, "%d timeslices available (%d max, %d free).\n",
- weatherdata->num_timeslices, MAX_TIMESLICE,
- MAX_TIMESLICE - weatherdata->num_timeslices);
- for (i = 0; i < weatherdata->num_timeslices; i++) {
- start = weather_debug_strftime_t(weatherdata->timeslice[i]->start);
- end = weather_debug_strftime_t(weatherdata->timeslice[i]->end);
+ g_string_append_printf(out, "%d timeslices available.\n",
+ wd->timeslices->len);
+ for (i = 0; i < wd->timeslices->len; i++) {
+ timeslice = g_array_index(wd->timeslices, xml_time*, i);
+ if (timeslice) {
+ start = weather_debug_strftime_t(timeslice->start);
+ end = weather_debug_strftime_t(timeslice->end);
+ } else {
+ start = g_strdup("-");
+ end = g_strdup("-");
+ }
is_interval = (gboolean) strcmp(start, end);
- loc = weather_dump_location(weatherdata->timeslice[i]->location,
+ loc = weather_dump_location((timeslice) ? timeslice->location : NULL,
is_interval);
g_string_append_printf(out, " #%3d: [%s %s %s] %s\n",
i + 1, start, is_interval ? "-" : "=",
diff --git a/panel-plugin/weather-parsers.c b/panel-plugin/weather-parsers.c
index e9c1bf9..04627f9 100644
--- a/panel-plugin/weather-parsers.c
+++ b/panel-plugin/weather-parsers.c
@@ -28,13 +28,12 @@
#define _XOPEN_SOURCE
#define _XOPEN_SOURCE_EXTENDED 1
#include "weather-parsers.h"
-#include <libxfce4panel/libxfce4panel.h>
+#include "weather-debug.h"
+
#include <time.h>
#include <stdlib.h>
#include <string.h>
-#include "weather-debug.h"
-
#define DATA(node) \
((gchar *) xmlNodeListGetString(node->doc, node->children, 1))
@@ -74,22 +73,16 @@ get_timeslice(xml_weather *data,
const time_t start_t,
const time_t end_t)
{
+ xml_time *timeslice;
guint i;
- for (i = 0; i < data->num_timeslices; i++) {
- if (data->timeslice[i]->start == start_t &&
- data->timeslice[i]->end == end_t)
- return data->timeslice[i];
+ for (i = 0; i < data->timeslices->len; i++) {
+ timeslice = g_array_index(data->timeslices, xml_time*, i);
+ if (timeslice &&
+ timeslice->start == start_t && timeslice->end == end_t)
+ return timeslice;
}
- if (data->num_timeslices == MAX_TIMESLICE - 1)
- return NULL;
-
- data->timeslice[data->num_timeslices] = g_slice_new0(xml_time);
- data->timeslice[data->num_timeslices]->start = start_t;
- data->timeslice[data->num_timeslices]->end = end_t;
- data->num_timeslices++;
-
- return data->timeslice[data->num_timeslices - 1];
+ return NULL;
}
@@ -229,13 +222,17 @@ parse_time(xmlNode *cur_node,
if (G_UNLIKELY(!start_t || !end_t))
return;
+ /* look for existing timeslice or add a new one */
timeslice = get_timeslice(data, start_t, end_t);
-
- if (G_UNLIKELY(!timeslice)) {
- g_warning("No timeslice found or created. "
- "Perhaps maximum of %d slices reached?", MAX_TIMESLICE);
- return;
+ if (! timeslice) {
+ timeslice = g_slice_new0(xml_time);
+ if (G_UNLIKELY(!timeslice))
+ return;
+ timeslice->start = start_t;
+ timeslice->end = end_t;
+ g_array_append_val(data->timeslices, timeslice);
}
+
for (child_node = cur_node->children; child_node;
child_node = child_node->next)
if (G_LIKELY(NODE_IS_TYPE(child_node, "location"))) {
@@ -249,17 +246,18 @@ parse_time(xmlNode *cur_node,
xml_weather *
parse_weather(xmlNode *cur_node)
{
- xml_weather *ret;
+ xml_weather *wd;
xmlNode *child_node;
if (G_UNLIKELY(!NODE_IS_TYPE(cur_node, "weatherdata"))) {
return NULL;
}
- if ((ret = g_slice_new0(xml_weather)) == NULL)
+ if ((wd = g_slice_new0(xml_weather)) == NULL)
return NULL;
- ret->num_timeslices = 0;
+ wd->timeslices = g_array_sized_new(FALSE, TRUE, sizeof(xml_time *), 200);
+
for (cur_node = cur_node->children; cur_node; cur_node = cur_node->next) {
if (cur_node->type != XML_ELEMENT_NODE)
continue;
@@ -274,10 +272,10 @@ parse_weather(xmlNode *cur_node)
for (child_node = cur_node->children; child_node;
child_node = child_node->next)
if (NODE_IS_TYPE(child_node, "time"))
- parse_time(child_node, ret);
+ parse_time(child_node, wd);
}
}
- return ret;
+ return wd;
}
@@ -574,21 +572,25 @@ xml_time_free(xml_time *timeslice)
void
-xml_weather_free(xml_weather *data)
+xml_weather_free(xml_weather *wd)
{
+ xml_time *timeslice;
guint i;
- g_assert(data != NULL);
- if (G_UNLIKELY(data == NULL))
+ g_assert(wd != NULL);
+ if (G_UNLIKELY(wd == NULL))
return;
- weather_debug("Freeing %u timeslices.", data->num_timeslices);
- for (i = 0; i < data->num_timeslices; i++)
- xml_time_free(data->timeslice[i]);
- if (G_LIKELY(data->current_conditions)) {
+ weather_debug("Freeing %u timeslices.", wd->timeslices->len);
+ for (i = 0; i < wd->timeslices->len; i++) {
+ timeslice = g_array_index(wd->timeslices, xml_time*, i);
+ xml_time_free(timeslice);
+ }
+ g_array_free(wd->timeslices, FALSE);
+ if (G_LIKELY(wd->current_conditions)) {
weather_debug("Freeing current conditions.");
- xml_time_free(data->current_conditions);
+ xml_time_free(wd->current_conditions);
}
- g_slice_free(xml_weather, data);
+ g_slice_free(xml_weather, wd);
}
diff --git a/panel-plugin/weather-parsers.h b/panel-plugin/weather-parsers.h
index 86166ea..c832f36 100644
--- a/panel-plugin/weather-parsers.h
+++ b/panel-plugin/weather-parsers.h
@@ -26,8 +26,6 @@
G_BEGIN_DECLS
-#define MAX_TIMESLICE 500
-
enum {
CLOUDS_PERC_LOW = 0,
CLOUDS_PERC_MED,
@@ -75,8 +73,7 @@ typedef struct {
} xml_time;
typedef struct {
- xml_time *timeslice[MAX_TIMESLICE];
- guint num_timeslices;
+ GArray *timeslices;
xml_time *current_conditions;
} xml_weather;
More information about the Xfce4-commits
mailing list