-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.h
106 lines (90 loc) · 2.63 KB
/
config.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef CONFIG_H
#define CONFIG_H
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#define CFG_FILE_EXT ".cfg"
#define CFG_MAX_KEY 32
#define CFG_MAX_VAL 64
#define CFG_MAX_ERR 64
typedef struct {
int off;
int col;
int row;
char msg[CFG_MAX_ERR];
} CfgError;
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
} CfgColor;
typedef enum {
CFG_TYPE_STRING,
CFG_TYPE_BOOL,
CFG_TYPE_INT,
CFG_TYPE_FLOAT,
CFG_TYPE_COLOR,
} CfgValType;
typedef union {
char string[CFG_MAX_VAL + 1];
bool boolean;
int integer;
float floating;
CfgColor color;
} CfgVal;
typedef struct {
CfgValType type;
char key[CFG_MAX_KEY + 1];
CfgVal val;
} CfgEntry;
typedef struct {
CfgEntry *entries;
int count;
int capacity;
} Cfg;
/**
* @brief Parses the source data and populates the Cfg object
*
* @param[in] src The source data
* @param[in] src_len Length of the source data
* @param[in,out] cfg The Cfg object to be populated
* @param[out] err Buffer to store error messages
*
* @return 0 if parsing is successful, -1 otherwise
*/
int cfg_parse(const char *src, int src_len, Cfg *cfg, CfgError *err);
/**
* @brief Loads and parses a config file
*
* @param[in] filename Path of the config file
* @param[in,out] cfg The Cfg object to be populated
* @param[out] err Buffer to store error messages
*
* @return 0 if loading and parsing are successful, -1 otherwise
*
* @see cfg_parse() The underlying "wrapped" function
*/
int cfg_parse_file(const char *filename, Cfg *cfg, CfgError *err);
char *cfg_get_string(Cfg *cfg, const char *key, char *fallback);
bool cfg_get_bool(Cfg *cfg, const char *key, bool fallback);
int cfg_get_int(Cfg *cfg, const char *key, int fallback);
float cfg_get_float(Cfg *cfg, const char *key, float fallback);
CfgColor cfg_get_color(Cfg *cfg, const char *key, CfgColor fallback);
int cfg_get_int_min(Cfg *cfg, const char *key, int fallback, int min);
int cfg_get_int_max(Cfg *cfg, const char *key, int fallback, int max);
int cfg_get_int_range(Cfg *cfg,
const char *key,
int fallback,
int min,
int max);
float cfg_get_float_min(Cfg *cfg, const char *key, float fallback, float min);
float cfg_get_float_max(Cfg *cfg, const char *key, float fallback, float max);
float cfg_get_float_range(Cfg *cfg,
const char *key,
float fallback,
float min,
float max);
void cfg_fprint(FILE *stream, Cfg *cfg);
void cfg_fprint_error(FILE *stream, CfgError *err);
#endif