masqmail

diff 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
line diff
     1.1 --- a/src/tables.c	Thu Feb 16 11:22:31 2012 +0100
     1.2 +++ b/src/tables.c	Thu Feb 16 13:34:44 2012 +0100
     1.3 @@ -19,8 +19,8 @@
     1.4  */
     1.5  
     1.6  #include <fnmatch.h>
     1.7 +#include "masqmail.h"
     1.8  
     1.9 -#include "masqmail.h"
    1.10  
    1.11  table_pair*
    1.12  create_pair(gchar *key, gpointer value)
    1.13 @@ -55,8 +55,9 @@
    1.14  
    1.15  	p = line;
    1.16  	q = buf;
    1.17 -	while ((*p != '\0') && (*p != delim) && q < buf + 255)
    1.18 +	while (*p && (*p != delim) && q < buf + 255) {
    1.19  		*(q++) = *(p++);
    1.20 +	}
    1.21  	*q = '\0';
    1.22  
    1.23  	pair = g_malloc(sizeof(table_pair));
    1.24 @@ -64,24 +65,26 @@
    1.25  
    1.26  	if (*p) {
    1.27  		p++;
    1.28 -		/*    while(isspace(*p)) p++; */
    1.29 +		/* while(isspace(*p)) p++; */
    1.30  		pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
    1.31 -	} else
    1.32 +	} else {
    1.33  		pair->value = (gpointer *) g_strdup("");
    1.34 +	}
    1.35  
    1.36  	return pair;
    1.37  }
    1.38  
    1.39  gpointer*
    1.40 -table_find_func(GList *table_list, gchar *key, int (*cmp_func) (const char *,
    1.41 -		const char *))
    1.42 +table_find_func(GList *table_list, gchar *key,
    1.43 +		int (*cmp_func) (const char *, const char *))
    1.44  {
    1.45  	GList *node;
    1.46  
    1.47  	foreach(table_list, node) {
    1.48  		table_pair *pair = (table_pair *) (node->data);
    1.49 -		if (cmp_func(pair->key, key) == 0)
    1.50 +		if (cmp_func(pair->key, key) == 0) {
    1.51  			return pair->value;
    1.52 +		}
    1.53  	}
    1.54  	return NULL;
    1.55  }
    1.56 @@ -115,27 +118,30 @@
    1.57  {
    1.58  	GList *list = NULL;
    1.59  	FILE *fptr;
    1.60 +	gchar buf[256];
    1.61  
    1.62 -	if ((fptr = fopen(fname, "rt"))) {
    1.63 -		gchar buf[256];
    1.64 +	if (!(fptr = fopen(fname, "rt"))) {
    1.65 +		logwrite(LOG_ALERT, "could not open table file %s: %s. Thus "
    1.66 +				"no aliasing will be done\n",
    1.67 +				fname, strerror(errno));
    1.68 +		return NULL;
    1.69 +	}
    1.70  
    1.71 -		while (fgets(buf, 255, fptr)) {
    1.72 -			if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
    1.73 -				table_pair *pair;
    1.74 -				g_strchomp(buf);
    1.75 -				pair = parse_table_pair(buf, delim);
    1.76 -				list = g_list_append(list, pair);
    1.77 -			}
    1.78 +	while (fgets(buf, sizeof buf, fptr)) {
    1.79 +		if (!*buf || *buf == '#' || *buf == '\n') {
    1.80 +			continue;
    1.81  		}
    1.82 -		fclose(fptr);
    1.83 -		if (!list)
    1.84 -			logwrite(LOG_NOTICE, "table file %s contained no entries\n", fname);
    1.85 -		return list;
    1.86 +		table_pair *pair;
    1.87 +		g_strchomp(buf);
    1.88 +		pair = parse_table_pair(buf, delim);
    1.89 +		list = g_list_append(list, pair);
    1.90  	}
    1.91 -	logwrite(LOG_ALERT, "could not open table file %s: %s."
    1.92 -	                    " Thus no aliasing will be done\n", fname, strerror(errno));
    1.93 -
    1.94 -	return NULL;
    1.95 +	fclose(fptr);
    1.96 +	if (!list) {
    1.97 +		logwrite(LOG_NOTICE, "table file %s contained no entries\n",
    1.98 +				fname);
    1.99 +	}
   1.100 +	return list;
   1.101  }
   1.102  
   1.103  void