-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathini.h
More file actions
151 lines (125 loc) · 4.57 KB
/
ini.h
File metadata and controls
151 lines (125 loc) · 4.57 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Description: simple read only ini parser
* History: yang@haipo.me, 2013/06/13, create
*/
# pragma once
# include <stdint.h>
# include <stdbool.h>
# include <netinet/in.h>
struct ini_arg
{
char *name;
char *value;
struct ini_arg *next;
};
struct ini_section
{
char *name;
struct ini_arg *args;
struct ini_section *next;
};
typedef struct ini_section ini_t;
/*
* Feature:
* 1: If a property name declared before any section is declared, it
* is in a "global" section. If the ini_read_* function argument
* section is empty string or NULL, they will find the property
* name in the "global" section.
*
* 2: There is no technical LIMIT on the length of section name or
* property name or value, or the num of sections and properties,
* the limit is the size of memory.
*
* 3: There is no special limit on the name of section and property.
* Note that the surround whitespace of the name of section and
* property and value is ignored, but they can contain whitespace.
* Note that the name of section and property are case insensitive.
*
* 4: Blank line is ignored.
*
* 5: Lines beginning with '#' or ';' are ignored and may be used to
* provide comments.
*
* 6: The second occurrence of a property name in the same section
* overwrite the previous one. The second occurrence of a section
* is joined with the previous one.
*
* 7: If a line end with '\', where a backslash followed immediately
* by EOL (end-of-line) causes the line break to be ignored, and
* the "logical line" to be continued on the next actual line from
* the INI file. Example:
* name = simple read only ini parser
* is the same with:
* name = simple \
* read only \
* ini parser
*/
/*
* Load a ini config file to memory, return NULL if fail.
*/
ini_t *ini_load(char *path);
/*
* Return value:
* If the combination of section and name found in config file, return 0.
* Return 1 if not found, the *value will be the default_value.
*
* Fail return -1;
*/
/*
* Read string from ini config handler.
* The string is allocated using malloc, so, you need free it after use.
*/
int ini_read_str(ini_t *handler,
char *section, char *name, char **value, char *default_value);
/*
* Read string from ini config handler.
* If the real length of value is greater than or equal to n, n - 1
* characters and a null terminator will be copied to value, else
* the remainder of value pads with null bytes.
*/
int ini_read_strn(ini_t *handler,
char *section, char *name, char *value, size_t n, char *default_value);
/*
* Read int or unsigned int or stdint from ini config handler.
* Support octal or hexadecimal base ("0" or "0x"/"0X" respectively).
*/
int ini_read_int(ini_t *handler,
char *section, char *name, int *value, int default_value);
int ini_read_unsigned(ini_t *handler,
char *section, char *name, unsigned *value, unsigned default_value);
int ini_read_int8(ini_t *handler,
char *section, char *name, int8_t *value, int8_t default_value);
int ini_read_uint8(ini_t *handler,
char *section, char *name, uint8_t *value, uint8_t default_value);
int ini_read_int16(ini_t *handler,
char *section, char *name, int16_t *value, int16_t default_value);
int ini_read_uint16(ini_t *handler,
char *section, char *name, uint16_t *value, uint16_t default_value);
int ini_read_int32(ini_t *handler,
char *section, char *name, int32_t *value, int32_t default_value);
int ini_read_uint32(ini_t *handler,
char *section, char *name, uint32_t *value, uint32_t default_value);
int ini_read_int64(ini_t *handler,
char *section, char *name, int64_t *value, int64_t default_value);
int ini_read_uint64(ini_t *handler,
char *section, char *name, uint64_t *value, uint64_t default_value);
/*
* Read float/double from ini config handler.
*/
int ini_read_float(ini_t *handler,
char *section, char *name, float *value, float default_value);
int ini_read_double(ini_t *handler,
char *section, char *name, double *value, double default_value);
/*
* Read a ipv4 addr such as: 127.0.0.1:8080 or 127.0.0.1 8080
*/
int ini_read_ipv4_addr(ini_t *handler,
char *section, char *name, struct sockaddr_in *addr, char *default_value);
/*
* Read a bool from ini config handler. The value in the ini config file can be
* "true" or "false", the case are ignored.
*/
int ini_read_bool(ini_t *handler,
char *section, char *name, bool *value, bool default_value);
/* Free a ini config handler */
void ini_free(ini_t *handler);