]> oss.titaniummirror.com Git - repo_shell.git/blobdiff - inih/examples/ini_example.c
Add support for /etc/repo_shell.cfg
[repo_shell.git] / inih / examples / ini_example.c
diff --git a/inih/examples/ini_example.c b/inih/examples/ini_example.c
new file mode 100644 (file)
index 0000000..0973572
--- /dev/null
@@ -0,0 +1,44 @@
+/* Example: parse a simple configuration file */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../ini.h"
+
+typedef struct
+{
+    int version;
+    const char* name;
+    const char* email;
+} configuration;
+
+static int handler(void* user, const char* section, const char* name,
+                   const char* value)
+{
+    configuration* pconfig = (configuration*)user;
+
+    #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
+    if (MATCH("protocol", "version")) {
+        pconfig->version = atoi(value);
+    } else if (MATCH("user", "name")) {
+        pconfig->name = strdup(value);
+    } else if (MATCH("user", "email")) {
+        pconfig->email = strdup(value);
+    } else {
+        return 0;  /* unknown section/name, error */
+    }
+    return 1;
+}
+
+int main(int argc, char* argv[])
+{
+    configuration config;
+
+    if (ini_parse("test.ini", handler, &config) < 0) {
+        printf("Can't load 'test.ini'\n");
+        return 1;
+    }
+    printf("Config loaded from 'test.ini': version=%d, name=%s, email=%s\n",
+        config.version, config.name, config.email);
+    return 0;
+}