masqmail

annotate src/tables.c @ 225:589c365d90b1

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