|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #include "string.h"
-
-
- typedef bool INI_ConfigLoadParamsStringValidator(const char*);
-
- void INI_ConfigEnsureValidString(INI_ConfigItem* item, const char* buffer) {
- if (item->validator) {
- INI_ConfigLoadParamsStringValidator* validator = item->validator;
- if (validator(buffer)) {
- memcpy(item->dest, buffer, item->type.size);
- return;
- }
- memcpy(item->dest, item->default_value, item->type.size);
- return;
- }
- memcpy(item->dest, buffer, item->type.size);
- }
-
- void INI_ConfigLoadString(INI_ConfigItem* item, const char* config_path) {
- char buffer[item->type.size];
- ini_gets(item->section, item->key, item->default_value, buffer, (int32_t) item->type.size, config_path);
- INI_ConfigEnsureValidString(item, buffer);
- }
-
- INI_ConfigSaveItemResult INI_ConfigSaveString(INI_ConfigItem* item, const char* config_path) {
- const char* dest = (const char*) item->dest;
- if (item->validator) {
- INI_ConfigLoadParamsStringValidator* validator = item->validator;
- if (!validator(dest)) {
- dest = (const char*) item->default_value;
- }
- }
-
- if (!ini_puts(item->section, item->key, dest, config_path)) {
- return INI_CONFIG_SAVE_ITEM_ERROR;
- }
-
- return INI_CONFIG_SAVE_ITEM_OK;
- }
-
- void INI_ConfigOverrideString(INI_ConfigItem* item, uint8_t argc, const char* argv[]) {
- if (!item->cmdline_option) {
- return;
- }
- const char* cmdline_buffer;
- if ((cmdline_buffer = INI_ConfigGetCommandlineOption(argc, argv, item->cmdline_option))) {
- INI_ConfigEnsureValidString(item, cmdline_buffer);
- }
- }
-
- 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);
- }
- }
-
- 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) : 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;
- }
|