This repository was archived by the owner on Feb 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsifig.h
More file actions
240 lines (178 loc) · 6.54 KB
/
sifig.h
File metadata and controls
240 lines (178 loc) · 6.54 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
sifig.h - v0.0.0 - a sili library for reading various configuration files.
===========================================================================
- YOU MUST DEFINE 'SIFIG_IMPLEMENTATION' in EXACTLY _one_ C file that
includes this header, BEFORE the include like this:
#include "sili.h"
#define SIFIG_IMPLEMENTATION
#include "sifig.h"
- All other files should just include the library without the #define macro.
===========================================================================
SUPPORT
- [~] .ini
- [-] .json
- [-] .xml
===========================================================================
DOCUMENTATION
- Refer to sili.h's "Documentation" section.
===========================================================================z
LICENSE
- This software is licensed under the zlib license (see the LICENSE at the
bottom of the file).
WARNING
- Sili and its supplementary libraries are designed to be fast, modern, but
also experimental. As a result some unwarranted results may occur during use,
such as:
1) Features not working as expected;
2) Functions having no or incompleted documentation;
3) API breaking changes between releases (especially before v1.0.0 release);
4) Little to no security checks for malicious code that attempt to exploit
parts of the code.
*/
#if SI_COMPILER_HAS_PRAGMA_ONCE
#pragma once
#endif
#ifndef SIFIG_INCLUDE_SI_H
#define SIFIG_INCLUDE_SI_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef SI_INCLUDE_SI_H
#include <sili.h>
#endif
#ifndef SIFIG_VERSION_MAJOR
#define SIFIG_VERSION_MAJOR 0
#define SIFIG_VERSION_MINOR 0
#define SIFIG_VERSION_PATCH 0
#endif
#ifndef SIFIG_VERSION_CURRENT
#define SIFIG_VERSION_CURRENT SI_VERSION(SIFIG_VERSION_MAJOR, SIFIG_VERSION_MINOR, SIFIG_VERSION_PATCH)
#endif
#ifndef SIFIG_NO_INI
/* TODO */
typedef siMap(siString) siIniSection;
/* TODO */
typedef siMap(siIniSection) siIniFile;
typedef struct siIniOptions {
siString comment;
bool lowerCase;
} siIniOptions;
typedef struct siIniIterator {
siString section;
siString key, value;
siString src;
} siIniIterator;
/* Creates an .ini object from a path file. */
SIDEF siIniFile sifig_iniMake(siString path, siAllocator alloc);
/* Creates an .ini object from a string. */
SIDEF siIniFile sifig_iniMakeStr(siString content, siAllocator alloc);
/* Creates an .ini object from a string with certain options. */
SIDEF siIniFile sifig_iniMakeEx(siString content, siIniOptions options, siAllocator alloc);
/* Frees the allocated memory from a .ini file. */
SIDEF void sifig_iniFree(siIniFile ini);
/* TODO */
SIDEF siIniIterator sifig_iniIterator(siString content);
/* TODO */
SIDEF bool sifig_iniIterate(siIniIterator* it);
/* TODO */
SIDEF bool sifig_iniIterateEx(siIniIterator* it, siString comment);
#ifdef SIFIG_IMPLEMENTATION
#ifndef SIFIG_IMPLEMENTATION_INI
#define SIFIG_IMPLEMENTATION_INI 1
#endif
#endif
#ifdef SIFIG_IMPLEMENTATION_INI
inline
siIniFile sifig_iniMake(siString path, siAllocator alloc) {
siString tmp = si_pathReadContents(path, si_allocatorHeap());
siIniFile res = sifig_iniMakeStr(tmp, alloc);
si_mfree((void*)tmp.data);
return res;
}
inline
siIniFile sifig_iniMakeStr(siString content, siAllocator alloc) {
return sifig_iniMakeEx(content, SI_COMP_LIT(siIniOptions, SI_STR(";"), false), alloc);
}
SIDEF
siIniFile sifig_iniMakeEx(siString content, siIniOptions options, siAllocator alloc) {
siIniFile ini = si_mapMakeReserve(siIniSection, 16, alloc);
siIniSection* curIni = nil;
siString curSection = SI_STR_EMPTY;
siIniIterator it = sifig_iniIterator(content);
while (sifig_iniIterateEx(&it, options.comment)) {
if (curSection.data != it.section.data) { /* NOTE(EimaMei): This always equals true on first runs. */
curIni = si_mapSetItem(&ini, si_stringCopy(it.section, alloc), SI_STRUCT_ZERO, siIniSection);
*curIni = si_mapMakeReserve(siString, 32, alloc);
curSection = it.section;
}
siString key = (options.lowerCase)
? si_stringLower(it.key, alloc)
: si_stringCopy(it.key, alloc);
siString value = si_stringCopy(it.value, alloc);
si_mapSet(curIni, key, &value);
}
return ini;
}
inline
siIniIterator sifig_iniIterator(siString content) {
return SI_COMP_LIT(siIniIterator, SI_STR_EMPTY, SI_STR_EMPTY, SI_STR_EMPTY, content);
}
inline
bool sifig_iniIterate(siIniIterator* it) {
return sifig_iniIterateEx(it, SI_STR(";"));
}
SIDEF
bool sifig_iniIterateEx(siIniIterator* it, siString comment) {
siString line;
while (si_stringSplitLinesIterate(&it->src, &line)) {
line = si_stringStrip(line);
SI_STOPIF(line.len == 0, continue);
if (line.data[0] == '[') {
isize len = si_stringFindByte(line, ']');
it->section = si_substr(line, 1, (len != -1) ? len : line.len);
continue;
}
if (si_stringHasPrefix(line, comment)) {
continue;
}
isize end = si_stringFindByte(line, '=');
SI_STOPIF(end == -1, return false);
it->key = si_stringStripRight(si_substrTo(line, end));
it->value = si_substrFrom(line, it->key.len + 1);
it->value = si_stringUnquote(si_stringStrip(it->value));
return true;
}
return false;
}
SIDEF
void sifig_iniFree(siIniFile ini) {
siString name; siIniSection section;
for_eachMapEx (name, section, ini) {
si_free(section.alloc, section.entries);
}
si_free(ini.alloc, ini.entries);
}
#endif /* SIFIG_IMPLEMENTATION_INI */
#endif /* SIFIG_NO_INI */
#ifdef __cplusplus
}
#endif
#endif /* SIFIG_INCLUDE_SI_H */
/*
------------------------------------------------------------------------------
Copyright (C) 2025 EimaMei
This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use of
this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
------------------------------------------------------------------------------
*/