Skip to content

Commit dfb736f

Browse files
committed
Add array decoding in ConfInventory()
1 parent 8b08c33 commit dfb736f

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/libnffile/conf/nfconf.c

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ int ConfGetValue(char *key) {
296296

297297
} // End of ConfGetValue
298298

299-
static void ConfPrintValue(toml_table_t *sectionConf, const char *tableName, const char *entry) {
299+
static void ConfPrintTableValue(toml_table_t *sectionConf, const char *tableName, const char *entry) {
300300
toml_value_t val;
301301
val = toml_table_string(sectionConf, entry);
302302
if (val.ok) {
@@ -319,7 +319,34 @@ static void ConfPrintValue(toml_table_t *sectionConf, const char *tableName, con
319319
// printf("%10s time : %s\n", entry, val.u.ts);
320320
}
321321

322-
} // End of ConfPrintValue
322+
} // End of ConfTablePrintValue
323+
324+
static void ConfPrintArrayValue(toml_array_t *sectionConf, const char *arrayName, int entry) {
325+
toml_value_t val;
326+
val = toml_array_string(sectionConf, entry);
327+
if (val.ok) {
328+
printf("%s:[%d] string : %s\n", arrayName, entry, val.u.s);
329+
}
330+
val = toml_array_bool(sectionConf, entry);
331+
if (val.ok) {
332+
printf("%s:[%d] bool : %i\n", arrayName, entry, val.u.b);
333+
}
334+
val = toml_array_int(sectionConf, entry);
335+
if (val.ok) {
336+
printf("%s:[%d] int : %lld\n", arrayName, entry, val.u.i);
337+
}
338+
val = toml_array_double(sectionConf, entry);
339+
if (val.ok) {
340+
printf("%s:[%d] double : %f\n", arrayName, entry, val.u.d);
341+
}
342+
val = toml_array_timestamp(sectionConf, entry);
343+
if (val.ok) {
344+
// printf("%10s time : %s\n", entry, val.u.ts);
345+
}
346+
347+
} // End of ConfPrintArrayValue
348+
349+
static void ConfPrintArray(toml_array_t *sectionConf, const char *arrayName);
323350

324351
static void ConfPrintTable(toml_table_t *sectionConf, const char *tableName) {
325352
int len = toml_table_len(sectionConf);
@@ -332,15 +359,33 @@ static void ConfPrintTable(toml_table_t *sectionConf, const char *tableName) {
332359
toml_array_t *a = toml_table_array(sectionConf, entry);
333360
toml_table_t *t = toml_table_table(sectionConf, entry);
334361
if (a) {
335-
printf("%s:%s is an array\n", tableName, entry);
362+
printf("%s:%s is an array ", tableName, entry);
363+
ConfPrintArray(a, entry);
336364
} else if (t) {
337365
printf("\n%s:%s is a table ", tableName, entry);
338366
ConfPrintTable(t, entry);
339367
} else {
340-
ConfPrintValue(sectionConf, tableName, entry);
368+
ConfPrintTableValue(sectionConf, tableName, entry);
369+
}
370+
}
371+
} // End of ConfPrintTable
372+
373+
static void ConfPrintArray(toml_array_t *sectionConf, const char *arrayName) {
374+
int len = toml_array_len(sectionConf);
375+
printf("with %d entries:\n", len);
376+
for (int i = 0; i < len; i++) {
377+
toml_array_t *a = toml_array_array(sectionConf, i);
378+
toml_table_t *t = toml_array_table(sectionConf, i);
379+
if (a) {
380+
printf("%s:[%d] is an array ", arrayName, i);
381+
} else if (t) {
382+
printf("\n%s:[%d] is a table ", arrayName, i);
383+
ConfPrintTable(t, "anonymous");
384+
} else {
385+
ConfPrintArrayValue(sectionConf, arrayName, i);
341386
}
342387
}
343-
}
388+
} // End of ConfPrintArray
344389

345390
void ConfInventory(char *confFile) {
346391
if (!confFile) return;

src/libnffile/conf/toml.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
* SOFTWARE.
24+
*
25+
* Modified version:
26+
* https://github.com/arp242/toml-c
27+
*
2428
*/
2529

2630
#define _POSIX_C_SOURCE 200809L

0 commit comments

Comments
 (0)