/
glob.c
57 lines (50 loc) · 1.02 KB
/
glob.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* glob.c: Simple glob support for the class libraries
*
* Author:
* Miguel de Icaza (miguel@ximian.com).
*
* (C) 2002 Ximian, Inc.
*/
#include <sys/types.h>
#include <glib.h>
#include <config.h>
#include <regex.h>
#include "wrapper.h"
gpointer
mono_glob_compile (const char *glob)
{
regex_t *compiled = g_new (regex_t, 1);
GString *str = g_string_new ("^");
const char *p;
for (p = glob; *p; p++){
switch (*p){
case '?':
g_string_append_c (str, '.');
break;
case '*':
g_string_append (str, ".*");
break;
case '[': case ']': case '\\': case '(': case ')':
case '^': case '$': case '.':
g_string_append_c (str, '\\');
/* fall */
default:
g_string_append_c (str, *p);
}
}
g_string_append_c (str, '$');
regcomp (compiled, str->str, 0);
return compiled;
}
int
mono_glob_match (gpointer handle, const char *str)
{
regex_t *compiled = (regex_t *) handle;
return regexec (compiled, str, 0, NULL, 0) == 0;
}
void
mono_glob_dispose (gpointer handle)
{
regfree ((regex_t *) handle);
}