Browse Source

Add other int types

Complete all int types for configs.
master
TheoryOfNekomata 1 year ago
parent
commit
8de2176284
2 changed files with 77 additions and 44 deletions
  1. +16
    -13
      ini-config.c
  2. +61
    -31
      ini-config.h

+ 16
- 13
ini-config.c View File

@@ -109,6 +109,9 @@ void INI_ConfigOverride##ID(INI_ConfigItem* item, uint8_t argc, const char* argv


INI_CONFIG_REGISTER_INT_TYPE(U8, uint8_t); INI_CONFIG_REGISTER_INT_TYPE(U8, uint8_t);
INI_CONFIG_REGISTER_INT_TYPE(U16, uint16_t); INI_CONFIG_REGISTER_INT_TYPE(U16, uint16_t);
INI_CONFIG_REGISTER_INT_TYPE(U32, uint32_t);
INI_CONFIG_REGISTER_INT_TYPE(I8, int8_t);
INI_CONFIG_REGISTER_INT_TYPE(I16, int16_t);
INI_CONFIG_REGISTER_INT_TYPE(I32, int32_t); INI_CONFIG_REGISTER_INT_TYPE(I32, int32_t);


typedef bool INI_ConfigLoadParamsStringValidator(const char*); typedef bool INI_ConfigLoadParamsStringValidator(const char*);
@@ -117,18 +120,18 @@ void INI_ConfigEnsureValidString(INI_ConfigItem* item, const char* buffer) {
if (item->validator) { if (item->validator) {
INI_ConfigLoadParamsStringValidator* validator = item->validator; INI_ConfigLoadParamsStringValidator* validator = item->validator;
if (validator(buffer)) { if (validator(buffer)) {
memcpy(item->dest, buffer, item->fns.size);
memcpy(item->dest, buffer, item->type.size);
return; return;
} }
memcpy(item->dest, item->default_value, item->fns.size);
memcpy(item->dest, item->default_value, item->type.size);
return; return;
} }
memcpy(item->dest, buffer, item->fns.size);
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) {
char buffer[item->fns.size];
ini_gets(item->section, item->key, item->default_value, buffer, (int32_t) item->fns.size, 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_ConfigEnsureValidString(item, buffer);
} }


@@ -160,19 +163,19 @@ void INI_ConfigOverrideString(INI_ConfigItem* item, uint8_t argc, const char* ar


void INI_ConfigLoad(INI_ConfigItem item[], const char* config_path) { void INI_ConfigLoad(INI_ConfigItem item[], const char* config_path) {
uint8_t i; uint8_t i;
for (i = 0; item[i].fns.size > 0; i += 1) {
if (!item[i].fns.load) {
for (i = 0; item[i].type.size > 0; i += 1) {
if (!item[i].type.load) {
continue; continue;
} }
item[i].fns.load(&item[i], config_path);
item[i].type.load(&item[i], config_path);
} }
} }


INI_ConfigSaveResult INI_ConfigSave(INI_ConfigItem item[], const char* config_path) { INI_ConfigSaveResult INI_ConfigSave(INI_ConfigItem item[], const char* config_path) {
uint8_t i; uint8_t i;
int32_t problems = 0; int32_t problems = 0;
for (i = 0; item[i].fns.size > 0; i += 1) {
int32_t result = item[i].fns.save ? item[i].fns.save(&item[i], config_path) : 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) { if (result < 0) {
problems |= (1 << (int32_t) i); problems |= (1 << (int32_t) i);
@@ -184,12 +187,12 @@ INI_ConfigSaveResult INI_ConfigSave(INI_ConfigItem item[], const char* config_pa


void INI_ConfigOverride(INI_ConfigItem item[], uint8_t argc, const char* argv[]) { void INI_ConfigOverride(INI_ConfigItem item[], uint8_t argc, const char* argv[]) {
uint8_t i; uint8_t i;
for (i = 0; item[i].fns.size > 0; i += 1) {
if (!item[i].fns.override) {
for (i = 0; item[i].type.size > 0; i += 1) {
if (!item[i].type.override) {
continue; continue;
} }


item[i].fns.override(&item[i], argc, argv);
item[i].type.override(&item[i], argc, argv);
// TODO specify command-line arg external from config item? // TODO specify command-line arg external from config item?


} }


+ 61
- 31
ini-config.h View File

@@ -14,25 +14,25 @@ typedef struct {


struct INI_ConfigItem; struct INI_ConfigItem;


typedef void INI_ConfigLoadType(struct INI_ConfigItem*, const char*);
typedef void INI_ConfigTypeLoad(struct INI_ConfigItem*, const char*);


typedef enum { typedef enum {
INI_CONFIG_SAVE_ITEM_ERROR = -1, INI_CONFIG_SAVE_ITEM_ERROR = -1,
INI_CONFIG_SAVE_ITEM_OK, INI_CONFIG_SAVE_ITEM_OK,
} INI_ConfigSaveItemResult; } INI_ConfigSaveItemResult;


typedef INI_ConfigSaveItemResult INI_ConfigSaveType(struct INI_ConfigItem*, const char*);
typedef INI_ConfigSaveItemResult INI_ConfigTypeSave(struct INI_ConfigItem*, const char*);


typedef void INI_ConfigOverrideType(struct INI_ConfigItem*, uint8_t, const char*[]);
typedef void INI_ConfigTypeOverride(struct INI_ConfigItem*, uint8_t, const char*[]);
typedef struct { typedef struct {
size_t size; size_t size;
INI_ConfigLoadType* load;
INI_ConfigSaveType* save;
INI_ConfigOverrideType* override;
} INI_ConfigTypeFns;
INI_ConfigTypeLoad* load;
INI_ConfigTypeSave* save;
INI_ConfigTypeOverride* override;
} INI_ConfigType;


typedef struct INI_ConfigItem { typedef struct INI_ConfigItem {
INI_ConfigTypeFns fns;
INI_ConfigType type;
const char* section; const char* section;
const char* key; const char* key;
const char* cmdline_option; const char* cmdline_option;
@@ -60,65 +60,95 @@ INI_ConfigSaveResult INI_ConfigSave(INI_ConfigItem[], const char*);


void INI_ConfigLoadU8(INI_ConfigItem*, const char*); void INI_ConfigLoadU8(INI_ConfigItem*, const char*);
void INI_ConfigLoadU16(INI_ConfigItem*, const char*); void INI_ConfigLoadU16(INI_ConfigItem*, const char*);
void INI_ConfigLoadU32(INI_ConfigItem*, const char*);
void INI_ConfigLoadI8(INI_ConfigItem*, const char*);
void INI_ConfigLoadI16(INI_ConfigItem*, const char*);
void INI_ConfigLoadI32(INI_ConfigItem*, const char*); void INI_ConfigLoadI32(INI_ConfigItem*, const char*);
void INI_ConfigLoadString(INI_ConfigItem*, const char*); void INI_ConfigLoadString(INI_ConfigItem*, const char*);


INI_ConfigSaveItemResult INI_ConfigSaveU8(INI_ConfigItem*, const char*); INI_ConfigSaveItemResult INI_ConfigSaveU8(INI_ConfigItem*, const char*);
INI_ConfigSaveItemResult INI_ConfigSaveU16(INI_ConfigItem*, const char*); INI_ConfigSaveItemResult INI_ConfigSaveU16(INI_ConfigItem*, const char*);
INI_ConfigSaveItemResult INI_ConfigSaveU32(INI_ConfigItem*, const char*);
INI_ConfigSaveItemResult INI_ConfigSaveI8(INI_ConfigItem*, const char*);
INI_ConfigSaveItemResult INI_ConfigSaveI16(INI_ConfigItem*, const char*);
INI_ConfigSaveItemResult INI_ConfigSaveI32(INI_ConfigItem*, const char*); INI_ConfigSaveItemResult INI_ConfigSaveI32(INI_ConfigItem*, const char*);
INI_ConfigSaveItemResult INI_ConfigSaveString(INI_ConfigItem*, const char*); INI_ConfigSaveItemResult INI_ConfigSaveString(INI_ConfigItem*, const char*);


void INI_ConfigOverrideU8(INI_ConfigItem*, uint8_t, const char*[]); void INI_ConfigOverrideU8(INI_ConfigItem*, uint8_t, const char*[]);
void INI_ConfigOverrideU16(INI_ConfigItem*, uint8_t, const char*[]); void INI_ConfigOverrideU16(INI_ConfigItem*, uint8_t, const char*[]);
void INI_ConfigOverrideU32(INI_ConfigItem*, uint8_t, const char*[]);
void INI_ConfigOverrideI8(INI_ConfigItem*, uint8_t, const char*[]);
void INI_ConfigOverrideI16(INI_ConfigItem*, uint8_t, const char*[]);
void INI_ConfigOverrideI32(INI_ConfigItem*, uint8_t, const char*[]); void INI_ConfigOverrideI32(INI_ConfigItem*, uint8_t, const char*[]);
void INI_ConfigOverrideString(INI_ConfigItem*, uint8_t, const char*[]); void INI_ConfigOverrideString(INI_ConfigItem*, uint8_t, const char*[]);


#define INI_CONFIG_TYPE_FNS_I32 (INI_ConfigTypeFns) { \
.size = sizeof(int32_t), \
.load = INI_ConfigLoadI32, \
.save = INI_ConfigSaveI32, \
.override = INI_ConfigOverrideI32, \
#define INI_CONFIG_TYPE_U8 (INI_ConfigType) { \
.size = sizeof(uint8_t), \
.load = INI_ConfigLoadU8, \
.save = INI_ConfigSaveU8, \
.override = INI_ConfigOverrideU8, \
} }


#define INI_CONFIG_TYPE_FNS_U16 (INI_ConfigTypeFns) { \
#define INI_CONFIG_TYPE_U16 (INI_ConfigType) { \
.size = sizeof(uint16_t), \ .size = sizeof(uint16_t), \
.load = INI_ConfigLoadU16, \ .load = INI_ConfigLoadU16, \
.save = INI_ConfigSaveU16, \ .save = INI_ConfigSaveU16, \
.override = INI_ConfigOverrideU16, \ .override = INI_ConfigOverrideU16, \
} }


#define INI_CONFIG_TYPE_FNS_U8 (INI_ConfigTypeFns) { \
.size = sizeof(uint8_t), \
.load = INI_ConfigLoadU8, \
.save = INI_ConfigSaveU8, \
.override = INI_ConfigOverrideU8, \
#define INI_CONFIG_TYPE_U32 (INI_ConfigType) { \
.size = sizeof(uint32_t), \
.load = INI_ConfigLoadU32, \
.save = INI_ConfigSaveU32, \
.override = INI_ConfigOverrideU32, \
}

#define INI_CONFIG_TYPE_I8 (INI_ConfigType) { \
.size = sizeof(int8_t), \
.load = INI_ConfigLoadI8, \
.save = INI_ConfigSaveI8, \
.override = INI_ConfigOverrideI8, \
} }


#define INI_CONFIG_TYPE_FNS_VOID (INI_ConfigTypeFns) { \
.size = 0, \
.load = NULL, \
.save = NULL, \
.override = NULL, \
#define INI_CONFIG_TYPE_I16 (INI_ConfigType) { \
.size = sizeof(int16_t), \
.load = INI_ConfigLoadI16, \
.save = INI_ConfigSaveI16, \
.override = INI_ConfigOverrideI16, \
} }


#define INI_CONFIG_TYPE_FNS_STRING(X) (INI_ConfigTypeFns) { \
.size = (X), \
#define INI_CONFIG_TYPE_I32 (INI_ConfigType) { \
.size = sizeof(int32_t), \
.load = INI_ConfigLoadI32, \
.save = INI_ConfigSaveI32, \
.override = INI_ConfigOverrideI32, \
}

#define INI_CONFIG_TYPE_FNS_STRING(X) (INI_ConfigType) { \
.size = (sizeof(char) * X), \
.load = INI_ConfigLoadString, \ .load = INI_ConfigLoadString, \
.save = INI_ConfigSaveString, \ .save = INI_ConfigSaveString, \
.override = INI_ConfigOverrideString, \ .override = INI_ConfigOverrideString, \
} }


#define INI_CONFIG_SERIALIZER_PAIR_NONE (INI_ConfigSerializerPair) { \
.serialize = NULL, \
.deserialize = NULL, \
}

#define INI_CONFIG_ITEM_NULL (INI_ConfigItem) { \ #define INI_CONFIG_ITEM_NULL (INI_ConfigItem) { \
INI_CONFIG_TYPE_FNS_VOID, \
(INI_ConfigType) { \
.size = 0, \
.load = NULL, \
.save = NULL, \
.override = NULL, \
}, \
NULL, \ NULL, \
NULL, \ NULL, \
NULL, \ NULL, \
NULL, \ NULL, \
NULL, \ NULL, \
{ \
.serialize = NULL, \
.deserialize = NULL, \
}, \
INI_CONFIG_SERIALIZER_PAIR_NONE, \
NULL, \ NULL, \
} }




Loading…
Cancel
Save