Skip to content

Commit 616bcde

Browse files
authored
Merge pull request #10 from choco-technologies/copilot/add-documentation-for-visibility-restrictions
docs: add section visibility restriction API documentation
2 parents bdd0455 + 147de22 commit 616bcde

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ dmini is a lightweight INI file parser/generator module designed for embedded sy
1616
- **Global Section Support**: Handle keys without section headers
1717
- **Comment Support**: Parse comments starting with `;` or `#`
1818
- **Whitespace Trimming**: Automatic trimming of keys and values
19+
- **Section Visibility Restriction**: Limit the visible scope of a context to a single section, with optional token-based protection
1920

2021
## API
2122

2223
### Context Management
2324
- `dmini_create()` - Create INI context
25+
- `dmini_create_with_token(owner_token)` - Create INI context protected by an owner token
2426
- `dmini_destroy()` - Free INI context
2527

2628
### Parsing
@@ -41,6 +43,10 @@ dmini is a lightweight INI file parser/generator module designed for embedded sy
4143
- `dmini_has_section(ctx, section)` - Check if section exists
4244
- `dmini_has_key(ctx, section, key)` - Check if key exists
4345

46+
### Section Visibility Restriction
47+
- `dmini_set_active_section(ctx, section, owner_token)` - Restrict the context to a single section
48+
- `dmini_clear_active_section(ctx, owner_token)` - Remove the section restriction
49+
4450
### Removal
4551
- `dmini_remove_section(ctx, section)` - Remove entire section
4652
- `dmini_remove_key(ctx, section, key)` - Remove single key
@@ -76,6 +82,47 @@ dmini_generate_file(ctx, "output.ini");
7682
dmini_destroy(ctx);
7783
```
7884
85+
## Section Visibility Restriction
86+
87+
The section visibility restriction feature allows a context to be locked to a
88+
single section. While the restriction is active all API calls treat
89+
`section == NULL` as a reference to the active section, and any attempt to
90+
access a different section returns not-found / the default value.
91+
92+
This is useful when you want to pass a context to a component that should only
93+
see one section of a larger configuration file.
94+
95+
An optional **owner token** can be set at creation time so that only the
96+
creator can change or clear the restriction.
97+
98+
```c
99+
// Create a context protected by a token
100+
dmini_context_t ctx = dmini_create_with_token(0xDEADBEEF);
101+
102+
dmini_parse_file(ctx, "config.ini");
103+
104+
// Restrict visibility to the "network" section
105+
dmini_set_active_section(ctx, "network", 0xDEADBEEF);
106+
107+
// All NULL-section calls now resolve to "network"
108+
const char* host = dmini_get_string(ctx, NULL, "host", "localhost");
109+
int port = dmini_get_int(ctx, NULL, "port", 80);
110+
111+
// Accessing any other section returns not-found / default
112+
const char* val = dmini_get_string(ctx, "database", "host", "n/a");
113+
// val == "n/a"
114+
115+
// Remove the restriction (requires the correct token)
116+
dmini_clear_active_section(ctx, 0xDEADBEEF);
117+
118+
dmini_destroy(ctx);
119+
```
120+
121+
If a wrong token is supplied to `dmini_set_active_section()` or
122+
`dmini_clear_active_section()` the function returns `DMINI_ERR_LOCKED (-6)`.
123+
A token value of `0` disables token protection (any caller may change the
124+
active section).
125+
79126
## Building
80127

81128
```bash
@@ -106,6 +153,7 @@ Test coverage includes:
106153
- Buffer generation with size queries
107154
- File I/O operations
108155
- Comments and whitespace handling
156+
- Section visibility restriction (active section with and without token protection)
109157

110158
## INI File Format
111159

apps/test_dmini/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The application tests the following dmini functionality:
4949

5050
1. **Context Management**
5151
- `dmini_create()` - Create INI context
52+
- `dmini_create_with_token()` - Create INI context with owner token
5253
- `dmini_destroy()` - Free INI context
5354

5455
2. **Parsing**
@@ -81,6 +82,11 @@ The application tests the following dmini functionality:
8182
- Whitespace trimming
8283
- Empty sections
8384

85+
8. **Section Visibility Restriction**
86+
- `dmini_set_active_section()` - Restrict context to a single section
87+
- `dmini_clear_active_section()` - Remove the section restriction
88+
- Token-based protection (`DMINI_ERR_LOCKED` on wrong token)
89+
8490
## Integration with CI
8591

8692
The test application is automatically run as part of the CI pipeline:

docs/dmini.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dmini - DMOD INI File Parser Module
1010
#include "dmini.h"
1111

1212
dmini_context_t dmini_create(void);
13+
dmini_context_t dmini_create_with_token(unsigned int owner_token);
1314
void dmini_destroy(dmini_context_t ctx);
1415

1516
int 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

3435
int dmini_remove_section(dmini_context_t ctx, const char* section);
3536
int 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
5863
key-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.
115125
NULL for section to remove from the global section. Returns DMINI_OK on
116126
success 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
120144
Functions 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");
182207
const 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

Comments
 (0)