masqmail

annotate src/tables.c @ 367:b27f66555ba8

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