changeset 3338:07ed916b7176 trunk

added support for raw text comparisons in tuple == operator (and that means != as well)
author Giacomo Lozito <james@develia.org>
date Sun, 12 Aug 2007 16:58:43 +0200
parents 99f34db2c3fc
children c23513d0ee17
files src/audacious/tuple_formatter.c src/tests/tuple_formatter_test.c
diffstat 2 files changed, 75 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/audacious/tuple_formatter.c	Sun Aug 12 21:21:46 2007 +0900
+++ b/src/audacious/tuple_formatter.c	Sun Aug 12 16:58:43 2007 +0200
@@ -380,35 +380,63 @@
     return (tuple_get_value_type(tuple, expression) != TUPLE_UNKNOWN) ? TRUE : FALSE;
 }
 
-/* builtin-keyword: ${==arg1,arg2}, returns TRUE if <arg1> and <arg2> match. */
+/* builtin-keyword: ${==arg1,arg2}, returns TRUE if <arg1> (a tuple field) and <arg2> (a tuple field) match. */
 static gboolean
 tuple_formatter_expression_match(Tuple *tuple, const gchar *expression)
 {
     gchar **args = g_strsplit(expression, ",", 2);
-    gchar *arg1, *arg2;
+    gchar *arg1 = NULL, *arg2 = NULL;
     gint ret;
 
-    if (tuple_get_value_type(tuple, args[0]) == TUPLE_UNKNOWN)
+    if (args[0][0] == '\"') /* check if arg1 is "raw text" */
+    {
+        if ( strlen(args[0]) > 1 )
+        {
+            args[0][strlen(args[0]) - 1] = '\0';
+            arg1 = g_strdup(&(args[0][1]));
+            args[0][strlen(args[0]) - 1] = '\"';
+        }
+        else /* bad formatted arg */
+            return FALSE;
+    }
+    else if (tuple_get_value_type(tuple, args[0]) == TUPLE_UNKNOWN)
+    {
+        g_strfreev(args);
+        return FALSE;
+    }
+    
+    if (args[1][0] == '\"') /* check if arg2 is "raw text" */
+    {
+        if ( strlen(args[1]) > 1 )
+        {
+            args[1][strlen(args[1]) - 1] = '\0';
+            arg2 = g_strdup(&(args[1][1]));
+            args[1][strlen(args[1]) - 1] = '\"';
+        }
+        else /* bad formatted arg */
+            return FALSE;
+    }
+    else if (tuple_get_value_type(tuple, args[1]) == TUPLE_UNKNOWN)
     {
         g_strfreev(args);
         return FALSE;
     }
 
-    if (tuple_get_value_type(tuple, args[1]) == TUPLE_UNKNOWN)
+    if (!arg1) /* if arg1 is not "raw text", get the tuple value */
     {
-        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[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]));
+    if (!arg2) /* if arg2 is not "raw text", get the tuple value */
+    {
+        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);
--- a/src/tests/tuple_formatter_test.c	Sun Aug 12 21:21:46 2007 +0900
+++ b/src/tests/tuple_formatter_test.c	Sun Aug 12 16:58:43 2007 +0200
@@ -148,6 +148,38 @@
     }
     g_free(tstr);
 
+    tstr = tuple_formatter_process_string(tuple, "${==splork,\"moo\":const text field matches}");
+    if (g_ascii_strcasecmp(tstr, "const text field matches"))
+    {
+        g_print("fail 15: '%s'\n", tstr);
+        return EXIT_FAILURE;
+    }
+    g_free(tstr);
+
+    tstr = tuple_formatter_process_string(tuple, "${==\"moo\",\"moo\":const text fields match}");
+    if (g_ascii_strcasecmp(tstr, "const text fields match"))
+    {
+        g_print("fail 16: '%s'\n", tstr);
+        return EXIT_FAILURE;
+    }
+    g_free(tstr);
+
+    tstr = tuple_formatter_process_string(tuple, "${!=splork,\"muu\":const text field doesn't match}");
+    if (g_ascii_strcasecmp(tstr, "const text field doesn't match"))
+    {
+        g_print("fail 17: '%s'\n", tstr);
+        return EXIT_FAILURE;
+    }
+    g_free(tstr);
+
+    tstr = tuple_formatter_process_string(tuple, "${!=\"moo\",\"muu\":const text fields do not match}");
+    if (g_ascii_strcasecmp(tstr, "const text fields do not match"))
+    {
+        g_print("fail 18: '%s'\n", tstr);
+        return EXIT_FAILURE;
+    }
+    g_free(tstr);
+
     mowgli_object_unref(tuple);
 
     return EXIT_SUCCESS;