From ed8593760b3f1bf16ad0c6e8fb6eb1e4d20c0a38 Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Mon, 28 Nov 2005 20:55:20 +0000 Subject: [PATCH] Add new functions for EM06 --- libemile/emile_second_get_next_property.c | 54 +++++++++++++++++++++++ libemile/emile_second_get_property.c | 18 ++++++++ libemile/emile_second_set_property.c | 29 ++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 libemile/emile_second_get_next_property.c create mode 100644 libemile/emile_second_get_property.c create mode 100644 libemile/emile_second_set_property.c diff --git a/libemile/emile_second_get_next_property.c b/libemile/emile_second_get_next_property.c new file mode 100644 index 0000000..92146fc --- /dev/null +++ b/libemile/emile_second_get_next_property.c @@ -0,0 +1,54 @@ +#include +#include + +#include "libemile.h" + +static char *read_line(char *s) +{ + int read = 0; + while (*s && (*s != '\n')) + { + read++; + s++; + } + return s + 1; +} + +static char *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 emile_second_get_next_property(char *configuration, int index, char *name, char *property) +{ + char *next_word, *next_line; + char *current_name, *current_property; + + next_line = configuration + index; + if (*next_line == 0) + return -1; + next_word = next_line; + next_line = read_line(next_line); + + current_name = read_word(next_word, &next_word); + strncpy(name, current_name, next_word - current_name); + name[next_word - current_name] = 0; + + current_property = read_word(next_word, &next_word); + strncpy(property, current_property, next_line - current_property); + property[next_line - current_property] = 0; + + return next_line - configuration; +} diff --git a/libemile/emile_second_get_property.c b/libemile/emile_second_get_property.c new file mode 100644 index 0000000..d24d745 --- /dev/null +++ b/libemile/emile_second_get_property.c @@ -0,0 +1,18 @@ +#include + +#include "libemile.h" + +int emile_second_get_property(char *configuration, char *name, char *property) +{ + int index = 0; + char current_name[256]; + while (1) + { + index = emile_second_get_next_property(configuration, index, current_name, property); + if (index == -1) + break; + if (strcmp(name, current_name) == 0) + return 0; + } + return -1; +} diff --git a/libemile/emile_second_set_property.c b/libemile/emile_second_set_property.c new file mode 100644 index 0000000..2981f44 --- /dev/null +++ b/libemile/emile_second_set_property.c @@ -0,0 +1,29 @@ +#include +#include + +#include "libemile.h" +void emile_second_set_property(char *configuration, char *name, char *property) +{ + int index = 0; + int current = 0; + char current_name[256]; + char current_property[256]; + + while (1) + { + index = emile_second_get_next_property(configuration, index, current_name, current_property); + if (index == -1) + break; + if (strcmp(name, current_name) != 0) + { + memcpy(configuration + current, current_name, strlen(current_name)); + current += strlen(current_name); + configuration[current++] = ' '; + memcpy(configuration + current, current_property, strlen(current_property)); + current += strlen(current_property); + } + } + if (configuration[current - 1] != '\n') + configuration[current - 1] = '\n'; + sprintf(configuration + current, "%s %s", name, property); +}