cql2ccl: quote term characters, except operators
authorAdam Dickmeiss <adam@indexdata.dk>
Sat, 27 Aug 2011 13:38:59 +0000 (15:38 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Sat, 27 Aug 2011 13:38:59 +0000 (15:38 +0200)
Fixes #4584.

src/cql2ccl.c

index df6e863..ab358f6 100644 (file)
@@ -26,36 +26,56 @@ static void pr_term(struct cql_node *cn,
 {
     while (cn)
     {
-        const char *cp;
-        for (cp = cn->u.st.term; *cp; cp++)
+        if (! *cn->u.st.term) /* empty term special case */
+            pr("\"\"", client_data);
+        else
         {
-            char x[4];
-
-            if (*cp == '\\' && cp[1])
-            {
-                x[0] = cp[0];
-                x[1] = cp[1];
-                x[2] = '\0';
-                cp++;
-            }
-            else if (*cp == '#')
-            {
-                strcpy(x, "\\#");
-            }
-            else if (*cp == '*')
+            const char *cp;
+            int quote_mode = 0;
+            for (cp = cn->u.st.term; *cp; cp++)
             {
-                strcpy(x, "?");
-            }
-            else if (*cp == '?')
-            {
-                strcpy(x, "#");
-            }
-            else
-            {
-                x[0] = *cp;
-                x[1] = '\0';
+                char x[4];
+                
+                if (*cp == '\\' && cp[1])
+                {
+                    x[0] = cp[0];
+                    x[1] = cp[1];
+                    x[2] = '\0';
+                    cp++;
+                    pr(x, client_data);
+                }
+                else if (*cp == '*')
+                {
+                    if (quote_mode)
+                    {
+                        pr("\"", client_data);
+                        quote_mode = 0;
+                    }
+                    pr("?", client_data);
+                }
+                else if (*cp == '?')
+                {
+                    if (quote_mode)
+                    {
+                        pr("\"", client_data);
+                        quote_mode = 0;
+                    }
+                    pr("#", client_data);
+                }
+                else
+                {
+                    if (!quote_mode)
+                    {
+                        pr("\"", client_data);
+                        quote_mode = 1;
+                    }
+                    x[0] = *cp;
+                    x[1] = '\0';
+                    pr(x, client_data);
+                }
             }
-            pr(x, client_data);
+            if (quote_mode)
+                pr("\"", client_data);
         }
         if (cn->u.st.extra_terms)
             pr(" ", client_data);