comparison src/tables.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children f671821d8222
comparison
equal deleted inserted replaced
9:31cc8a89cb74 10:26e34ae9a3e3
17 */ 17 */
18 18
19 #include "masqmail.h" 19 #include "masqmail.h"
20 #include <fnmatch.h> 20 #include <fnmatch.h>
21 21
22 table_pair *create_pair(gchar *key, gpointer value) 22 table_pair*
23 create_pair(gchar * key, gpointer value)
23 { 24 {
24 table_pair *pair; 25 table_pair *pair;
25
26 pair = g_malloc(sizeof(table_pair));
27 pair->key = g_strdup(key);
28 pair->value = value;
29 26
30 return pair; 27 pair = g_malloc(sizeof(table_pair));
28 pair->key = g_strdup(key);
29 pair->value = value;
30
31 return pair;
31 } 32 }
32 33
33 table_pair *create_pair_string(gchar *key, gpointer value) 34 table_pair*
35 create_pair_string(gchar * key, gpointer value)
34 { 36 {
35 table_pair *pair; 37 table_pair *pair;
36
37 pair = g_malloc(sizeof(table_pair));
38 pair->key = g_strdup(key);
39 pair->value = (gpointer)(g_strdup(value));
40 38
41 return pair; 39 pair = g_malloc(sizeof(table_pair));
40 pair->key = g_strdup(key);
41 pair->value = (gpointer) (g_strdup(value));
42
43 return pair;
42 } 44 }
43 45
44 table_pair *parse_table_pair(gchar *line, char delim) 46 table_pair*
47 parse_table_pair(gchar * line, char delim)
45 { 48 {
46 gchar buf[256]; 49 gchar buf[256];
47 gchar *p, *q; 50 gchar *p, *q;
48 table_pair *pair; 51 table_pair *pair;
49 52
50 p = line; 53 p = line;
51 q = buf; 54 q = buf;
52 while((*p != 0) && (*p != delim) && q < buf+255) 55 while ((*p != 0) && (*p != delim) && q < buf + 255)
53 *(q++) = *(p++); 56 *(q++) = *(p++);
54 *q = 0; 57 *q = 0;
55 58
56 pair = g_malloc(sizeof(table_pair)); 59 pair = g_malloc(sizeof(table_pair));
57 pair->key = g_strdup(g_strstrip(buf)); 60 pair->key = g_strdup(g_strstrip(buf));
58 61
59 if(*p){ 62 if (*p) {
60 p++; 63 p++;
61 /* while(isspace(*p)) p++; */ 64 /* while(isspace(*p)) p++; */
62 pair->value = (gpointer *)(g_strdup(g_strstrip(p))); 65 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
63 }else 66 } else
64 pair->value = (gpointer *)g_strdup(""); 67 pair->value = (gpointer *) g_strdup("");
65 68
66 return pair; 69 return pair;
67 } 70 }
68 71
69 gpointer *table_find_func(GList *table_list, gchar *key, int (*cmp_func)(const char *, const char *)) 72 gpointer*
73 table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *))
70 { 74 {
71 GList *node; 75 GList *node;
72 76
73 foreach(table_list, node){ 77 foreach(table_list, node) {
74 table_pair *pair = (table_pair *)(node->data); 78 table_pair *pair = (table_pair *) (node->data);
75 if(cmp_func(pair->key, key) == 0) 79 if (cmp_func(pair->key, key) == 0)
76 return pair->value; 80 return pair->value;
77 } 81 }
78 return NULL; 82 return NULL;
79 } 83 }
80 84
81 gpointer *table_find(GList *table_list, gchar *key) 85 gpointer*
86 table_find(GList * table_list, gchar * key)
82 { 87 {
83 return table_find_func(table_list, key, strcmp); 88 return table_find_func(table_list, key, strcmp);
84 } 89 }
85 90
86 gpointer *table_find_case(GList *table_list, gchar *key) 91 gpointer*
92 table_find_case(GList * table_list, gchar * key)
87 { 93 {
88 return table_find_func(table_list, key, strcasecmp); 94 return table_find_func(table_list, key, strcasecmp);
89 } 95 }
90 96
91 static 97 static int
92 int fnmatch0(const char *pattern, const char *string) 98 fnmatch0(const char *pattern, const char *string)
93 { 99 {
94 return fnmatch(pattern, string, 0); 100 return fnmatch(pattern, string, 0);
95 } 101 }
96 102
97 gpointer *table_find_fnmatch(GList *table_list, gchar *key) 103 gpointer*
104 table_find_fnmatch(GList * table_list, gchar * key)
98 { 105 {
99 return table_find_func(table_list, key, fnmatch0); 106 return table_find_func(table_list, key, fnmatch0);
100 } 107 }
101 108
102 GList *table_read(gchar *fname, gchar delim) 109 GList*
110 table_read(gchar * fname, gchar delim)
103 { 111 {
104 GList *list = NULL; 112 GList *list = NULL;
105 FILE *fptr; 113 FILE *fptr;
106 114
107 if((fptr = fopen(fname, "rt"))){ 115 if ((fptr = fopen(fname, "rt"))) {
108 gchar buf[256]; 116 gchar buf[256];
109 117
110 while(fgets(buf, 255, fptr)){ 118 while (fgets(buf, 255, fptr)) {
111 if(buf[0] && (buf[0] != '#') && (buf[0] != '\n')){ 119 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
112 table_pair *pair; 120 table_pair *pair;
113 g_strchomp(buf); 121 g_strchomp(buf);
114 pair = parse_table_pair(buf, delim); 122 pair = parse_table_pair(buf, delim);
115 list = g_list_append(list, pair); 123 list = g_list_append(list, pair);
116 } 124 }
117 } 125 }
118 fclose(fptr); 126 fclose(fptr);
119 return list; 127 return list;
120 } 128 }
121 logwrite(LOG_ALERT, "could not open table file %s: %s\n", fname, strerror(errno)); 129 logwrite(LOG_ALERT, "could not open table file %s: %s\n", fname, strerror(errno));
122 130
123 return NULL; 131 return NULL;
124 } 132 }
125 133
126 void destroy_table(GList *table) 134 void
135 destroy_table(GList * table)
127 { 136 {
128 GList *node; 137 GList *node;
129 138
130 foreach(table, node){ 139 foreach(table, node) {
131 table_pair *p = (table_pair *)(node->data); 140 table_pair *p = (table_pair *) (node->data);
132 g_free(p->key); 141 g_free(p->key);
133 g_free(p->value); 142 g_free(p->value);
134 g_free(p); 143 g_free(p);
135 } 144 }
136 g_list_free(table); 145 g_list_free(table);
137 } 146 }
138