--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
+ * All Rights Reserved.
+ * -->
+ *
+ * \brief Configuration file for ini reader module.
+ *
+ * \version $Id$
+ *
+ * \author Luca Ottaviano <lottaviano@develer.com>
+ */
+
+#ifndef CFG_INI_READER_H
+#define CFG_INI_READER_H
+
+/**
+ * Maximum ini file line length (chars).
+ * $WIZ$ type = "int"; min = 1
+ */
+#define CONFIG_INI_MAX_LINE_LEN 512
+
+#endif /* CFG_INI_READER_H */
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \brief Ini file reader module.
+ *
+ * \version $Id$
+ * \author Luca Ottaviano <lottaviano@develer.com>
+ */
+
+#include "ini_reader.h"
+#include "cfg/cfg_ini_reader.h"
+#include <string.h>
+#include <stdio.h>
+#include <ctype.h>
+
+/*
+ * Returns when the line containing the section is found.
+ * The file pointer is positioned at the start of the next line.
+ * Returns EOF if no section was found, 0 otherwise.
+ */
+static int findSection(KFile *fd, const char *section, char *line, size_t size)
+{
+ while (kfile_gets(fd, line, size) != EOF)
+ {
+ char *ptr = line;
+ unsigned i;
+ /* accept only sections that begin at first char */
+ if (*ptr++ != '[')
+ continue;
+ /* find the end-of-section character */
+ for (i = 0; i < size && *ptr != ']'; ++i, ++ptr)
+ ;
+ /* did we find the correct section? */
+ if(strncmp(&line[1], section, i))
+ continue;
+ else
+ return 0;
+ }
+ return EOF;
+}
+
+/*
+ * Fills the argument with the key found in line
+ */
+static char *getKey(const char *line, char *key, size_t size)
+{
+ /* null-terminated string */
+ while (isspace(*line))
+ ++line;
+ int i = 0;
+ while (*line != '=' && !isspace(*line) && size)
+ {
+ key[i++] = *line;
+ ++line;
+ --size;
+ }
+ size ? (key[i] = '\0') : (key[i-1] = '\0');
+ return key;
+}
+
+/*
+ * Fills the argument with the value found in line.
+ */
+static char *getValue(const char *line, char *value, size_t size)
+{
+ while (*line++ != '=')
+ ;
+ while (isspace(*line))
+ ++line;
+ int i = 0;
+ while (*line && size)
+ {
+ value[i++] = *line++;
+ --size;
+ }
+ size ? (value[i] = '\0') : (value[i-1] = '\0');
+ return value;
+}
+
+/**
+ * Look for key inside a section.
+ *
+ * The function reads lines from input file. It fills the line parameter to allow splitting
+ * the key-value couple. It returns with error if a new section begins and no key was found.
+ * \return 0 if key was found, EOF on errors.
+ */
+static int findKey(KFile *fd, const char *key, char *line, size_t size)
+{
+ while (kfile_gets(fd, line, size) != EOF && *line != '[')
+ {
+ char curr_key[30];
+ getKey(line, curr_key, 30);
+ /* check key */
+ if (!strcmp(curr_key, key))
+ return 0;
+ }
+ return EOF;
+}
+
+/*
+ * On errors, the function returns EOF and fills the buffer with the default value.
+ */
+int ini_getString(KFile *fd, const char *section, const char *key, const char *default_value, char *buf, size_t size)
+{
+ char line[CONFIG_INI_MAX_LINE_LEN];
+ if (kfile_seek(fd, 0, KSM_SEEK_SET) == EOF)
+ goto error;
+
+ if (findSection(fd, section, line, CONFIG_INI_MAX_LINE_LEN) == EOF)
+ goto error;
+
+ if (findKey(fd, key, line, CONFIG_INI_MAX_LINE_LEN) == EOF)
+ goto error;
+ else
+ getValue(line, buf, size);
+ return 0;
+
+error:
+ strncpy(buf, default_value, size);
+ if (size > 0)
+ buf[size - 1] = '\0';
+ return EOF;
+}
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \brief Ini file reader module.
+ *
+ * The format accepted is:
+ * - Sections must begin at beginning of line. [ Long name ] will be found only if " Long name " is specified as section name.
+ * - key can contain any spaces at the beginning and before '=' but not in the middle. Eg. "long key name" is not valid.
+ * - values will be stripped of spaces at the beginning and will run until end-of-line. Eg. "= long value" will be treated as "long value".
+ * - no nested sections are allowed.
+ * - no comments are allowed inside a line with key=value pair.
+ * - every line that doesn't contain a '=' or doesn't start with '[' will be ignored.
+ *
+ * \version $Id$
+ * \author Luca Ottaviano <lottaviano@develer.com>
+ */
+
+#ifndef INI_READER_H
+#define INI_READER_H
+
+#include <kern/kfile.h>
+
+/**
+ * \brief Returns the value for the given string in char* format.
+ * Reads the whole input file looking for section and key and fills the provided buffer with
+ * the corresponding value.
+ * On errors, the function fills the provided buffer with the default value and returns EOF.
+ * \param fd An initialized KFile structure.
+ * \param section The section to be looked for.
+ * \param key The key to search for.
+ * \param default_value The default value.
+ * \param buf The buffer to be filled.
+ * \param size The size of the provided buffer.
+ * \return 0 if section and key were found, EOF on errors.
+ */
+int ini_getString(KFile *fd, const char *section, const char *key, const char *default_value, char *buf, size_t size);
+
+#endif /* INI_READER_H */
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
+ * -->
+ *
+ * \brief Test function for ini_reader module.
+ *
+ * $test$: cp bertos/cfg/cfg_kfile.h $cfgdir/
+ * $test$: echo "#undef CONFIG_KFILE_GETS" >> $cfgdir/cfg_kfile.h
+ * $test$: echo "#define CONFIG_KFILE_GETS 1" >> $cfgdir/cfg_kfile.h
+ *
+ * \version $Id$
+ * \author Luca Ottaviano <lottaviano@develer.com>
+ */
+
+#include <emul/kfile_posix.h>
+#include <cfg/test.h>
+
+#include <string.h> // strcmp
+
+#include "ini_reader.h"
+
+const char ini_file[] = "./test/ini_reader_file.ini";
+static KFilePosix kf;
+
+int ini_reader_testSetup(void)
+{
+ kdbg_init();
+ if (!kfile_posix_init(&kf, ini_file, "r"))
+ {
+ kprintf("No test file found\n");
+ return -1;
+ }
+ return 0;
+}
+
+int ini_reader_testRun(void)
+{
+ char buf[30];
+ memset(buf, 0, 30);
+
+ ASSERT(ini_getString(&kf.fd, "First", "String", "default", buf, 30) != EOF);
+ ASSERT(strcmp(buf, "noot") == 0);
+
+ ASSERT(ini_getString(&kf.fd, "Second", "Val", "default", buf, 30) != EOF);
+ ASSERT(strcmp(buf, "2") == 0);
+
+ ASSERT(ini_getString(&kf.fd, "First", "Empty", "default", buf, 30) != EOF);
+ ASSERT(strcmp(buf, "") == 0);
+
+ ASSERT(ini_getString(&kf.fd, "Second", "Bar", "default", buf, 30) == EOF);
+ ASSERT(strcmp(buf, "default") == 0);
+
+ ASSERT(ini_getString(&kf.fd, "Foo", "Bar", "default", buf, 30) == EOF);
+ ASSERT(strcmp(buf, "default") == 0);
+
+ ASSERT(ini_getString(&kf.fd, "Second", "Long key", "", buf, 30) == EOF);
+
+ ASSERT(ini_getString(&kf.fd, "Second", "comment", "", buf, 30) != EOF);
+ ASSERT(strcmp(buf, "line with #comment") == 0);
+
+ ASSERT(ini_getString(&kf.fd, "Long section with spaces", "value", "", buf, 30) != EOF);
+ ASSERT(strcmp(buf, "long value") == 0);
+ return 0;
+}
+
+int ini_reader_testTearDown(void)
+{
+ return kfile_close(&kf.fd);
+}
+
+TEST_MAIN(ini_reader);
--- /dev/null
+[First]
+String=noot
+Val=1
+Empty=
+#comment
+
+[Second]
+Val = 2
+String = mies
+Long key = "long value"
+comment = line with #comment
+#comment=3
+
+[Long section with spaces]
+value = long value
CC=gcc
#FIXME: -Ibertos/emul should not be needed
-CFLAGS="-W -Wall -Wextra -O0 -g3 -ggdb -Ibertos -Ibertos/emul -std=gnu99 -fno-builtin -D_DEBUG -DARCH=(ARCH_EMUL|ARCH_UNITTEST) -DCPU_FREQ=(12288000UL)"
+CFLAGS="-W -Wall -Wextra -O0 -g3 -ggdb -Ibertos -Ibertos/emul -std=gnu99 -fno-builtin -D_DEBUG -DARCH=(ARCH_EMUL|ARCH_UNITTEST) \
+-DCPU_FREQ=(12288000UL) -ffunction-sections -fdata-sections -Wl,--gc-sections"
CXX=g++
CXXFLAGS="$CFLAGS"
-o -name "*_test.c" -print` }
TESTOUT="testout"
-SRC_LIST="bertos/algo/ramp.c bertos/drv/kdebug.c bertos/drv/timer.c bertos/fs/battfs.c bertos/kern/coop.c bertos/kern/idle.c bertos/kern/kfile.c bertos/kern/monitor.c bertos/kern/proc.c bertos/kern/signal.c bertos/kern/sem.c bertos/mware/event.c bertos/mware/formatwr.c bertos/mware/hex.c bertos/mware/sprintf.c bertos/os/hptime.c bertos/struct/kfile_fifo.c bertos/fs/fatfs/ff.c bertos/emul/diskio_emul.c bertos/fs/fat.c bertos/emul/switch_ctx_emul.S"
+SRC_LIST="bertos/algo/ramp.c bertos/drv/kdebug.c bertos/drv/timer.c bertos/fs/battfs.c bertos/kern/coop.c bertos/kern/idle.c bertos/kern/kfile.c bertos/kern/monitor.c bertos/kern/proc.c bertos/kern/signal.c bertos/kern/sem.c bertos/mware/event.c bertos/mware/formatwr.c bertos/mware/hex.c bertos/mware/sprintf.c bertos/os/hptime.c bertos/struct/kfile_fifo.c bertos/fs/fatfs/ff.c bertos/emul/diskio_emul.c bertos/fs/fat.c bertos/emul/switch_ctx_emul.S bertos/mware/ini_reader.c bertos/emul/kfile_posix.c"
buildout='/dev/null'
runout='/dev/null'