Initial import of programs to send emails to speakers for an event
[cascardo/avaliacao2008.git] / regex.c
1 /*
2  *  Copyright (C) 2007  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <glib.h>
20 #include <villa.h>
21 #include <stdlib.h>
22 #include "avaliacao.h"
23
24 gchar *regex_str;
25 gsize regex_len;
26 GRegex *regex;
27
28 gboolean
29 regex_eval (const GMatchInfo *info, GString *result, gpointer data)
30 {
31   char *smap;
32   int slen;
33   CBMAP *map;
34   gchar *match;
35   const char *val;
36   match = g_match_info_fetch (info, 1);
37   smap = vlcurval (db, &slen);
38   map = cbmapload (smap, slen);
39   val = cbmapget (map, match + 1, -1, NULL);
40   g_string_append (result, val);
41   g_free (smap);
42   g_free (match);
43   cbmapclose (map);
44   return FALSE;
45 }
46
47 char *
48 regex_create_message ()
49 {
50   GError *error;
51   char *message;
52   error = NULL;
53   message = g_regex_replace_eval (regex, regex_str, regex_len,
54                                   0, 0, regex_eval, NULL, &error);
55   return message;
56 }
57
58 void
59 regex_init ()
60 {
61   GIOChannel *channel;
62   GError *error;
63   GIOStatus st;
64   error = NULL;
65   regex_str = NULL;
66   channel = g_io_channel_new_file ("message", "r", &error);
67   if (channel == NULL)
68     {
69       g_critical ("Could not open file: %s", error->message);
70       g_error_free (error);
71       abort ();
72     }
73   st = g_io_channel_read_to_end (channel, &regex_str, &regex_len, &error);
74   if (st != G_IO_STATUS_NORMAL)
75     {
76       g_critical ("Could not read file: %s", error->message);
77       g_error_free (error);
78       abort ();
79     }
80   g_io_channel_close (channel);
81   regex = g_regex_new ("(?<!\\\\)(\\\\[[:alpha:]][[:alnum:]]*)", 0, 0, &error);
82   if (regex == NULL)
83     {
84       g_critical ("Error creating regex: %s", error->message);
85       g_error_free (error);
86       g_free (regex_str);
87       abort ();
88     }
89 }