Skip to content
This repository

Migrate dunst from iniparser to inih. #18

Closed
wants to merge 1 commit into from

2 participants

Gergely Nagy Sascha Kruse
Gergely Nagy

Instead of using iniparser, which is rarely packaged, and is an external dependency, use an embedded copy of inih instead, which is only a hundred or so lines, and can do pretty much the same as iniparser.

The benefit is one less external dependency, and it makes it easier for distributions to package dunst.

This also fixes issue #12.

Gergely Nagy Migrate dunst from iniparser to inih.
Instead of using iniparser, which is rarely packaged, and is an
external dependency, use an embedded copy of inih instead, which is
only a hundred or so lines, and can do pretty much the same as
iniparser.

The benefit is one less external dependency, and it makes it easier
for distributions to package dunst.

Signed-off-by: Gergely Nagy <algernon@balabit.hu>
c3f25f8
Sascha Kruse
Owner

Did you use 2 different editors to write this? Because it's a mixture of tabs and spaces. It's not a problem; I'm just curious how this happend.

Other than that everything looks fine. I've pulled (locally) and fixed the issues above. If I don't encounter any problems, I'll merge this into master soon. I'll also add a AUTHORS file; how would you like to be mentioned there?

Gergely Nagy

As for the editor, I used only one, but forgot to set it to only use spaces, and it defaulted to using a mixture when I told it not to use GNU-style indentation. Sorry about that!

Regarding the AUTHORS file: I don't think it's necessary to mention me there, it's a tiny little patch, and attribution in git log is good enough for me. But if you want to, whatever I used for the Signed-off-by is good.

Gergely Nagy

Since you merged this, and touched it up, lets close the pull request too! :)

Gergely Nagy algernon closed this June 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 20, 2012
Gergely Nagy Migrate dunst from iniparser to inih.
Instead of using iniparser, which is rarely packaged, and is an
external dependency, use an embedded copy of inih instead, which is
only a hundred or so lines, and can do pretty much the same as
iniparser.

The benefit is one less external dependency, and it makes it easier
for distributions to package dunst.

Signed-off-by: Gergely Nagy <algernon@balabit.hu>
c3f25f8
This page is out of date. Refresh to see the latest.
6  Makefile
@@ -3,7 +3,7 @@
3 3
 
4 4
 include config.mk
5 5
 
6  
-SRC = draw.c dunst.c
  6
+SRC = draw.c dunst.c ini.c
7 7
 OBJ = ${SRC:.c=.o}
8 8
 
9 9
 all: doc options dunst
@@ -20,9 +20,9 @@ options:
20 20
 
21 21
 ${OBJ}: config.mk
22 22
 
23  
-dunst: draw.o dunst.o
  23
+dunst: draw.o dunst.o ini.o
24 24
 	@echo CC -o $@
25  
-	@${CC} ${CFLAGS} -o $@ dunst.o draw.o ${LDFLAGS}
  25
+	@${CC} ${CFLAGS} -o $@ dunst.o draw.o ini.o ${LDFLAGS}
26 26
 
27 27
 clean:
28 28
 	@echo cleaning
5  config.mk
@@ -13,12 +13,15 @@ XFTLIBS  = -lXft -lXrender -lfreetype -lz -lfontconfig
13 13
 XINERAMALIBS  = -lXinerama
14 14
 XINERAMAFLAGS = -DXINERAMA
15 15
 
  16
+# inih flags
  17
+INIFLAGS = -DINI_ALLOW_MULTILINE=0
  18
+
16 19
 # includes and libs
17 20
 INCS = -I${X11INC} -I/usr/lib/dbus-1.0/include -I/usr/include/dbus-1.0 ${XFTINC}
18 21
 LIBS = -L${X11LIB} -lX11 -lXext -lXss -ldbus-1 ${XFTLIBS} -lpthread -liniparser -lrt ${XINERAMALIBS}
19 22
 
20 23
 # flags
21  
-CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
  24
+CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} ${INIFLAGS}
22 25
 CFLAGS   = -g -ansi -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
23 26
 LDFLAGS  = ${LIBS}
24 27
 
320  dunst.c
@@ -15,10 +15,9 @@
15 15
 #endif
16 16
 #include <X11/extensions/scrnsaver.h>
17 17
 
18  
-#include <iniparser.h>
19  
-
20 18
 #include "dunst.h"
21 19
 #include "draw.h"
  20
+#include "ini.h"
22 21
 
23 22
 #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
24 23
 #define LENGTH(X)               (sizeof X / sizeof X[0])
@@ -865,35 +864,190 @@ parse_cmdline(int argc, char *argv[]) {
865 864
     }
866 865
 }
867 866
 
868  
-char *
869  
-create_iniparser_key(char *section, char *key) {
870  
-    char *new_key;
  867
+static int
  868
+dunst_ini_get_boolean(const char *value) {
  869
+
  870
+    switch (value[0])
  871
+    {
  872
+        case 'y':
  873
+        case 'Y':
  874
+        case 't':
  875
+        case 'T':
  876
+        case '1':
  877
+	    return True;
  878
+        case 'n':
  879
+        case 'N':
  880
+        case 'f':
  881
+        case 'F':
  882
+        case '0':
  883
+	    return False;
  884
+        default:
  885
+	    return False;
  886
+   }
  887
+}
  888
+
  889
+static rule_t *
  890
+dunst_rules_find_or_create(const char *section) {
  891
+
  892
+    rule_t *current_rule = rules, *last_rule;
  893
+
  894
+    while (current_rule && strcmp(current_rule->name, section) != 0) {
  895
+	current_rule = current_rule->next;
  896
+    }
  897
+
  898
+    if (current_rule) {
  899
+	return current_rule;
  900
+    }
  901
+
  902
+    dunst_printf(DEBUG, "adding rule %s\n", section);
871 903
 
872  
-    new_key = malloc(sizeof(char) * (strlen(section)
873  
-                                   + strlen(key)
874  
-                                   + strlen(":")
875  
-                                   + 1 /* \0 */ ));
  904
+    current_rule = initrule();
  905
+    current_rule->name = strdup(section);
  906
+    current_rule->appname = NULL;
  907
+    current_rule->summary = NULL;
  908
+    current_rule->body = NULL;
  909
+    current_rule->icon = NULL;
  910
+    current_rule->timeout = -1;
  911
+    current_rule->urgency = -1;
  912
+    current_rule->fg = NULL;
  913
+    current_rule->bg = NULL;
  914
+    current_rule->format = NULL;
  915
+
  916
+    last_rule = rules;
  917
+    while (last_rule && last_rule->next) {
  918
+	last_rule = last_rule->next;
  919
+    }
876 920
 
877  
-    sprintf(new_key, "%s:%s", section, key);
878  
-    return new_key;
  921
+    if (last_rule == NULL) {
  922
+	last_rule = current_rule;
  923
+	rules = last_rule;
  924
+    } else {
  925
+	last_rule->next = current_rule;
  926
+    }
  927
+
  928
+    return current_rule;
  929
+}
  930
+
  931
+static char *
  932
+dunst_ini_get_string(const char *value) {
  933
+
  934
+    char *s;
  935
+
  936
+    if (value[0] == '"')
  937
+	s = strdup(value + 1);
  938
+    else
  939
+	s = strdup(value);
  940
+
  941
+    if (s[strlen(s) - 1] == '"')
  942
+	s[strlen(s) - 1] = '\0';
  943
+
  944
+    return s;
  945
+}
  946
+
  947
+static int
  948
+dunst_ini_handle(void *user_data, const char *section,
  949
+		 const char *name, const char *value) {
  950
+
  951
+    if (strcmp(section, "global") == 0) {
  952
+	if (strcmp(name, "font") == 0)
  953
+	    font = dunst_ini_get_string(value);
  954
+	else if (strcmp(name, "format") == 0)
  955
+	    format = dunst_ini_get_string(value);
  956
+	else if (strcmp(name, "sort") == 0)
  957
+	    sort = dunst_ini_get_boolean(value);
  958
+	else if (strcmp(name, "indicate_hidden") == 0)
  959
+	    indicate_hidden = dunst_ini_get_boolean(value);
  960
+	else if (strcmp(name, "key") == 0)
  961
+	    key_string = dunst_ini_get_string(value);
  962
+	else if (strcmp(name, "history_key") == 0)
  963
+	    history_key_string = dunst_ini_get_string(value);
  964
+	else if (strcmp(name, "geometry") == 0) {
  965
+	    geom = dunst_ini_get_string(value);
  966
+	    geometry.mask = XParseGeometry(geom,
  967
+					   &geometry.x, &geometry.y,
  968
+					   &geometry.w, &geometry.h);
  969
+	} else if (strcmp(name, "modifier") == 0) {
  970
+	    char *mod = dunst_ini_get_string(value);
  971
+
  972
+	    if (mod == NULL) {
  973
+		mask = 0;
  974
+	    } else if (!strcmp(mod, "ctrl")) {
  975
+		mask = ControlMask;
  976
+	    } else if (!strcmp(mod, "mod4")) {
  977
+		mask = Mod4Mask;
  978
+	    } else if (!strcmp(mod, "mod3")) {
  979
+		mask = Mod3Mask;
  980
+	    } else if (!strcmp(mod, "mod2")) {
  981
+		mask = Mod2Mask;
  982
+	    } else if (!strcmp(mod, "mod1")) {
  983
+		mask = Mod1Mask;
  984
+	    } else {
  985
+		mask = 0;
  986
+	    }
  987
+	    free (mod);
  988
+	}
  989
+    } else if (strcmp(section, "urgency_low") == 0) {
  990
+	if (strcmp(name, "background") == 0)
  991
+	    lowbgcolor = dunst_ini_get_string(value);
  992
+	else if (strcmp(name, "foreground") == 0)
  993
+	    lowfgcolor = dunst_ini_get_string(value);
  994
+	else if (strcmp(name, "timeout") == 0)
  995
+	    timeouts[LOW] = atoi(value);
  996
+    } else if (strcmp(section, "urgency_normal") == 0) {
  997
+	if (strcmp(name, "background") == 0)
  998
+	    normbgcolor = dunst_ini_get_string(value);
  999
+	else if (strcmp(name, "foreground") == 0)
  1000
+	    normfgcolor = dunst_ini_get_string(value);
  1001
+	else if (strcmp(name, "timeout") == 0)
  1002
+	    timeouts[NORM] = atoi(value);
  1003
+    } else if (strcmp(section, "urgency_critical") == 0) {
  1004
+	if (strcmp(name, "background") == 0)
  1005
+	    critbgcolor = dunst_ini_get_string(value);
  1006
+	else if (strcmp(name, "foreground") == 0)
  1007
+	    critfgcolor = dunst_ini_get_string(value);
  1008
+	else if (strcmp(name, "timeout") == 0)
  1009
+	    timeouts[CRIT] = atoi(value);
  1010
+    } else {
  1011
+
  1012
+	rule_t *current_rule = dunst_rules_find_or_create(section);
  1013
+
  1014
+	if (strcmp(name, "appname") == 0)
  1015
+	    current_rule->appname = dunst_ini_get_string(value);
  1016
+	else if (strcmp(name, "summary") == 0)
  1017
+	    current_rule->summary = dunst_ini_get_string(value);
  1018
+	else if (strcmp(name, "body") == 0)
  1019
+	    current_rule->body = dunst_ini_get_string(value);
  1020
+	else if (strcmp(name, "icon") == 0)
  1021
+	    current_rule->icon = dunst_ini_get_string(value);
  1022
+	else if (strcmp(name, "timeout") == 0)
  1023
+	    current_rule->timeout = atoi(value);
  1024
+	else if (strcmp(name, "urgency") == 0) {
  1025
+	    const char *urg = value;
  1026
+
  1027
+	    if (strcmp(urg, "low") == 0)
  1028
+		current_rule->urgency = LOW;
  1029
+	    else if (strcmp(urg, "normal") == 0)
  1030
+		current_rule->urgency = NORM;
  1031
+	    else if (strcmp(urg, "critical") == 0)
  1032
+		current_rule->urgency = CRIT;
  1033
+	    else
  1034
+		fprintf(stderr, "unknown urgency: %s, ignoring\n", urg);
  1035
+	} else if (strcmp(name, "foreground") == 0)
  1036
+	    current_rule->fg = dunst_ini_get_string(value);
  1037
+	else if (strcmp(name, "background") == 0)
  1038
+	    current_rule->bg = dunst_ini_get_string(value);
  1039
+	else if (strcmp(name, "format") == 0)
  1040
+	    current_rule->format = dunst_ini_get_string(value);
  1041
+    }
  1042
+
  1043
+    return 1;
879 1044
 }
880 1045
 
881 1046
 void
882 1047
 parse_dunstrc(void) {
883 1048
 
884  
-    char *mod = NULL;
885  
-    char *urg = NULL;
886  
-    char *secname = NULL;
887  
-    rule_t *last_rule;
888  
-    rule_t *new_rule;
889  
-    char *key;
890 1049
     char *config_path = NULL;
891 1050
 
892  
-    int nsec = 0;
893  
-    int i;
894  
-
895  
-    dictionary *ini;
896  
-
897 1051
     dunst_printf(DEBUG, "Begin parsing of dunstrc\n");
898 1052
 
899 1053
     if (config_file == NULL) {
@@ -914,130 +1068,12 @@ parse_dunstrc(void) {
914 1068
     }
915 1069
 
916 1070
     dunst_printf(DEBUG, "Reading %s\n", config_file);
917  
-    ini = iniparser_load(config_file);
918  
-    if (ini == NULL) {
919  
-        puts("no dunstrc found -> skipping\n");
920  
-    }
921  
-
922  
-    font = iniparser_getstring(ini, "global:font", font);
923  
-    format = iniparser_getstring(ini, "global:format", format);
924  
-    sort = iniparser_getboolean(ini, "global:sort", sort);
925  
-    indicate_hidden = iniparser_getboolean(ini, "global:indicate_hidden", indicate_hidden);
926  
-    idle_threshold = iniparser_getint(ini, "global:idle_threshold", idle_threshold);
927  
-    key_string = iniparser_getstring(ini, "global:key", key_string);
928  
-    history_key_string = iniparser_getstring(ini, "global:history_key", history_key_string);
929  
-
930  
-    geom = iniparser_getstring(ini, "global:geometry", geom);
931  
-                geometry.mask = XParseGeometry(geom,
932  
-                        &geometry.x, &geometry.y,
933  
-                        &geometry.w, &geometry.h);
934  
-
935  
-    mod = iniparser_getstring(ini, "global:modifier", mod);
936  
-
937  
-    if (mod == NULL) {
938  
-        mask = 0;
939  
-    } else if (!strcmp(mod, "ctrl")) {
940  
-        mask = ControlMask;
941  
-    } else if (!strcmp(mod, "mod4")) {
942  
-        mask = Mod4Mask;
943  
-    } else if (!strcmp(mod, "mod3")) {
944  
-        mask = Mod3Mask;
945  
-    } else if (!strcmp(mod, "mod2")) {
946  
-        mask = Mod2Mask;
947  
-    } else if (!strcmp(mod, "mod1")) {
948  
-        mask = Mod1Mask;
949  
-    } else {
950  
-        mask = 0;
951  
-    }
952  
-
953  
-    lowbgcolor = iniparser_getstring(ini, "urgency_low:background", lowbgcolor);
954  
-    lowfgcolor = iniparser_getstring(ini, "urgency_low:foreground", lowfgcolor);
955  
-    timeouts[LOW] = iniparser_getint(ini, "urgency_low:timeout", timeouts[LOW]);
956  
-    normbgcolor = iniparser_getstring(ini, "urgency_normal:background", normbgcolor);
957  
-    normfgcolor = iniparser_getstring(ini, "urgency_normal:foreground", normfgcolor);
958  
-    timeouts[NORM] = iniparser_getint(ini, "urgency_normal:timeout", timeouts[NORM]);
959  
-    critbgcolor = iniparser_getstring(ini, "urgency_critical:background", critbgcolor);
960  
-    critfgcolor = iniparser_getstring(ini, "urgency_critical:foreground", critfgcolor);
961  
-    timeouts[CRIT] = iniparser_getint(ini, "urgency_critical:timeout", timeouts[CRIT]);
962  
-
963  
-    /* parse rules */
964  
-    nsec = iniparser_getnsec(ini);
965 1071
 
966  
-    /* init last_rule */
967  
-    last_rule = rules;
968  
-    while (last_rule && last_rule->next) {
969  
-        last_rule = last_rule->next;
970  
-    }
971  
-
972  
-    for (i = 0; i < nsec; i++) {
973  
-        secname = iniparser_getsecname(ini, i);
974  
-
975  
-        /* every section not handled above is considered a rule */
976  
-        if (strcmp(secname, "global") == 0
977  
-         || strcmp(secname, "urgency_low") == 0
978  
-         || strcmp(secname, "urgency_normal") == 0
979  
-         || strcmp(secname, "urgency_critical") == 0) {
980  
-            continue;
981  
-        }
982  
-
983  
-        dunst_printf(DEBUG, "adding rule %s\n", secname);
984  
-
985  
-        new_rule = initrule();
986  
-
987  
-        new_rule->name = secname;
988  
-        key = create_iniparser_key(secname, "appname");
989  
-        new_rule->appname = iniparser_getstring(ini, key, NULL);
990  
-        free(key);
991  
-        key = create_iniparser_key(secname, "summary");
992  
-        new_rule->summary= iniparser_getstring(ini, key, NULL);
993  
-        free(key);
994  
-        key = create_iniparser_key(secname, "body");
995  
-        new_rule->body= iniparser_getstring(ini, key, NULL);
996  
-        free(key);
997  
-        key = create_iniparser_key(secname, "icon");
998  
-        new_rule->icon= iniparser_getstring(ini, key, NULL);
999  
-        free(key);
1000  
-        key = create_iniparser_key(secname, "timeout");
1001  
-        new_rule->timeout= iniparser_getint(ini, key, -1);
1002  
-        free(key);
1003  
-
1004  
-        key = create_iniparser_key(secname, "urgency");
1005  
-        urg = iniparser_getstring(ini, key, NULL);
1006  
-        free(key);
1007  
-        if (urg == NULL) {
1008  
-            new_rule->urgency = -1;
1009  
-        } else if (!strcmp(urg, "low")) {
1010  
-            new_rule->urgency = LOW;
1011  
-        } else if (!strcmp(urg, "normal")) {
1012  
-            new_rule->urgency = NORM;
1013  
-        } else if (!strcmp(urg, "critical")) {
1014  
-            new_rule->urgency = CRIT;
1015  
-        } else {
1016  
-            fprintf(stderr, "unknown urgency: %s, ignoring\n", urg);
1017  
-        }
1018  
-
1019  
-        key = create_iniparser_key(secname, "foreground");
1020  
-        new_rule->fg= iniparser_getstring(ini, key, NULL);
1021  
-        free(key);
1022  
-        key = create_iniparser_key(secname, "background");
1023  
-        new_rule->bg= iniparser_getstring(ini, key, NULL);
1024  
-        free(key);
1025  
-        key = create_iniparser_key(secname, "format");
1026  
-        new_rule->format= iniparser_getstring(ini, key, NULL);
1027  
-        free(key);
1028  
-
1029  
-        if (last_rule == NULL) {
1030  
-            last_rule = new_rule;
1031  
-            rules = last_rule;
1032  
-        } else {
1033  
-            last_rule->next = new_rule;
1034  
-            last_rule = last_rule->next;
1035  
-        }
1036  
-        print_rule(last_rule);
  1072
+    if (ini_parse(config_file, dunst_ini_handle, NULL) < 0) {
  1073
+        puts("no dunstrc found -> skipping\n");
1037 1074
     }
1038 1075
 
1039 1076
     print_rules();
1040  
-
1041 1077
 }
1042 1078
 
1043 1079
 
149  ini.c
... ...
@@ -0,0 +1,149 @@
  1
+/* inih -- simple .INI file parser
  2
+
  3
+inih is released under the New BSD license (see LICENSE.txt). Go to the project
  4
+home page for more info:
  5
+
  6
+http://code.google.com/p/inih/
  7
+
  8
+*/
  9
+
  10
+#include <stdio.h>
  11
+#include <ctype.h>
  12
+#include <string.h>
  13
+
  14
+#include "ini.h"
  15
+
  16
+#define MAX_LINE 200
  17
+#define MAX_SECTION 50
  18
+#define MAX_NAME 50
  19
+
  20
+/* Strip whitespace chars off end of given string, in place. Return s. */
  21
+static char* rstrip(char* s)
  22
+{
  23
+    char* p = s + strlen(s);
  24
+    while (p > s && isspace(*--p))
  25
+        *p = '\0';
  26
+    return s;
  27
+}
  28
+
  29
+/* Return pointer to first non-whitespace char in given string. */
  30
+static char* lskip(const char* s)
  31
+{
  32
+    while (*s && isspace(*s))
  33
+        s++;
  34
+    return (char*)s;
  35
+}
  36
+
  37
+/* Return pointer to first char c or ';' comment in given string, or pointer to
  38
+   null at end of string if neither found. ';' must be prefixed by a whitespace
  39
+   character to register as a comment. */
  40
+static char* find_char_or_comment(const char* s, char c)
  41
+{
  42
+    int was_whitespace = 0;
  43
+    while (*s && *s != c && !(was_whitespace && *s == ';')) {
  44
+        was_whitespace = isspace(*s);
  45
+        s++;
  46
+    }
  47
+    return (char*)s;
  48
+}
  49
+
  50
+/* Version of strncpy that ensures dest (size bytes) is null-terminated. */
  51
+static char* strncpy0(char* dest, const char* src, size_t size)
  52
+{
  53
+    strncpy(dest, src, size);
  54
+    dest[size - 1] = '\0';
  55
+    return dest;
  56
+}
  57
+
  58
+/* See documentation in header file. */
  59
+int ini_parse_file(FILE* file,
  60
+                   int (*handler)(void*, const char*, const char*,
  61
+                                  const char*),
  62
+                   void* user)
  63
+{
  64
+    /* Uses a fair bit of stack (use heap instead if you need to) */
  65
+    char line[MAX_LINE];
  66
+    char section[MAX_SECTION] = "";
  67
+    char prev_name[MAX_NAME] = "";
  68
+
  69
+    char* start;
  70
+    char* end;
  71
+    char* name;
  72
+    char* value;
  73
+    int lineno = 0;
  74
+    int error = 0;
  75
+
  76
+    /* Scan through file line by line */
  77
+    while (fgets(line, sizeof(line), file) != NULL) {
  78
+        lineno++;
  79
+        start = lskip(rstrip(line));
  80
+
  81
+        if (*start == ';' || *start == '#') {
  82
+            /* Per Python ConfigParser, allow '#' comments at start of line */
  83
+        }
  84
+#if INI_ALLOW_MULTILINE
  85
+        else if (*prev_name && *start && start > line) {
  86
+            /* Non-black line with leading whitespace, treat as continuation
  87
+               of previous name's value (as per Python ConfigParser). */
  88
+            if (!handler(user, section, prev_name, start) && !error)
  89
+                error = lineno;
  90
+        }
  91
+#endif
  92
+        else if (*start == '[') {
  93
+            /* A "[section]" line */
  94
+            end = find_char_or_comment(start + 1, ']');
  95
+            if (*end == ']') {
  96
+                *end = '\0';
  97
+                strncpy0(section, start + 1, sizeof(section));
  98
+                *prev_name = '\0';
  99
+            }
  100
+            else if (!error) {
  101
+                /* No ']' found on section line */
  102
+                error = lineno;
  103
+            }
  104
+        }
  105
+        else if (*start && *start != ';') {
  106
+            /* Not a comment, must be a name[=:]value pair */
  107
+            end = find_char_or_comment(start, '=');
  108
+            if (*end != '=') {
  109
+                end = find_char_or_comment(start, ':');
  110
+            }
  111
+            if (*end == '=' || *end == ':') {
  112
+                *end = '\0';
  113
+                name = rstrip(start);
  114
+                value = lskip(end + 1);
  115
+                end = find_char_or_comment(value, '\0');
  116
+                if (*end == ';')
  117
+                    *end = '\0';
  118
+                rstrip(value);
  119
+
  120
+                /* Valid name[=:]value pair found, call handler */
  121
+                strncpy0(prev_name, name, sizeof(prev_name));
  122
+                if (!handler(user, section, name, value) && !error)
  123
+                    error = lineno;
  124
+            }
  125
+            else if (!error) {
  126
+                /* No '=' or ':' found on name[=:]value line */
  127
+                error = lineno;
  128
+            }
  129
+        }
  130
+    }
  131
+
  132
+    return error;
  133
+}
  134
+
  135
+/* See documentation in header file. */
  136
+int ini_parse(const char* filename,
  137
+              int (*handler)(void*, const char*, const char*, const char*),
  138
+              void* user)
  139
+{
  140
+    FILE* file;
  141
+    int error;
  142
+
  143
+    file = fopen(filename, "r");
  144
+    if (!file)
  145
+        return -1;
  146
+    error = ini_parse_file(file, handler, user);
  147
+    fclose(file);
  148
+    return error;
  149
+}
55  ini.h
... ...
@@ -0,0 +1,55 @@
  1
+/* inih -- simple .INI file parser
  2
+
  3
+inih is released under the New BSD license (see LICENSE.txt). Go to the project
  4
+home page for more info:
  5
+
  6
+http://code.google.com/p/inih/
  7
+
  8
+*/
  9
+
  10
+#ifndef __INI_H__
  11
+#define __INI_H__
  12
+
  13
+/* Make this header file easier to include in C++ code */
  14
+#ifdef __cplusplus
  15
+extern "C" {
  16
+#endif
  17
+
  18
+#include <stdio.h>
  19
+
  20
+/* Parse given INI-style file. May have [section]s, name=value pairs
  21
+   (whitespace stripped), and comments starting with ';' (semicolon). Section
  22
+   is "" if name=value pair parsed before any section heading. name:value
  23
+   pairs are also supported as a concession to Python's ConfigParser.
  24
+
  25
+   For each name=value pair parsed, call handler function with given user
  26
+   pointer as well as section, name, and value (data only valid for duration
  27
+   of handler call). Handler should return nonzero on success, zero on error.
  28
+
  29
+   Returns 0 on success, line number of first error on parse error (doesn't
  30
+   stop on first error), or -1 on file open error.
  31
+*/
  32
+int ini_parse(const char* filename,
  33
+              int (*handler)(void* user, const char* section,
  34
+                             const char* name, const char* value),
  35
+              void* user);
  36
+
  37
+/* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
  38
+   close the file when it's finished -- the caller must do that. */
  39
+int ini_parse_file(FILE* file,
  40
+                   int (*handler)(void* user, const char* section,
  41
+                                  const char* name, const char* value),
  42
+                   void* user);
  43
+
  44
+/* Nonzero to allow multi-line value parsing, in the style of Python's
  45
+   ConfigParser. If allowed, ini_parse() will call the handler with the same
  46
+   name for each subsequent line parsed. */
  47
+#ifndef INI_ALLOW_MULTILINE
  48
+#define INI_ALLOW_MULTILINE 1
  49
+#endif
  50
+
  51
+#ifdef __cplusplus
  52
+}
  53
+#endif
  54
+
  55
+#endif /* __INI_H__ */
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.