forked from lifenjoiner/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readconfig.h
executable file
·134 lines (101 loc) · 3.25 KB
/
readconfig.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef _READCONFIG_
#define _READCONFIG_
#include <stdio.h>
#include "stringlist.h"
#include "stringchunk.h"
#include "array.h"
#include "common.h"
/* A valid line of a configuration file has the following structure:
* <Option> <value>
* Where `<Option>' is the name of a option, here we call it `KEY NAME'.
* And `<value>' is the option's value, we just call it `value'.
* A line started with `#' is a comment, which will be ignored when it is read.
* A valid option can be followed a comment which will be ignored too:
* <Option> <value> # I'm a comment.
*
*/
/* Set the max length of a key name */
#define KEY_NAME_MAX_SIZE 64
/* A value must have a type. We just need these types for now. */
typedef enum _OptionType{
TYPE_ALIAS = -1,
TYPE_UNDEFINED = 0,
TYPE_INT32,
TYPE_BOOLEAN,
TYPE_PATH,
TYPE_STRING
} OptionType;
typedef enum _MultilineStrategy{
STRATEGY_DEFAULT = 0,
STRATEGY_REPLACE,
STRATEGY_APPEND,
STRATEGY_APPEND_DISCARD_DEFAULT
} MultilineStrategy;
typedef union _VType{
const char *str;
int32_t INT32;
BOOL boolean;
} VType;
typedef enum _OptionStatus{
STATUS_DEPRECATED = -2,
STATUS_UNUSED = 0,
STATUS_DEFAULT_VALUE,
STATUS_SPECIAL_VALUE
}OptionStatus;
/* An option */
typedef struct _Option{
/* Designate if this option is used. */
OptionStatus Status;
MultilineStrategy Strategy;
/* Type */
OptionType Type;
/* Value holder */
union {
StringList str;
int32_t INT32;
BOOL boolean;
struct {
char *Target;
char *Prepending;
} Aliasing;
} Holder;
char *Delimiters;
} ConfigOption;
/* The exposed type(The infomations about a configuration file) to read options from a configuration file. */
typedef struct _ConfigFileInfo
{
/* Static, once inited, never changed. */
FILE *fp;
StringList StrBuffer;
/* An array of all the options. */
StringChunk Options;
} ConfigFileInfo;
int ConfigInitInfo(ConfigFileInfo *Info);
int ConfigOpenFile(ConfigFileInfo *Info, const char *File);
int ConfigCloseFile(ConfigFileInfo *Info);
int ConfigAddOption(ConfigFileInfo *Info,
char *KeyName,
MultilineStrategy Strategy,
OptionType Type,
VType Initial
);
int ConfigAddAlias(ConfigFileInfo *Info,
const char *Target,
const char *Alias,
const char *Prepending,
const char *StringDelimiters
);
int ConfigSetStringDelimiters(ConfigFileInfo *Info,
char *KeyName,
const char *Delimiters
);
int ConfigRead(ConfigFileInfo *Info);
const char *ConfigGetRawString(ConfigFileInfo *Info, char *KeyName);
StringList *ConfigGetStringList(ConfigFileInfo *Info, char *KeyName);
int32_t ConfigGetNumberOfStrings(ConfigFileInfo *Info, char *KeyName);
int32_t ConfigGetInt32(ConfigFileInfo *Info, char *KeyName);
BOOL ConfigGetBoolean(ConfigFileInfo *Info, char *KeyName);
/* Won't change the Option's status */
void ConfigSetDefaultValue(ConfigFileInfo *Info, VType Value, char *KeyName);
void ConfigFree(ConfigFileInfo *Info);
#endif // _READCONFIG_