Mercurial > masqmail
comparison src/tables.c @ 0:08114f7dcc23 0.2.21
this is masqmail-0.2.21 from oliver kurth
author | meillo@marmaro.de |
---|---|
date | Fri, 26 Sep 2008 17:05:23 +0200 |
parents | |
children | 26e34ae9a3e3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:08114f7dcc23 |
---|---|
1 /* MasqMail | |
2 Copyright (C) 1999-2001 Oliver Kurth | |
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 | |
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 */ | |
18 | |
19 #include "masqmail.h" | |
20 #include <fnmatch.h> | |
21 | |
22 table_pair *create_pair(gchar *key, gpointer value) | |
23 { | |
24 table_pair *pair; | |
25 | |
26 pair = g_malloc(sizeof(table_pair)); | |
27 pair->key = g_strdup(key); | |
28 pair->value = value; | |
29 | |
30 return pair; | |
31 } | |
32 | |
33 table_pair *create_pair_string(gchar *key, gpointer value) | |
34 { | |
35 table_pair *pair; | |
36 | |
37 pair = g_malloc(sizeof(table_pair)); | |
38 pair->key = g_strdup(key); | |
39 pair->value = (gpointer)(g_strdup(value)); | |
40 | |
41 return pair; | |
42 } | |
43 | |
44 table_pair *parse_table_pair(gchar *line, char delim) | |
45 { | |
46 gchar buf[256]; | |
47 gchar *p, *q; | |
48 table_pair *pair; | |
49 | |
50 p = line; | |
51 q = buf; | |
52 while((*p != 0) && (*p != delim) && q < buf+255) | |
53 *(q++) = *(p++); | |
54 *q = 0; | |
55 | |
56 pair = g_malloc(sizeof(table_pair)); | |
57 pair->key = g_strdup(g_strstrip(buf)); | |
58 | |
59 if(*p){ | |
60 p++; | |
61 /* while(isspace(*p)) p++; */ | |
62 pair->value = (gpointer *)(g_strdup(g_strstrip(p))); | |
63 }else | |
64 pair->value = (gpointer *)g_strdup(""); | |
65 | |
66 return pair; | |
67 } | |
68 | |
69 gpointer *table_find_func(GList *table_list, gchar *key, int (*cmp_func)(const char *, const char *)) | |
70 { | |
71 GList *node; | |
72 | |
73 foreach(table_list, node){ | |
74 table_pair *pair = (table_pair *)(node->data); | |
75 if(cmp_func(pair->key, key) == 0) | |
76 return pair->value; | |
77 } | |
78 return NULL; | |
79 } | |
80 | |
81 gpointer *table_find(GList *table_list, gchar *key) | |
82 { | |
83 return table_find_func(table_list, key, strcmp); | |
84 } | |
85 | |
86 gpointer *table_find_case(GList *table_list, gchar *key) | |
87 { | |
88 return table_find_func(table_list, key, strcasecmp); | |
89 } | |
90 | |
91 static | |
92 int fnmatch0(const char *pattern, const char *string) | |
93 { | |
94 return fnmatch(pattern, string, 0); | |
95 } | |
96 | |
97 gpointer *table_find_fnmatch(GList *table_list, gchar *key) | |
98 { | |
99 return table_find_func(table_list, key, fnmatch0); | |
100 } | |
101 | |
102 GList *table_read(gchar *fname, gchar delim) | |
103 { | |
104 GList *list = NULL; | |
105 FILE *fptr; | |
106 | |
107 if((fptr = fopen(fname, "rt"))){ | |
108 gchar buf[256]; | |
109 | |
110 while(fgets(buf, 255, fptr)){ | |
111 if(buf[0] && (buf[0] != '#') && (buf[0] != '\n')){ | |
112 table_pair *pair; | |
113 g_strchomp(buf); | |
114 pair = parse_table_pair(buf, delim); | |
115 list = g_list_append(list, pair); | |
116 } | |
117 } | |
118 fclose(fptr); | |
119 return list; | |
120 } | |
121 logwrite(LOG_ALERT, "could not open table file %s: %s\n", fname, strerror(errno)); | |
122 | |
123 return NULL; | |
124 } | |
125 | |
126 void destroy_table(GList *table) | |
127 { | |
128 GList *node; | |
129 | |
130 foreach(table, node){ | |
131 table_pair *p = (table_pair *)(node->data); | |
132 g_free(p->key); | |
133 g_free(p->value); | |
134 g_free(p); | |
135 } | |
136 g_list_free(table); | |
137 } | |
138 |