masqmail-0.2

view src/tables.c @ 5:3cafdebd1479

removed remaining reference to docs in Makefile
author meillo@marmaro.de
date Fri, 26 Sep 2008 22:15:26 +0200
parents
children 26e34ae9a3e3
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 *create_pair(gchar *key, gpointer value)
23 {
24 table_pair *pair;
26 pair = g_malloc(sizeof(table_pair));
27 pair->key = g_strdup(key);
28 pair->value = value;
30 return pair;
31 }
33 table_pair *create_pair_string(gchar *key, gpointer value)
34 {
35 table_pair *pair;
37 pair = g_malloc(sizeof(table_pair));
38 pair->key = g_strdup(key);
39 pair->value = (gpointer)(g_strdup(value));
41 return pair;
42 }
44 table_pair *parse_table_pair(gchar *line, char delim)
45 {
46 gchar buf[256];
47 gchar *p, *q;
48 table_pair *pair;
50 p = line;
51 q = buf;
52 while((*p != 0) && (*p != delim) && q < buf+255)
53 *(q++) = *(p++);
54 *q = 0;
56 pair = g_malloc(sizeof(table_pair));
57 pair->key = g_strdup(g_strstrip(buf));
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("");
66 return pair;
67 }
69 gpointer *table_find_func(GList *table_list, gchar *key, int (*cmp_func)(const char *, const char *))
70 {
71 GList *node;
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 }
81 gpointer *table_find(GList *table_list, gchar *key)
82 {
83 return table_find_func(table_list, key, strcmp);
84 }
86 gpointer *table_find_case(GList *table_list, gchar *key)
87 {
88 return table_find_func(table_list, key, strcasecmp);
89 }
91 static
92 int fnmatch0(const char *pattern, const char *string)
93 {
94 return fnmatch(pattern, string, 0);
95 }
97 gpointer *table_find_fnmatch(GList *table_list, gchar *key)
98 {
99 return table_find_func(table_list, key, fnmatch0);
100 }
102 GList *table_read(gchar *fname, gchar delim)
103 {
104 GList *list = NULL;
105 FILE *fptr;
107 if((fptr = fopen(fname, "rt"))){
108 gchar buf[256];
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));
123 return NULL;
124 }
126 void destroy_table(GList *table)
127 {
128 GList *node;
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 }