Initial import of programs to send emails to speakers for an event
[cascardo/avaliacao2008.git] / normal.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 <stdio.h>
21 #include <stdlib.h>
22 #include <depot.h>
23 #include <cabin.h>
24
25 int main (int argc, char **argv)
26 {
27   GIOChannel *channel;
28   GError *error;
29   GIOStatus st;
30   gchar *str;
31   gsize len;
32   DEPOT *db;
33   int records;
34   if (argc < 2)
35     {
36       g_critical ("No input file.");
37       abort ();
38     }
39   error = NULL;
40   channel = g_io_channel_new_file (argv[1], "r", &error);
41   if (channel == NULL)
42     {
43       g_critical ("Could not open file: %s", error->message);
44       g_error_free (error);
45       abort ();
46     }
47   db = dpopen ("palestras", DP_OWRITER | DP_OCREAT, 0);
48   if (db == NULL)
49     {
50       g_critical ("Could not create database.");
51       abort ();
52     }
53   records = 0;
54   for (st = g_io_channel_read_line (channel, &str, &len, NULL, &error);
55        st == G_IO_STATUS_NORMAL;
56        st = g_io_channel_read_line (channel, &str, &len, NULL, &error))
57     {
58       char **fields;
59       int i;
60       fields = g_strsplit (g_strchomp (str), ";", 0);
61       if (fields != NULL || fields[0] != NULL)
62         {
63           for (i = 1; fields[i] != NULL; i+=3)
64             {
65               if (fields[i+2] != NULL)
66                 {
67                   CBMAP *map;
68                   char *val;
69                   int vlen;
70                   map = cbmapopen ();
71                   cbmapput (map, "id", -1, fields[0], -1, 0);
72                   cbmapput (map, "palestra", -1, fields[i], -1, 0);
73                   cbmapput (map, "palestrantes", -1, fields[i+1], -1, 0);
74                   cbmapput (map, "email", -1, fields[i+2], -1, 0);
75                   val = cbmapdump (map, &vlen);
76                   cbmapclose (map);
77                   dpput (db, (char *) &(records), sizeof records, val, vlen, DP_DOVER);
78                   records++;
79                   free (val);
80                 }
81             }
82         }
83       g_strfreev (fields);
84     }
85   dpclose (db);
86   g_io_channel_close (channel);
87   return 0;
88 }