Object | +---DConfigTree
The ConfigTree class implements methods for storing config options, including reading and writing to a config file, and changing and removing options. Config options are structured in sections with multiple options with values.
#include <stdio.h> #include "ofc/DConfig.h" #include "ofc/DFile.h" int main(int argc, char *argv[]) { DConfigTree *tree = [DConfigTree new]; DFile *file = [DFile new]; char name1[] = "example.ini"; char name2[] = "example2.ini"; if ([file open :name1 :"r"]) // Open the config file { if ([tree read :file :name1]) // Read the config file { [file close]; printf("Config %s the option Option1 in SectionA.\n", ([tree has :"SectionA" :"Option1"] ? "has" : "has not")); // Check for an option if ([tree set :"SectionB" :"Option2" :"Value2"]) // Insert an option printf("SectionB - Option2 succesfully inserted.\n"); else printf("Could not insert an option for sectionB.\n"); if ([tree remove :"SectionA" :"Option2"]) // Remove an option printf("SectionA - Option2 succesfully removed.\n"); else printf("Could not remove option2 for sectionA.\n"); if ([file open :name2 :"w"]) // Open the config file for writing { if ([tree write :file :name2]) // Write the modified config file printf("Config tree succesfully written in \"%s\".\n", name2); else printf("Could not write \"%s\".\n", name2); [file close]; } else printf("Could not write \"%s\".\n", name2); } else { [file close]; printf("Could not read \"%s\".\n", name1); } } else printf("Could not open \"%s\":%d.\n", name1, [file error]); [file free]; // Cleanup [tree free]; return 0; }