diff options
| -rw-r--r-- | src/block-parser.c | 25 | ||||
| -rw-r--r-- | src/generators.c | 13 | ||||
| -rw-r--r-- | src/replace-table.c | 89 |
3 files changed, 76 insertions, 51 deletions
diff --git a/src/block-parser.c b/src/block-parser.c index cfe9047..8bf7181 100644 --- a/src/block-parser.c +++ b/src/block-parser.c @@ -219,25 +219,6 @@ push_string (BlockParser *parser, const gchar *piece) } } -static gchar * -extract_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); -} - /* * block_parser_read_and_parse: reads input from the file and returns parsed line * - if there's a multiline block, string before the opening placeholder and @@ -284,7 +265,7 @@ block_parser_read_and_parse (BlockParser *parser, FILE *stream) if (parser->conditionals && (g_str_has_prefix (token, "ifdef(") || g_str_has_prefix (token, "ifndef("))) { data = g_new0 (BlockData, 1); data->is_conditional = TRUE; - data->conditional_key = extract_arg (token); + data->conditional_key = extract_token_arg (token); data->should_ignore = ((g_str_has_prefix (token, "ifdef(") && g_hash_table_lookup (parser->conditionals, data->conditional_key) == NULL) || (g_str_has_prefix (token, "ifndef(") && g_hash_table_lookup (parser->conditionals, data->conditional_key) != NULL)); if (data->should_ignore && parser->ignore_level == 0) @@ -297,7 +278,7 @@ block_parser_read_and_parse (BlockParser *parser, FILE *stream) } if (parser->conditionals && g_str_has_prefix (token, "endif(")) { - s = extract_arg (token); + s = extract_token_arg (token); data = g_queue_peek_head (parser->active_tree); if (data == NULL || !data->is_conditional || strcmp (data->conditional_key, s) != 0) { log_error ("block_parser_read_and_parse: something is wrong with the parser table: expected '%s' but got '%s'\n", s, data->conditional_key); @@ -313,7 +294,7 @@ block_parser_read_and_parse (BlockParser *parser, FILE *stream) } if (parser->conditionals && g_str_has_prefix (token, "else(")) { - s = extract_arg (token); + s = extract_token_arg (token); data = g_queue_peek_head (parser->active_tree); if (data == NULL || !data->is_conditional || strcmp (data->conditional_key, s) != 0) { log_error ("block_parser_read_and_parse: something is wrong with the parser table: expected '%s' but got '%s'\n", s, data->conditional_key); diff --git a/src/generators.c b/src/generators.c index 4f0963d..1623ad5 100644 --- a/src/generators.c +++ b/src/generators.c @@ -297,9 +297,10 @@ write_html_album (TGallerySetup *setup, res = TRUE; - global_replace_table = replace_table_new_from_defines (theme->defines); - block_parser = block_parser_new (); defines = clone_string_hash_table (theme->defines); + global_replace_table = replace_table_new (); + replace_table_set_defines (global_replace_table, defines); + block_parser = block_parser_new (); block_parser_set_conditionals (block_parser, defines); /* Get number of real pictures in the list */ @@ -463,7 +464,8 @@ write_html_album (TGallerySetup *setup, } /* Generate images (preview, original, thumbnail) */ - local_replace_table = replace_table_new_from_defines (defines); + local_replace_table = replace_table_new (); + replace_table_set_defines (local_replace_table, defines); s1 = NULL; switch (item->type) { @@ -625,9 +627,10 @@ write_html_image (TGallerySetup *setup, preload_imgname = NULL; res = TRUE; - replace_table = replace_table_new_from_defines (theme->defines); - block_parser = block_parser_new (); defines = clone_string_hash_table (theme->defines); + replace_table = replace_table_new (); + replace_table_set_defines (replace_table, defines); + block_parser = block_parser_new (); block_parser_set_conditionals (block_parser, defines); /* Get our index in the album */ 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); +} + |
