From 65d13cc046e08361e30b8c6c970830e296ef3984 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 9 Aug 2021 08:47:38 -0700 Subject: [PATCH] Cleaning up language parsing a little bit. --- include/dawn/locale/language.h | 3 +++ src/game/game.c | 5 ----- src/locale/language.c | 26 ++++++++++++++++++++------ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/include/dawn/locale/language.h b/include/dawn/locale/language.h index 69d1810e..a3e6db51 100644 --- a/include/dawn/locale/language.h +++ b/include/dawn/locale/language.h @@ -10,6 +10,9 @@ #include "../file/asset.h" #include "../file/csv.h" +#define LANGUAGE_HEADER_KEY "Key" +#define LANGUAGE_HEADER_VALUE "Value" + /** Definition for a Language */ typedef struct { /** The buffer to read the asset from. */ diff --git a/src/game/game.c b/src/game/game.c index fbb60047..f0c39eaf 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -13,11 +13,6 @@ bool gameInit(game_t *game) { // Init the engine and the rendering pipeline engineInit(&game->engine, game); - // Load the language. - languageInit(&language, "locale/language/en-US.csv"); - char output[CSV_CELL_SIZE_MAX]; - int32_t row = languageGet(&language, "hello.world", output); - // Send off to the game instance #if SETTING_GAME == SETTING_GAME_POKER return pokerGameInit(game); diff --git a/src/locale/language.c b/src/locale/language.c index acf2c37d..223d0c90 100644 --- a/src/locale/language.c +++ b/src/locale/language.c @@ -11,22 +11,36 @@ void languageInit(language_t *language, char *filename) { // Open the asset buffer language->asset = assetBufferOpen(filename); - // Get the header row. + // Get the headers csvHeadersGet(language->asset, &language->header); - language->headerIndexKey = csvColumnGetIndex(&language->header, "Key"); - language->headerIndexValue = csvColumnGetIndex(&language->header, "Value"); + + // Get the row indexes + language->headerIndexKey = csvColumnGetIndex( + &language->header, LANGUAGE_HEADER_KEY + ); + language->headerIndexValue = csvColumnGetIndex( + &language->header, LANGUAGE_HEADER_VALUE + ); } int32_t languageGet( language_t *language, char *key, char output[CSV_CELL_SIZE_MAX] ) { csvrow_t row; + // Reset the buffer assetBufferStart(language->asset); + + // Search the CSV int32_t rowIndex = csvRowSearch( language->asset, &row, language->headerIndexKey, key ); - if(rowIndex == -1) return rowIndex; - arrayCopy(sizeof(char), row.columns[language->headerIndexValue], CSV_CELL_SIZE_MAX, output); + if(rowIndex == -1) return rowIndex;// Didn't find anything + + // Copy the string + arrayCopy( + sizeof(char), row.columns[language->headerIndexValue], + CSV_CELL_SIZE_MAX, output + ); return rowIndex; } @@ -36,7 +50,7 @@ int32_t languageGetAndParse(language_t *language, char *key, char csvBuffer[CSV_CELL_SIZE_MAX]; int32_t i = languageGet(language, key, csvBuffer); if(i == -1) return -1; - return stringHandlebarsBuffer(buffer, variables, variableCount, csvBuffer); + return stringHandlebarsBuffer(csvBuffer, variables, variableCount, buffer); } void languageDispose(language_t *language) {