foxpy / qc

Quad C: Custom Cruel C Crate
MIT License
4 stars 0 forks source link

feature request: config parser #18

Open foxpy opened 3 years ago

foxpy commented 3 years ago

Let it have its own simple ini-like format:

[section]
key=value
another_key="long value" # comment
# another comment

[another-section]
key="another value"
another_key=2000
whitespace =   "does not matter"
newline
= "does not matter, too"

[flags-section]
flag1=true
flag2=false
flag3=on
flag4=off
flag5=1
flag6=0

[more-advanced-section]
array1=["arrays", "have", "values", "of", "the", "same", "type"]
array2=[
  "arrays",
  "can",
  "make",
  "use",
  "of",
  "newlines"
]
foxpy commented 3 years ago

So far so good. I have some idea of how public API should look like.

typedef struct qc_cfg qc_cfg;
qc_result qc_cfg_open_file(FILE* file, qc_cfg** dst, qc_err* err);
qc_result qc_cfg_open_string(char const* str, qc_cfg** dst, qc_err* err);
void qc_cfg_close(qc_cfg* cfg);

typedef struct qc_cfg_section qc_cfg_section;
qc_result qc_cfg_get_section(qc_cfg* cfg, char const* name, qc_cfg_section** dst, qc_err* err);
void qc_cfg_close_section(qc_cfg_section* section);

qc_result qc_cfg_get_boolean(qc_cfg_section* section, char const* name, bool* dst, qc_err* err);
qc_result qc_cfg_get_unsigned(qc_cfg_section* section, char const* name, size_t* dst, qc_err* err);
qc_result qc_cfg_get_signed(qc_cfg_section* section, char const* name, ptrdiff_t* dst, qc_err* err);
qc_result qc_cfg_get_double(qc_cfg_section* section, char const* name, double* dst, qc_err* err);
qc_result qc_cfg_get_string(qc_cfg_section* section, char const* name, char** dst, qc_err* err);

typedef struct qc_cfg_array qc_cfg_array;
qc_result qc_cfg_get_array(qc_cfg_section* section, char const* name, qc_cfg_array** dst, qc_err* err);
void qc_cfg_close_array(qc_cfg_array* array);

size_t qc_cfg_array_length(qc_cfg_array* array);
qc_result qc_cfg_get_boolean(qc_cfg_array* array, size_t index, bool* dst, qc_err* err);
qc_result qc_cfg_get_unsigned(qc_cfg_array* array, size_t index, size_t* dst, qc_err* err);
qc_result qc_cfg_get_signed(qc_cfg_array* array, size_t index, ptrdiff_t* dst, qc_err* err);
qc_result qc_cfg_get_double(qc_cfg_array* array, size_t index, double* dst, qc_err* err);
qc_result qc_cfg_get_string(qc_cfg_array* array, size_t index, char** dst, qc_err* err);
foxpy commented 3 years ago

Haha, turns out this does not even compile (I have overlapping function signatures)! Already fixed in config-parser branch.