comparison src/tables.c @ 385:01769f722a18

Refactoring in tables.c.
author markus schnalke <meillo@marmaro.de>
date Thu, 16 Feb 2012 13:34:44 +0100
parents b27f66555ba8
children
comparison
equal deleted inserted replaced
384:4848c16ed1c1 385:01769f722a18
17 ** along with this program; if not, write to the Free Software 17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */ 19 */
20 20
21 #include <fnmatch.h> 21 #include <fnmatch.h>
22 #include "masqmail.h"
22 23
23 #include "masqmail.h"
24 24
25 table_pair* 25 table_pair*
26 create_pair(gchar *key, gpointer value) 26 create_pair(gchar *key, gpointer value)
27 { 27 {
28 table_pair *pair; 28 table_pair *pair;
53 gchar *p, *q; 53 gchar *p, *q;
54 table_pair *pair; 54 table_pair *pair;
55 55
56 p = line; 56 p = line;
57 q = buf; 57 q = buf;
58 while ((*p != '\0') && (*p != delim) && q < buf + 255) 58 while (*p && (*p != delim) && q < buf + 255) {
59 *(q++) = *(p++); 59 *(q++) = *(p++);
60 }
60 *q = '\0'; 61 *q = '\0';
61 62
62 pair = g_malloc(sizeof(table_pair)); 63 pair = g_malloc(sizeof(table_pair));
63 pair->key = g_strdup(g_strstrip(buf)); 64 pair->key = g_strdup(g_strstrip(buf));
64 65
65 if (*p) { 66 if (*p) {
66 p++; 67 p++;
67 /* while(isspace(*p)) p++; */ 68 /* while(isspace(*p)) p++; */
68 pair->value = (gpointer *) (g_strdup(g_strstrip(p))); 69 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
69 } else 70 } else {
70 pair->value = (gpointer *) g_strdup(""); 71 pair->value = (gpointer *) g_strdup("");
72 }
71 73
72 return pair; 74 return pair;
73 } 75 }
74 76
75 gpointer* 77 gpointer*
76 table_find_func(GList *table_list, gchar *key, int (*cmp_func) (const char *, 78 table_find_func(GList *table_list, gchar *key,
77 const char *)) 79 int (*cmp_func) (const char *, const char *))
78 { 80 {
79 GList *node; 81 GList *node;
80 82
81 foreach(table_list, node) { 83 foreach(table_list, node) {
82 table_pair *pair = (table_pair *) (node->data); 84 table_pair *pair = (table_pair *) (node->data);
83 if (cmp_func(pair->key, key) == 0) 85 if (cmp_func(pair->key, key) == 0) {
84 return pair->value; 86 return pair->value;
87 }
85 } 88 }
86 return NULL; 89 return NULL;
87 } 90 }
88 91
89 gpointer* 92 gpointer*
113 GList* 116 GList*
114 table_read(gchar *fname, gchar delim) 117 table_read(gchar *fname, gchar delim)
115 { 118 {
116 GList *list = NULL; 119 GList *list = NULL;
117 FILE *fptr; 120 FILE *fptr;
121 gchar buf[256];
118 122
119 if ((fptr = fopen(fname, "rt"))) { 123 if (!(fptr = fopen(fname, "rt"))) {
120 gchar buf[256]; 124 logwrite(LOG_ALERT, "could not open table file %s: %s. Thus "
125 "no aliasing will be done\n",
126 fname, strerror(errno));
127 return NULL;
128 }
121 129
122 while (fgets(buf, 255, fptr)) { 130 while (fgets(buf, sizeof buf, fptr)) {
123 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) { 131 if (!*buf || *buf == '#' || *buf == '\n') {
124 table_pair *pair; 132 continue;
125 g_strchomp(buf);
126 pair = parse_table_pair(buf, delim);
127 list = g_list_append(list, pair);
128 }
129 } 133 }
130 fclose(fptr); 134 table_pair *pair;
131 if (!list) 135 g_strchomp(buf);
132 logwrite(LOG_NOTICE, "table file %s contained no entries\n", fname); 136 pair = parse_table_pair(buf, delim);
133 return list; 137 list = g_list_append(list, pair);
134 } 138 }
135 logwrite(LOG_ALERT, "could not open table file %s: %s." 139 fclose(fptr);
136 " Thus no aliasing will be done\n", fname, strerror(errno)); 140 if (!list) {
137 141 logwrite(LOG_NOTICE, "table file %s contained no entries\n",
138 return NULL; 142 fname);
143 }
144 return list;
139 } 145 }
140 146
141 void 147 void
142 destroy_table(GList *table) 148 destroy_table(GList *table)
143 { 149 {