forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathreadconfig.h
executable file
·135 lines (102 loc) · 3.4 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
135
#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.
*
* {A context #Context begin
* }A context #Context end
*/
/* Set the max length of a key name */
#define KEY_NAME_MAX_SIZE 64
/* Set the max length of a option's caption */
#define CAPTION_MAX_SIZE 128
/* Each option can have a caption, which is a kind of explanatory text. */
/* A value must have a type. Here we just need these three types. */
typedef enum _OptionType{
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_ALIAS = -1,
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;
} Holder;
const char *Delimiters;
/* Caption */
char *Caption;
} 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;
/* Config COntexts */
StringChunk Contexts;
/* An array of all the options. */
StringChunk Options;
} ConfigFileInfo;
char *GetKeyNameAndValue(char *Line, const char *Delimiters);
int ConfigInitInfo(ConfigFileInfo *Info, const char *Contexts);
int ConfigOpenFile(ConfigFileInfo *Info, const char *File);
int ConfigCloseFile(ConfigFileInfo *Info);
int ConfigAddOption(ConfigFileInfo *Info,
char *KeyName,
MultilineStrategy Strategy,
OptionType Type,
VType Initial,
char *Caption
);
int ConfigAddAlias(ConfigFileInfo *Info, char *Alias, char *Target);
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 ConfigDisplay(ConfigFileInfo *Info);
#endif // _READCONFIG_