summaryrefslogtreecommitdiff
path: root/src/block-parser.c
diff options
context:
space:
mode:
authorTomas Bzatek <tbzatek@users.sourceforge.net>2012-12-24 20:10:27 +0100
committerTomas Bzatek <tbzatek@users.sourceforge.net>2012-12-24 20:10:27 +0100
commitc5e75352f27a02501b3a1a9189f10a581c12e7fb (patch)
treeb3bdae02b91bbb7ab4f5bb48b9704dbf378ce533 /src/block-parser.c
parentdff418800174265b1a591ac44eb244e2b16f94a5 (diff)
downloadcataract-c5e75352f27a02501b3a1a9189f10a581c12e7fb.tar.xz
block-parser: Add support for custom block functions
Similar to replace-table custom functions, this brings an ability to register custom block functions that can be called from templates with variable arguments. The syntax is as follows: <!-- $(if (function_name(arg1, "arg2", 'arg3'))) --> ... <!-- $(endif (function_name(arg1, "arg2", 'arg3'))) --> An optional exclamation mark before the function name reverts the result (negates) the predicate. This allows template flexibility with respect to the currently processed image.
Diffstat (limited to 'src/block-parser.c')
-rw-r--r--src/block-parser.c57
1 files changed, 54 insertions, 3 deletions
diff --git a/src/block-parser.c b/src/block-parser.c
index 0697d2a..8aa882a 100644
--- a/src/block-parser.c
+++ b/src/block-parser.c
@@ -38,6 +38,7 @@ struct BlockParser {
GQueue *active_tree;
gchar *current_line;
GHashTable *conditionals;
+ GHashTable *functions;
guint ignore_level;
};
@@ -48,10 +49,15 @@ typedef struct {
gboolean finished;
gboolean is_conditional;
+ gboolean is_conditional_function;
gchar *conditional_key;
gboolean should_ignore;
} BlockData;
+typedef struct {
+ BlockParserConditionalFunction callback;
+ gpointer user_data;
+} BlockParserFuncData;
static void
@@ -75,6 +81,7 @@ block_parser_new ()
parser = g_new0 (BlockParser, 1);
parser->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, block_parser_destroy_notify);
+ parser->functions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
parser->active_tree = g_queue_new ();
return parser;
@@ -85,6 +92,7 @@ block_parser_free (BlockParser *parser)
{
g_return_if_fail (parser != NULL);
+ g_hash_table_destroy (parser->functions);
g_hash_table_destroy (parser->table);
g_queue_free (parser->active_tree);
g_free (parser);
@@ -125,6 +133,24 @@ block_parser_register_key (BlockParser *parser, const gchar *key, const gchar *r
}
/*
+ * block_parser_register_function: function called to determine whether a block should be ignored or not
+ *
+ */
+void
+block_parser_register_function (BlockParser *parser, const gchar *conditional_name, BlockParserConditionalFunction callback, gpointer user_data)
+{
+ BlockParserFuncData *func_data;
+
+ g_return_if_fail (parser != NULL);
+
+ func_data = g_malloc0 (sizeof (BlockParserFuncData));
+ func_data->callback = callback;
+ func_data->user_data = user_data;
+
+ g_hash_table_insert (parser->functions, g_strdup (conditional_name), func_data);
+}
+
+/*
* block_parser_get_data: return retrieved data or NULL if none read yet
* returns newly allocated string, caller is responsible for freeing
*
@@ -241,6 +267,9 @@ block_parser_read_and_parse (BlockParser *parser, FILE *stream)
gchar *key;
BlockData *data;
gboolean handled;
+ gchar **args;
+ gboolean negation;
+ BlockParserFuncData *func_data;
g_return_val_if_fail (parser != NULL, NULL);
@@ -262,12 +291,34 @@ block_parser_read_and_parse (BlockParser *parser, FILE *stream)
g_free (s);
/* match conditionals */
- if (parser->conditionals && (token_has_prefix (token, "ifdef") || token_has_prefix (token, "ifndef"))) {
+ if ((parser->conditionals && (token_has_prefix (token, "ifdef") || token_has_prefix (token, "ifndef"))) ||
+ token_has_prefix (token, "if"))
+ {
data = g_new0 (BlockData, 1);
data->is_conditional = TRUE;
+ data->is_conditional_function = token_has_prefix (token, "if");
data->conditional_key = extract_token_arg (token);
- data->should_ignore = ((token_has_prefix (token, "ifdef") && g_hash_table_lookup (parser->conditionals, data->conditional_key) == NULL) ||
- (token_has_prefix (token, "ifndef") && g_hash_table_lookup (parser->conditionals, data->conditional_key) != NULL));
+
+ if (! data->is_conditional_function) {
+ data->should_ignore = ((token_has_prefix (token, "ifdef") && g_hash_table_lookup (parser->conditionals, data->conditional_key) == NULL) ||
+ (token_has_prefix (token, "ifndef") && g_hash_table_lookup (parser->conditionals, data->conditional_key) != NULL));
+ } else {
+ s = parse_function (data->conditional_key, &args, &negation);
+ data->should_ignore = TRUE;
+ if (s == NULL) {
+ log_error ("block_parser_read_and_parse: invalid conditional function specified: '%s'\n", data->conditional_key);
+ } else {
+ func_data = g_hash_table_lookup (parser->functions, s);
+ if (func_data) { /* when not found, ignore the block as the function cannot be called */
+ data->should_ignore = ! func_data->callback (args, func_data->user_data);
+ if (negation)
+ data->should_ignore = ! data->should_ignore;
+ }
+ g_strfreev (args);
+ g_free (s);
+ }
+ }
+
if (data->should_ignore && parser->ignore_level == 0)
parser->ignore_level = 1;
else