|
- #include "ini-config.h"
-
- const char* INI_ConfigGetCommandlineOption(uint8_t argc, const char* argv[], const char* val) {
- size_t n = strlen(val);
- int c = argc;
-
- while (--c > 0) {
- if (!strncmp(argv[c], val, n)) {
- if (!*(argv[c] + n) && c < argc - 1) {
- /* coverity treats unchecked argv as "tainted" */
- if (!argv[c + 1] || strlen(argv[c + 1]) > 1024)
- return NULL;
- return argv[c + 1];
- }
-
- if (argv[c][n] == '=')
- return &argv[c][n + 1];
- return argv[c] + n;
- }
- }
-
- return NULL;
- }
-
- void INI_ConfigLoad(INI_ConfigItem item[], const char* config_path) {
- uint8_t i;
- for (i = 0; item[i].type.size > 0; i += 1) {
- if (!item[i].type.load) {
- continue;
- }
- item[i].type.load(&item[i], config_path, item);
- }
- }
-
- INI_ConfigSaveResult INI_ConfigSave(INI_ConfigItem item[], const char* config_path) {
- uint8_t i;
- int32_t problems = 0;
- for (i = 0; item[i].type.size > 0; i += 1) {
- int32_t result = item[i].type.save ? item[i].type.save(&item[i], config_path, item) : 0;
-
- if (result < 0) {
- problems |= (1 << (int32_t) i);
- }
- }
-
- return -problems;
- }
-
- void INI_ConfigOverride(INI_ConfigItem item[], uint8_t argc, const char* argv[]) {
- uint8_t i;
- for (i = 0; item[i].type.size > 0; i += 1) {
- if (!item[i].type.override) {
- continue;
- }
-
- item[i].type.override(&item[i], argc, argv);
- // TODO specify command-line arg external from config item?
-
- }
- }
-
- INI_ConfigInitializeResult INI_ConfigInitialize(INI_ConfigItem item[], const char* config_path, uint8_t argc, const char* argv[]) {
- INI_ConfigLoad(item, config_path);
- INI_ConfigSaveResult save_result = INI_ConfigSave(item, config_path);
- if (save_result < 0) {
- return INI_CONFIG_INITIALIZE_RESULT_ERROR;
- }
- INI_ConfigOverride(item, argc, argv);
- if (save_result > 0) {
- return INI_CONFIG_INITIALIZE_RESULT_WARNING;
- }
- return INI_CONFIG_INITIALIZE_RESULT_OK;
- }
|