Skip to content

Commit

Permalink
Merge pull request #306 from Bobbar/port_u32_config_support
Browse files Browse the repository at this point in the history
Add config support for unsigned integers.
  • Loading branch information
fgsfdsfgs authored Dec 16, 2023
2 parents 0a7faf2 + 5aa7997 commit dee7ab7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions port/src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef enum {
CFG_NONE,
CFG_S32,
CFG_F32,
CFG_U32,
CFG_STR
} configtype;

Expand All @@ -27,6 +28,7 @@ struct configentry {
union {
struct { f32 min_f32, max_f32; };
struct { s32 min_s32, max_s32; };
struct { u32 min_u32, max_u32; };
u32 max_str;
};
} settings[CONFIG_MAX_SETTINGS];
Expand All @@ -38,6 +40,11 @@ static inline s32 configClampInt(s32 val, s32 min, s32 max)
return (val < min) ? min : ((val > max) ? max : val);
}

static inline u32 configClampUInt(u32 val, u32 min, u32 max)
{
return (val < min) ? min : ((val > max) ? max : val);
}

static inline f32 configClampFloat(f32 val, f32 min, f32 max)
{
return (val < min) ? min : ((val > max) ? max : val);
Expand Down Expand Up @@ -101,6 +108,17 @@ void configRegisterInt(const char *key, s32 *var, s32 min, s32 max)
}
}

void configRegisterUInt(const char* key, u32* var, u32 min, u32 max)
{
struct configentry* cfg = configFindOrAddEntry(key);
if (cfg) {
cfg->type = CFG_U32;
cfg->ptr = var;
cfg->min_u32 = min;
cfg->max_u32 = max;
}
}

void configRegisterFloat(const char *key, f32 *var, f32 min, f32 max)
{
struct configentry *cfg = configFindOrAddEntry(key);
Expand Down Expand Up @@ -129,6 +147,7 @@ static void configSetFromString(const char *key, const char *val)

s32 tmp_s32;
f32 tmp_f32;
u32 tmp_u32;
switch (cfg->type) {
case CFG_S32:
tmp_s32 = atoi(val);
Expand All @@ -144,6 +163,13 @@ static void configSetFromString(const char *key, const char *val)
}
*(f32 *)cfg->ptr = tmp_f32;
break;
case CFG_U32:
tmp_u32 = atoll(val);
if (cfg->min_u32 < cfg->max_u32) {
tmp_u32 = configClampUInt(tmp_u32, cfg->min_u32, cfg->max_u32);
}
*(u32*)cfg->ptr = tmp_u32;
break;
case CFG_STR:
strncpy(cfg->ptr, val, cfg->max_str ? cfg->max_str - 1 : 4096);
break;
Expand All @@ -167,6 +193,12 @@ static void configSaveEntry(struct configentry *cfg, FILE *f)
}
fprintf(f, "%s=%f\n", cfg->key + cfg->seclen + 1, *(f32 *)cfg->ptr);
break;
case CFG_U32:
if (cfg->min_u32 < cfg->max_u32) {
*(u32*)cfg->ptr = configClampUInt(*(u32*)cfg->ptr, cfg->min_u32, cfg->max_u32);
}
fprintf(f, "%s=%u\n", cfg->key + cfg->seclen + 1, *(u32*)cfg->ptr);
break;
case CFG_STR:
fprintf(f, "%s=%s\n", cfg->key + cfg->seclen + 1, (char *)cfg->ptr);
break;
Expand Down

0 comments on commit dee7ab7

Please sign in to comment.