Merge branch 'tg/next__media_port_lego.lua' into next
[libquvi-scripts.git] / tests / lib / re.c
blob426ca9ea7fd31fb2a72af28d4630a5d0550fff93
1 /* libquvi-scripts
2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi-scripts <http://quvi.sourceforge.net>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <glib.h>
24 #include "tests.h"
26 #define _W "%s: %s"
28 gboolean match(const gchar *s, const gchar *p)
30 GMatchInfo *m;
31 GError *err;
32 GRegex *re;
33 gboolean r;
35 err = NULL;
36 re = g_regex_new(p, G_REGEX_MULTILINE, 0, &err);
38 if (err != NULL)
40 g_warning(_W, __func__, err->message);
41 g_error_free(err);
42 return (FALSE);
45 m = NULL;
46 r = g_regex_match(re, s, 0, &m);
48 g_match_info_free(m);
49 m = NULL;
51 g_regex_unref(re);
52 re = NULL;
54 return (r);
57 capture_t capture_new(const gchar *str, const gchar *patt,
58 const GRegexCompileFlags flags)
60 capture_t p = g_new0(struct capture_s, 1);
62 p->re = g_regex_new(patt, flags, 0, &p->e);
63 if (p->e != NULL)
65 g_warning(_W, __func__, p->e->message);
66 capture_free(p);
67 return (NULL);
70 g_regex_match_full(p->re, str, -1, 0, 0, &p->m, &p->e);
71 if (p->e != NULL)
73 g_warning(_W, __func__, p->e->message);
74 capture_free(p);
75 return (NULL);
77 return (p);
80 gboolean capture_matches(capture_t p)
82 g_assert(p != NULL);
83 g_assert(p->m != NULL);
84 return (g_match_info_matches(p->m));
87 gchar *capture_fetch(capture_t p, const gint i)
89 g_assert(p != NULL);
90 g_assert(p->m != NULL);
91 return (g_match_info_fetch(p->m, i));
94 gboolean capture_next(capture_t p)
96 g_assert(p != NULL);
97 g_assert(p->m != NULL);
99 g_match_info_next(p->m, &p->e);
100 if (p->e != NULL)
102 g_warning(_W, __func__, p->e->message);
103 return (FALSE);
105 return (TRUE);
108 void capture_free(capture_t p)
110 if (p == NULL)
111 return;
113 g_match_info_free(p->m);
114 g_regex_unref(p->re);
116 if (p->e != NULL)
117 g_error_free(p->e);
119 g_free(p);
122 /* vim: set ts=2 sw=2 tw=72 expandtab: */