Add XkbNameMatchesPattern implementation from xkbfile
authorDan Nicholson <dbn.lists@gmail.com>
Sun, 12 Apr 2009 19:30:12 +0000 (12:30 -0700)
committerDan Nicholson <dbn.lists@gmail.com>
Mon, 13 Apr 2009 13:24:36 +0000 (06:24 -0700)
The xkbcomp listing code matches a glob type pattern against installed
xkb files. This adds a Xkbc implementation of the pattern matching code.

src/misc.c
src/xkbcomp/listing.c
src/xkbmisc.h

index 744c588..e49921a 100644 (file)
@@ -259,3 +259,38 @@ _XkbcKSCheckCase(KeySym ks)
 
     return rtrn;
 }
+
+#define UNMATCHABLE(c) ((c) == '(' || (c) == ')' || (c) == '/')
+
+Bool
+XkbcNameMatchesPattern(char *name, char *ptrn)
+{
+    while (ptrn[0] != '\0') {
+        if (name[0] == '\0') {
+            if (ptrn[0] == '*') {
+                ptrn++;
+                continue;
+            }
+            return False;
+        }
+
+        if (ptrn[0] == '?') {
+            if (UNMATCHABLE(name[0]))
+                return False;
+        }
+        else if (ptrn[0] == '*') {
+            if (!UNMATCHABLE(name[0]) &&
+                XkbcNameMatchesPattern(name + 1, ptrn))
+                return True;
+            return XkbcNameMatchesPattern(name, ptrn + 1);
+        }
+        else if (ptrn[0] != name[0])
+            return False;
+
+        name++;
+        ptrn++;
+    }
+
+    /* if we get here, the pattern is exhausted (-:just like me:-) */
+    return (name[0] == '\0');
+}
index 2845032..e509b96 100644 (file)
@@ -117,6 +117,7 @@ SOFTWARE.
 # define FileName(file) file->d_name
 #endif
 
+#include "xkbmisc.h"
 #include "xkbpath.h"
 #include "parseutils.h"
 #include "misc.h"
@@ -306,7 +307,7 @@ AddDirectory(char *head, char *ptrn, char *rest, char *map)
         filename = FileName(file);
         if (!filename || filename[0] == '.')
             continue;
-        if (ptrn && (!XkbNameMatchesPattern(filename, ptrn)))
+        if (ptrn && (!XkbcNameMatchesPattern(filename, ptrn)))
             continue;
         tmp =
             (char *) uAlloc((head ? strlen(head) : 0) + strlen(filename) + 2);
@@ -401,12 +402,12 @@ MapMatches(char *mapToConsider, char *ptrn)
     int i;
 
     if (ptrn != NULL)
-        return XkbNameMatchesPattern(mapToConsider, ptrn);
+        return XkbcNameMatchesPattern(mapToConsider, ptrn);
     if (nMapOnly < 1)
         return True;
     for (i = 0; i < nMapOnly; i++)
     {
-        if (XkbNameMatchesPattern(mapToConsider, mapOnly[i]))
+        if (XkbcNameMatchesPattern(mapToConsider, mapOnly[i]))
             return True;
     }
     return False;
index 16747a1..61e94e6 100644 (file)
@@ -63,6 +63,9 @@ _XkbcKSCheckCase(KeySym sym);
 #define XkbcKSIsLower(k) (_XkbcKSCheckCase(k) & _XkbKSLower)
 #define XkbcKSIsUpper(k) (_XkbcKSCheckCase(k) & _XkbKSUpper)
 
+extern Bool
+XkbcNameMatchesPattern(char *name, char *ptrn);
+
 /***====================================================================***/
 
 extern char *