Add textfile_caseget() function

This commit is contained in:
Marcel Holtmann 2006-08-31 08:31:39 +00:00
parent 75dc7cdf39
commit 406212bb1c
2 changed files with 34 additions and 18 deletions

View File

@ -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)
{

View File

@ -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);