From 45e29cda8aa89b0db32e5954ffb8e1fb67c3df79 Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Sun, 6 May 2007 23:12:29 +0000 Subject: [PATCH] emile_config_get() can manage many types, given by tag id --- tools/emile_config.c | 56 ++++++++++++++++++++++++++++---------------- tools/emile_config.h | 2 +- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/tools/emile_config.c b/tools/emile_config.c index 1bd94d2..7ecf759 100644 --- a/tools/emile_config.c +++ b/tools/emile_config.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "emile_config.h" @@ -95,16 +96,12 @@ static int read_line(FILE* fd, char *name, char *value) value[index] = 0; } -static char* set_tag(char* string, int tag, char* value) +static char* set_tag(char* string, int tag, int len, void* data) { - int len; - - len = strlen(value); *string++ = tag; - *string++ = len + 2; - strcpy(string, value); + *string++ = len + 1; + memcpy(string, data, len); string += len; - *string++ = 0; *string = CONFIG_END; /* mark end of string */ @@ -137,23 +134,25 @@ static int read_header(FILE* fd, char* header, int size) { if (strcmp("partition", name) == 0) { - header = set_tag(header, CONFIG_PARTITION, value); + header = set_tag(header, CONFIG_PARTITION, strlen(value) + 1, value); } else if (strcmp("first_level", name) == 0) { - header = set_tag(header, CONFIG_FIRST_LEVEL, value); + header = set_tag(header, CONFIG_FIRST_LEVEL, strlen(value) + 1, value); } else if (strcmp("second_level", name) == 0) { - header = set_tag(header, CONFIG_SECOND_LEVEL, value); + header = set_tag(header, CONFIG_SECOND_LEVEL, strlen(value) + 1, value); } else if (strcmp("timeout", name) == 0) { - header = set_tag(header, CONFIG_TIMEOUT, value); + int v = atoi(value);; + header = set_tag(header, CONFIG_TIMEOUT, sizeof(int), &v); } else if (strcmp("default", name) == 0) { - header = set_tag(header, CONFIG_DEFAULT, value); + int v = atoi(value);; + header = set_tag(header, CONFIG_DEFAULT, sizeof(int), &v); } else if (strcmp("title", name) == 0) { @@ -238,19 +237,19 @@ static int read_description(FILE* fd, char* desc, int size) break; } found = 1; - desc = set_tag(desc, CONFIG_TITLE, value); + desc = set_tag(desc, CONFIG_TITLE, strlen(value) + 1, value); } else if (strcmp("kernel", name) == 0) { - desc = set_tag(desc, CONFIG_KERNEL, value); + desc = set_tag(desc, CONFIG_KERNEL, strlen(value) + 1, value); } else if (strcmp("args", name) == 0) { - desc = set_tag(desc, CONFIG_ARGS, value); + desc = set_tag(desc, CONFIG_ARGS, strlen(value) + 1, value); } else if (strcmp("initrd", name) == 0) { - desc = set_tag(desc, CONFIG_INITRD, value); + desc = set_tag(desc, CONFIG_INITRD, strlen(value) + 1, value); } else { @@ -286,21 +285,38 @@ int emile_config_read_first_entry(emile_config* config) return read_description(config->fd, config->current, config->current_size); } -char* emile_config_get(emile_config* config, int tag) +int emile_config_get(emile_config* config, int tag, ...) { + int ret = -1; + va_list arg; + char **s; + int *v; + + va_start(arg, tag); switch(tag) { case CONFIG_PARTITION: case CONFIG_FIRST_LEVEL: case CONFIG_SECOND_LEVEL: + s = va_arg(arg, char**); + *s = get_tag(config->header, tag); + ret = 0; + break; case CONFIG_TIMEOUT: case CONFIG_DEFAULT: - return get_tag(config->header, tag); + v = va_arg(arg, int*); + *v = *(int*)get_tag(config->header, tag); + ret = 0; + break; case CONFIG_TITLE: case CONFIG_KERNEL: case CONFIG_ARGS: case CONFIG_INITRD: - return get_tag(config->current, tag); + s = va_arg(arg, char**); + *s = get_tag(config->current, tag); + ret = 0; + break; } - return NULL; + va_end(arg); + return -1; } diff --git a/tools/emile_config.h b/tools/emile_config.h index a8e73eb..b0b1cc4 100644 --- a/tools/emile_config.h +++ b/tools/emile_config.h @@ -31,4 +31,4 @@ extern void emile_config_close(emile_config* config); extern int emile_config_read_first_entry(emile_config* config); extern int emile_config_read_next(emile_config* config); -extern char* emile_config_get(emile_config* config, int tag); +extern int emile_config_get(emile_config* config, int tag, ...);