2 * Simple pattern matching
5 * Gonzalo Paniagua Javier (gonzalo@novell.com
7 * (C) 2006 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49 struct _GPatternSpec
{
54 compile_pattern (const gchar
*pattern
)
60 MatchType last
= MATCH_INVALID
;
70 str
= g_string_new ("");
71 for (i
= 0, len
= strlen (pattern
); i
< len
; i
++) {
73 if (c
== '*' || c
== '?') {
75 data
= g_new0 (PData
, 1);
76 data
->type
= MATCH_LITERAL
;
77 data
->str
= g_string_free (str
, FALSE
);
78 list
= g_slist_append (list
, data
);
79 str
= g_string_new ("");
82 if (last
== MATCH_ANYTHING
&& c
== '*')
85 data
= g_new0 (PData
, 1);
86 data
->type
= (c
== '*') ? MATCH_ANYTHING
: MATCH_ANYCHAR
;
87 list
= g_slist_append (list
, data
);
90 g_string_append_c (str
, c
);
95 if (last
== MATCH_ANYTHING
&& str
->len
== 0) {
96 data
->type
= MATCH_ANYTHING_END
;
98 } else if (str
->len
> 0) {
99 data
= g_new0 (PData
, 1);
100 data
->type
= MATCH_LITERAL
;
101 data
->str
= str
->str
;
103 list
= g_slist_append (list
, data
);
105 g_string_free (str
, free_str
);
111 print_pattern (gpointer data
, gpointer user_data
)
113 PData
*d
= (PData
*) data
;
115 printf ("Type: %s", d
->type
== MATCH_LITERAL
? "literal" : d
->type
== MATCH_ANYCHAR
? "any char" : "anything");
116 if (d
->type
== MATCH_LITERAL
)
117 printf (" String: %s", d
->str
);
123 g_pattern_spec_new (const gchar
*pattern
)
127 g_return_val_if_fail (pattern
!= NULL
, NULL
);
128 spec
= g_new0 (GPatternSpec
, 1);
130 spec
->pattern
= compile_pattern (pattern
);
132 g_slist_foreach (spec
->pattern
, print_pattern
, NULL
);
140 free_pdata (gpointer data
, gpointer user_data
)
142 PData
*d
= (PData
*) data
;
150 g_pattern_spec_free (GPatternSpec
*pspec
)
153 g_slist_foreach (pspec
->pattern
, free_pdata
, NULL
);
154 g_slist_free (pspec
->pattern
);
155 pspec
->pattern
= NULL
;
161 match_string (GSList
*list
, const gchar
*str
, size_t idx
, size_t max
)
165 while (list
&& idx
< max
) {
166 PData
*data
= (PData
*) list
->data
;
168 if (data
->type
== MATCH_ANYTHING_END
)
171 if (data
->type
== MATCH_LITERAL
) {
172 len
= strlen (data
->str
);
173 if (strncmp (&str
[idx
], data
->str
, len
) != 0)
179 * When recursing, we need this to avoid returning FALSE
180 * because 'list' will not be NULL
182 data
= (PData
*) list
->data
;
183 if (data
->type
== MATCH_ANYTHING_END
)
186 } else if (data
->type
== MATCH_ANYCHAR
) {
189 } else if (data
->type
== MATCH_ANYTHING
) {
191 if (match_string (list
->next
, str
, idx
++, max
))
196 g_assert_not_reached ();
200 return (list
== NULL
&& idx
>= max
);
203 g_pattern_match_string (GPatternSpec
*pspec
, const gchar
*string
)
205 g_return_val_if_fail (pspec
!= NULL
, FALSE
);
206 g_return_val_if_fail (string
!= NULL
, FALSE
);
208 if (pspec
->pattern
== NULL
)
210 return match_string (pspec
->pattern
, string
, 0, strlen (string
));