Polishing the CSV and Language Parser(s).
This commit is contained in:
@ -7,47 +7,38 @@
|
||||
|
||||
#include "language.h"
|
||||
|
||||
void languageInit(language_t *language) {
|
||||
language->stringCount = 0;
|
||||
void languageInit(language_t *language, char *filename) {
|
||||
// Open the asset buffer
|
||||
language->asset = assetBufferOpen(filename);
|
||||
|
||||
// Get the header row.
|
||||
csvHeadersGet(language->asset, &language->header);
|
||||
language->headerIndexKey = csvColumnGetIndex(&language->header, "Key");
|
||||
language->headerIndexValue = csvColumnGetIndex(&language->header, "Value");
|
||||
}
|
||||
|
||||
languagestring_t * languageAddString(language_t *lang, char *key, char *text) {
|
||||
languagestring_t *string;
|
||||
|
||||
string = lang->strings + lang->stringCount;
|
||||
string->key = key;
|
||||
string->text = text;
|
||||
|
||||
lang->stringCount++;
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
languagestring_t * languageGetStringByKey(language_t *language, char *key) {
|
||||
int32_t i;
|
||||
languagestring_t *string;
|
||||
|
||||
for(i = 0; i < language->stringCount; i++) {
|
||||
string = language->strings + i;
|
||||
if(strcmp(string->key, key) != 0) continue;
|
||||
return string;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char * languageGetText(language_t *language, languagestring_t *string) {
|
||||
// TODO: Buffer this from somewhere.
|
||||
return string->text;
|
||||
}
|
||||
|
||||
int32_t languageGetTextWithVariables(
|
||||
language_t *language, languagestring_t *string,
|
||||
stringhandlebarvariable_t *variables, int32_t variableCount,
|
||||
char *buffer
|
||||
int32_t languageGet(
|
||||
language_t *language, char *key, char output[CSV_CELL_SIZE_MAX]
|
||||
) {
|
||||
char *source;
|
||||
source = languageGetText(language, string);
|
||||
if(source == NULL) return 0;
|
||||
return stringHandlebarsBuffer(source, variables, variableCount, buffer);
|
||||
csvrow_t row;
|
||||
assetBufferStart(language->asset);
|
||||
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);
|
||||
return rowIndex;
|
||||
}
|
||||
|
||||
int32_t languageGetAndParse(language_t *language, char *key,
|
||||
stringhandlebarvariable_t *variables, int32_t variableCount, char *buffer
|
||||
) {
|
||||
char csvBuffer[CSV_CELL_SIZE_MAX];
|
||||
int32_t i = languageGet(language, key, csvBuffer);
|
||||
if(i == -1) return -1;
|
||||
return stringHandlebarsBuffer(buffer, variables, variableCount, csvBuffer);
|
||||
}
|
||||
|
||||
void languageDispose(language_t *language) {
|
||||
assetBufferClose(language->asset);
|
||||
}
|
@ -8,53 +8,44 @@
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "../util/string.h"
|
||||
#include "../file/asset.h"
|
||||
#include "../file/csv.h"
|
||||
|
||||
/**
|
||||
* Initializes a language.
|
||||
* @param language Language to initialize.
|
||||
* @param filename The filename of the asset that the language uses.
|
||||
*/
|
||||
void languageInit(language_t *language);
|
||||
void languageInit(language_t *language, char *filename);
|
||||
|
||||
/**
|
||||
* Adds a string to a language.
|
||||
* @param lang Language to add to.
|
||||
* @param key Key to use for the string.
|
||||
* @param text Value to use for the language string
|
||||
* @return The language string added to the language.
|
||||
*/
|
||||
languagestring_t * languageAddString(language_t *lang, char *key, char *text);
|
||||
|
||||
/**
|
||||
* Returns the language string for a given language string key.
|
||||
* Get the value for a given key out of the language buffer.
|
||||
*
|
||||
* @param language Language to get from.
|
||||
* @param key Key to get from.
|
||||
* @return The string matching the key, or NULL if no match.
|
||||
* @param key Key to get.
|
||||
* @param output Output buffer to return.
|
||||
* @return Row within the CSV it was found, otherwise -1.
|
||||
*/
|
||||
languagestring_t * languageGetStringByKey(language_t *language, char *key);
|
||||
int32_t languageGet(
|
||||
language_t *language, char *key, char output[CSV_CELL_SIZE_MAX]
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns the value for a given language string.
|
||||
* Returns the parsed handlebars out of the language CSV.
|
||||
*
|
||||
* @param language Language to get from.
|
||||
* @param string Language string to get.
|
||||
* @return The string.
|
||||
* @param language Language to buffer from.
|
||||
* @param key Key to get out of the CSV.
|
||||
* @param variables Array of variables to parse the handlebars with.
|
||||
* @param variableCount Count of variables in the array.
|
||||
* @param buffer Buffer to store the output data in.
|
||||
* @return Forwarded result of stringHandlebarsBuffer.
|
||||
*/
|
||||
char * languageGetText(language_t *language, languagestring_t *string);
|
||||
int32_t languageGetAndParse(language_t *language, char *key,
|
||||
stringhandlebarvariable_t *variables, int32_t variableCount, char *buffer
|
||||
);
|
||||
|
||||
/**
|
||||
* Retreives the value of a given language string, and replaces the variables in
|
||||
* the string with the provided variable values.
|
||||
*
|
||||
* @param language Language to read from.
|
||||
* @param string String to get and parse.
|
||||
* @param variables Variables to use in the parsing.
|
||||
* @param variableCount How many variables in the array.
|
||||
* @param buffer The buffer to write the output data to.
|
||||
* @return The count of characters that were written to the buffer.
|
||||
* Cleanup a previously initialized language.
|
||||
* @param language Language to dispose.
|
||||
*/
|
||||
int32_t languageGetTextWithVariables(
|
||||
language_t *language, languagestring_t *string,
|
||||
stringhandlebarvariable_t *variables, int32_t variableCount,
|
||||
char *buffer
|
||||
);
|
||||
void languageDispose(language_t *language);
|
Reference in New Issue
Block a user