Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
enum-map: rewrite to use uthash
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 27, 2024
1 parent 17f51b4 commit 3e51422
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 85 deletions.
7 changes: 3 additions & 4 deletions include/libtrx/config/file.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "../enum_str.h"
#include "../enum_map.h"
#include "../json.h"
#include "./option.h"

Expand All @@ -17,7 +17,6 @@ void ConfigFile_DumpOptions(

int ConfigFile_ReadEnum(
JSON_OBJECT *obj, const char *name, int default_value,
const ENUM_STRING_MAP *enum_map);
const char *enum_name);
void ConfigFile_WriteEnum(
JSON_OBJECT *obj, const char *name, int value,
const ENUM_STRING_MAP *enum_map);
JSON_OBJECT *obj, const char *name, int value, const char *enum_name);
4 changes: 2 additions & 2 deletions include/libtrx/config/map.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../enum_str.h"
#include "../enum_map.h"
#include "../utils.h"
#include "./option.h"

Expand Down Expand Up @@ -39,4 +39,4 @@
.type = COT_ENUM, \
.target = &parent.target_, \
.default_value = &(int32_t) { default_value_ }, \
.param = ENUM_STRING_MAP(enum_map) },
.param = ENUM_MAP_NAME(enum_map) },
25 changes: 25 additions & 0 deletions include/libtrx/enum_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdint.h>

#define ENUM_MAP_DEFINE(enum_name, enum_value, str_value) \
EnumMap_Define(ENUM_MAP_NAME(enum_name), enum_value, str_value);
#define ENUM_MAP_DEFINE_SELF(enum_name, enum_value) \
EnumMap_Define(ENUM_MAP_NAME(enum_name), enum_value, #enum_value);

#define ENUM_MAP_GET(enum_name, str_value, default_value) \
EnumMap_Get(ENUM_MAP_NAME(enum_name), str_value, default_value)

#define ENUM_MAP_TO_STRING(enum_name, enum_value) \
EnumMap_ToString(ENUM_MAP_NAME(enum_name), enum_value)

#define ENUM_MAP_NAME(enum_name) #enum_name

// The function to put the EnumMap_Define calls in
extern void EnumMap_Init(void);

void EnumMap_Shutdown(void);

void EnumMap_Define(
const char *enum_name, int32_t enum_value, const char *str_value);
int32_t EnumMap_Get(
const char *enum_name, const char *str_value, int32_t default_value);
const char *EnumMap_ToString(const char *enum_name, int32_t enum_value);
23 changes: 0 additions & 23 deletions include/libtrx/enum_str.h

This file was deleted.

5 changes: 5 additions & 0 deletions include/libtrx/game/objects/ids.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#if TR_VERSION == 1
#include "./ids_tr1.def"
#elif TR_VERSION == 2
#include "./ids_tr2.def"
#endif
6 changes: 1 addition & 5 deletions include/libtrx/game/objects/ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
typedef enum {
NO_OBJECT = -1,
#define OBJ_ID_DEFINE(object_id, enum_value) object_id = enum_value,
#if TR_VERSION == 1
#include "./ids_tr1.def"
#elif TR_VERSION == 2
#include "./ids_tr2.def"
#endif
#include "./ids.def"
O_NUMBER_OF,
} GAME_OBJECT_ID;
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ sources = [
'src/engine/audio_sample.c',
'src/engine/audio_stream.c',
'src/engine/image.c',
'src/enum_str.c',
'src/enum_map.c',
'src/event_manager.c',
'src/filesystem.c',
'src/game/backpack.c',
Expand Down
29 changes: 8 additions & 21 deletions src/config/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ void ConfigFile_LoadOptions(JSON_OBJECT *root_obj, const CONFIG_OPTION *options)
case COT_ENUM:
*(int *)opt->target = ConfigFile_ReadEnum(
root_obj, M_ResolveOptionName(opt->name),
*(int *)opt->default_value,
(const ENUM_STRING_MAP *)opt->param);
*(int *)opt->default_value, opt->param);
}
opt++;
}
Expand Down Expand Up @@ -173,38 +172,26 @@ void ConfigFile_DumpOptions(JSON_OBJECT *root_obj, const CONFIG_OPTION *options)
case COT_ENUM:
ConfigFile_WriteEnum(
root_obj, M_ResolveOptionName(opt->name), *(int *)opt->target,
(const ENUM_STRING_MAP *)opt->param);
(const char *)opt->param);
break;
}
opt++;
}
}

int ConfigFile_ReadEnum(
JSON_OBJECT *obj, const char *name, int default_value,
const ENUM_STRING_MAP *enum_map)
JSON_OBJECT *const obj, const char *const name, const int default_value,
const char *const enum_name)
{
const char *value_str = JSON_ObjectGetString(obj, name, NULL);
if (value_str) {
while (enum_map->text) {
if (!strcmp(value_str, enum_map->text)) {
return enum_map->value;
}
enum_map++;
}
if (value_str != NULL) {
return EnumMap_Get(enum_name, value_str, default_value);
}
return default_value;
}

void ConfigFile_WriteEnum(
JSON_OBJECT *obj, const char *name, int value,
const ENUM_STRING_MAP *enum_map)
JSON_OBJECT *obj, const char *name, int value, const char *enum_name)
{
while (enum_map->text) {
if (enum_map->value == value) {
JSON_ObjectAppendString(obj, name, enum_map->text);
break;
}
enum_map++;
}
JSON_ObjectAppendString(obj, name, EnumMap_ToString(enum_name, value));
}
99 changes: 99 additions & 0 deletions src/enum_map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "enum_map.h"

#include "memory.h"

#include <stdio.h>
#include <uthash.h>

typedef struct {
char *key;
int32_t value;
UT_hash_handle hh;
} M_ENTRY;

typedef struct {
char *key;
char *str_value;
UT_hash_handle hh;
} M_INVERSE_ENTRY;

static M_ENTRY *m_Map = NULL;
static M_INVERSE_ENTRY *m_InverseMap = NULL;

void EnumMap_Define(
const char *const enum_name, const int32_t enum_value,
const char *const str_value)
{
{
const size_t key_len = strlen(enum_name) + strlen(str_value) + 2;
char *const key = Memory_Alloc(key_len);
snprintf(key, key_len, "%s|%s", enum_name, str_value);

M_ENTRY *const entry = Memory_Alloc(sizeof(M_ENTRY));
entry->key = key;
entry->value = enum_value;
HASH_ADD_KEYPTR(hh, m_Map, entry->key, strlen(entry->key), entry);
}

{
const size_t key_len =
snprintf(NULL, 0, "%s|%d", enum_name, enum_value) + 1;
char *const key = Memory_Alloc(key_len);
snprintf(key, key_len, "%s|%d", enum_name, enum_value);

M_INVERSE_ENTRY *const entry = Memory_Alloc(sizeof(M_INVERSE_ENTRY));
entry->key = key;
entry->str_value = Memory_DupStr(str_value);
HASH_ADD_KEYPTR(
hh, m_InverseMap, entry->key, strlen(entry->key), entry);
}
}

int32_t EnumMap_Get(
const char *const enum_name, const char *const str_value,
int32_t default_value)
{
size_t key_len = strlen(enum_name) + strlen(str_value) + 2;
char key[key_len];
snprintf(key, key_len, "%s|%s", enum_name, str_value);

M_ENTRY *entry;
HASH_FIND_STR(m_Map, key, entry);
return entry != NULL ? entry->value : default_value;
}

const char *EnumMap_ToString(
const char *const enum_name, const int32_t enum_value)
{
size_t key_len = snprintf(NULL, 0, "%s|%d", enum_name, enum_value) + 1;
char key[key_len];
snprintf(key, key_len, "%s|%d", enum_name, enum_value);

M_INVERSE_ENTRY *entry;
HASH_FIND_STR(m_InverseMap, key, entry);
return entry != NULL ? entry->str_value : NULL;
}

void EnumMap_Shutdown(void)
{
{
M_ENTRY *current, *tmp;
HASH_ITER(hh, m_Map, current, tmp)
{
HASH_DEL(m_Map, current);
Memory_Free(current->key);
Memory_Free(current);
}
}

{
M_INVERSE_ENTRY *current, *tmp;
HASH_ITER(hh, m_InverseMap, current, tmp)
{
HASH_DEL(m_InverseMap, current);
Memory_Free(current->str_value);
Memory_Free(current->key);
Memory_Free(current);
}
}
}
16 changes: 0 additions & 16 deletions src/enum_str.c

This file was deleted.

23 changes: 10 additions & 13 deletions src/game/console/cmd/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,9 @@ static bool M_GetCurrentValue(
snprintf(target, target_size, "%.2f", *(double *)option->target);
break;
case COT_ENUM:
for (const ENUM_STRING_MAP *enum_map = option->param;
enum_map->text != NULL; enum_map++) {
if (enum_map->value == *(int32_t *)option->target) {
strncpy(target, enum_map->text, target_size);
}
}
snprintf(
target, target_size, "%s",
EnumMap_ToString(option->param, *(int32_t *)option->target));
break;
}
return true;
Expand Down Expand Up @@ -149,16 +146,16 @@ static bool M_SetCurrentValue(
break;
}

case COT_ENUM:
for (const ENUM_STRING_MAP *enum_map = option->param;
enum_map->text != NULL; enum_map++) {
if (String_Equivalent(enum_map->text, new_value)) {
*(int32_t *)option->target = enum_map->value;
return true;
}
case COT_ENUM: {
const int32_t new_value_typed =
EnumMap_Get(option->param, new_value, -1);
if (new_value_typed != -1) {
*(int32_t *)option->target = new_value_typed;
return true;
}
break;
}
}

return false;
}
Expand Down

0 comments on commit 3e51422

Please sign in to comment.