Cleaning up language parsing a little bit.

This commit is contained in:
2021-08-09 08:47:38 -07:00
parent 68986fd108
commit 65d13cc046
3 changed files with 23 additions and 11 deletions

View File

@ -10,6 +10,9 @@
#include "../file/asset.h" #include "../file/asset.h"
#include "../file/csv.h" #include "../file/csv.h"
#define LANGUAGE_HEADER_KEY "Key"
#define LANGUAGE_HEADER_VALUE "Value"
/** Definition for a Language */ /** Definition for a Language */
typedef struct { typedef struct {
/** The buffer to read the asset from. */ /** The buffer to read the asset from. */

View File

@ -13,11 +13,6 @@ bool gameInit(game_t *game) {
// Init the engine and the rendering pipeline // Init the engine and the rendering pipeline
engineInit(&game->engine, game); 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 // Send off to the game instance
#if SETTING_GAME == SETTING_GAME_POKER #if SETTING_GAME == SETTING_GAME_POKER
return pokerGameInit(game); return pokerGameInit(game);

View File

@ -11,22 +11,36 @@ void languageInit(language_t *language, char *filename) {
// Open the asset buffer // Open the asset buffer
language->asset = assetBufferOpen(filename); language->asset = assetBufferOpen(filename);
// Get the header row. // Get the headers
csvHeadersGet(language->asset, &language->header); 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( int32_t languageGet(
language_t *language, char *key, char output[CSV_CELL_SIZE_MAX] language_t *language, char *key, char output[CSV_CELL_SIZE_MAX]
) { ) {
csvrow_t row; csvrow_t row;
// Reset the buffer
assetBufferStart(language->asset); assetBufferStart(language->asset);
// Search the CSV
int32_t rowIndex = csvRowSearch( int32_t rowIndex = csvRowSearch(
language->asset, &row, language->headerIndexKey, key language->asset, &row, language->headerIndexKey, key
); );
if(rowIndex == -1) return rowIndex; if(rowIndex == -1) return rowIndex;// Didn't find anything
arrayCopy(sizeof(char), row.columns[language->headerIndexValue], CSV_CELL_SIZE_MAX, output);
// Copy the string
arrayCopy(
sizeof(char), row.columns[language->headerIndexValue],
CSV_CELL_SIZE_MAX, output
);
return rowIndex; return rowIndex;
} }
@ -36,7 +50,7 @@ int32_t languageGetAndParse(language_t *language, char *key,
char csvBuffer[CSV_CELL_SIZE_MAX]; char csvBuffer[CSV_CELL_SIZE_MAX];
int32_t i = languageGet(language, key, csvBuffer); int32_t i = languageGet(language, key, csvBuffer);
if(i == -1) return -1; if(i == -1) return -1;
return stringHandlebarsBuffer(buffer, variables, variableCount, csvBuffer); return stringHandlebarsBuffer(csvBuffer, variables, variableCount, buffer);
} }
void languageDispose(language_t *language) { void languageDispose(language_t *language) {