~exec64/freekdl

freekdl/freekdl.h -rw-r--r-- 14.9 KiB
fece6d45Harry Jeffery Remove inline keyword, unrecognised by clang 5 months ago
                                                                                
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
Copyright 2021 Harry Jeffery

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef FREEKDL_H
#define FREEKDL_H

#include <stddef.h>
#include <stdint.h>

typedef int fkdl_bool;
#define FKDL_TRUE 1
#define FKDL_FALSE 0

enum fkdl_value_type
{
  FKDL_INVALID_VALUE,
  FKDL_NULL,
  FKDL_STRING,
  FKDL_NUMBER,
  FKDL_BOOL
};

/** A UTF-8 string. Intended as a means for passing strings, not a working area
 * for building and manipulating strings in place.
*/
struct fkdl_string
{
  /** Raw UTF-8 bytes of the string. May contain NULL bytes, but also always NULL terminated. */
  char *data;
  /** Length of the string in bytes, excluding the NULL terminator added by FreeKDL. */
  size_t len;
};

/** A KDL value */
struct fkdl_value
{
  /** The type of value held */
  enum fkdl_value_type type;
  /** If non-empty, the value's type annotation excluding the wrapping parenthesise. */
  struct fkdl_string annotation;
  /** The raw value represented as a string. 
   * 
   * For example, "null", "true", "false". Escaped strings are expanded during
   * parsing, so "a\nb" contains a newline, not a '\' or 'n'. Numbers have their
   * underscores removed during parsing, but do not change between bases, i.e. hex
   * values will remain hex, as will binary, decimal, and octal.
   */
  struct fkdl_string as_string;
};

struct fkdl_property
{
  struct fkdl_string key;
  struct fkdl_value value;
};

/** In memory representation of a KDL node. Must have an identifier
 * but may have any number of arguments, properties, or children.
 * 
 * May be manipulated directly so long as all dynamic arrays are
 * kept in good order. "Good order" meaning that:
 * 1) empty arrays are either a NULL pointer, or a valid pointer to a buffer.
 * 2) Length variables are always up to date.
 * 3) Capacity variables are valid for the buffer, or 0 if the buffer is NULL.
 * 
 * Several helper functions are provided to simplify manipulation of the node
 * struct.
 */
struct fkdl_node
{
  /** The name or "identifier" of the node */
  struct fkdl_string identifier;
  /** If non-empty, the type annotation of the node, excluding
   * the enclosing parenthesise.
   */
  struct fkdl_string annotation;

  /** Dynamic array of this node's arguments. */
  struct fkdl_value *arguments;
  /** Length of the arguments array in items, not bytes. */
  size_t argumentsLen;
  /** Capacity of the arguments array in items, not bytes. */
  size_t argumentsCap;

  /** Dynamic array of this node's properties. Kept in
   * alphabetical key order.
   */
  struct fkdl_property *properties;
  /** Length of the properties array in items, not bytes. */
  size_t propertiesLen;
  /** Capacity of the properties array in items, not bytes. */
  size_t propertiesCap;

  /** Dynamic array of this node's children. */
  struct fkdl_node *children;
  /** Length of the children array in items, not bytes. */
  size_t childrenLen;
  /** Capacity of the children array in items, not bytes. */
  size_t childrenCap;
};

/** A KDL document, containing zero or more nodes. */
struct fkdl_document
{
  struct fkdl_node *nodes;
  size_t nodesLen;
  size_t nodesCap;
};

/** The interface for specifying how memory is managed by FreeKDL.
 * All functions must be implemented and behave like the
 * C standard library equivalents.
 * 
 * It is acceptable for malloc or realloc to fail, FreeKDL will
 * handle these failures gracefully, returning an error.
 *
 * The context pointer is to allow custom state to be passed into
 * the allocator. This could be used for setting and tracking
 * memory limits for FreeKDL, or for using a custom memory manager.
 */
struct fkdl_allocator
{
  void *(*malloc)(void *context, size_t len);
  void *(*realloc)(void *context, void *ptr, size_t len);
  void (*free)(void *context, void *ptr);
  void *context;
};

/** Representation of an error returned by FreeKDL. */
struct fkdl_error
{
  /** The position in the input where FreeKDL ran into a problem, in bytes. */
  size_t index;
  /** The line of input corresponding to index, where 1 is the first line. */
  size_t line;
  /** The position in the current line, corresponding to index. */
  size_t column;
  /** Further information about the error.*/
  const char *message;
};

/** Reads a UTF-8 string and populates an fkdl_document.
 * 
 * If inputLen is zero, strlen() is used to determine the length of input.
 * 
 * @param input The UTF-8 string to read. May contain NULL bytes.
 * @param inputLen The length of input in bytes, excluding any NULL terminator.
 * @param output A pointer to the fkdl_document to be populated.
 * @param allocator A pointer to the allocator to use. If NULL, defaults to malloc/free.
 * @param error A pointer to the fkdl_error to be populated on failure. May be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_readDocument(
  const char *input,
  size_t inputLen,
  struct fkdl_document *output,
  struct fkdl_allocator *allocator,
  struct fkdl_error *error
);

/** Serialises a fkdl_document to a UTF-8 string in the buffer pointed to by output.
 * 
 * If output is passed as NULL, no output will be written but the number of bytes
 * that would have been written is still written to bytesWritten. This behaviour
 * can be used to determine how much space to allocate for the generated text.
 * 
 * @param output A pointer to the buffer to populate with UTF-8 text. May be NULL.
 * @param outputLen The length of output in bytes.
 * @param bytesWritten If non-NULL, the pointee is set to the number of bytes generated.
 * @param document The fkdl_document to serialise.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_writeDocument(
  char *output,
  size_t outputLen,
  size_t *bytesWritten,
  const struct fkdl_document *document
);

/** Adds a value to a node as an additional argument.
 * 
 * A deep copy is NOT performed, so any buffers owned by
 * value must be considered owned by the node upon the
 * successful completion of this function.
 * 
 * @param node The node to add the value to.
 * @param value The value to add to the node.
 * @param allocator The allocator interface to use, may be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_nodeAddArgument(
  struct fkdl_node *node,
  const struct fkdl_value *value,
  struct fkdl_allocator *allocator);

/** Removes a given argument from node using a 0-based index.
 * 
 * @param node The node to remove the argument from.
 * @param index The value to add to the node.
 * @param allocator The allocator interface to use, may be NULL.
 */
fkdl_bool fkdl_nodeRemoveArgument(
  struct fkdl_node *node,
  size_t argumentIndex,
  struct fkdl_allocator *allocator);

/** Adds a property to a node.
 * 
 * A deep copy is NOT performed, so any buffers owned by
 * the property must be considered owned by the node upon the
 * successful completion of this function.
 * 
 * If a property exists with the same key, it will be replaced
 * by the new one.
 * 
 * @param node The node to add the property to.
 * @param property The property to add to the node.
 * @param allocator The allocator interface to use, may be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_nodeAddProperty(
  struct fkdl_node *node,
  const struct fkdl_property *property,
  struct fkdl_allocator *allocator);

/** Removes a property from a node.
 * 
 * @param node The node to add the property to.
 * @param key A UTF-8 string matching the key of the property to remove.
 * @param allocator The allocator interface to use, may be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_nodeRemoveProperty(
  struct fkdl_node *node,
  const struct fkdl_string *key,
  struct fkdl_allocator *allocator);

/** Set a string property on a node.
 * 
 * A convenience function for adding a key-value property
 * pair to a node. Both key and value are assumed to be
 * NULL-terminated UTF-8 strings, which will be copied into
 * the new property.
 * 
 * If a property exists with the same key, it will be replaced
 * by the new one.
 * 
 * @param node The node to add the property to.
 * @param key A NULL-terminated UTF-8 C-string containing the key.
 * @param value A NULL-terminated UTF-8 C-string containing the value.
 * @param allocator The allocator interface to use, may be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_nodeSetStringProperty(
  struct fkdl_node *node,
  const char *key,
  const char *value,
  struct fkdl_allocator *allocator);

/** Set a boolean property on a node.
 * 
 * A convenience function for adding a key-value property
 * pair to a node. Key is assumed to be a NULL-terminated
 * UTF-8 string, which will be copied into the new property.
 * 
 * If a property exists with the same key, it will be replaced
 * by the new one.
 * 
 * @param node The node to add the property to.
 * @param key A NULL-terminated UTF-8 C-string containing the key.
 * @param value A FKDL_BOOL containing the value to set.
 * @param allocator The allocator interface to use, may be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_nodeSetBoolProperty(
  struct fkdl_node *node,
  const char *key,
  fkdl_bool value,
  struct fkdl_allocator *allocator);

/** Set a null property on a node.
 * 
 * A convenience function for adding a key-value property
 * pair to a node. Key is assumed to be a NULL-terminated
 * UTF-8 string, which will be copied into the new property.
 * 
 * If a property exists with the same key, it will be replaced
 * by the new one.
 * 
 * @param node The node to add the property to.
 * @param key A NULL-terminated UTF-8 C-string containing the key.
 * @param allocator The allocator interface to use, may be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_nodeSetNullProperty(
  struct fkdl_node *node,
  const char *key,
  struct fkdl_allocator *allocator);

/** Set a numeric property on a node.
 * 
 * A convenience function for adding a key-value property
 * pair to a node. Both key and value are assumed to be
 * NULL-terminated UTF-8 strings, which will be copied into
 * the new property.
 * 
 * If a property exists with the same key, it will be replaced
 * by the new one.
 * 
 * Any valid number in a KDL document may be passed.
 * 
 * @param node The node to add the property to.
 * @param key A NULL-terminated UTF-8 C-string containing the key.
 * @param value A NULL-terminated UTF-8 C-string containing the value.
 * @param allocator The allocator interface to use, may be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_nodeSetNumberProperty(
  struct fkdl_node *node,
  const char *key,
  const char *value,
  struct fkdl_allocator *allocator);

/** Add one node to another node as a child.
 * 
 * child is NOT deep copied, so all buffers owned by child must be
 * considered owned by node after this function completes successfully.
 * 
 * @param node The node to gain a new child.
 * @param child The node to gain a parent.
 * @param allocator The allocator interface to use, may be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_nodeAddChild(
  struct fkdl_node *node,
  const struct fkdl_node *child,
  struct fkdl_allocator *allocator);

/** Cleanup a string.
 * 
 * Cleans up the buffers owned by a string. The string pointer itself
 * is not freed, which allows it to be a local stack variable.
 * Therefore, if you called malloc() to create the memory for string
 * itself, you're still responsible for calling free().
 * 
 * @param string The string to cleanup.
 * @param allocator The allocator interface to use, may be NULL.
 */
void fkdl_cleanupString(struct fkdl_string *string, struct fkdl_allocator *allocator);

/** Cleanup a value.
 * 
 * Cleans up the buffers owned by a value. The value pointer itself
 * is not freed, which allows it to be a local stack variable.
 * Therefore, if you called malloc() to create the memory for value
 * itself, you're still responsible for calling free().
 * 
 * @param value The value to cleanup.
 * @param allocator The allocator interface to use, may be NULL.
 */
void fkdl_cleanupValue(struct fkdl_value *value, struct fkdl_allocator *allocator);

/** Cleanup a property.
 * 
 * Cleans up the buffers owned by a property. The property pointer itself
 * is not freed, which allows it to be a local stack variable.
 * Therefore, if you called malloc() to create the memory for property
 * itself, you're still responsible for calling free().
 * 
 * @param property The property to cleanup.
 * @param allocator The allocator interface to use, may be NULL.
 */
void fkdl_cleanupProperty(struct fkdl_property *property, struct fkdl_allocator *allocator);

/** Cleanup a node.
 * 
 * Cleans up the buffers owned by a node. The node pointer itself
 * is not freed, which allows it to be a local stack variable.
 * Therefore, if you called malloc() to create the memory for node
 * itself, you're still responsible for calling free().
 * 
 * @param node The node to cleanup.
 * @param allocator The allocator interface to use, may be NULL.
 */
void fkdl_cleanupNode(struct fkdl_node *node, struct fkdl_allocator *allocator);

/** Cleanup a document.
 * 
 * Cleans up the buffers owned by a document. The document pointer itself
 * is not freed, which allows it to be a local stack variable.
 * Therefore, if you called malloc() to create the memory for document
 * itself, you're still responsible for calling free().
 * 
 * @param document The document to cleanup.
 * @param allocator The allocator interface to use, may be NULL.
 */
void fkdl_cleanupDocument(struct fkdl_document *document, struct fkdl_allocator *allocator);

/** Add a node to a document.
 * 
 * node is NOT deep copied, so all buffers owned by node must be
 * considered owned by the document after this function completes
 * successfully.
 * 
 * @param document The document to gain a new child.
 * @param node The node to be added.
 * @param allocator The allocator interface to use, may be NULL.
 * @return FKDL_TRUE on success, FKDL_FALSE on error.
 */
fkdl_bool fkdl_documentAddNode(
  struct fkdl_document *document,
  const struct fkdl_node *node,
  struct fkdl_allocator *allocator);

#endif