annotate src/tables.c @ 14:a8f3424347dc

replaced number 0 with character \0 where appropriate
author meillo@marmaro.de
date Wed, 29 Oct 2008 21:21:26 +0100
parents 26e34ae9a3e3
children f671821d8222
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
1 /* MasqMail
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
2 Copyright (C) 1999-2001 Oliver Kurth
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
3
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
6 the Free Software Foundation; either version 2 of the License, or
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
7 (at your option) any later version.
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
8
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
12 GNU General Public License for more details.
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
13
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
15 along with this program; if not, write to the Free Software
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
17 */
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
18
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
19 #include "masqmail.h"
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
20 #include <fnmatch.h>
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
21
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
22 table_pair*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
23 create_pair(gchar * key, gpointer value)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
24 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
25 table_pair *pair;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
26
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
27 pair = g_malloc(sizeof(table_pair));
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
28 pair->key = g_strdup(key);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
29 pair->value = value;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
30
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
31 return pair;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
32 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
33
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
34 table_pair*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
35 create_pair_string(gchar * key, gpointer value)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
36 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
37 table_pair *pair;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
38
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
39 pair = g_malloc(sizeof(table_pair));
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
40 pair->key = g_strdup(key);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
41 pair->value = (gpointer) (g_strdup(value));
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
42
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
43 return pair;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
44 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
45
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
46 table_pair*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
47 parse_table_pair(gchar * line, char delim)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
48 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
49 gchar buf[256];
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
50 gchar *p, *q;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
51 table_pair *pair;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
52
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
53 p = line;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
54 q = buf;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
55 while ((*p != 0) && (*p != delim) && q < buf + 255)
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
56 *(q++) = *(p++);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
57 *q = 0;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
58
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
59 pair = g_malloc(sizeof(table_pair));
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
60 pair->key = g_strdup(g_strstrip(buf));
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
61
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
62 if (*p) {
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
63 p++;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
64 /* while(isspace(*p)) p++; */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
65 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
66 } else
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
67 pair->value = (gpointer *) g_strdup("");
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
68
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
69 return pair;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
70 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
71
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
72 gpointer*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
73 table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *))
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
74 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
75 GList *node;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
76
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
77 foreach(table_list, node) {
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
78 table_pair *pair = (table_pair *) (node->data);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
79 if (cmp_func(pair->key, key) == 0)
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
80 return pair->value;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
81 }
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
82 return NULL;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
83 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
84
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
85 gpointer*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
86 table_find(GList * table_list, gchar * key)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
87 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
88 return table_find_func(table_list, key, strcmp);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
89 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
90
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
91 gpointer*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
92 table_find_case(GList * table_list, gchar * key)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
93 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
94 return table_find_func(table_list, key, strcasecmp);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
95 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
96
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
97 static int
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
98 fnmatch0(const char *pattern, const char *string)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
99 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
100 return fnmatch(pattern, string, 0);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
101 }
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
102
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
103 gpointer*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
104 table_find_fnmatch(GList * table_list, gchar * key)
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
105 {
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
106 return table_find_func(table_list, key, fnmatch0);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
107 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
108
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
109 GList*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
110 table_read(gchar * fname, gchar delim)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
111 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
112 GList *list = NULL;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
113 FILE *fptr;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
114
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
115 if ((fptr = fopen(fname, "rt"))) {
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
116 gchar buf[256];
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
117
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
118 while (fgets(buf, 255, fptr)) {
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
119 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
120 table_pair *pair;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
121 g_strchomp(buf);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
122 pair = parse_table_pair(buf, delim);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
123 list = g_list_append(list, pair);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
124 }
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
125 }
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
126 fclose(fptr);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
127 return list;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
128 }
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
129 logwrite(LOG_ALERT, "could not open table file %s: %s\n", fname, strerror(errno));
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
130
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
131 return NULL;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
132 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
133
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
134 void
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
135 destroy_table(GList * table)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
136 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
137 GList *node;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
138
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
139 foreach(table, node) {
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
140 table_pair *p = (table_pair *) (node->data);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
141 g_free(p->key);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
142 g_free(p->value);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
143 g_free(p);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
144 }
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
145 g_list_free(table);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
146 }