masqmail-0.2
diff src/tables.c @ 0:08114f7dcc23
this is masqmail-0.2.21 from oliver kurth
author | meillo@marmaro.de |
---|---|
date | Fri, 26 Sep 2008 17:05:23 +0200 |
parents | |
children | 26e34ae9a3e3 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/tables.c Fri Sep 26 17:05:23 2008 +0200 1.3 @@ -0,0 +1,138 @@ 1.4 +/* MasqMail 1.5 + Copyright (C) 1999-2001 Oliver Kurth 1.6 + 1.7 + This program is free software; you can redistribute it and/or modify 1.8 + it under the terms of the GNU General Public License as published by 1.9 + the Free Software Foundation; either version 2 of the License, or 1.10 + (at your option) any later version. 1.11 + 1.12 + This program is distributed in the hope that it will be useful, 1.13 + but WITHOUT ANY WARRANTY; without even the implied warranty of 1.14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.15 + GNU General Public License for more details. 1.16 + 1.17 + You should have received a copy of the GNU General Public License 1.18 + along with this program; if not, write to the Free Software 1.19 + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1.20 +*/ 1.21 + 1.22 +#include "masqmail.h" 1.23 +#include <fnmatch.h> 1.24 + 1.25 +table_pair *create_pair(gchar *key, gpointer value) 1.26 +{ 1.27 + table_pair *pair; 1.28 + 1.29 + pair = g_malloc(sizeof(table_pair)); 1.30 + pair->key = g_strdup(key); 1.31 + pair->value = value; 1.32 + 1.33 + return pair; 1.34 +} 1.35 + 1.36 +table_pair *create_pair_string(gchar *key, gpointer value) 1.37 +{ 1.38 + table_pair *pair; 1.39 + 1.40 + pair = g_malloc(sizeof(table_pair)); 1.41 + pair->key = g_strdup(key); 1.42 + pair->value = (gpointer)(g_strdup(value)); 1.43 + 1.44 + return pair; 1.45 +} 1.46 + 1.47 +table_pair *parse_table_pair(gchar *line, char delim) 1.48 +{ 1.49 + gchar buf[256]; 1.50 + gchar *p, *q; 1.51 + table_pair *pair; 1.52 + 1.53 + p = line; 1.54 + q = buf; 1.55 + while((*p != 0) && (*p != delim) && q < buf+255) 1.56 + *(q++) = *(p++); 1.57 + *q = 0; 1.58 + 1.59 + pair = g_malloc(sizeof(table_pair)); 1.60 + pair->key = g_strdup(g_strstrip(buf)); 1.61 + 1.62 + if(*p){ 1.63 + p++; 1.64 + /* while(isspace(*p)) p++; */ 1.65 + pair->value = (gpointer *)(g_strdup(g_strstrip(p))); 1.66 + }else 1.67 + pair->value = (gpointer *)g_strdup(""); 1.68 + 1.69 + return pair; 1.70 +} 1.71 + 1.72 +gpointer *table_find_func(GList *table_list, gchar *key, int (*cmp_func)(const char *, const char *)) 1.73 +{ 1.74 + GList *node; 1.75 + 1.76 + foreach(table_list, node){ 1.77 + table_pair *pair = (table_pair *)(node->data); 1.78 + if(cmp_func(pair->key, key) == 0) 1.79 + return pair->value; 1.80 + } 1.81 + return NULL; 1.82 +} 1.83 + 1.84 +gpointer *table_find(GList *table_list, gchar *key) 1.85 +{ 1.86 + return table_find_func(table_list, key, strcmp); 1.87 +} 1.88 + 1.89 +gpointer *table_find_case(GList *table_list, gchar *key) 1.90 +{ 1.91 + return table_find_func(table_list, key, strcasecmp); 1.92 +} 1.93 + 1.94 +static 1.95 +int fnmatch0(const char *pattern, const char *string) 1.96 +{ 1.97 + return fnmatch(pattern, string, 0); 1.98 +} 1.99 + 1.100 +gpointer *table_find_fnmatch(GList *table_list, gchar *key) 1.101 +{ 1.102 + return table_find_func(table_list, key, fnmatch0); 1.103 +} 1.104 + 1.105 +GList *table_read(gchar *fname, gchar delim) 1.106 +{ 1.107 + GList *list = NULL; 1.108 + FILE *fptr; 1.109 + 1.110 + if((fptr = fopen(fname, "rt"))){ 1.111 + gchar buf[256]; 1.112 + 1.113 + while(fgets(buf, 255, fptr)){ 1.114 + if(buf[0] && (buf[0] != '#') && (buf[0] != '\n')){ 1.115 + table_pair *pair; 1.116 + g_strchomp(buf); 1.117 + pair = parse_table_pair(buf, delim); 1.118 + list = g_list_append(list, pair); 1.119 + } 1.120 + } 1.121 + fclose(fptr); 1.122 + return list; 1.123 + } 1.124 + logwrite(LOG_ALERT, "could not open table file %s: %s\n", fname, strerror(errno)); 1.125 + 1.126 + return NULL; 1.127 +} 1.128 + 1.129 +void destroy_table(GList *table) 1.130 +{ 1.131 + GList *node; 1.132 + 1.133 + foreach(table, node){ 1.134 + table_pair *p = (table_pair *)(node->data); 1.135 + g_free(p->key); 1.136 + g_free(p->value); 1.137 + g_free(p); 1.138 + } 1.139 + g_list_free(table); 1.140 +} 1.141 +