Last active
November 23, 2023 13:22
-
-
Save rlapz/52cdc7f303eba7d7956adda27a8562bc to your computer and use it in GitHub Desktop.
A f*cking simple, error prone, and slow key-value-based file configuration reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __FSCONF_H__ | |
#define __FSCONF_H__ | |
/* A f*cking simple, error prone, and slow key-value-based file configuration | |
* | |
* File name: file.fsconf | |
* ------------------------ | |
* key0(value)\n | |
* key1(value)\n | |
* ------------------------ | |
* | |
* Example: | |
* ------------------------ | |
* mailbox_dir(mailbox)\n | |
* smtp_host_port(127.0.0.1:9002)\n | |
* pop3_host_port(127.0.0.1:9003)\n | |
* mysql_host_port(127.0.0.1:9004)\n | |
* ------------------------ | |
* NOTE: '\n' is a must! | |
*/ | |
#include <stddef.h> | |
#include <string.h> | |
static const char * | |
fsconf_get(char dest[], size_t size, const char src[], const char key[]) | |
{ | |
const char *start = strstr(src, key); | |
if (start == NULL) | |
return NULL; | |
start += strlen(key); | |
if (*start != '(') | |
return NULL; | |
/* skips '(' */ | |
start++; | |
const char *end = strstr(start, ")\n"); | |
if (end == NULL) | |
return NULL; | |
const size_t val_len = end - start; | |
if (val_len >= size) | |
return NULL; | |
memcpy(dest, start, val_len); | |
dest[val_len] = '\0'; | |
return dest; | |
} | |
#endif /* __FSCONF_H__ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment