diff options
| author | Marcel Holtmann <marcel@holtmann.org> | 2006-08-31 08:31:39 +0000 | 
|---|---|---|
| committer | Marcel Holtmann <marcel@holtmann.org> | 2006-08-31 08:31:39 +0000 | 
| commit | 406212bb1c90b4454167d0ed9984d4f6c6a4efe0 (patch) | |
| tree | cd6d3c5c27be86b431094eaf1424d938a5f28dd9 /common | |
| parent | 75dc7cdf39d34c98244c7b98340a1541c421ed68 (diff) | |
Add textfile_caseget() function
Diffstat (limited to 'common')
| -rw-r--r-- | common/textfile.c | 51 | ||||
| -rw-r--r-- | common/textfile.h | 1 | 
2 files changed, 34 insertions, 18 deletions
| diff --git a/common/textfile.c b/common/textfile.c index f8b9545e..d76398c4 100644 --- a/common/textfile.c +++ b/common/textfile.c @@ -25,6 +25,7 @@  #include <config.h>  #endif +#define _GNU_SOURCE  #include <stdio.h>  #include <errno.h>  #include <fcntl.h> @@ -93,13 +94,17 @@ int create_name(char *buf, size_t size, const char *path, const char *address, c  	return snprintf(buf, size, "%s/%s/%s", path, address, name);  } -static inline char *find_key(char *map, const char *key, size_t len) +static inline char *find_key(char *map, const char *key, size_t len, int icase)  { -	char *off = strstr(map, key); +	char *off = (icase) ? strcasestr(map, key) : strstr(map, key);  	while (off && ((off > map && *(off - 1) != '\r' && -				*(off - 1) != '\n') || *(off + len) != ' ')) -		off = strstr(off + len, key); +				*(off - 1) != '\n') || *(off + len) != ' ')) { +		if (icase) +			off = strcasestr(off + len, key); +		else +			off = strstr(off + len, key); +	}  	return off;  } @@ -126,7 +131,7 @@ static inline int write_key_value(int fd, const char *key, const char *value)  	return err;  } -static int write_key(const char *pathname, const char *key, const char *value) +static int write_key(const char *pathname, const char *key, const char *value, int icase)  {  	struct stat st;  	char *map, *off, *end, *str; @@ -163,7 +168,7 @@ static int write_key(const char *pathname, const char *key, const char *value)  		goto unlock;  	} -	off = find_key(map, key, strlen(key)); +	off = find_key(map, key, strlen(key), icase);  	if (!off) {  		if (value) {  			munmap(map, size); @@ -240,17 +245,7 @@ close:  	return -err;  } -int textfile_put(const char *pathname, const char *key, const char *value) -{ -	return write_key(pathname, key, value); -} - -int textfile_del(const char *pathname, const char *key) -{ -	return write_key(pathname, key, NULL); -} - -char *textfile_get(const char *pathname, const char *key) +static char *read_key(const char *pathname, const char *key, int icase)  {  	struct stat st;  	char *map, *off, *end, *str = NULL; @@ -280,7 +275,7 @@ char *textfile_get(const char *pathname, const char *key)  	}  	len = strlen(key); -	off = find_key(map, key, len); +	off = find_key(map, key, len, icase);  	if (!off) {  		err = EILSEQ;  		goto unmap; @@ -314,6 +309,26 @@ close:  	return str;  } +int textfile_put(const char *pathname, const char *key, const char *value) +{ +	return write_key(pathname, key, value, 0); +} + +int textfile_del(const char *pathname, const char *key) +{ +	return write_key(pathname, key, NULL, 0); +} + +char *textfile_get(const char *pathname, const char *key) +{ +	return read_key(pathname, key, 0); +} + +char *textfile_caseget(const char *pathname, const char *key) +{ +	return read_key(pathname, key, 1); +} +  int textfile_foreach(const char *pathname,  		void (*func)(char *key, char *value, void *data), void *data)  { diff --git a/common/textfile.h b/common/textfile.h index 74e67eca..d89720d7 100644 --- a/common/textfile.h +++ b/common/textfile.h @@ -32,6 +32,7 @@ int create_name(char *buf, size_t size, const char *path,  int textfile_put(const char *pathname, const char *key, const char *value);  int textfile_del(const char *pathname, const char *key);  char *textfile_get(const char *pathname, const char *key); +char *textfile_caseget(const char *pathname, const char *key);  int textfile_foreach(const char *pathname,  		void (*func)(char *key, char *value, void *data), void *data); | 
