# HG changeset patch # User William Pitcock # Date 1186669328 18000 # Node ID 4e7cc6d9b525dd673ec518ecdbca8e5bbede47bc # Parent 5cc45a1f118a17fa88f0236dfbe78a4a7ac3aa3a add (empty)? function to formatter diff -r 5cc45a1f118a -r 4e7cc6d9b525 src/audacious/tuple_formatter.c --- 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 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; } diff -r 5cc45a1f118a -r 4e7cc6d9b525 src/tests/tuple_formatter_test.c --- 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;