From efaf856a4466776fb845e95ad60fb7edda80ee19 Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Sat, 25 Aug 2007 21:31:10 +0000 Subject: [PATCH] First release --- libconfig/Makefile | 36 +++++++++++ libconfig/config_get_indexed_property.c | 40 ++++++++++++ libconfig/config_get_next_property.c | 72 ++++++++++++++++++++++ libconfig/config_get_property.c | 14 +++++ libconfig/config_remove_indexed_property.c | 52 ++++++++++++++++ libconfig/config_remove_property.c | 15 +++++ libconfig/config_set_indexed_property.c | 58 +++++++++++++++++ libconfig/config_set_property.c | 15 +++++ libconfig/libconfig.h | 14 +++++ 9 files changed, 316 insertions(+) create mode 100644 libconfig/Makefile create mode 100644 libconfig/config_get_indexed_property.c create mode 100644 libconfig/config_get_next_property.c create mode 100644 libconfig/config_get_property.c create mode 100644 libconfig/config_remove_indexed_property.c create mode 100644 libconfig/config_remove_property.c create mode 100644 libconfig/config_set_indexed_property.c create mode 100644 libconfig/config_set_property.c create mode 100644 libconfig/libconfig.h diff --git a/libconfig/Makefile b/libconfig/Makefile new file mode 100644 index 0000000..672c0bb --- /dev/null +++ b/libconfig/Makefile @@ -0,0 +1,36 @@ +# +# (c) 2005-2007 Laurent Vivier +# + +TOP = $(shell pwd) +VPATH=$(TOP) + +AS=$(CROSS_COMPILE)as +CC=$(CROSS_COMPILE)gcc +LD=$(CROSS_COMPILE)ld +AR=$(CROSS_COMPILE)ar + +TARGET = native +CFLAGS += -nostdlib -nodefaultlibs -Wall -Werror -Wno-multichar +ifeq ($(TARGET), m68k-linux) +68000FLAGS = -m68000 -Wa,-m68000 +CFLAGS += -fpic -O2 -Os +else +CFLAGS += -g +endif +CPPFLAGS = -I$(TOP)/../libstream + +LIBRARY = libconfig.a + +SOURCES = config_get_indexed_property.c config_get_next_property.c \ + config_get_property.c config_remove_property.c \ + config_set_property.c config_set_indexed_property.c \ + config_remove_indexed_property.c + +HEADERS = libconfig.h + +all: + test -d $(TARGET) || mkdir $(TARGET) + $(MAKE) -C $(TARGET) -f $(TOP)/Makefile $(LIBRARY) TOP=$(TOP) + +include $(TOP)/../Rules.mk diff --git a/libconfig/config_get_indexed_property.c b/libconfig/config_get_indexed_property.c new file mode 100644 index 0000000..4d2ca99 --- /dev/null +++ b/libconfig/config_get_indexed_property.c @@ -0,0 +1,40 @@ +/* + * + * (c) 2004-2007 Laurent Vivier + * + */ + +#include +#include + +#include "libconfig.h" + +int config_get_indexed_property(char *configuration, char *index_name, char *index_property, char *name, char *property) +{ + int found = (index_property == NULL); /* means not indexed */ + int index = 0; + char current_name[256]; + + while (1) + { + index = config_get_next_property(configuration, index, + current_name, property); + if (index == -1) + return -1; + if (found) + { + if ((index_name != NULL) && + (strcmp(index_name, current_name) == 0)) + return -1; + if (strcmp(name, current_name) == 0) + return 0; + } + else + { + if ( (strcmp(index_name, current_name) == 0) && + (strcmp(index_property, property) == 0) ) + found = 1; + } + } + return -1; +} diff --git a/libconfig/config_get_next_property.c b/libconfig/config_get_next_property.c new file mode 100644 index 0000000..267355a --- /dev/null +++ b/libconfig/config_get_next_property.c @@ -0,0 +1,72 @@ +/* + * + * (c) 2004-2007 Laurent Vivier + * + */ + +#include + +#include "libconfig.h" + +static inline char *read_line(char *s) +{ + int read = 0; + while (*s && (*s != '\n')) + { + read++; + s++; + } + if (*s == 0) + return s; + return s + 1; +} + +char *config_read_word(char *line, char **next) +{ + char *word; + + while ( (*line == ' ') || (*line == '\t') || (*line == '\n') ) + line++; + + word = line; + + while ( *line && (*line != ' ') && (*line != '\t') && (*line != '\n') ) + line++; + + *next = line; + + return word; +} + +int config_get_next_property(char *configuration, int index, char *name, char *property) +{ + char *next_word, *next_line; + char *current_name, *current_property; + + next_line = (char*)configuration + index; + if (*next_line == 0) + return -1; + next_word = next_line; + next_line = read_line(next_line); + + current_name = config_read_word(next_word, &next_word); + strncpy(name, current_name, next_word - current_name); + name[next_word - current_name] = 0; + + current_property = config_read_word(next_word, &next_word); + if (next_line - current_property != 0) + { + strncpy(property, current_property, next_line - current_property); + + /* remove '\n' if needed */ + + if (*(next_line - 1) == '\n') + property[next_line - current_property - 1] = 0; + else + property[next_line - current_property] = 0; + } + else + *property = 0; + + return next_line - (char*)configuration; +} diff --git a/libconfig/config_get_property.c b/libconfig/config_get_property.c new file mode 100644 index 0000000..dd22f95 --- /dev/null +++ b/libconfig/config_get_property.c @@ -0,0 +1,14 @@ +/* + * + * (c) 2004-2007 Laurent Vivier + * + */ + +#include + +#include "libconfig.h" + +int config_get_property(char *configuration, char *name, char *property) +{ + return config_get_indexed_property(configuration, NULL, NULL, name, property); +} diff --git a/libconfig/config_remove_indexed_property.c b/libconfig/config_remove_indexed_property.c new file mode 100644 index 0000000..abd1a5e --- /dev/null +++ b/libconfig/config_remove_indexed_property.c @@ -0,0 +1,52 @@ +/* + * + * (c) 2005 Laurent Vivier + * + */ + +#include +#include + +#include "libconfig.h" + +void config_remove_indexed_property(char *configuration, char *index_name, + char *index_property, char *name) +{ + int found = (index_property == NULL); /* means not indexed */ + int index = 0; + int last_index; + int current = 0; + char current_name[256]; + char current_property[256]; + + while (configuration[index]) + { + last_index = index; + index = config_get_next_property(configuration, + index, current_name, + current_property); + if (index == -1) + return; + if (found) + { + if (strcmp(name, current_name) != 0) + { + int len; + len = strlen(configuration + index); + memcpy(configuration + last_index, + configuration + index, len); + if (configuration[index + len - 1] == '\n') + len--; + configuration[index + len] = 0; + return; + } + } else { + if ( (strcmp(index_name, current_name) == 0) && + (strcmp(index_property, current_property) == 0) ) + found = 1; + } + } + if (configuration[current-1] == '\n') + current--; + configuration[current++] = 0; +} diff --git a/libconfig/config_remove_property.c b/libconfig/config_remove_property.c new file mode 100644 index 0000000..4769e14 --- /dev/null +++ b/libconfig/config_remove_property.c @@ -0,0 +1,15 @@ +/* + * + * (c) 2005 Laurent Vivier + * + */ + +#include +#include + +#include "libconfig.h" + +void config_remove_property(char *configuration, char *name) +{ + config_remove_indexed_property(configuration, name, NULL, NULL); +} diff --git a/libconfig/config_set_indexed_property.c b/libconfig/config_set_indexed_property.c new file mode 100644 index 0000000..23be7e2 --- /dev/null +++ b/libconfig/config_set_indexed_property.c @@ -0,0 +1,58 @@ +/* + * + * (c) 2005 Laurent Vivier + * + */ + +#include +#include + +#include "libconfig.h" + +void config_set_indexed_property(char *configuration, char *index_name, + char *index_property, char *name, + char *property) +{ + int found = (index_property == NULL); /* means not indexed */ + int index = 0; + int last_index; + char current_name[256]; + char current_property[256]; + + while (configuration[index]) + { + last_index = index; + index = config_get_next_property(configuration, index, + current_name, + current_property); + if (index == -1) + return; + if (found) + { + if (strcmp(name, current_name) != 0) + { + int len, len_new, len_old; + + len = strlen(configuration + index); + len_old = index - last_index; + len_new = strlen(name) + 1 + + strlen(property) + 1; + + memcpy(configuration + last_index + len_new, + configuration + index, len); + + sprintf(configuration + last_index, + "%s %s", name, property); + configuration[index + len_new] = '\n'; + if (configuration[index + len - 1] == '\n') + len--; + configuration[index + len] = 0; + return; + } + } else { + if ( (strcmp(index_name, current_name) == 0) && + (strcmp(index_property, property) == 0) ) + found = 1; + } + } +} diff --git a/libconfig/config_set_property.c b/libconfig/config_set_property.c new file mode 100644 index 0000000..3080806 --- /dev/null +++ b/libconfig/config_set_property.c @@ -0,0 +1,15 @@ +/* + * + * (c) 2005 Laurent Vivier + * + */ + +#include +#include + +#include "libconfig.h" + +void config_set_property(char *configuration, char *name, char *property) +{ + config_set_indexed_property(configuration, NULL, NULL, name, property); +} diff --git a/libconfig/libconfig.h b/libconfig/libconfig.h new file mode 100644 index 0000000..ad45b0f --- /dev/null +++ b/libconfig/libconfig.h @@ -0,0 +1,14 @@ +/* + * + * (c) 2007 Laurent Vivier + * + */ + +extern char *config_read_word(char *line, char **next); +extern int config_get_next_property(char *configuration, int index, char *name, char *property); +extern int config_get_indexed_property(char *configuration, char *index_name, char *index_property, char *name, char *property); +extern int config_get_property(char *configuration, char *name, char *property); +extern void config_remove_property(char *configuration, char *name); +extern void config_set_property(char *configuration, char *name, char *property); +extern void config_set_indexed_property(char *configuration, char *index_name, char *index_property, char *name, char *property); +extern void config_remove_indexed_property(char *configuration, char *index_name, char *index_property, char *name);