1 /***********************************************************************************************************************************
2 Ini Handler
3 ***********************************************************************************************************************************/
4 #ifndef COMMON_INI_H
5 #define COMMON_INI_H
6 
7 /***********************************************************************************************************************************
8 Ini object
9 ***********************************************************************************************************************************/
10 typedef struct Ini Ini;
11 
12 #include "common/io/read.h"
13 #include "common/io/write.h"
14 #include "common/type/object.h"
15 #include "common/type/variant.h"
16 
17 /***********************************************************************************************************************************
18 Constructors
19 ***********************************************************************************************************************************/
20 Ini *iniNew(void);
21 
22 /***********************************************************************************************************************************
23 Functions
24 ***********************************************************************************************************************************/
25 // Move to a new parent mem context
26 __attribute__((always_inline)) static inline Ini *
iniMove(Ini * const this,MemContext * const parentNew)27 iniMove(Ini *const this, MemContext *const parentNew)
28 {
29     return objMove(this, parentNew);
30 }
31 
32 // Parse ini from a string. Comments are ignored and additional whitespace around sections, keys, and values is trimmed. Should be
33 // used *only* to read user-generated config files, for code-generated info files see iniLoad().
34 void iniParse(Ini *this, const String *content);
35 
36 // Set an ini value
37 void iniSet(Ini *this, const String *section, const String *key, const String *value);
38 
39 /***********************************************************************************************************************************
40 Getters/Setters
41 ***********************************************************************************************************************************/
42 // Get an ini value -- error if it does not exist
43 const String *iniGet(const Ini *this, const String *section, const String *key);
44 
45 // Get an ini value -- if it does not exist then return specified default
46 const String *iniGetDefault(const Ini *this, const String *section, const String *key, const String *defaultValue);
47 
48 // Ini key list
49 StringList *iniGetList(const Ini *this, const String *section, const String *key);
50 
51 // The key's value is a list
52 bool iniSectionKeyIsList(const Ini *this, const String *section, const String *key);
53 
54 // List of keys for a section
55 StringList *iniSectionKeyList(const Ini *this, const String *section);
56 
57 // List of sections
58 StringList *iniSectionList(const Ini *this);
59 
60 /***********************************************************************************************************************************
61 Destructor
62 ***********************************************************************************************************************************/
63 __attribute__((always_inline)) static inline void
iniFree(Ini * const this)64 iniFree(Ini *const this)
65 {
66     objFree(this);
67 }
68 
69 /***********************************************************************************************************************************
70 Helper Functions
71 ***********************************************************************************************************************************/
72 // Load an ini file and return data to a callback. Intended to read info files that were generated by code so do not have comments
73 // or extraneous spaces, and where all values are valid JSON. This allows syntax characters such as [, =, #, and whitespace to be
74 // used in keys.
75 void iniLoad(
76     IoRead *read,
77     void (*callbackFunction)(void *data, const String *section, const String *key, const String *value, const Variant *valueVar),
78     void *callbackData);
79 
80 /***********************************************************************************************************************************
81 Macros for function logging
82 ***********************************************************************************************************************************/
83 #define FUNCTION_LOG_INI_TYPE                                                                                                      \
84     Ini *
85 #define FUNCTION_LOG_INI_FORMAT(value, buffer, bufferSize)                                                                         \
86     objToLog(value, "Ini", buffer, bufferSize)
87 
88 #endif
89