Skip to content

Commit b0b032d

Browse files
committed
New INI File API.
Signed-off-by: Timothy Rule (VM/EMT3) <[email protected]>
1 parent 0b10f3a commit b0b032d

File tree

17 files changed

+740
-11
lines changed

17 files changed

+740
-11
lines changed

doc/Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ DOC_TITLE_marshal := "Marshal API Reference"
3636
DOC_INPUT_mdf := dse/clib/mdf/mdf.h
3737
DOC_CDIR_mdf := dse/clib/mdf/mdf.c
3838
DOC_OUTPUT_mdf := doc/content/apis/clib/mdf/index.md
39-
DOC_LINKTITLE_mdf := Mdf
40-
DOC_TITLE_mdf := "Mdf API Reference"
39+
DOC_LINKTITLE_mdf := MDF
40+
DOC_TITLE_mdf := "MDF API Reference"
41+
42+
# Module "ini"
43+
DOC_INPUT_ini := dse/clib/ini/ini.h
44+
DOC_CDIR_ini := dse/clib/ini/ini.c
45+
DOC_OUTPUT_ini := doc/content/apis/clib/ini/index.md
46+
DOC_LINKTITLE_ini := INI File
47+
DOC_TITLE_ini := "INI FIle API Reference"
4148

4249
# Module "schedule"
4350
DOC_INPUT_schedule := dse/clib/schedule/schedule.h
@@ -47,13 +54,14 @@ DOC_LINKTITLE_schedule := Schedule
4754
DOC_TITLE_schedule := "Schedule API Reference"
4855

4956
# Targets
50-
DOC_C_MODULES := marshal mdf schedule
57+
DOC_C_MODULES := marshal mdf ini schedule
5158

5259

5360
.PHONY: examples
5461
examples:
5562
cd ..;mkdir -p doc/content/apis/clib/examples
5663
cd ..;cp dse/clib/examples/mdf_file/mdf_file.c doc/content/apis/clib/examples/mdf_file.c
64+
cd ..;cp dse/clib/examples/ini_file/ini_file.c doc/content/apis/clib/examples/ini_file.c
5765
cd ..;cp dse/clib/examples/schedule/schedule.c doc/content/apis/clib/examples/schedule.c
5866

5967
.PHONY: index
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 Robert Bosch GmbH
2+
3+
#include <stdio.h>
4+
#include <dse/clib/ini/ini.h>
5+
6+
#define INI_FILE "dse_clib.ini"
7+
8+
int main(void)
9+
{
10+
// Open and read an INI file (if it exists).
11+
IniDesc ini = ini_open(INI_FILE);
12+
13+
// Get an INI value.
14+
printf("foo (initial value) = %s\n", ini_get_val(&ini, "foo"));
15+
16+
// Set an INI value.
17+
ini_set_val(&ini, "foo", "fubar", true); /* Overwrite existing value. */
18+
ini_set_val(&ini, "bar", "bar", false); /* Only if not already set. */
19+
printf("foo (updated value) = %s\n", ini_get_val(&ini, "foo"));
20+
printf("bar (default value) = %s\n", ini_get_val(&ini, "bar"));
21+
22+
// Expand an environment var.
23+
ini_set_val(&ini, "user", "${USER}", true);
24+
ini_expand_vars(&ini);
25+
printf("user (env value) = %s\n", ini_get_val(&ini, "user"));
26+
27+
// Delete a value.
28+
ini_delete_key(&ini, "user");
29+
printf("user (deleted) = %s\n", ini_get_val(&ini, "user"));
30+
31+
// Save the INI file.
32+
ini_write(&ini, INI_FILE);
33+
34+
// Close the desc object (and release used memory).
35+
ini_close(&ini);
36+
return 0;
37+
}

doc/content/apis/clib/ini/index.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
title: INI FIle API Reference
3+
linkTitle: INI
4+
---
5+
## INI File API
6+
7+
8+
Simple INI File API for reading and modifying INI files.
9+
10+
11+
### Example
12+
13+
14+
The following example demonstrates how to use the INI File API.
15+
16+
{{< readfile file="../examples/ini_file.c" code="true" lang="c" >}}
17+
18+
19+
20+
21+
## Typedefs
22+
23+
### IniDesc
24+
25+
```c
26+
typedef struct IniDesc {
27+
int lines;
28+
}
29+
```
30+
31+
## Functions
32+
33+
### ini_close
34+
35+
Release any resources allocated by the INI File object.
36+
37+
#### Parameters
38+
39+
ini (IniDesc*)
40+
: INI File object.
41+
42+
43+
44+
### ini_delete_key
45+
46+
Delete the specified key from the INI File object.
47+
48+
#### Parameters
49+
50+
ini (IniDesc*)
51+
: INI File object.
52+
53+
key (const char*)
54+
: The key to delete.
55+
56+
57+
58+
### ini_expand_vars
59+
60+
Expand environment variables contained within the INI File values.
61+
62+
#### Example INI File
63+
64+
```ini
65+
user=${USER:-default-user}`
66+
```
67+
68+
#### Parameters
69+
70+
ini (IniDesc*)
71+
: INI File object.
72+
73+
74+
75+
### ini_get_val
76+
77+
Set a key-value pair on the INI File object.
78+
79+
#### Parameters
80+
81+
ini (IniDesc*)
82+
: INI File object.
83+
84+
key (const char*)
85+
: The key to get.
86+
87+
overwrite (bool)
88+
: When true, if the key already exists then overwrite with the provided value.
89+
90+
#### Returns
91+
92+
char*
93+
: The corresponding value of key, or NULL if key is was not found.
94+
95+
96+
97+
### ini_open
98+
99+
Configure and load an INI File object.
100+
101+
#### Parameters
102+
103+
path (const char*)
104+
: Path to an INI File to load. If NULL or missing no error will occur.
105+
106+
#### Returns
107+
108+
IniDesc (struct)
109+
: INI File object.
110+
111+
112+
113+
### ini_set_val
114+
115+
Set a key-value pair on the INI File object.
116+
117+
#### Parameters
118+
119+
ini (IniDesc*)
120+
: INI File object.
121+
122+
key (const char*)
123+
: The key to set.
124+
125+
val (const char*)
126+
: The corresponding value to set.
127+
128+
overwrite (bool)
129+
: When true, if the key already exists then overwrite with the provided value.
130+
131+
132+
133+
### ini_write
134+
135+
Write the key-value pair of the INI File object to the named file.
136+
137+
#### Parameters
138+
139+
ini (IniDesc*)
140+
: INI File object.
141+
142+
path (const char*)
143+
: The file to write the key-value pairs to.
144+
145+
146+

doc/content/apis/clib/mdf/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Mdf API Reference
3-
linkTitle: Mdf
2+
title: MDF API Reference
3+
linkTitle: MDF
44
---
55
## MDF API
66

@@ -70,7 +70,7 @@ typedef struct MdfDesc {
7070

7171
### mdf_create
7272

73-
Create and configure an `MdfDesc` object to represet an MDF stream.
73+
Create and configure an `MdfDesc` object to represent an MDF stream.
7474

7575
#### Parameters
7676

dse/clib/collections/vector.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
typedef struct Vector Vector;
1717
typedef int (*VectorCompar)(const void*, const void*);
1818
typedef int (*VectorRangeCallback)(void* item, void* data);
19+
typedef void (*VectorItemDestroy)(void* item, void* data);
1920

2021

2122
typedef struct Vector {
@@ -111,6 +112,17 @@ static __inline__ void* vector_at(Vector* v, size_t index, void* item)
111112
return v->items + offset;
112113
}
113114

115+
static __inline__ void* vector_set_at(Vector* v, size_t index, void* item)
116+
{
117+
if (v == NULL || v->items == NULL) return NULL;
118+
if (index >= v->length) return NULL;
119+
size_t offset = index * v->item_size;
120+
if (item) {
121+
memcpy(v->items + offset, item, v->item_size);
122+
}
123+
return v->items + offset;
124+
}
125+
114126
static __inline__ int vector_delete_at(Vector* v, size_t index)
115127
{
116128
if (v == NULL) return -EINVAL;
@@ -175,10 +187,17 @@ static __inline__ int vector_range(Vector* v, void* from_key, void* to_key,
175187
return 0;
176188
}
177189

178-
static __inline__ void vector_clear(Vector* v)
190+
static __inline__ void vector_clear(
191+
Vector* v, VectorItemDestroy func, void* data)
179192
{
180193
if (v == NULL) return;
181194
if (v->items == NULL) return;
195+
if (func) {
196+
for (size_t i = 0; i < v->length; i++) {
197+
void* item = v->items + (i * v->item_size);
198+
func(item, data);
199+
}
200+
}
182201
memset(v->items, 0, v->capacity * v->item_size);
183202
v->length = 0;
184203
}

dse/clib/examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ set(DSE_CLIB_INCLUDE_DIR "${DSE_CLIB_SOURCE_DIR}/../..")
3737
# Examples
3838
# --------
3939
add_subdirectory(mdf_file)
40+
add_subdirectory(ini_file)
4041
add_subdirectory(interceptor)
4142
add_subdirectory(schedule)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2025 Robert Bosch GmbH
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
cmake_minimum_required(VERSION 3.21)
6+
7+
# set(CMAKE_VERBOSE_MAKEFILE ON)
8+
9+
project(ini_file VERSION ${VERSION})
10+
set(EXAMPLE_PATH "examples/ini_file")
11+
set(TARGET "ini")
12+
13+
add_executable(${TARGET}
14+
${DSE_CLIB_SOURCE_DIR}/ini/ini.c
15+
${DSE_CLIB_SOURCE_DIR}/util/strings.c
16+
ini_file.c
17+
)
18+
target_include_directories(${TARGET}
19+
PRIVATE
20+
${DSE_CLIB_INCLUDE_DIR}
21+
)
22+
install(
23+
TARGETS
24+
${TARGET}
25+
DESTINATION
26+
${EXAMPLE_PATH}/bin
27+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 Robert Bosch GmbH
2+
3+
#include <stdio.h>
4+
#include <dse/clib/ini/ini.h>
5+
6+
#define INI_FILE "dse_clib.ini"
7+
8+
int main(void)
9+
{
10+
// Open and read an INI file (if it exists).
11+
IniDesc ini = ini_open(INI_FILE);
12+
13+
// Get an INI value.
14+
printf("foo (initial value) = %s\n", ini_get_val(&ini, "foo"));
15+
16+
// Set an INI value.
17+
ini_set_val(&ini, "foo", "fubar", true); /* Overwrite existing value. */
18+
ini_set_val(&ini, "bar", "bar", false); /* Only if not already set. */
19+
printf("foo (updated value) = %s\n", ini_get_val(&ini, "foo"));
20+
printf("bar (default value) = %s\n", ini_get_val(&ini, "bar"));
21+
22+
// Expand an environment var.
23+
ini_set_val(&ini, "user", "${USER}", true);
24+
ini_expand_vars(&ini);
25+
printf("user (env value) = %s\n", ini_get_val(&ini, "user"));
26+
27+
// Delete a value.
28+
ini_delete_key(&ini, "user");
29+
printf("user (deleted) = %s\n", ini_get_val(&ini, "user"));
30+
31+
// Save the INI file.
32+
ini_write(&ini, INI_FILE);
33+
34+
// Close the desc object (and release used memory).
35+
ini_close(&ini);
36+
return 0;
37+
}

0 commit comments

Comments
 (0)