Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Config {
- bool show_welcome;
- bool kill_yourself;
- char* last_message;
- };
- char *get_key(const char **buf) {
- char *key = (char*)malloc(sizeof(char) * 12 + 1);
- if (key == NULL) {
- printf("Failed to allocate buffer for key\n");
- return NULL;
- }
- unsigned int key_size = 0;
- unsigned int key_cap = 12;
- while (**buf != '\0' && **buf != '\n' && **buf != '=') {
- if (**buf == ' ' || **buf == '\t') {
- *buf = *buf + 1;
- continue;
- }
- if (key_size >= key_cap) {
- key_cap *= 2;
- key = realloc(key, sizeof(char) * key_cap + 1);
- if (key == NULL) {
- printf("Failed to reallocate buffer for key\n");
- return NULL;
- }
- }
- key[key_size] = **buf;
- key_size++;
- *buf = *buf + 1;
- }
- key[key_size] = '\0';
- return key;
- }
- char *get_val(const char **buf) {
- char *key = (char*)malloc(sizeof(char) * 12 + 1);
- if (key == NULL) {
- printf("Failed to allocate buffer for key\n");
- return NULL;
- }
- // eat whitespaces till we reach a valid character
- while (**buf != '\0' && (**buf == ' ' || **buf == '\t')) {
- *buf = *buf + 1;
- }
- bool is_string = **buf == '"' ? true : false;
- if (is_string) *buf = *buf + 1;
- unsigned int key_size = 0;
- unsigned int key_cap = 12;
- while (**buf != '\0') {
- if (is_string && **buf == '"') {
- *buf = *buf + 1;
- break;
- } else if (**buf == '\n' || **buf == '=') {
- break;
- } else if (!is_string && (**buf == ' ' || **buf == '\t')) {
- *buf = *buf + 1;
- continue;
- }
- if (key_size >= key_cap) {
- key_cap *= 2;
- key = realloc(key, sizeof(char) * key_cap + 1);
- if (key == NULL) {
- printf("Failed to reallocate buffer for key\n");
- return NULL;
- }
- }
- key[key_size] = **buf;
- key_size++;
- *buf = *buf + 1;
- }
- key[key_size] = '\0';
- return key;
- }
- int parse_input(const char *buf, struct Config* cfg) {
- while (*buf != '\0') {
- char *key = get_key(&buf);
- if (key == NULL) return 1;
- buf++;
- char *val = get_val(&buf);
- if (val == NULL) return 1;
- buf++;
- if (strcmp(key, "show_welcome") == 0) {
- if (strcmp(val, "true") == 0) {
- cfg->show_welcome = true;
- } else if (strcmp(val, "false") != 0) {
- printf("WARNING: wrong type of value for `show_welcome`, defaulting to `false`\n");
- }
- free(key);
- free(val);
- } else if (strcmp(key, "kill_yourself") == 0) {
- if (strcmp(val, "true") == 0) {
- cfg->kill_yourself = true;
- } else if (strcmp(val, "false") != 0) {
- printf("WARNING: wrong type of value for `kill_yourself`, defaulting to `false`\n");
- }
- free(key);
- free(val);
- } else if (strcmp(key, "last_message") == 0) {
- free(cfg->last_message);
- cfg->last_message = val;
- free(key);
- } else {
- printf("WARNING: unknown key '%s'\n", key);
- free(key);
- free(val);
- }
- }
- return 0;
- }
- int main(int argc, const char** argv) {
- if (argc < 2) {
- printf("Usage: %s [FILE]\n", argv[0]);
- return 0;
- };
- FILE *fp = fopen(argv[1], "r");
- if (fp == NULL) {
- printf("Failed to open file: %s\n", argv[1]);
- return 1;
- }
- fseek(fp, 0L, SEEK_END);
- unsigned int sz = ftell(fp);
- rewind(fp);
- char *buf = (char*)malloc(sizeof(char) * sz + 1);
- if (!buf) {
- printf("Failed to create buffer\n");
- return 1;
- }
- fread(buf, sz, sizeof(char), fp);
- buf[sz] = '\0';
- fflush(fp);
- fclose(fp);
- struct Config *cfg = (struct Config*)malloc(sizeof(struct Config));
- cfg->show_welcome = false;
- cfg->kill_yourself = false;
- cfg->last_message = (char*)malloc(sizeof(char));
- cfg->last_message[0] = '\0';
- int ret = parse_input(buf, cfg);
- printf("Config {\n"
- "\tshow_welcome: %s,\n"
- "\tkill_yourself: %s,\n"
- "\tlast_message: \"%s\"\n"
- "}\n",
- cfg->show_welcome ? "true" : "false",
- cfg->kill_yourself ? "true" : "false",
- cfg->last_message
- );
- free(buf);
- free(cfg->last_message);
- free(cfg);
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement