Polishing the CSV and Language Parser(s).
This commit is contained in:
@ -9,8 +9,17 @@
|
||||
#include "../libs.h"
|
||||
#include "asset.h"
|
||||
|
||||
#define CSV_BUFFER_SIZE 128
|
||||
#define CSV_BUFFER_ROW_SIZE 16
|
||||
/** Maximum characters that a cell can support */
|
||||
#define CSV_BUFFER_SIZE 32
|
||||
|
||||
/** Maximum characters in any given cell */
|
||||
#define CSV_CELL_SIZE_MAX 1024
|
||||
|
||||
/** Maximum number of columns/cells in a given row */
|
||||
#define CSV_ROW_COLUMNS_MAX 16
|
||||
|
||||
/** Count of characters maximum that a row can support */
|
||||
#define CSV_ROW_CHARACTERS_MAX CSV_CELL_SIZE_MAX * CSV_ROW_COLUMNS_MAX
|
||||
|
||||
/** Result of a CSV buffer operation. */
|
||||
typedef struct {
|
||||
@ -23,28 +32,61 @@ typedef struct {
|
||||
} csvbufferresult_t;
|
||||
|
||||
/** Callback to receive data for each cell in a CSV being buffered */
|
||||
typedef void csvbuffercallback_t(
|
||||
typedef bool csvbuffercallback_t(
|
||||
assetbuffer_t *asset, void *user, int32_t row, int32_t column, char *data
|
||||
);
|
||||
|
||||
/** Representation of a CSV Row's complete data. */
|
||||
typedef struct {
|
||||
/** Characters within the row */
|
||||
char data[CSV_ROW_CHARACTERS_MAX];
|
||||
/** Pointer to the start of each string within the row */
|
||||
char *columns[CSV_ROW_COLUMNS_MAX];
|
||||
/** How many columns within the row */
|
||||
int32_t columnCount;
|
||||
} csvrow_t;
|
||||
|
||||
/** Callback to receive buffer data for a CSV row */
|
||||
typedef void csvbufferrowcallback_t(
|
||||
typedef bool csvbufferrowcallback_t(
|
||||
assetbuffer_t *asset, void *user, int32_t row, csvrow_t *csv
|
||||
);
|
||||
|
||||
/** Callback to receive buffer data for a CSV row, but includes CSV headers. */
|
||||
typedef bool csvbufferrowwitheaderscallback_t(
|
||||
assetbuffer_t *asset, void *user, int32_t row,
|
||||
char **columns, int32_t columnCount
|
||||
csvrow_t *header, csvrow_t *current
|
||||
);
|
||||
|
||||
/** Data used by the cell callback for when the row buffering is progressing */
|
||||
typedef struct {
|
||||
/** Which row the current buffer is on */
|
||||
int32_t row;
|
||||
/** Count of columns (in the current row) */
|
||||
int32_t columnCount;
|
||||
/** Array of characters for the row */
|
||||
char rowData[CSV_BUFFER_ROW_SIZE * CSV_BUFFER_SIZE];
|
||||
/** Pointers to the start of each string within the row */
|
||||
char *columns[CSV_BUFFER_ROW_SIZE];
|
||||
/** Information about the current row being parsed */
|
||||
csvrow_t rowCurrent;
|
||||
/** Pointer to custom user data */
|
||||
void *user;
|
||||
/** Pointer to custom user callback */
|
||||
csvbufferrowcallback_t *callback;
|
||||
} csvbufferrowdata_t;
|
||||
} csvbufferrowdata_t;
|
||||
|
||||
/** Data used by the row callback for when the header row is parsed */
|
||||
typedef struct {
|
||||
/** Information about the header row */
|
||||
csvrow_t headerRow;
|
||||
/** Pointer to custom user data */
|
||||
void *user;
|
||||
/** Pointer to custom user callback */
|
||||
csvbufferrowwitheaderscallback_t *callback;
|
||||
} csvbufferrowwithheadersdata_t;
|
||||
|
||||
/** Data used while searching a CSV */
|
||||
typedef struct {
|
||||
/** Row to store the data in */
|
||||
csvrow_t *row;
|
||||
/** Row's index */
|
||||
int32_t rowIndex;
|
||||
/** Column to check */
|
||||
int32_t column;
|
||||
/** Value to check row */
|
||||
char *value;
|
||||
} csvsearchdata_t;
|
@ -7,16 +7,23 @@
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../file/asset.h"
|
||||
#include "../file/csv.h"
|
||||
|
||||
/** Maximum number of strings a language can hold. */
|
||||
#define LANGUAGE_STRING_MAX 128
|
||||
/** Definition for a Language */
|
||||
typedef struct {
|
||||
/** The buffer to read the asset from. */
|
||||
assetbuffer_t *asset;
|
||||
/** CSV Row for the header */
|
||||
csvrow_t header;
|
||||
/** The index in the header row that the key column is in. */
|
||||
int32_t headerIndexKey;
|
||||
/** The index in the header row that the value column is in. */
|
||||
int32_t headerIndexValue;
|
||||
} language_t;
|
||||
|
||||
typedef struct {
|
||||
language_t *language;
|
||||
csvrow_t *row;
|
||||
char *key;
|
||||
char *text;
|
||||
} languagestring_t;
|
||||
|
||||
typedef struct {
|
||||
languagestring_t strings[LANGUAGE_STRING_MAX];
|
||||
int32_t stringCount;
|
||||
} language_t;
|
||||
} languagecsvget_t;
|
@ -64,10 +64,15 @@ int32_t assetBufferEnd(assetbuffer_t *buffer) {
|
||||
return feof((FILE *)buffer);
|
||||
}
|
||||
|
||||
void assetBufferSkip(assetbuffer_t *buffer, int32_t n) {
|
||||
fseek((FILE *)buffer, n, SEEK_CUR);
|
||||
int32_t assetBufferStart(assetbuffer_t *buffer) {
|
||||
return fseek((FILE *)buffer, 0, SEEK_SET);
|
||||
}
|
||||
|
||||
int32_t assetBufferSkip(assetbuffer_t *buffer, int32_t n) {
|
||||
return fseek((FILE *)buffer, n, SEEK_CUR);
|
||||
}
|
||||
|
||||
|
||||
void assetShaderLoad(shader_t *shader, char *fileVertex, char *fileFragment) {
|
||||
// Load the vertex shader into memory
|
||||
char *vertexShader = assetStringLoad(fileVertex);
|
||||
|
@ -48,12 +48,21 @@ int32_t assetBufferRead(assetbuffer_t *buffer, char *data, int32_t size);
|
||||
*/
|
||||
int32_t assetBufferEnd(assetbuffer_t *buffer);
|
||||
|
||||
/**
|
||||
* Rewinds an asset buffer to the start.
|
||||
*
|
||||
* @param buffer Buffer to rewind
|
||||
* @return 0 if successful, otherwise unsuccessful.
|
||||
*/
|
||||
int32_t assetBufferStart(assetbuffer_t *buffer);
|
||||
|
||||
/**
|
||||
* Method to skip n bytes in the buffer
|
||||
* @param buffer The buffer pointing to an asset.
|
||||
* @param n Count of bytes to skip.
|
||||
* @return 0 if successful, otherwise unsuccessful.
|
||||
*/
|
||||
void assetBufferSkip(assetbuffer_t *buffer, int32_t n);
|
||||
int32_t assetBufferSkip(assetbuffer_t *buffer, int32_t n);
|
||||
|
||||
/**
|
||||
* Load a shader program from a vertex and fragment shader file.
|
||||
|
153
src/file/csv.c
153
src/file/csv.c
@ -7,15 +7,14 @@
|
||||
|
||||
#include "csv.h"
|
||||
|
||||
// void csvPopulate(assetbuffer_t *asset, csv_t *output) {
|
||||
|
||||
// }
|
||||
|
||||
csvbufferresult_t csvBuffer(assetbuffer_t *asset, csvbuffercallback_t *callback, void *user){
|
||||
csvbufferresult_t csvBuffer(
|
||||
assetbuffer_t *asset, csvbuffercallback_t *callback, void *user
|
||||
) {
|
||||
int32_t cellChar, read, i, currentColumnCount;
|
||||
char temp[CSV_BUFFER_SIZE];
|
||||
char cell[CSV_BUFFER_SIZE];
|
||||
char buffer[CSV_BUFFER_SIZE];
|
||||
char cell[CSV_CELL_SIZE_MAX];
|
||||
char c;
|
||||
bool callbackResponse;
|
||||
bool insideEncapsulation = false;
|
||||
csvbufferresult_t result = {
|
||||
.cellCount = 0,
|
||||
@ -29,19 +28,19 @@ csvbufferresult_t csvBuffer(assetbuffer_t *asset, csvbuffercallback_t *callback,
|
||||
|
||||
// Begin buffering.
|
||||
while(true) {
|
||||
// Read n bytes
|
||||
read = assetBufferRead(asset, temp, CSV_BUFFER_SIZE);
|
||||
// Read n bytes into our buffer
|
||||
read = assetBufferRead(asset, buffer, CSV_BUFFER_SIZE);
|
||||
|
||||
// Now read back those bytes.
|
||||
for(i = 0; i < read; i++) {
|
||||
c = temp[i];
|
||||
c = buffer[i];
|
||||
|
||||
// Characters we flat out ignore
|
||||
if(c == '\r') continue;
|
||||
|
||||
// Handle quote marks.
|
||||
if(c == '"') {
|
||||
if(temp[i+1] == '"') {// "" means a single quote (double-escaped)
|
||||
if(buffer[i+1] == '"') {// "" means a single quote (double-escaped)
|
||||
i++;
|
||||
cell[cellChar++] = c;
|
||||
} else if(insideEncapsulation) {
|
||||
@ -59,7 +58,8 @@ csvbufferresult_t csvBuffer(assetbuffer_t *asset, csvbuffercallback_t *callback,
|
||||
|
||||
// Fire off the callback
|
||||
if(callback != NULL) {
|
||||
callback(asset, user, result.rowCount, result.cellCount, cell);
|
||||
callbackResponse = callback(asset, user, result.rowCount, currentColumnCount, cell);
|
||||
if(!callbackResponse) return result;
|
||||
}
|
||||
|
||||
// Prepare for next row/cell
|
||||
@ -84,57 +84,140 @@ csvbufferresult_t csvBuffer(assetbuffer_t *asset, csvbuffercallback_t *callback,
|
||||
// If this is an empty row we don't count it, otherwise we do.
|
||||
if(currentColumnCount != 0) result.rowCount++;
|
||||
|
||||
// Finish buffering.
|
||||
assetBufferClose(asset);
|
||||
return result;
|
||||
}
|
||||
|
||||
void _csvBufferRowParserCallback(assetbuffer_t *asset, void *user, int32_t row, int32_t column, char *data) {
|
||||
|
||||
bool _csvBufferRowParserCallback(
|
||||
assetbuffer_t *asset, void *user, int32_t row, int32_t column, char *data
|
||||
) {
|
||||
csvbufferrowdata_t *rowData = (csvbufferrowdata_t *)user;
|
||||
|
||||
// Now did we change rows?
|
||||
if(row != rowData->row) {
|
||||
// Yes we did, let's buffer the previous row.
|
||||
rowData->row = row;// Update row, basically this forces 1 indexing.
|
||||
if(rowData->callback != NULL) {
|
||||
rowData->callback(
|
||||
asset, rowData->user, rowData->row,
|
||||
rowData->columns, rowData->columnCount
|
||||
);
|
||||
if(!rowData->callback(
|
||||
asset, rowData->user, rowData->row, &rowData->rowCurrent
|
||||
)) return false;
|
||||
}
|
||||
rowData->columnCount = 0;
|
||||
// Begin next row
|
||||
rowData->row = row;
|
||||
rowData->rowCurrent.columnCount = 0;
|
||||
}
|
||||
|
||||
// Determine string info for the cell
|
||||
int32_t length = strlen(data);
|
||||
int32_t offset = (column * CSV_BUFFER_ROW_SIZE);
|
||||
int32_t offset = (column * CSV_CELL_SIZE_MAX);
|
||||
|
||||
// Now copy that data to the buffer
|
||||
arrayCopy(
|
||||
sizeof(char), data, length + 1,
|
||||
rowData->rowData + offset
|
||||
);
|
||||
rowData->columns[column] = rowData->rowData + offset;
|
||||
rowData->columnCount++;
|
||||
// Now copy the string data to the buffer
|
||||
arrayCopy(sizeof(char), data, length + 1, rowData->rowCurrent.data + offset);
|
||||
// Update the pointer to the string
|
||||
rowData->rowCurrent.columns[column] = rowData->rowCurrent.data + offset;
|
||||
rowData->rowCurrent.columnCount++;
|
||||
return true;
|
||||
}
|
||||
|
||||
csvbufferresult_t csvBufferRow(assetbuffer_t *asset, csvbufferrowcallback_t *callback, void *user) {
|
||||
csvbufferresult_t csvBufferRow(
|
||||
assetbuffer_t *asset, csvbufferrowcallback_t *callback, void *user
|
||||
) {
|
||||
csvbufferrowdata_t data;
|
||||
csvbufferresult_t result;
|
||||
|
||||
data.row = 0;
|
||||
data.columnCount = 0;
|
||||
data.user = user;
|
||||
data.callback = callback;
|
||||
data.rowCurrent.columnCount = 0;
|
||||
|
||||
// Perform a per-cell buffer and run the parser callback.
|
||||
result = csvBuffer(asset, &_csvBufferRowParserCallback, &data);
|
||||
|
||||
// Because the buffer may not fire for the last row we handle it here.
|
||||
if(data.columnCount > 0 && callback != NULL) {
|
||||
data.row++;// Because .row is 0 indexed until the "next line", we increment
|
||||
callback(asset, user, data.row, data.columns, data.columnCount);
|
||||
if(data.rowCurrent.columnCount > 0 && callback != NULL) {
|
||||
if(!callback(asset, user, data.row, &data.rowCurrent)) return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
bool _csvBufferRowWithHeadersCallback(
|
||||
assetbuffer_t *asset, void *user, int32_t row, csvrow_t *csv
|
||||
) {
|
||||
csvbufferrowwithheadersdata_t *data = (csvbufferrowwithheadersdata_t *)user;
|
||||
int32_t i;
|
||||
|
||||
// Take the headers for row 0
|
||||
if(row == 0) {
|
||||
csvRowPopulate(csv, &data->headerRow);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fire the callback
|
||||
return data->callback(asset, data->user, row, &data->headerRow, csv);
|
||||
}
|
||||
|
||||
csvbufferresult_t csvBufferRowWithHeaders(
|
||||
assetbuffer_t *asset, csvbufferrowwitheaderscallback_t *callback, void *user
|
||||
) {
|
||||
csvbufferrowwithheadersdata_t data;
|
||||
data.user = user;
|
||||
data.callback = callback;
|
||||
|
||||
return csvBufferRow(asset, &_csvBufferRowWithHeadersCallback, &data);
|
||||
}
|
||||
|
||||
void csvRowPopulate(csvrow_t *source, csvrow_t *dest) {
|
||||
int32_t i;
|
||||
|
||||
dest->columnCount = source->columnCount;
|
||||
|
||||
// Copy the raw characters from the source buffer.
|
||||
arrayCopy(sizeof(char), source->data, CSV_ROW_CHARACTERS_MAX, dest->data);
|
||||
|
||||
// Now update the destination pointers to reference the data buffer.
|
||||
for(i = 0; i < source->columnCount; i++) {
|
||||
dest->columns[i] = dest->data + (i * CSV_CELL_SIZE_MAX);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool _csvHeadersGetCallback(
|
||||
assetbuffer_t *asset, void *user, int32_t row, csvrow_t *current
|
||||
) {
|
||||
csvrow_t *rowData = (csvrow_t *)user;
|
||||
csvRowPopulate(current, rowData);
|
||||
return false;// False to break the loop
|
||||
}
|
||||
|
||||
csvbufferresult_t csvHeadersGet(assetbuffer_t *asset, csvrow_t *row) {
|
||||
return csvBufferRow(asset, &_csvHeadersGetCallback, row);
|
||||
}
|
||||
|
||||
int32_t csvColumnGetIndex(csvrow_t *row, char *key) {
|
||||
return arrayFindString(row->columns, row->columnCount, key);
|
||||
}
|
||||
|
||||
|
||||
bool _csvRowSearchCallback(
|
||||
assetbuffer_t *asset, void *user, int32_t row, csvrow_t *csv
|
||||
) {
|
||||
csvsearchdata_t *data = (csvsearchdata_t *)user;
|
||||
// Does the search match?
|
||||
if(strcmp(csv->columns[data->column], data->value) != 0) return true;
|
||||
// Matched, copy and end.
|
||||
csvRowPopulate(csv, data->row);
|
||||
data->rowIndex = row;
|
||||
return false;
|
||||
}
|
||||
int32_t csvRowSearch(
|
||||
assetbuffer_t *asset, csvrow_t *row, int32_t column, char *value
|
||||
) {
|
||||
csvsearchdata_t data = {
|
||||
.column = column,
|
||||
.row = row,
|
||||
.rowIndex = -1,
|
||||
.value = value
|
||||
};
|
||||
csvBufferRow(asset, &_csvRowSearchCallback, &data);
|
||||
return data.rowIndex;
|
||||
}
|
100
src/file/csv.h
100
src/file/csv.h
@ -10,7 +10,101 @@
|
||||
#include "asset.h"
|
||||
#include "../util/array.h"
|
||||
|
||||
csvbufferresult_t csvBuffer(assetbuffer_t *asset, csvbuffercallback_t *callback, void *user);
|
||||
/**
|
||||
* Buffer each cell within a CSV Asset Buffer with a callback.
|
||||
*
|
||||
* @param asset Asset to buffer the CSV from.
|
||||
* @param callback Callback to fire for each cell parsed.
|
||||
* @param user Pointer to any custom user data.
|
||||
* @return The result of the CSV Buffer Operation.
|
||||
*/
|
||||
csvbufferresult_t csvBuffer(
|
||||
assetbuffer_t *asset, csvbuffercallback_t *callback, void *user
|
||||
);
|
||||
|
||||
void _csvBufferRowParserCallback(assetbuffer_t *asset, void *user, int32_t row, int32_t column, char *data);
|
||||
csvbufferresult_t csvBufferRow(assetbuffer_t *asset, csvbufferrowcallback_t *callback, void *user);
|
||||
/** Callback for when the CSV Row Buffer is parsing a row */
|
||||
bool _csvBufferRowParserCallback(
|
||||
assetbuffer_t *asset, void *user, int32_t row, int32_t column, char *data
|
||||
);
|
||||
|
||||
/**
|
||||
* Buffer each row within a CSV Asset Buffer with a callback.
|
||||
*
|
||||
* @param asset Asset to buffer the CSV from.
|
||||
* @param callback Callback to fire when a row is parsed.
|
||||
* @param user Pointer to any custom user data.
|
||||
* @return The result of the CSV Buffer Operation.
|
||||
*/
|
||||
csvbufferresult_t csvBufferRow(
|
||||
assetbuffer_t *asset, csvbufferrowcallback_t *callback, void *user
|
||||
);
|
||||
|
||||
|
||||
/** Parser that the header parser buffer uses. */
|
||||
bool _csvBufferRowWithHeadersCallback(
|
||||
assetbuffer_t *asset, void *user, int32_t row, csvrow_t *csv
|
||||
);
|
||||
|
||||
/**
|
||||
* Buffer each row within a CSV Asset Buffer with a callback. This will also
|
||||
* provide the callback with the header row (The first row), already parsed.
|
||||
*
|
||||
* @param asset
|
||||
* @param callback
|
||||
* @param user
|
||||
* @return csvbufferresult_t
|
||||
*/
|
||||
csvbufferresult_t csvBufferRowWithHeaders(
|
||||
assetbuffer_t *asset, csvbufferrowwitheaderscallback_t *callback, void *user
|
||||
);
|
||||
|
||||
/**
|
||||
* Essentially a CSV Row Cloner, copies data from one CSV row to another and
|
||||
* updates the char pointers for you.
|
||||
*
|
||||
* @param source The source CSV row to copy from.
|
||||
* @param dest The destination CSV row to copy to.
|
||||
*/
|
||||
void csvRowPopulate(csvrow_t *source, csvrow_t *dest);
|
||||
|
||||
|
||||
/** Callback used to parse and get the headers row */
|
||||
bool _csvHeadersGetCallback(
|
||||
assetbuffer_t *asset, void *user, int32_t row, csvrow_t *current
|
||||
);
|
||||
/**
|
||||
* Parses just the headers row from a CSV and stores the output into the
|
||||
* provided buffers.
|
||||
*
|
||||
* @param asset Asset to get the headers from.
|
||||
* @param row The CSV Row to store the headers data in.
|
||||
* @return The result of the buffer operation.
|
||||
*/
|
||||
csvbufferresult_t csvHeadersGet(assetbuffer_t *asset, csvrow_t *row);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the column index within the CSV Row for the specific key.
|
||||
* @param row Row to get from.
|
||||
* @param key Key to search for
|
||||
* @return The column index for the key, or -1 if not found.
|
||||
*/
|
||||
int32_t csvColumnGetIndex(csvrow_t *row, char *key);
|
||||
|
||||
/** Callback to use while scanning the CSV to find the matching row */
|
||||
bool _csvRowSearchCallback(
|
||||
assetbuffer_t *asset, void *user, int32_t row, csvrow_t *csv
|
||||
);
|
||||
/**
|
||||
* Search the CSV for a matching row. Will return data only if the given column
|
||||
* matches the given value.
|
||||
*
|
||||
* @param asset Asset buffer to search.
|
||||
* @param row Where the returned row will be stored.
|
||||
* @param column Column to check.
|
||||
* @param value Value to check in that column
|
||||
* @return The row index that the result was found on, or -1 if not found.
|
||||
*/
|
||||
int32_t csvRowSearch(
|
||||
assetbuffer_t *asset, csvrow_t *row, int32_t column, char *value
|
||||
);
|
@ -7,23 +7,16 @@
|
||||
|
||||
#include "game.h"
|
||||
|
||||
void bruhCallback(
|
||||
assetbuffer_t *asset, void *user, int32_t row,
|
||||
char **columns, int32_t columnCount
|
||||
) {
|
||||
int32_t bruh;
|
||||
for(int32_t i = 0; i < columnCount; i++) {
|
||||
char *string = columns [i];
|
||||
printf(string);
|
||||
}
|
||||
}
|
||||
language_t language;
|
||||
|
||||
bool gameInit(game_t *game) {
|
||||
// Init the engine and the rendering pipeline
|
||||
engineInit(&game->engine, game);
|
||||
|
||||
assetbuffer_t *buffer = assetBufferOpen("locale/language/en-US.csv");
|
||||
csvbufferresult_t result = csvBufferRow(buffer, &bruhCallback, NULL);
|
||||
// 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
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <dawn/dawn.h>
|
||||
#include "../engine/engine.h"
|
||||
|
||||
#include "../file/csv.h"
|
||||
#include "../locale/language.h"
|
||||
|
||||
#if SETTING_GAME == SETTING_GAME_POKER
|
||||
#include "poker/pokergame.h"
|
||||
|
@ -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);
|
@ -78,4 +78,12 @@ void arraySortUint8(uint8_t *array, int32_t length) {
|
||||
}
|
||||
int32_t _arraySorterUint8(const void* left, const void* right) {
|
||||
return *((uint8_t *)left) - *((uint8_t *)right);
|
||||
}
|
||||
|
||||
int32_t arrayFindString(char **array, int32_t arrayLength, char *value) {
|
||||
int32_t i;
|
||||
for(i = 0; i < arrayLength; i++) {
|
||||
if(strcmp(array[i], value) == 0) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
@ -89,4 +89,14 @@ int32_t _arraySorterInt32(const void *left, const void* right);
|
||||
*/
|
||||
void arraySortUint8(uint8_t *array, int32_t length);
|
||||
/** Internal uint8_t array sorter. */
|
||||
int32_t _arraySorterUint8(const void* left, const void* right);
|
||||
int32_t _arraySorterUint8(const void* left, const void* right);
|
||||
|
||||
/**
|
||||
* Find the index of a string within a string array. (Array of char pointers).
|
||||
*
|
||||
* @param array Array to check
|
||||
* @param arrayLength Length of th earray.
|
||||
* @param value The value to search for.
|
||||
* @return The index that the strings exists within the array.
|
||||
*/
|
||||
int32_t arrayFindString(char **array, int32_t arrayLength, char *value);
|
Reference in New Issue
Block a user