This commit is contained in:
2020-03-19 01:46:59 +01:00
parent dd17316ba3
commit fe13be2fd0
4 changed files with 306 additions and 27 deletions

37
examples/parser_simple.c Normal file
View File

@@ -0,0 +1,37 @@
#include "../cfg.h"
int main(int argc, char** argv) {
if (argc != 2) {
printf("Usage: %s <config>\n", argv[0]);
return 1;
}
FILE* f = fopen(argv[1], "rb");
fseek(f, 0, SEEK_END);
ssize_t s = ftell(f);
fseek(f, 0, SEEK_SET);
char* contents = malloc(s + 1);
fread(contents, 1, s, f);
fclose(f);
contents[s] = 0;
string arg = string_from_cstr(contents);
parsed_config c = config_parse(&arg);
if (!c.ok) {
printf("Error: %s\n", c.err.str);
return 1;
}
string str = config_str(&c.c, 0);
char* cstr = string_cstr(&str);
printf("%s\n", cstr);
free_string(str);
free(cstr);
free(contents);
return 0;
}