changeset 3286:8576de468e23 trunk

Add != operator.
author William Pitcock <nenolod@atheme-project.org>
date Thu, 09 Aug 2007 08:55:54 -0500
parents 740c6f845554
children d003b429bd03
files src/audacious/tuple_formatter.c src/tests/tuple_formatter_test.c
diffstat 2 files changed, 81 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/audacious/tuple_formatter.c	Thu Aug 09 08:22:08 2007 -0500
+++ b/src/audacious/tuple_formatter.c	Thu Aug 09 08:55:54 2007 -0500
@@ -98,9 +98,9 @@
                         level++;
                     }
                 }
-                else if (*iter == '}' && (sel == argument && --level != 0))
+                else if (*iter == '}' && (sel == argument && --level > 0))
                     g_string_append_c(sel, *iter);
-                else if (*iter == '}' && ((sel != argument) || (sel == argument && level == 0)))
+                else if (*iter == '}' && ((sel != argument) || (sel == argument && level <= 0)))
                 {
                     if (sel == argument)
                         iter++;
@@ -219,6 +219,51 @@
     return (tuple_get_value_type(tuple, expression) != TUPLE_UNKNOWN) ? TRUE : FALSE;
 }
 
+/* builtin-keyword: ${==arg1,arg2}, returns TRUE if <arg1> and <arg2> match. */
+static gboolean
+tuple_formatter_expression_match(Tuple *tuple, const gchar *expression)
+{
+    gchar **args = g_strsplit(expression, ",", 2);
+    gchar *arg1, *arg2;
+    gint ret;
+
+    if (tuple_get_value_type(tuple, args[0]) == TUPLE_UNKNOWN)
+    {
+        g_strfreev(args);
+        return FALSE;
+    }
+
+    if (tuple_get_value_type(tuple, args[1]) == TUPLE_UNKNOWN)
+    {
+        g_strfreev(args);
+        return FALSE;
+    }
+
+    if (tuple_get_value_type(tuple, args[0]) == TUPLE_STRING)
+        arg1 = g_strdup(tuple_get_string(tuple, args[0]));
+    else
+        arg1 = g_strdup_printf("%d", tuple_get_int(tuple, args[0]));
+
+    if (tuple_get_value_type(tuple, args[1]) == TUPLE_STRING)
+        arg2 = g_strdup(tuple_get_string(tuple, args[1]));
+    else
+        arg2 = g_strdup_printf("%d", tuple_get_int(tuple, args[1]));
+
+    ret = g_ascii_strcasecmp(arg1, arg2);
+    g_free(arg1);
+    g_free(arg2);
+    g_strfreev(args);
+
+    return ret ? FALSE : TRUE;
+}
+
+/* builtin-keyword: ${!=arg1,arg2}. returns TRUE if <arg1> and <arg2> don't match. */
+static gboolean
+tuple_formatter_expression_nonmatch(Tuple *tuple, const gchar *expression)
+{
+    return tuple_formatter_expression_match(tuple, expression) ^ 1;
+}
+
 /* processes a string containing instructions. does initialization phases
    if not already done */
 gchar *
@@ -229,6 +274,8 @@
     if (initialized == FALSE)
     {
         tuple_formatter_register_expression("?", tuple_formatter_expression_exists);
+        tuple_formatter_register_expression("==", tuple_formatter_expression_match);
+        tuple_formatter_register_expression("!=", tuple_formatter_expression_nonmatch);
         initialized = TRUE;
     }
 
--- a/src/tests/tuple_formatter_test.c	Thu Aug 09 08:22:08 2007 -0500
+++ b/src/tests/tuple_formatter_test.c	Thu Aug 09 08:55:54 2007 -0500
@@ -58,5 +58,37 @@
     }
     g_free(tstr);
 
+    tstr = tuple_formatter_process_string(tuple, "${==splork,splork:fields given matched}");
+    if (g_ascii_strcasecmp(tstr, "fields given matched"))
+    {
+        g_print("fail 4: '%s'\n", tstr);
+        return EXIT_FAILURE;
+    }
+    g_free(tstr);
+
+    tstr = tuple_formatter_process_string(tuple, "${==splork,splork:${splork}}");
+    if (g_ascii_strcasecmp(tstr, "moo"))
+    {
+        g_print("fail 5: '%s'\n", tstr);
+        return EXIT_FAILURE;
+    }
+    g_free(tstr);
+
+    tstr = tuple_formatter_process_string(tuple, "${!=splork,splorkerz:fields did not match}");
+    if (g_ascii_strcasecmp(tstr, "fields did not match"))
+    {
+        g_print("fail 6: '%s'\n", tstr);
+        return EXIT_FAILURE;
+    }
+    g_free(tstr);
+
+    tstr = tuple_formatter_process_string(tuple, "${!=splork,splorkerz:${splorkerz}}");
+    if (g_ascii_strcasecmp(tstr, "42"))
+    {
+        g_print("fail 7: '%s'\n", tstr);
+        return EXIT_FAILURE;
+    }
+    g_free(tstr);
+
     return EXIT_SUCCESS;
 }