summaryrefslogtreecommitdiff
path: root/src/properties-table.c
diff options
context:
space:
mode:
authorTomas Bzatek <tbzatek@users.sourceforge.net>2014-09-13 20:37:57 +0200
committerTomas Bzatek <tbzatek@users.sourceforge.net>2014-09-13 20:37:57 +0200
commitdc32c58499caa8b1ec4e975afad75ca0d862e990 (patch)
tree91322afabcb457406ab512d20163f0dd73ce2c5a /src/properties-table.c
parent302127b03d44277c829642468561ac9e518b7a21 (diff)
downloadcataract-dc32c58499caa8b1ec4e975afad75ca0d862e990.tar.xz
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.
Diffstat (limited to 'src/properties-table.c')
-rw-r--r--src/properties-table.c227
1 files changed, 227 insertions, 0 deletions
diff --git a/src/properties-table.c b/src/properties-table.c
new file mode 100644
index 0000000..19d7a36
--- /dev/null
+++ b/src/properties-table.c
@@ -0,0 +1,227 @@
+/* Cataract - Static web photo gallery generator
+ * Copyright (C) 2014 Tomas Bzatek <tbzatek@users.sourceforge.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+#include <string.h>
+
+#include <glib.h>
+
+#include "properties-table.h"
+
+
+typedef enum {
+ PROP_DATA_TYPE_STRING,
+ PROP_DATA_TYPE_INT,
+ PROP_DATA_TYPE_BOOL,
+ PROP_DATA_TYPE_DOUBLE
+} PropertyDataType;
+
+typedef struct {
+ PropertyDataType type;
+ union {
+ gchar *s;
+ long int i;
+ gboolean b;
+ double d;
+ } data;
+} PropertyData;
+
+
+static void
+free_property_data (PropertyData *data)
+{
+ if (data == NULL)
+ return;
+
+ switch (data->type) {
+ case PROP_DATA_TYPE_STRING:
+ g_free (data->data.s);
+ break;
+ default:
+ break;
+ }
+ g_free (data);
+}
+
+
+/*
+ * properties_table_new: creates new property table
+ */
+PropertiesTable *
+properties_table_new ()
+{
+ return g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) free_property_data);
+}
+
+/*
+ * properties_table_dup: creates deep copy of the table
+ */
+PropertiesTable *
+properties_table_dup (PropertiesTable *table)
+{
+ PropertiesTable *t;
+ GHashTableIter iter;
+ gpointer key, value;
+ PropertyData *data;
+
+ g_return_val_if_fail (table != NULL, NULL);
+
+ t = properties_table_new ();
+ g_hash_table_iter_init (&iter, table);
+ while (g_hash_table_iter_next (&iter, &key, &value)) {
+ data = (PropertyData *) value;
+ switch (data->type) {
+ case PROP_DATA_TYPE_STRING:
+ properties_table_add_string (t, GPOINTER_TO_INT (key), data->data.s);
+ break;
+ case PROP_DATA_TYPE_INT:
+ properties_table_add_int (t, GPOINTER_TO_INT (key), data->data.i);
+ break;
+ case PROP_DATA_TYPE_BOOL:
+ properties_table_add_bool (t, GPOINTER_TO_INT (key), data->data.b);
+ break;
+ case PROP_DATA_TYPE_DOUBLE:
+ properties_table_add_double (t, GPOINTER_TO_INT (key), data->data.d);
+ break;
+ }
+ }
+
+ return t;
+}
+
+/*
+ * properties_table_free: destroys properties table and frees all data
+ */
+void
+properties_table_free (PropertiesTable *table)
+{
+ g_assert (table != NULL);
+ g_hash_table_destroy (table);
+}
+
+void
+properties_table_add_string (PropertiesTable *table, int property_id, const gchar *value)
+{
+ PropertyData *data;
+
+ g_assert (table != NULL);
+
+ data = g_malloc0 (sizeof (PropertyData));
+ data->type = PROP_DATA_TYPE_STRING;
+ data->data.s = g_strdup (value);
+ g_hash_table_replace (table, GINT_TO_POINTER (property_id), data);
+}
+
+void
+properties_table_add_int (PropertiesTable *table, int property_id, long int value)
+{
+ PropertyData *data;
+
+ g_assert (table != NULL);
+
+ data = g_malloc0 (sizeof (PropertyData));
+ data->type = PROP_DATA_TYPE_INT;
+ data->data.i = value;
+ g_hash_table_replace (table, GINT_TO_POINTER (property_id), data);
+}
+
+void
+properties_table_add_bool (PropertiesTable *table, int property_id, gboolean value)
+{
+ PropertyData *data;
+
+ g_assert (table != NULL);
+
+ data = g_malloc0 (sizeof (PropertyData));
+ data->type = PROP_DATA_TYPE_BOOL;
+ data->data.b = value;
+ g_hash_table_replace (table, GINT_TO_POINTER (property_id), data);
+}
+
+void
+properties_table_add_double (PropertiesTable *table, int property_id, double value)
+{
+ PropertyData *data;
+
+ g_assert (table != NULL);
+
+ data = g_malloc0 (sizeof (PropertyData));
+ data->type = PROP_DATA_TYPE_DOUBLE;
+ data->data.d = value;
+ g_hash_table_replace (table, GINT_TO_POINTER (property_id), data);
+}
+
+
+/*
+ * properties_table_get_*: gets data for the specified id
+ */
+const gchar *
+properties_table_get_string (PropertiesTable *table, int property_id)
+{
+ PropertyData *data;
+
+ g_assert (table != NULL);
+
+ data = g_hash_table_lookup (table, GINT_TO_POINTER (property_id));
+ if (data && data->type == PROP_DATA_TYPE_STRING)
+ return data->data.s;
+ return NULL;
+}
+
+gboolean
+properties_table_get_int (PropertiesTable *table, int property_id, long int *value)
+{
+ PropertyData *data;
+
+ g_assert (table != NULL);
+
+ data = g_hash_table_lookup (table, GINT_TO_POINTER (property_id));
+ if (data && data->type == PROP_DATA_TYPE_INT) {
+ *value = data->data.i;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+gboolean
+properties_table_get_bool (PropertiesTable *table, int property_id, gboolean *value)
+{
+ PropertyData *data;
+
+ g_assert (table != NULL);
+
+ data = g_hash_table_lookup (table, GINT_TO_POINTER (property_id));
+ if (data && data->type == PROP_DATA_TYPE_BOOL) {
+ *value = data->data.b;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+gboolean
+properties_table_get_double (PropertiesTable *table, int property_id, double *value)
+{
+ PropertyData *data;
+
+ g_assert (table != NULL);
+
+ data = g_hash_table_lookup (table, GINT_TO_POINTER (property_id));
+ if (data && data->type == PROP_DATA_TYPE_DOUBLE) {
+ *value = data->data.d;
+ return TRUE;
+ }
+ return FALSE;
+}