masqmail-0.2

view src/tables.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children f671821d8222
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include "masqmail.h"
20 #include <fnmatch.h>
22 table_pair*
23 create_pair(gchar * key, gpointer value)
24 {
25 table_pair *pair;
27 pair = g_malloc(sizeof(table_pair));
28 pair->key = g_strdup(key);
29 pair->value = value;
31 return pair;
32 }
34 table_pair*
35 create_pair_string(gchar * key, gpointer value)
36 {
37 table_pair *pair;
39 pair = g_malloc(sizeof(table_pair));
40 pair->key = g_strdup(key);
41 pair->value = (gpointer) (g_strdup(value));
43 return pair;
44 }
46 table_pair*
47 parse_table_pair(gchar * line, char delim)
48 {
49 gchar buf[256];
50 gchar *p, *q;
51 table_pair *pair;
53 p = line;
54 q = buf;
55 while ((*p != 0) && (*p != delim) && q < buf + 255)
56 *(q++) = *(p++);
57 *q = 0;
59 pair = g_malloc(sizeof(table_pair));
60 pair->key = g_strdup(g_strstrip(buf));
62 if (*p) {
63 p++;
64 /* while(isspace(*p)) p++; */
65 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
66 } else
67 pair->value = (gpointer *) g_strdup("");
69 return pair;
70 }
72 gpointer*
73 table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *))
74 {
75 GList *node;
77 foreach(table_list, node) {
78 table_pair *pair = (table_pair *) (node->data);
79 if (cmp_func(pair->key, key) == 0)
80 return pair->value;
81 }
82 return NULL;
83 }
85 gpointer*
86 table_find(GList * table_list, gchar * key)
87 {
88 return table_find_func(table_list, key, strcmp);
89 }
91 gpointer*
92 table_find_case(GList * table_list, gchar * key)
93 {
94 return table_find_func(table_list, key, strcasecmp);
95 }
97 static int
98 fnmatch0(const char *pattern, const char *string)
99 {
100 return fnmatch(pattern, string, 0);
101 }
103 gpointer*
104 table_find_fnmatch(GList * table_list, gchar * key)
105 {
106 return table_find_func(table_list, key, fnmatch0);
107 }
109 GList*
110 table_read(gchar * fname, gchar delim)
111 {
112 GList *list = NULL;
113 FILE *fptr;
115 if ((fptr = fopen(fname, "rt"))) {
116 gchar buf[256];
118 while (fgets(buf, 255, fptr)) {
119 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
120 table_pair *pair;
121 g_strchomp(buf);
122 pair = parse_table_pair(buf, delim);
123 list = g_list_append(list, pair);
124 }
125 }
126 fclose(fptr);
127 return list;
128 }
129 logwrite(LOG_ALERT, "could not open table file %s: %s\n", fname, strerror(errno));
131 return NULL;
132 }
134 void
135 destroy_table(GList * table)
136 {
137 GList *node;
139 foreach(table, node) {
140 table_pair *p = (table_pair *) (node->data);
141 g_free(p->key);
142 g_free(p->value);
143 g_free(p);
144 }
145 g_list_free(table);
146 }