masqmail

view src/tables.c @ 367:b27f66555ba8

Reformated multiline comments to have leading asterisks on each line Now we use: /* ** comment */ This makes the indent style simpler, too.
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Oct 2011 10:20:59 +0200
parents 41958685480d
children 01769f722a18
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999-2001 Oliver Kurth
4 ** Copyright (C) 2008 markus schnalke <meillo@marmaro.de>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
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 General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
21 #include <fnmatch.h>
23 #include "masqmail.h"
25 table_pair*
26 create_pair(gchar *key, gpointer value)
27 {
28 table_pair *pair;
30 pair = g_malloc(sizeof(table_pair));
31 pair->key = g_strdup(key);
32 pair->value = value;
34 return pair;
35 }
37 table_pair*
38 create_pair_string(gchar *key, gpointer value)
39 {
40 table_pair *pair;
42 pair = g_malloc(sizeof(table_pair));
43 pair->key = g_strdup(key);
44 pair->value = (gpointer) (g_strdup(value));
46 return pair;
47 }
49 table_pair*
50 parse_table_pair(gchar *line, char delim)
51 {
52 gchar buf[256];
53 gchar *p, *q;
54 table_pair *pair;
56 p = line;
57 q = buf;
58 while ((*p != '\0') && (*p != delim) && q < buf + 255)
59 *(q++) = *(p++);
60 *q = '\0';
62 pair = g_malloc(sizeof(table_pair));
63 pair->key = g_strdup(g_strstrip(buf));
65 if (*p) {
66 p++;
67 /* while(isspace(*p)) p++; */
68 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
69 } else
70 pair->value = (gpointer *) g_strdup("");
72 return pair;
73 }
75 gpointer*
76 table_find_func(GList *table_list, gchar *key, int (*cmp_func) (const char *,
77 const char *))
78 {
79 GList *node;
81 foreach(table_list, node) {
82 table_pair *pair = (table_pair *) (node->data);
83 if (cmp_func(pair->key, key) == 0)
84 return pair->value;
85 }
86 return NULL;
87 }
89 gpointer*
90 table_find(GList *table_list, gchar *key)
91 {
92 return table_find_func(table_list, key, strcmp);
93 }
95 gpointer*
96 table_find_case(GList *table_list, gchar *key)
97 {
98 return table_find_func(table_list, key, strcasecmp);
99 }
101 static int
102 fnmatch0(const char *pattern, const char *string)
103 {
104 return fnmatch(pattern, string, 0);
105 }
107 gpointer*
108 table_find_fnmatch(GList *table_list, gchar *key)
109 {
110 return table_find_func(table_list, key, fnmatch0);
111 }
113 GList*
114 table_read(gchar *fname, gchar delim)
115 {
116 GList *list = NULL;
117 FILE *fptr;
119 if ((fptr = fopen(fname, "rt"))) {
120 gchar buf[256];
122 while (fgets(buf, 255, fptr)) {
123 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
124 table_pair *pair;
125 g_strchomp(buf);
126 pair = parse_table_pair(buf, delim);
127 list = g_list_append(list, pair);
128 }
129 }
130 fclose(fptr);
131 if (!list)
132 logwrite(LOG_NOTICE, "table file %s contained no entries\n", fname);
133 return list;
134 }
135 logwrite(LOG_ALERT, "could not open table file %s: %s."
136 " Thus no aliasing will be done\n", fname, strerror(errno));
138 return NULL;
139 }
141 void
142 destroy_table(GList *table)
143 {
144 GList *node;
146 foreach(table, node) {
147 table_pair *p = (table_pair *) (node->data);
148 g_free(p->key);
149 g_free(p->value);
150 g_free(p);
151 }
152 g_list_free(table);
153 }