2009-03-22 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / wrapper / glob.c
blob90aabe079b80df4f17ac014c4dc77fb4603793ad
1 /*
2 * glob.c: Simple glob support for the class libraries
4 * Author:
5 * Miguel de Icaza (miguel@ximian.com).
7 * (C) 2002 Ximian, Inc.
8 */
9 #include <sys/types.h>
10 #include <glib.h>
11 #include <config.h>
12 #include <regex.h>
13 #include "wrapper.h"
15 gpointer
16 mono_glob_compile (const char *glob)
18 regex_t *compiled = g_new (regex_t, 1);
19 GString *str = g_string_new ("^");
20 const char *p;
22 for (p = glob; *p; p++){
23 switch (*p){
24 case '?':
25 g_string_append_c (str, '.');
26 break;
27 case '*':
28 g_string_append (str, ".*");
29 break;
31 case '[': case ']': case '\\': case '(': case ')':
32 case '^': case '$': case '.':
33 g_string_append_c (str, '\\');
34 /* fall */
35 default:
36 g_string_append_c (str, *p);
39 g_string_append_c (str, '$');
40 regcomp (compiled, str->str, 0);
42 return compiled;
45 int
46 mono_glob_match (gpointer handle, const char *str)
48 regex_t *compiled = (regex_t *) handle;
50 return regexec (compiled, str, 0, NULL, 0) == 0;
53 void
54 mono_glob_dispose (gpointer handle)
56 regfree ((regex_t *) handle);