summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorDaniel Carl <danielcarl@gmx.de>2014-09-01 00:06:45 +0200
committerDaniel Carl <danielcarl@gmx.de>2014-09-01 00:41:52 +0200
commitef4c14cc9b1121d591e3c88cfb57a1e7b1688900 (patch)
tree85164864476b99c24badd2cc7e542b11edcc1eb6 /src/util.c
parent84e8e88d1374291702fba050533de855a8426e32 (diff)
Added utility function for wildcard matching.
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index d07909b..6a7d3b4 100644
--- a/src/util.c
+++ b/src/util.c
@@ -508,6 +508,78 @@ gboolean util_parse_expansion(const char **input, GString *str, int flags,
}
/**
+ * Compares given string against also given pattern.
+ * * matches any sequence of characters
+ * ? matches any single character except of /
+ * \? matches a ?
+ * . matches a .
+ * ~ matches a ~
+ */
+gboolean util_wildmatch(const char *pattern, const char *string)
+{
+ int i;
+ char ul, pl;
+ const char *p, *s;
+
+ p = pattern;
+ s = string;
+
+ while (*p) {
+ switch (*p) {
+ case '?':
+ /* match single char except of / or end */
+ if (*s == '/' || !*s) {
+ return false;
+ }
+ break;
+
+ case '\\':
+ /* \ escapes next * or ? char */
+ if (*(p + 1) == '*' || *(p + 1) == '?') {
+ p++;
+ if (*p != *s) {
+ return false;
+ }
+ }
+ break;
+
+ case '*':
+ /* Try to match as much as possible. Try to match the complete
+ * uri, if that fails move forward in uri and chack for a
+ * match. */
+ i = strlen(s);
+ while (i >= 0 && !util_wildmatch(p + 1, s + i)) {
+ i--;
+ }
+ return i >= 0;
+
+ default:
+ ul = *s;
+ if (VB_IS_UPPER(ul)) {
+ ul += 'a' - 'A';
+ }
+ pl = *p;
+ if (VB_IS_UPPER(pl)) {
+ pl += 'a' - 'A';
+ }
+ if (ul != pl) {
+ return false;
+ }
+ break;
+ }
+ p++;
+ s++;
+ }
+
+ /* if there is uri left on pattern end - this is no match */
+ if (!*p) {
+ return !*s;
+ }
+
+ return false;
+}
+
+/**
* Fills the given list store by matching data of also given src list.
*/
gboolean util_fill_completion(GtkListStore *store, const char *input, GList *src)