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