This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
game-string: introduce uthash-powered game string table
- Loading branch information
Showing
4 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <stdbool.h> | ||
#include <stdint.h> | ||
|
||
#define GS_DEFINE(id, value) GameString_Define(#id, value); | ||
#define GS(id) GameString_Get(#id) | ||
#define GS_ID(id) (#id) | ||
|
||
typedef const char *GAME_STRING_ID; | ||
|
||
void GameString_Define(const char *key, const char *value); | ||
bool GameString_IsKnown(const char *key); | ||
const char *GameString_Get(const char *key); | ||
void GameString_Clear(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "game/game_string.h" | ||
|
||
#include "memory.h" | ||
#include <uthash.h> | ||
|
||
typedef struct { | ||
char *key; | ||
char *value; | ||
UT_hash_handle hh; | ||
} STRING_TABLE_ENTRY; | ||
|
||
static STRING_TABLE_ENTRY *m_StringTable = NULL; | ||
|
||
void GameString_Define(const char *key, const char *value) | ||
{ | ||
STRING_TABLE_ENTRY *entry; | ||
|
||
HASH_FIND_STR(m_StringTable, key, entry); | ||
if (entry == NULL) { | ||
entry = (STRING_TABLE_ENTRY *)Memory_Alloc(sizeof(STRING_TABLE_ENTRY)); | ||
entry->key = Memory_DupStr(key); | ||
entry->value = Memory_DupStr(value); | ||
HASH_ADD_KEYPTR( | ||
hh, m_StringTable, entry->key, strlen(entry->key), entry); | ||
} else { | ||
Memory_Free(entry->value); | ||
entry->value = Memory_DupStr(value); | ||
} | ||
} | ||
|
||
bool GameString_IsKnown(const char *key) | ||
{ | ||
STRING_TABLE_ENTRY *entry; | ||
HASH_FIND_STR(m_StringTable, key, entry); | ||
return entry != NULL; | ||
} | ||
|
||
const char *GameString_Get(const char *key) | ||
{ | ||
STRING_TABLE_ENTRY *entry; | ||
HASH_FIND_STR(m_StringTable, key, entry); | ||
return entry ? entry->value : NULL; | ||
} | ||
|
||
void GameString_Clear(void) | ||
{ | ||
STRING_TABLE_ENTRY *entry, *tmp; | ||
|
||
HASH_ITER(hh, m_StringTable, entry, tmp) | ||
{ | ||
HASH_DEL(m_StringTable, entry); | ||
Memory_Free(entry->key); | ||
Memory_Free(entry->value); | ||
Memory_Free(entry); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[wrap-file] | ||
directory = uthash-2.3.0 | ||
source_url = https://github.com/troydhanson/uthash/archive/v2.3.0.tar.gz | ||
source_filename = uthash-2.3.0.tar.gz | ||
source_hash = e10382ab75518bad8319eb922ad04f907cb20cccb451a3aa980c9d005e661acc | ||
patch_filename = uthash_2.3.0-1_patch.zip | ||
patch_url = https://wrapdb.mesonbuild.com/v2/uthash_2.3.0-1/get_patch | ||
patch_hash = d0b7cf9788c3735ee6a08bb649c58be1f5fad4b95e50e7681cbdf44882da9d7e | ||
|
||
[provide] | ||
uthash = uthash_dep |