masqmail-0.2

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