masqmail
view src/tables.c @ 415:0430194f7ef8
Debug message cosmetic.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Wed, 29 Feb 2012 14:36:07 +0100 |
parents | b27f66555ba8 |
children |
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>
22 #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 && (*p != delim) && q < buf + 255) {
59 *(q++) = *(p++);
60 }
61 *q = '\0';
63 pair = g_malloc(sizeof(table_pair));
64 pair->key = g_strdup(g_strstrip(buf));
66 if (*p) {
67 p++;
68 /* while(isspace(*p)) p++; */
69 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
70 } else {
71 pair->value = (gpointer *) g_strdup("");
72 }
74 return pair;
75 }
77 gpointer*
78 table_find_func(GList *table_list, gchar *key,
79 int (*cmp_func) (const char *, const char *))
80 {
81 GList *node;
83 foreach(table_list, node) {
84 table_pair *pair = (table_pair *) (node->data);
85 if (cmp_func(pair->key, key) == 0) {
86 return pair->value;
87 }
88 }
89 return NULL;
90 }
92 gpointer*
93 table_find(GList *table_list, gchar *key)
94 {
95 return table_find_func(table_list, key, strcmp);
96 }
98 gpointer*
99 table_find_case(GList *table_list, gchar *key)
100 {
101 return table_find_func(table_list, key, strcasecmp);
102 }
104 static int
105 fnmatch0(const char *pattern, const char *string)
106 {
107 return fnmatch(pattern, string, 0);
108 }
110 gpointer*
111 table_find_fnmatch(GList *table_list, gchar *key)
112 {
113 return table_find_func(table_list, key, fnmatch0);
114 }
116 GList*
117 table_read(gchar *fname, gchar delim)
118 {
119 GList *list = NULL;
120 FILE *fptr;
121 gchar buf[256];
123 if (!(fptr = fopen(fname, "rt"))) {
124 logwrite(LOG_ALERT, "could not open table file %s: %s. Thus "
125 "no aliasing will be done\n",
126 fname, strerror(errno));
127 return NULL;
128 }
130 while (fgets(buf, sizeof buf, fptr)) {
131 if (!*buf || *buf == '#' || *buf == '\n') {
132 continue;
133 }
134 table_pair *pair;
135 g_strchomp(buf);
136 pair = parse_table_pair(buf, delim);
137 list = g_list_append(list, pair);
138 }
139 fclose(fptr);
140 if (!list) {
141 logwrite(LOG_NOTICE, "table file %s contained no entries\n",
142 fname);
143 }
144 return list;
145 }
147 void
148 destroy_table(GList *table)
149 {
150 GList *node;
152 foreach(table, node) {
153 table_pair *p = (table_pair *) (node->data);
154 g_free(p->key);
155 g_free(p->value);
156 g_free(p);
157 }
158 g_list_free(table);
159 }