From bff1ce5f8adfdca2882b1e58549059f541bf086f Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sat, 4 Mar 2023 19:43:39 +0800 Subject: [PATCH] Add reference to item array parameter Some config items may refer to other config items values for transformations. --- source/ini-config.c | 4 ++-- source/ini-config.h | 4 ++-- source/types/int.c | 14 +++++++------- source/types/string.c | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/source/ini-config.c b/source/ini-config.c index 7558eb4..642ab9b 100644 --- a/source/ini-config.c +++ b/source/ini-config.c @@ -28,7 +28,7 @@ void INI_ConfigLoad(INI_ConfigItem item[], const char* config_path) { if (!item[i].type.load) { continue; } - item[i].type.load(&item[i], config_path); + item[i].type.load(&item[i], config_path, item); } } @@ -36,7 +36,7 @@ INI_ConfigSaveResult INI_ConfigSave(INI_ConfigItem item[], const char* config_pa 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; + 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); diff --git a/source/ini-config.h b/source/ini-config.h index aae3e73..f6e735b 100644 --- a/source/ini-config.h +++ b/source/ini-config.h @@ -29,7 +29,7 @@ struct INI_ConfigItem; /** * Function for loading a config item value from file to memory. */ -typedef void INI_ConfigTypeLoad(struct INI_ConfigItem*, const char*); +typedef void INI_ConfigTypeLoad(struct INI_ConfigItem*, const char*, void*); /** * Result enum for saving config items. @@ -49,7 +49,7 @@ typedef enum { /** * Function for saving a config item value from memory to file. */ -typedef INI_ConfigSaveItemResult INI_ConfigTypeSave(struct INI_ConfigItem*, const char*); +typedef INI_ConfigSaveItemResult INI_ConfigTypeSave(struct INI_ConfigItem*, const char*, void*); /** * Function for retrieving a config item value from the command-line to memory. diff --git a/source/types/int.c b/source/types/int.c index e517759..a1fd2cd 100644 --- a/source/types/int.c +++ b/source/types/int.c @@ -3,9 +3,9 @@ #define INI_CONFIG_IMPLEMENT_INT_TYPE(ID, T) \ typedef bool INI_ConfigValidate##ID(T); \ \ -typedef T INI_ConfigDeserialize##ID(const char*); \ +typedef T INI_ConfigDeserialize##ID(const char*, void*); \ \ -typedef void INI_ConfigSerialize##ID(T, const char[128]); \ +typedef void INI_ConfigSerialize##ID(T, const char[128], void*); \ \ void INI_ConfigEnsureValid##ID(INI_ConfigItem* item, T raw_value, T default_value) { \ T* dest = item->dest; \ @@ -21,7 +21,7 @@ void INI_ConfigEnsureValid##ID(INI_ConfigItem* item, T raw_value, T default_valu *dest = raw_value; \ } \ \ -void INI_ConfigLoad##ID(INI_ConfigItem* item, const char* config_path) { \ +void INI_ConfigLoad##ID(INI_ConfigItem* item, const char* config_path, void* item_array) { \ static T raw_value; \ static T default_value; \ default_value = *((T*) item->default_value); \ @@ -30,18 +30,18 @@ void INI_ConfigLoad##ID(INI_ConfigItem* item, const char* config_path) { \ INI_ConfigSerialize##ID* serialize = item->transformer.serialize; \ const char serialized_default_value[128]; \ if (default_value) { \ - serialize(default_value, serialized_default_value); \ + serialize(default_value, serialized_default_value, item_array); \ } \ char buffer[128]; \ ini_gets(item->section, item->key, serialized_default_value, buffer, 128, config_path); \ - raw_value = deserialize(buffer); \ + raw_value = deserialize(buffer, item_array); \ } else { \ raw_value = ini_getl(item->section, item->key, default_value, config_path); \ } \ INI_ConfigEnsureValid##ID(item, raw_value, default_value); \ } \ \ -INI_ConfigSaveItemResult INI_ConfigSave##ID(INI_ConfigItem* item, const char* config_path) { \ +INI_ConfigSaveItemResult INI_ConfigSave##ID(INI_ConfigItem* item, const char* config_path, void* item_array) { \ T dest = *((T*) item->dest); \ if (item->validator) { \ INI_ConfigValidate##ID* validate = item->validator; \ @@ -53,7 +53,7 @@ INI_ConfigSaveItemResult INI_ConfigSave##ID(INI_ConfigItem* item, const char* co if (item->transformer.deserialize && item->transformer.serialize) { \ INI_ConfigSerialize##ID* serialize = item->transformer.serialize; \ const char serialized_value[128]; \ - serialize(dest, serialized_value); \ + serialize(dest, serialized_value, item_array); \ if (!ini_puts(item->section, item->key, serialized_value, config_path)) { \ return -1; \ } \ diff --git a/source/types/string.c b/source/types/string.c index 36a36c8..b01332b 100644 --- a/source/types/string.c +++ b/source/types/string.c @@ -15,13 +15,13 @@ void INI_ConfigEnsureValidString(INI_ConfigItem* item, const char* buffer) { memcpy(item->dest, buffer, item->type.size); } -void INI_ConfigLoadString(INI_ConfigItem* item, const char* config_path) { +void INI_ConfigLoadString(INI_ConfigItem* item, const char* config_path, void* item_array) { 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) { +INI_ConfigSaveItemResult INI_ConfigSaveString(INI_ConfigItem* item, const char* config_path, void* item_array) { const char* dest = (const char*) item->dest; if (item->validator) { INI_ConfigLoadParamsStringValidator* validator = item->validator;