@@ -10,6 +10,7 @@ dmini - DMOD INI File Parser Module
1010#include " dmini.h"
1111
1212dmini_context_t dmini_create (void);
13+ dmini_context_t dmini_create_with_token(unsigned int owner_token);
1314void dmini_destroy(dmini_context_t ctx);
1415
1516int dmini_parse_string(dmini_context_t ctx, const char* data);
@@ -33,6 +34,10 @@ int dmini_has_key(dmini_context_t ctx, const char* section, const char* key);
3334
3435int dmini_remove_section(dmini_context_t ctx, const char* section);
3536int dmini_remove_key(dmini_context_t ctx, const char* section, const char* key);
37+
38+ int dmini_set_active_section(dmini_context_t ctx, const char* section,
39+ unsigned int owner_token);
40+ int dmini_clear_active_section(dmini_context_t ctx, unsigned int owner_token);
3641```
3742
3843## DESCRIPTION
@@ -57,6 +62,11 @@ INI files consist of sections, key-value pairs, and comments:
5762**dmini_create()** creates a new INI context for storing sections and
5863key-value pairs. Returns a context pointer or NULL on error.
5964
65+ **dmini_create_with_token()** creates a new INI context protected by a magic
66+ number token. The token must be supplied to **dmini_set_active_section()** and
67+ **dmini_clear_active_section()** when a non-zero token is used. Passing token
68+ value 0 is equivalent to calling **dmini_create()**.
69+
6070**dmini_destroy()** frees all memory associated with an INI context.
6171
6272### Parsing
@@ -115,6 +125,20 @@ context. Returns DMINI_OK on success or an error code on failure.
115125NULL for section to remove from the global section. Returns DMINI_OK on
116126success or an error code on failure.
117127
128+ ### Section Visibility Restriction
129+
130+ **dmini_set_active_section()** restricts the context so that only the named
131+ section is visible to consumers of this context. While the restriction is
132+ active, all API calls treat `section == NULL` as a reference to the active
133+ section, and any attempt to access a different section returns not-found / the
134+ default value. Pass NULL as section to restrict to the global (unnamed)
135+ section. If the context was created with a non-zero token, the matching token
136+ must be supplied; otherwise DMINI_ERR_LOCKED is returned.
137+
138+ **dmini_clear_active_section()** removes the active-section restriction so
139+ that the full content of the context becomes visible again. Requires the
140+ correct token when the context was created with a non-zero token.
141+
118142## RETURN VALUES
119143
120144Functions return the following error codes:
@@ -125,6 +149,7 @@ Functions return the following error codes:
125149* **DMINI_ERR_INVALID** (-3) - Invalid parameter
126150* **DMINI_ERR_NOT_FOUND** (-4) - Section or key not found
127151* **DMINI_ERR_FILE** (-5) - File I/O error
152+ * **DMINI_ERR_LOCKED** (-6) - Wrong owner token supplied to set/clear active section
128153
129154## EXAMPLES
130155
@@ -182,6 +207,35 @@ dmini_set_string(ctx, NULL, "global_key", "global_value");
182207const char* val = dmini_get_string(ctx, NULL, "global_key", "default");
183208```
184209
210+ ### Section Visibility Restriction
211+
212+ ``` c
213+ // Create a context protected by a token
214+ dmini_context_t ctx = dmini_create_with_token(0xDEADBEEF );
215+
216+ dmini_parse_file (ctx, "config.ini");
217+
218+ // Restrict visibility to the "network" section
219+ dmini_set_active_section(ctx, "network", 0xDEADBEEF);
220+
221+ // All NULL-section calls now resolve to "network"
222+ const char* host = dmini_get_string(ctx, NULL, "host", "localhost");
223+ int port = dmini_get_int(ctx, NULL, "port", 80);
224+
225+ // Accessing any other section returns not-found / default
226+ const char* db = dmini_get_string(ctx, "database", "host", "n/a");
227+ // db == "n/a"
228+
229+ // Wrong token returns DMINI_ERR_LOCKED
230+ int rc = dmini_clear_active_section(ctx, 0x12345678);
231+ // rc == DMINI_ERR_LOCKED
232+
233+ // Correct token clears the restriction
234+ dmini_clear_active_section(ctx, 0xDEADBEEF);
235+
236+ dmini_destroy(ctx);
237+ ```
238+
185239## MEMORY FOOTPRINT
186240
187241* **dmini library**: 536B RAM, 5KB ROM
0 commit comments