fixes compile
[platform/upstream/ibus-libpinyin.git] / src / PYRegex.h
1 /* vim:set et ts=4 sts=4:
2  *
3  * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
4  *
5  * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21 #ifndef __PY_REGEX_H_
22 #define __PY_REGEX_H_
23
24 #include <sys/types.h>
25 #include <regex.h>
26 #include <glib.h>
27
28 namespace PY {
29
30 class Regex {
31 public:
32     Regex (const gchar *regex, gint cflags = REG_EXTENDED)
33     {
34         int retval;
35         retval = regcomp (&m_regex, regex, cflags);
36         if (retval != 0) {
37             gchar errorbuf[128];
38             regerror (retval, &m_regex, errorbuf, sizeof (errorbuf));
39             g_debug ("regex error: %s", errorbuf);
40             g_assert_not_reached ();
41         }
42     }
43
44     ~Regex (void)
45     {
46         regfree (&m_regex);
47     }
48
49     gboolean match (const gchar *str) const
50     {
51         int retval;
52         regmatch_t match;
53         retval = regexec (&m_regex, str, 1, &match, 0);
54         return retval == 0;
55     }
56
57     gboolean operator & (const gchar *str) const
58     {
59         return match (str);
60     }
61
62 private:
63     regex_t m_regex;
64 };
65
66 };
67
68 #endif