diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2011-06-04 18:18:32 +0200 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2011-06-04 18:18:32 +0200 |
| commit | 02fcb53e3a986a85fa8c0248a306132c8996acd8 (patch) | |
| tree | 2d0b71e8fa241a972b7a049620f5aa8d92431d39 /src/replace-table.c | |
| parent | fb5a52884dd17d17bab47de7f53c17c05ced6e1b (diff) | |
| download | cataract-02fcb53e3a986a85fa8c0248a306132c8996acd8.tar.xz | |
replace-table: Link to defines table instead of copying on creation
This allows actual defines to be used without explicit update.
Also brings the benefit of placing empty string when define is not defined.
Diffstat (limited to 'src/replace-table.c')
| -rw-r--r-- | src/replace-table.c | 89 |
1 files changed, 65 insertions, 24 deletions
diff --git a/src/replace-table.c b/src/replace-table.c index 9cd02ea..344a782 100644 --- a/src/replace-table.c +++ b/src/replace-table.c @@ -23,36 +23,25 @@ #include <unistd.h> #include <glib.h> -#include <glib/gprintf.h> #include "replace-table.h" #include "gallery-utils.h" +struct ReplaceTable { + GHashTable *table; + GHashTable *defines; +}; + ReplaceTable * replace_table_new () { - return (ReplaceTable *) g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); -} + ReplaceTable *table; -/* - * replace_table_new_from_defines: creates new replace table object and fills it with defines from the hash table - * - */ -ReplaceTable * -replace_table_new_from_defines (GHashTable *defines) -{ - GHashTable *table; - GHashTableIter iter; - gchar *key, *value; - - table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - - g_hash_table_iter_init (&iter, defines); - while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value)) - g_hash_table_insert (table, g_strdup_printf ("value(%s)", key), g_strdup (value)); + table = g_new0 (ReplaceTable, 1); + table->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); return table; } @@ -62,7 +51,8 @@ replace_table_free (ReplaceTable *table) { g_return_if_fail (table != NULL); - g_hash_table_destroy (table); + g_hash_table_destroy (table->table); + g_free (table); } @@ -78,7 +68,7 @@ replace_table_add_key (ReplaceTable *table, const gchar *tag, const gchar *value g_return_if_fail (table != NULL); if (tag != NULL && value != NULL) - g_hash_table_replace (table, g_strdup (tag), g_strdup (value)); + g_hash_table_replace (table->table, g_strdup (tag), g_strdup (value)); } void @@ -87,7 +77,7 @@ replace_table_add_key_int (ReplaceTable *table, const gchar *tag, gint value) g_return_if_fail (table != NULL); if (tag != NULL) - g_hash_table_replace (table, g_strdup (tag), g_strdup_printf ("%d", value)); + g_hash_table_replace (table->table, g_strdup (tag), g_strdup_printf ("%d", value)); } void @@ -101,11 +91,22 @@ replace_table_add_key_printf (ReplaceTable *table, const gchar *tag, const gchar if (tag != NULL && format != NULL) { va_start (args, format); g_vasprintf (&s, format, args); - g_hash_table_replace (table, g_strdup (tag), s); + g_hash_table_replace (table->table, g_strdup (tag), s); va_end (args); } } +/* + * replace_table_set_defines: set a hash table to be used for defines lookup + * + */ +void +replace_table_set_defines (ReplaceTable *table, GHashTable *defines) +{ + g_return_if_fail (table != NULL); + + table->defines = defines; +} /* * replace_table_process: process buffer and replace all tags filled in the replace table @@ -120,6 +121,7 @@ replace_table_process (gchar **buffer, ReplaceTable *table) gchar *start, *end; gchar *b; gchar *replace_value; + gchar *s; GString *dst; gboolean tag_parameter; gboolean handled; @@ -138,7 +140,22 @@ replace_table_process (gchar **buffer, ReplaceTable *table) /* push the string before the token */ g_string_append_len (dst, b, start - b); - replace_value = g_strdup (g_hash_table_lookup (table, token)); + replace_value = NULL; + /* match defines */ + if (table->defines && g_str_has_prefix (token, "value(")) { + s = extract_token_arg (token); + replace_value = g_strdup (g_hash_table_lookup (table->defines, s)); + /* fall back to empty string if not defined */ + if (replace_value == NULL) + replace_value = g_strdup (""); + g_free (s); + } + + /* lookup in the replace table */ + if (! replace_value) + replace_value = g_strdup (g_hash_table_lookup (table->table, token)); + + /* process the data */ if (replace_value) { if (! tag_parameter) adjust_tags_normal (&replace_value); @@ -260,3 +277,27 @@ get_next_token (const gchar *s, gchar **start, gchar **end, gboolean *tag_parame return g_strndup (dollar + 2, end_paren - dollar - 2); } +/* + * extract_token_arg: retrieves token argument "xxx(value)" + * - returns newly allocated string, caller is responsible for freeing + * + */ +gchar * +extract_token_arg (const gchar *str) +{ + const gchar *start; + const gchar *end; + + start = strstr (str, "("); + if (start == NULL) + return NULL; + start++; + + end = strstr (str, ")"); + if (end == NULL) + return NULL; + end++; + + return g_strndup (start, end - start - 1); +} + |
