changeset 73:653d583376c9

Added the inifile_error() function and renamed inifile_option_simple() to _once()
author David Demelier <markand@malikania.fr>
date Sun, 13 Nov 2011 16:38:29 +0100
parents 262c053206f6
children fc8169ab4627
files inifile.c inifile.h
diffstat 2 files changed, 36 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/inifile.c	Sun Nov 13 15:43:30 2011 +0100
+++ b/inifile.c	Sun Nov 13 16:38:29 2011 +0100
@@ -157,12 +157,24 @@
 	struct section *s;
 	int found = -1;
 
-	if (file->q_section == NULL)
-		file->q_section = s = STAILQ_FIRST(&file->sections);
-	else
-		s = STAILQ_NEXT(file->q_section, next);
+	/*
+	 * If multiple redefinition is allowed, seek the next
+	 * section from this first one.
+	 */
+	if (!I_NOREDEFINE(file) && (s = file->q_section) != NULL) {
+		/*
+		 * But if the user want to find an other section we
+		 * need to start from beginning.
+		 */
+		if (strcmp(s->key, section) == 0)
+			s = STAILQ_NEXT(s, next);
+		else
+			s = STAILQ_FIRST(&file->sections);
+	} else if (file->q_section && strcmp(file->q_section->key, section) != 0)
+		/* Seek from beginning since there is no order */
+		s = STAILQ_FIRST(&file->sections);
 
-	while (s != NULL) {
+	STAILQ_FOREACH(s, &file->sections, next) {
 		if (strcmp(s->key, section) == 0) {
 			file->q_section = s;
 			found = 0;
@@ -204,7 +216,7 @@
  */
 
 char *
-inifile_option_simple(struct inifile *file, const char *sect, const char *key)
+inifile_option_once(struct inifile *file, const char *sect, const char *key)
 {
 	struct option *o;
 	struct section *s;
@@ -220,6 +232,19 @@
 	return NULL;
 }
 
+/*
+ * Return the last error or "No error" if there is not error at all.
+ */
+
+char *
+inifile_error(void)
+{
+	if (inifileError[0] == '\0')
+		return "No error";
+
+	return inifileError;
+}
+
 void
 inifile_free(struct inifile *file, int freeSections, int freeOptions)
 {
@@ -338,7 +363,7 @@
 		if (*lp == '\0' || *lp == '#' || *lp == ';')
 			return file;
 
-#ifdef DEBUG
+#ifdef INIFILE_DEBUG
 		printf("-- line[%d] == [%s]\n", file->lineno, file->line);
 #endif
 
@@ -402,7 +427,7 @@
 
 	*lp = endSection + 1;
 
-#ifdef DEBUG
+#ifdef INIFILE_DEBUG
 	printf("-- current section is now [%s]\n", file->current->key);
 #endif
 
@@ -476,7 +501,7 @@
 	if ((option = xoptiondup(&tmp)) == NULL)
 		return -1;
 
-#ifdef DEBUG
+#ifdef INIFILE_DEBUG
 	printf("-- next option [%s] is set to [%s]\n", tmp.key, tmp.value);
 #endif
 
--- a/inifile.h	Sun Nov 13 15:43:30 2011 +0100
+++ b/inifile.h	Sun Nov 13 16:38:29 2011 +0100
@@ -28,8 +28,8 @@
 char		**inifile_sections(struct inifile *, int *);
 int		inifile_select(struct inifile *, const char *);
 char		*inifile_option(struct inifile *, const char *);
-char		*inifile_option_simple(struct inifile *,
-		     const char *, const char *);
+char		*inifile_option_once(struct inifile *, const char *, const char *);
+char		*inifile_error(void);
 void		inifile_free(struct inifile *, int, int);
 
 #endif /* _INIFILE_H_ */