Mercurial > audlegacy
changeset 3290:4e7cc6d9b525 trunk
add (empty)? function to formatter
author | William Pitcock <nenolod@atheme-project.org> |
---|---|
date | Thu, 09 Aug 2007 09:22:08 -0500 |
parents | 5cc45a1f118a |
children | 02335e399a16 |
files | src/audacious/tuple_formatter.c src/tests/tuple_formatter_test.c |
diffstat | 2 files changed, 37 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/audacious/tuple_formatter.c Thu Aug 09 09:05:02 2007 -0500 +++ b/src/audacious/tuple_formatter.c Thu Aug 09 09:22:08 2007 -0500 @@ -43,6 +43,7 @@ * tuple as integer value of "value" * - ${==field,field:expr}: evaluates expr if both fields are the same * - ${!=field,field:expr}: evaluates expr if both fields are not the same + * - ${(empty)?field:expr}: evaluates expr if field is empty or does not exist * * everything else is treated as raw text. * additionally, plugins can add additional instructions! @@ -264,6 +265,33 @@ return tuple_formatter_expression_match(tuple, expression) ^ 1; } +/* builtin-keyword: ${empty?}. returns TRUE if <arg> is empty. */ +static gboolean +tuple_formatter_expression_empty(Tuple *tuple, const gchar *expression) +{ + gboolean ret = TRUE; + const gchar *iter; + TupleValueType type = tuple_get_value_type(tuple, expression); + + if (type == TUPLE_UNKNOWN) + return TRUE; + + if (type == TUPLE_INT && tuple_get_int(tuple, expression) != 0) + return FALSE; + + iter = tuple_get_string(tuple, expression); + + while (ret && *iter != '\0') + { + if (*iter == ' ') + iter++; + else + ret = FALSE; + } + + return ret; +} + /* processes a string containing instructions. does initialization phases if not already done */ gchar * @@ -276,6 +304,7 @@ tuple_formatter_register_expression("?", tuple_formatter_expression_exists); tuple_formatter_register_expression("==", tuple_formatter_expression_match); tuple_formatter_register_expression("!=", tuple_formatter_expression_nonmatch); + tuple_formatter_register_expression("(empty)?", tuple_formatter_expression_empty); initialized = TRUE; }
--- a/src/tests/tuple_formatter_test.c Thu Aug 09 09:05:02 2007 -0500 +++ b/src/tests/tuple_formatter_test.c Thu Aug 09 09:22:08 2007 -0500 @@ -98,6 +98,14 @@ } g_free(tstr); + tstr = tuple_formatter_process_string(tuple, "${(empty)?splorky:${splorkerz}}"); + if (g_ascii_strcasecmp(tstr, "42")) + { + g_print("fail 9: '%s'\n", tstr); + return EXIT_FAILURE; + } + g_free(tstr); + mowgli_object_unref(tuple); return EXIT_SUCCESS;