Cleaning up language parsing a little bit.

This commit is contained in:
2021-08-09 08:47:38 -07:00
parent 0ce771490c
commit b38d298de1
3 changed files with 23 additions and 11 deletions

View File

@ -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. */

View File

@ -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);

View File

@ -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) {