aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorYaroslav de la Peña Smirnov <yps@yaroslavps.com>2021-08-19 01:42:49 +0300
committerYaroslav de la Peña Smirnov <yps@yaroslavps.com>2021-08-19 01:42:49 +0300
commit6e20bab8f5fcfbe4fe816425704f9edd8ad88444 (patch)
tree5f11825e1afc4dbc75ed9778afcdd9e12cddcb05 /example
parent5ed7b33fa8ce0c91af1a58bc17636d6bf0406d24 (diff)
downloadparcini-6e20bab8f5fcfbe4fe816425704f9edd8ad88444.tar.gz
parcini-6e20bab8f5fcfbe4fe816425704f9edd8ad88444.zip
Example and new helper funcion
* Added an example program an ini file to example/ * Added a new helper function `parcini_value_handle` to handle and copy parsed values, depending on the expected value type.
Diffstat (limited to 'example')
-rw-r--r--example/example.c99
-rw-r--r--example/example.ini8
2 files changed, 107 insertions, 0 deletions
diff --git a/example/example.c b/example/example.c
new file mode 100644
index 0000000..1036845
--- /dev/null
+++ b/example/example.c
@@ -0,0 +1,99 @@
+#define _POSIX_C_SOURCE 200809L
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "parcini.h"
+
+#define EXAMPLE_INI "example/example.ini"
+
+struct {
+ struct {
+ char *name;
+ long int age;
+ } personal;
+ struct {
+ char *email;
+ bool visible;
+ } contact;
+} example = {
+ .personal.name = NULL,
+ .contact.email = NULL,
+};
+
+static bool
+handle_keyvalue(struct parcini_line *parsed)
+{
+#define MATCHES(s, k) !strcmp(parsed->section, s) && !strcmp(parsed->key, k)
+
+ if (MATCHES("personal", "name")) {
+ return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING,
+ &example.personal.name);
+ }
+ if (MATCHES("personal", "age")) {
+ return parcini_value_handle(&parsed->value, PARCINI_VALUE_INTEGER,
+ &example.personal.age);
+ }
+ if (MATCHES("contact", "email")) {
+ return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING,
+ &example.contact.email);
+ }
+ if (MATCHES("contact", "visible")) {
+ return parcini_value_handle(&parsed->value, PARCINI_VALUE_BOOLEAN,
+ &example.contact.visible);
+ }
+
+ return false;
+}
+
+int
+main(void)
+{
+ parcini_t *parser = parcini_from_file(EXAMPLE_INI);
+ if (parser == NULL) {
+ fprintf(stderr, "error opening file: %s\n", strerror(errno));
+ return 1;
+ }
+
+ struct parcini_line parsed;
+ enum parcini_result res;
+ while ((res = parcini_parse_next_line(parser, &parsed)) != PARCINI_EOF) {
+ switch (res) {
+ case PARCINI_EMPTY_LINE:
+ case PARCINI_SECTION:
+ continue;
+
+ case PARCINI_KEYVALUE:
+ if (!handle_keyvalue(&parsed)) {
+ fprintf(stderr,
+ "line %ld: error: unexpected section '%s'/key '%s' or "
+ "invalid value type\n",
+ parsed.lineno, parsed.section, parsed.key);
+ goto cleanup;
+ }
+ continue;
+
+ case PARCINI_MEMORY_ERROR:
+ case PARCINI_STREAM_ERROR:
+ fprintf(stderr,
+ "An error occurred while trying to read the next line\n");
+ goto cleanup;
+
+ default:
+ fprintf(stderr, "line %ld: parse error\n", parsed.lineno);
+ goto cleanup;
+ };
+ }
+
+ printf("parsed example.ini:\n");
+ printf("name:\t%s\n", example.personal.name);
+ printf("age:\t%ld\n", example.personal.age);
+ printf("email:\t%s\n", example.contact.email);
+ printf("visible:\t%s\n", example.contact.visible ? "true" : "false");
+
+cleanup:
+ free(example.personal.name);
+ free(example.contact.email);
+ parcini_destroy(parser);
+}
diff --git a/example/example.ini b/example/example.ini
new file mode 100644
index 0000000..9dd5a0e
--- /dev/null
+++ b/example/example.ini
@@ -0,0 +1,8 @@
+# An example ini file
+[personal]
+name = "Ivan Ivanovich Ivanov"
+age = 69
+
+[contact]
+email = "ivan@mail.xyz"
+visible = yes