From dc32c58499caa8b1ec4e975afad75ca0d862e990 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sat, 13 Sep 2014 20:37:57 +0200 Subject: Introduce properties table This is a new internal properties storage for attributes that can be defined both in the item and album scope, with the item scope taking priority. The big advantage is a better distinguishment from an undefined, default value. It also makes easier to retrieve attributes with properly defined scope priorities. --- src/xml-parser.c | 74 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 16 deletions(-) (limited to 'src/xml-parser.c') diff --git a/src/xml-parser.c b/src/xml-parser.c index 9f95cf3..a13e502 100644 --- a/src/xml-parser.c +++ b/src/xml-parser.c @@ -158,13 +158,13 @@ xml_file_get_node_value_with_default (TXMLFile *file, const gchar *x_path, const gchar *s; s = xml_file_get_node_value (file, x_path); - return s ? s : g_strdup (_default); } /* * xml_file_get_node_attribute: retrieve attribute value from XPath node + * - returns NULL if node is not present */ gchar * xml_file_get_node_attribute (TXMLFile *file, const gchar *x_path, const gchar *attr) @@ -198,35 +198,67 @@ xml_file_get_node_attribute (TXMLFile *file, const gchar *x_path, const gchar *a return attrv; } -long int -xml_file_get_node_attribute_long (TXMLFile *file, const gchar *x_path, const gchar *attr, const int _default) +gchar * +xml_file_get_node_attribute_with_default (TXMLFile *file, const gchar *x_path, const gchar *attr, const gchar *_default) +{ + gchar *s = xml_file_get_node_attribute (file, x_path, attr); + return s != NULL ? s : g_strdup (_default); +} + +/* + * xml_file_get_node_attribute_*: retrieve attribute value from XPath node, storing it in *value, returning TRUE if value has been set. + */ +gboolean +xml_file_get_node_attribute_long (TXMLFile *file, const gchar *x_path, const gchar *attr, long int *value) { gchar *s; long int i; + char *endptr = NULL; s = xml_file_get_node_attribute (file, x_path, attr); if (s == NULL) - return _default; + return FALSE; - i = atol (s); + i = strtol (s, &endptr, 10); + if (endptr != NULL && *endptr != '\0') { + g_free (s); + return FALSE; + } g_free (s); - return i; + *value = i; + return TRUE; +} + +long int +xml_file_get_node_attribute_long_with_default (TXMLFile *file, const gchar *x_path, const gchar *attr, const long int _default) +{ + long int i; + return xml_file_get_node_attribute_long (file, x_path, attr, &i) ? i : _default; } gboolean -xml_file_get_node_attribute_boolean (TXMLFile *file, const gchar *x_path, const gchar *attr, const gboolean _default) +xml_file_get_node_attribute_boolean (TXMLFile *file, const gchar *x_path, const gchar *attr, gboolean *value) { gchar *s; - gboolean b; s = xml_file_get_node_attribute (file, x_path, attr); - b = s ? (strcasecmp (s, "yes") == 0 || strcasecmp (s, "true") == 0) : _default; + if (s == NULL) + return FALSE; + + *value = (strcasecmp (s, "yes") == 0 || strcasecmp (s, "true") == 0); g_free (s); - return b; + return TRUE; } -double -xml_file_get_node_attribute_double (TXMLFile *file, const gchar *x_path, const gchar *attr, const double _default) +gboolean +xml_file_get_node_attribute_boolean_with_default (TXMLFile *file, const gchar *x_path, const gchar *attr, const gboolean _default) +{ + gboolean b; + return xml_file_get_node_attribute_boolean (file, x_path, attr, &b) ? b : _default; +} + +gboolean +xml_file_get_node_attribute_double (TXMLFile *file, const gchar *x_path, const gchar *attr, double *value) { gchar *s; double d; @@ -234,13 +266,23 @@ xml_file_get_node_attribute_double (TXMLFile *file, const gchar *x_path, const g s = xml_file_get_node_attribute (file, x_path, attr); if (s == NULL) - return _default; + return FALSE; d = strtod (s, &endptr); - if (endptr != NULL && *endptr != '\0') - d = _default; + if (endptr != NULL && *endptr != '\0') { + g_free (s); + return FALSE; + } g_free (s); - return d; + *value = d; + return TRUE; +} + +double +xml_file_get_node_attribute_double_with_default (TXMLFile *file, const gchar *x_path, const gchar *attr, const double _default) +{ + double d; + return xml_file_get_node_attribute_double (file, x_path, attr, &d) ? d : _default; } -- cgit v1.2.3