diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2006-02-07 09:10:07 +0000 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2006-02-07 09:10:07 +0000 |
commit | 71dc4067220c5b994885f63127b4b7eb947002fd (patch) | |
tree | 993e1bcb31145dd26f525be0028ce142ce29f72a /common | |
parent | b7d130c68efdeea62d459e5c6c968c89412d9f46 (diff) |
Add support for deleting keys
Diffstat (limited to 'common')
-rw-r--r-- | common/test_textfile.c | 86 | ||||
-rw-r--r-- | common/textfile.c | 48 | ||||
-rw-r--r-- | common/textfile.h | 1 |
3 files changed, 116 insertions, 19 deletions
diff --git a/common/test_textfile.c b/common/test_textfile.c index 80761526..c2b2c993 100644 --- a/common/test_textfile.c +++ b/common/test_textfile.c @@ -38,12 +38,13 @@ int main(int argc, char *argv[]) { char filename[] = "/tmp/textfile"; char key[18], value[512], *str; - int i, j, fd; + int i, j, fd, max = 10; fd = creat(filename, 0644); close(fd); - for (i = 1; i < 101; i++) { + + for (i = 1; i < max + 1; i++) { sprintf(key, "00:00:00:00:00:%02X", i); memset(value, 0, sizeof(value)); @@ -64,5 +65,86 @@ int main(int argc, char *argv[]) free(str); } + + sprintf(key, "00:00:00:00:00:%02X", max); + + memset(value, 0, sizeof(value)); + for (j = 0; j < max; j++) + value[j] = 'y'; + + if (textfile_put(filename, key, value) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + sprintf(key, "00:00:00:00:00:%02X", 1); + + memset(value, 0, sizeof(value)); + for (j = 0; j < max; j++) + value[j] = 'z'; + + if (textfile_put(filename, key, value) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + printf("\n"); + + for (i = 1; i < max + 1; i++) { + sprintf(key, "00:00:00:00:00:%02X", i); + + str = textfile_get(filename, key); + if (str) { + printf("%s %s\n", key, str); + free(str); + } + } + + + sprintf(key, "00:00:00:00:00:%02X", 2); + + if (textfile_del(filename, key) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + sprintf(key, "00:00:00:00:00:%02X", max - 3); + + if (textfile_del(filename, key) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + printf("\n"); + + for (i = 1; i < max + 1; i++) { + sprintf(key, "00:00:00:00:00:%02X", i); + + str = textfile_get(filename, key); + if (str) { + printf("%s %s\n", key, str); + free(str); + } + } + + sprintf(key, "00:00:00:00:00:%02X", 1); + + if (textfile_del(filename, key) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + sprintf(key, "00:00:00:00:00:%02X", max); + + if (textfile_del(filename, key) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + sprintf(key, "00:00:00:00:00:%02X", max + 1); + + if (textfile_del(filename, key) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + printf("\n"); + + for (i = 1; i < max + 1; i++) { + sprintf(key, "00:00:00:00:00:%02X", i); + + str = textfile_get(filename, key); + if (str) { + printf("%s %s\n", key, str); + free(str); + } + } + return 0; } diff --git a/common/textfile.c b/common/textfile.c index ec445b1a..3aa74d9a 100644 --- a/common/textfile.c +++ b/common/textfile.c @@ -86,6 +86,17 @@ int create_file(char *filename, mode_t mode) return 0; } +static inline char *find_key(char *map, char *key, size_t len) +{ + char *off = strstr(map, key); + + while (off && ((off > map && *(off - 1) != '\r' && + *(off - 1) != '\n') || *(off + len) != ' ')) + off = strstr(off + len, key); + + return off; +} + static inline int write_key_value(int fd, char *key, char *value) { char *str; @@ -108,18 +119,7 @@ static inline int write_key_value(int fd, char *key, char *value) return err; } -static inline char *find_key(char *map, char *key, size_t len) -{ - char *off = strstr(map, key); - - while (off && ((off > map && *(off - 1) != '\r' && - *(off - 1) != '\n') || *(off + len) != ' ')) - off = strstr(off + len, key); - - return off; -} - -int textfile_put(char *pathname, char *key, char *value) +static int write_key(char *pathname, char *key, char *value) { struct stat st; char *map, *off, *end, *str; @@ -156,9 +156,11 @@ int textfile_put(char *pathname, char *key, char *value) off = find_key(map, key, strlen(key)); if (!off) { - munmap(map, size); - pos = lseek(fd, size, SEEK_SET); - err = write_key_value(fd, key, value); + if (value) { + munmap(map, size); + pos = lseek(fd, size, SEEK_SET); + err = write_key_value(fd, key, value); + } goto unlock; } @@ -178,7 +180,8 @@ int textfile_put(char *pathname, char *key, char *value) munmap(map, size); ftruncate(fd, base); pos = lseek(fd, base, SEEK_SET); - err = write_key_value(fd, key, value); + if (value) + err = write_key_value(fd, key, value); goto unlock; } @@ -198,7 +201,8 @@ int textfile_put(char *pathname, char *key, char *value) munmap(map, size); ftruncate(fd, base); pos = lseek(fd, base, SEEK_SET); - err = write_key_value(fd, key, value); + if (value) + err = write_key_value(fd, key, value); write(fd, str, len); @@ -219,6 +223,16 @@ close: return -err; } +int textfile_put(char *pathname, char *key, char *value) +{ + return write_key(pathname, key, value); +} + +int textfile_del(char *pathname, char *key) +{ + return write_key(pathname, key, NULL); +} + char *textfile_get(char *pathname, char *key) { struct stat st; diff --git a/common/textfile.h b/common/textfile.h index 803fc231..62ad4b80 100644 --- a/common/textfile.h +++ b/common/textfile.h @@ -25,4 +25,5 @@ int create_dirs(char *filename, mode_t mode); int create_file(char *filename, mode_t mode); int textfile_put(char *pathname, char *key, char *value); +int textfile_del(char *pathname, char *key); char *textfile_get(char *pathname, char *key); |