Compare commits
5 Commits
9ec21f85a0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 4cd3355ef1 | |||
| 98d70b96d1 | |||
| 64735bdf43 | |||
| 7b87347b77 | |||
| b5b29d7061 |
@@ -1,9 +1,60 @@
|
|||||||
#
|
|
||||||
msgid ""
|
msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Language: en_US\n"
|
"Project-Id-Version: ExampleApp 1.0\n"
|
||||||
|
"Language: en\n"
|
||||||
"Content-Type: text/plain; charset=UTF-8\n"
|
"Content-Type: text/plain; charset=UTF-8\n"
|
||||||
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
"Plural-Forms: nplurals=4; plural=(n==1 ? 0 : n==2 ? 1 : (n<7 ? 2 : 3));\n"
|
||||||
|
|
||||||
msgid "ui.test"
|
#: ui/menu.c:10
|
||||||
msgstr "Hello this is a test."
|
msgid "ui.title"
|
||||||
|
msgstr ""
|
||||||
|
"Welcome"
|
||||||
|
|
||||||
|
#: ui/user.c:22
|
||||||
|
msgid "ui.greeting"
|
||||||
|
msgstr "Hello, %s!"
|
||||||
|
|
||||||
|
#: ui/files.c:40
|
||||||
|
msgid "ui.file_status"
|
||||||
|
msgstr "%s has %d files."
|
||||||
|
|
||||||
|
#: ui/cart.c:55
|
||||||
|
msgid "cart.item_count"
|
||||||
|
msgid_plural "cart.item_count"
|
||||||
|
msgstr[0] "%d item"
|
||||||
|
msgstr[1] "%d items (dual)"
|
||||||
|
msgstr[2] "%d items (few)"
|
||||||
|
msgstr[3] "%d items (many)"
|
||||||
|
|
||||||
|
#: ui/notifications.c:71
|
||||||
|
msgid ""
|
||||||
|
"ui.multiline_help"
|
||||||
|
msgstr ""
|
||||||
|
"Line one of the help text.\n"
|
||||||
|
"Line two continues here.\n"
|
||||||
|
"Line three ends here."
|
||||||
|
|
||||||
|
#: ui/errors.c:90
|
||||||
|
msgid ""
|
||||||
|
"error.upload_failed.long"
|
||||||
|
msgstr ""
|
||||||
|
"Upload failed for file \"%s\".\n"
|
||||||
|
"Please try again later or contact support."
|
||||||
|
|
||||||
|
#: ui/messages.c:110
|
||||||
|
msgid ""
|
||||||
|
"user.invite_status"
|
||||||
|
msgid_plural ""
|
||||||
|
"user.invite_status"
|
||||||
|
msgstr[0] ""
|
||||||
|
"%s invited %d user.\n"
|
||||||
|
"Please review the request."
|
||||||
|
msgstr[1] ""
|
||||||
|
"%s invited %d users (dual).\n"
|
||||||
|
"Please review the requests."
|
||||||
|
msgstr[2] ""
|
||||||
|
"%s invited %d users (few).\n"
|
||||||
|
"Please review the requests."
|
||||||
|
msgstr[3] ""
|
||||||
|
"%s invited %d users (many).\n"
|
||||||
|
"Please review the requests."
|
||||||
@@ -14,6 +14,8 @@ module('locale')
|
|||||||
screenSetBackground(colorCornflowerBlue())
|
screenSetBackground(colorCornflowerBlue())
|
||||||
camera = cameraCreate(CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC)
|
camera = cameraCreate(CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC)
|
||||||
|
|
||||||
|
text = localeGetText('cart.item_count', 52, 2)
|
||||||
|
|
||||||
function sceneDispose()
|
function sceneDispose()
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -39,6 +41,6 @@ function sceneRender()
|
|||||||
-- )
|
-- )
|
||||||
-- spriteBatchFlush()
|
-- spriteBatchFlush()
|
||||||
|
|
||||||
textDraw(10, 10, "Hello World\nHow are you?", colorRed())
|
textDraw(10, 10, text, colorWhite())
|
||||||
spriteBatchFlush()
|
spriteBatchFlush()
|
||||||
end
|
end
|
||||||
@@ -36,6 +36,19 @@ errorret_t assetFileInit(
|
|||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileRewind(assetfile_t *file) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(file->zipFile, "Asset file must be opened before rewinding.");
|
||||||
|
|
||||||
|
if(file->position == 0) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChain(assetFileClose(file));
|
||||||
|
errorChain(assetFileOpen(file));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
errorret_t assetFileOpen(assetfile_t *file) {
|
errorret_t assetFileOpen(assetfile_t *file) {
|
||||||
assertNotNull(file, "Asset file cannot be NULL.");
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
assertNotNull(file->filename, "Asset file filename cannot be NULL.");
|
assertNotNull(file->filename, "Asset file filename cannot be NULL.");
|
||||||
@@ -86,4 +99,211 @@ errorret_t assetFileDispose(assetfile_t *file) {
|
|||||||
}
|
}
|
||||||
memoryZero(file, sizeof(assetfile_t));
|
memoryZero(file, sizeof(assetfile_t));
|
||||||
errorOk();
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Line Reader;
|
||||||
|
void assetFileLineReaderInit(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
assetfile_t *file,
|
||||||
|
uint8_t *readBuffer,
|
||||||
|
const size_t readBufferSize,
|
||||||
|
uint8_t *outBuffer,
|
||||||
|
const size_t outBufferSize
|
||||||
|
) {
|
||||||
|
assertNotNull(reader, "Line reader cannot be NULL.");
|
||||||
|
assertNotNull(file, " File cannot be NULL.");
|
||||||
|
assertNotNull(readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
assertNotNull(outBuffer, "Output buffer cannot be NULL.");
|
||||||
|
assertTrue(readBufferSize > 0, "Read buffer size must be greater than 0.");
|
||||||
|
assertTrue(outBufferSize > 0, "Output buffer size must be greater than 0.");
|
||||||
|
|
||||||
|
memoryZero(reader, sizeof(assetfilelinereader_t));
|
||||||
|
|
||||||
|
reader->file = file;
|
||||||
|
reader->readBuffer = readBuffer;
|
||||||
|
reader->readBufferSize = readBufferSize;
|
||||||
|
reader->outBuffer = outBuffer;
|
||||||
|
reader->outBufferSize = outBufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t assetFileLineReaderUnreadBytes(const assetfilelinereader_t *reader) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertTrue(reader->bufferEnd >= reader->bufferStart, "Invalid buffer state.");
|
||||||
|
return reader->bufferEnd - reader->bufferStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t *assetFileLineReaderUnreadPtr(const assetfilelinereader_t *reader) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
return reader->readBuffer + reader->bufferStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
static errorret_t assetFileLineReaderAppend(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
const uint8_t *src,
|
||||||
|
size_t srcLength
|
||||||
|
) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->outBuffer, "Out buffer cannot be NULL.");
|
||||||
|
|
||||||
|
if(srcLength == 0) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* reserve room for optional NUL terminator */
|
||||||
|
if (reader->lineLength + srcLength >= reader->outBufferSize) {
|
||||||
|
errorThrow("Line length exceeds output buffer size.");
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryCopy(reader->outBuffer + reader->lineLength, src, srcLength);
|
||||||
|
reader->lineLength += srcLength;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void assetFileLineReaderTerminate(assetfilelinereader_t *reader) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->outBuffer, "Out buffer cannot be NULL.");
|
||||||
|
assertTrue(reader->lineLength < reader->outBufferSize, "Line length exceeds out buffer.");
|
||||||
|
reader->outBuffer[reader->lineLength] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t assetFileLineReaderFindNewline(const assetfilelinereader_t *reader) {
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
|
||||||
|
for (i = reader->bufferStart; i < reader->bufferEnd; ++i) {
|
||||||
|
if (reader->readBuffer[i] == '\n') {
|
||||||
|
return (ssize_t)i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileLineReaderFill(assetfilelinereader_t *reader) {
|
||||||
|
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->file, "File cannot be NULL.");
|
||||||
|
assertNotNull(reader->readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
|
||||||
|
if(reader->eof) errorOk();
|
||||||
|
|
||||||
|
errorret_t ret;
|
||||||
|
|
||||||
|
size_t unreadBytes = assetFileLineReaderUnreadBytes(reader);
|
||||||
|
|
||||||
|
/* If buffer is fully consumed, refill from start. */
|
||||||
|
if (unreadBytes == 0) {
|
||||||
|
reader->bufferStart = 0;
|
||||||
|
reader->bufferEnd = 0;
|
||||||
|
|
||||||
|
errorChain(assetFileRead(
|
||||||
|
reader->file,
|
||||||
|
reader->readBuffer,
|
||||||
|
reader->readBufferSize
|
||||||
|
));
|
||||||
|
|
||||||
|
if(reader->file->lastRead == 0) {
|
||||||
|
reader->eof = true;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
reader->bufferStart = 0;
|
||||||
|
reader->bufferEnd = reader->file->lastRead;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* There are unread bytes left but no newline in them.
|
||||||
|
* If bufferStart > 0, slide unread bytes to front so we can read more.
|
||||||
|
* This only happens when necessary to make space.
|
||||||
|
*/
|
||||||
|
if(reader->bufferEnd == reader->readBufferSize) {
|
||||||
|
if(reader->bufferStart == 0) {
|
||||||
|
/*
|
||||||
|
* Entire read buffer is unread and contains no newline.
|
||||||
|
* Caller must have a large enough outBuffer to accumulate across fills,
|
||||||
|
* so we consume these bytes into outBuffer before refilling.
|
||||||
|
*/
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryMove(
|
||||||
|
reader->readBuffer,
|
||||||
|
reader->readBuffer + reader->bufferStart,
|
||||||
|
unreadBytes
|
||||||
|
);
|
||||||
|
reader->bufferStart = 0;
|
||||||
|
reader->bufferEnd = unreadBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChain(assetFileRead(
|
||||||
|
reader->file,
|
||||||
|
reader->readBuffer + reader->bufferEnd,
|
||||||
|
reader->readBufferSize - reader->bufferEnd
|
||||||
|
));
|
||||||
|
|
||||||
|
if(reader->file->lastRead == 0) {
|
||||||
|
reader->eof = true;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
reader->bufferEnd += reader->file->lastRead;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileLineReaderNext(assetfilelinereader_t *reader) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->file, "File cannot be NULL.");
|
||||||
|
assertNotNull(reader->readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
assertNotNull(reader->outBuffer, "Out buffer cannot be NULL.");
|
||||||
|
|
||||||
|
reader->lineLength = 0;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
ssize_t newlineIndex = assetFileLineReaderFindNewline(reader);
|
||||||
|
|
||||||
|
if (newlineIndex >= 0) {
|
||||||
|
size_t chunkLength = (size_t)newlineIndex - reader->bufferStart;
|
||||||
|
errorret_t ret;
|
||||||
|
|
||||||
|
/* strip CR in CRLF */
|
||||||
|
if (chunkLength > 0 && reader->readBuffer[(size_t)newlineIndex - 1] == '\r') {
|
||||||
|
chunkLength--;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChain(assetFileLineReaderAppend(
|
||||||
|
reader,
|
||||||
|
reader->readBuffer + reader->bufferStart,
|
||||||
|
chunkLength
|
||||||
|
));
|
||||||
|
|
||||||
|
reader->bufferStart = (size_t)newlineIndex + 1;
|
||||||
|
assetFileLineReaderTerminate(reader);
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (assetFileLineReaderUnreadBytes(reader) > 0) {
|
||||||
|
errorChain(assetFileLineReaderAppend(
|
||||||
|
reader,
|
||||||
|
assetFileLineReaderUnreadPtr(reader),
|
||||||
|
assetFileLineReaderUnreadBytes(reader)
|
||||||
|
));
|
||||||
|
|
||||||
|
reader->bufferStart = reader->bufferEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(reader->eof) {
|
||||||
|
if(reader->lineLength > 0) {
|
||||||
|
assetFileLineReaderTerminate(reader);
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorThrow("End of file reached.");
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChain(assetFileLineReaderFill(reader));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -51,6 +51,13 @@ errorret_t assetFileInit(
|
|||||||
*/
|
*/
|
||||||
errorret_t assetFileOpen(assetfile_t *file);
|
errorret_t assetFileOpen(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewind the file to the initial position.
|
||||||
|
*
|
||||||
|
* @param file The asset file to rewind.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileRewind(assetfile_t *file);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read bytes from the asset file. Assumes the file has already been opened
|
* Read bytes from the asset file. Assumes the file has already been opened
|
||||||
* prior to trying to read anything.
|
* prior to trying to read anything.
|
||||||
@@ -80,4 +87,52 @@ errorret_t assetFileClose(assetfile_t *file);
|
|||||||
* @param file The asset file to dispose.
|
* @param file The asset file to dispose.
|
||||||
* @return An error code if the file could not be disposed properly.
|
* @return An error code if the file could not be disposed properly.
|
||||||
*/
|
*/
|
||||||
errorret_t assetFileDispose(assetfile_t *file);
|
errorret_t assetFileDispose(assetfile_t *file);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
assetfile_t *file;
|
||||||
|
uint8_t *readBuffer;
|
||||||
|
size_t readBufferSize;
|
||||||
|
uint8_t *outBuffer;
|
||||||
|
size_t outBufferSize;
|
||||||
|
|
||||||
|
// A
|
||||||
|
size_t bufferStart;
|
||||||
|
size_t bufferEnd;
|
||||||
|
bool_t eof;//?
|
||||||
|
|
||||||
|
// Updated each reach:
|
||||||
|
size_t lineLength;
|
||||||
|
} assetfilelinereader_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a line reader for the given asset file. The line reader will read
|
||||||
|
* lines from the file into the provided line buffer, using the provided buffer
|
||||||
|
* for reading chunks of the file.
|
||||||
|
*
|
||||||
|
* @param file The asset file to read from. Must already be opened.
|
||||||
|
* @param readBuffer Buffer to use for reading chunks of the file.
|
||||||
|
* @param readBufferSize Size of the read buffer.
|
||||||
|
* @param outBuffer Buffer to read lines into. Lines will be null-terminated.
|
||||||
|
* @param outBufferSize Size of the output buffer.
|
||||||
|
* @return An initialized line reader structure.
|
||||||
|
*/
|
||||||
|
void assetFileLineReaderInit(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
assetfile_t *file,
|
||||||
|
uint8_t *readBuffer,
|
||||||
|
const size_t readBufferSize,
|
||||||
|
uint8_t *outBuffer,
|
||||||
|
const size_t outBufferSize
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the next line from the asset file into the line buffer. The line
|
||||||
|
* buffer is null-terminated and does not include the newline character.
|
||||||
|
*
|
||||||
|
* @param reader The line reader to read from.
|
||||||
|
* @return An error code if a failure occurs, or errorOk() if a line was read
|
||||||
|
* successfully. If the end of the file is reached, errorEndOfFile() is
|
||||||
|
* returned.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileLineReaderNext(assetfilelinereader_t *reader);
|
||||||
@@ -6,14 +6,802 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "assetlocaleloader.h"
|
#include "assetlocaleloader.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include "util/math.h"
|
||||||
|
#include "util/string.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
errorret_t assetLocaleLoader(assetfile_t *file) {
|
errorret_t assetLocaleFileInit(
|
||||||
errorThrow("Locale asset loading is not yet implemented.");
|
assetlocalefile_t *localeFile,
|
||||||
|
const char_t *path
|
||||||
|
) {
|
||||||
|
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
||||||
|
assertNotNull(path, "Locale file path cannot be NULL.");
|
||||||
|
|
||||||
|
memoryZero(localeFile, sizeof(assetlocalefile_t));
|
||||||
|
|
||||||
|
// Init the asset file.
|
||||||
|
errorChain(assetFileInit(&localeFile->file, path, NULL, NULL));
|
||||||
|
|
||||||
|
// Open the file handle
|
||||||
|
errorChain(assetFileOpen(&localeFile->file));
|
||||||
|
|
||||||
|
// Get the blank key, this is basically the header info for po files
|
||||||
|
char_t buffer[1024];
|
||||||
|
errorChain(assetLocaleGetString(localeFile, "", 0, buffer, sizeof(buffer)));
|
||||||
|
errorChain(assetLocaleParseHeader(localeFile, buffer, sizeof(buffer)));
|
||||||
|
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetLocaleLoad(
|
errorret_t assetLocaleFileDispose(assetlocalefile_t *localeFile) {
|
||||||
const char_t *path,
|
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
||||||
void *nothing
|
|
||||||
|
errorChain(assetFileClose(&localeFile->file));
|
||||||
|
errorChain(assetFileDispose(&localeFile->file));
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleParseHeader(
|
||||||
|
assetlocalefile_t *localeFile,
|
||||||
|
char_t *headerBuffer,
|
||||||
|
const size_t headerBufferSize
|
||||||
) {
|
) {
|
||||||
errorThrow("Locale asset loading is not yet implemented.");
|
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
||||||
|
assertNotNull(headerBuffer, "Header buffer cannot be NULL.");
|
||||||
|
assertTrue(headerBufferSize > 0, "Header buffer size must be > 0.");
|
||||||
|
|
||||||
|
// Find "Plural-Forms: " line and parse out plural form info
|
||||||
|
char_t *pluralFormsLine = strstr(headerBuffer, "Plural-Forms:");
|
||||||
|
if(!pluralFormsLine) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
pluralFormsLine += strlen("Plural-Forms:");
|
||||||
|
|
||||||
|
// Expect nplurals
|
||||||
|
char_t *npluralsStr = strstr(pluralFormsLine, "nplurals=");
|
||||||
|
if(!npluralsStr) {
|
||||||
|
errorThrow("Failed to find nplurals in Plural-Forms header.");
|
||||||
|
}
|
||||||
|
npluralsStr += strlen("nplurals=");
|
||||||
|
localeFile->pluralStateCount = (uint8_t)atoi(npluralsStr);
|
||||||
|
|
||||||
|
if(localeFile->pluralStateCount == 0) {
|
||||||
|
errorThrow("nplurals must be greater than 0.");
|
||||||
|
}
|
||||||
|
if(localeFile->pluralStateCount > ASSET_LOCALE_FILE_PLURAL_FORM_COUNT) {
|
||||||
|
errorThrow(
|
||||||
|
"nplurals exceeds maximum supported plural forms: %d > %d",
|
||||||
|
localeFile->pluralStateCount,
|
||||||
|
ASSET_LOCALE_FILE_PLURAL_FORM_COUNT
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expect plural=
|
||||||
|
char_t *pluralStr = strstr(pluralFormsLine, "plural=");
|
||||||
|
if(!pluralStr) {
|
||||||
|
errorThrow("Failed to find plural in Plural-Forms header.");
|
||||||
|
}
|
||||||
|
pluralStr += strlen("plural=");
|
||||||
|
|
||||||
|
// Expect ( [expressions] )
|
||||||
|
char_t *openParen = strchr(pluralStr, '(');
|
||||||
|
char_t *closeParen = strrchr(pluralStr, ')');
|
||||||
|
if(!openParen || !closeParen || closeParen < openParen) {
|
||||||
|
errorThrow("Failed to find plural expression in Plural-Forms header.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse:
|
||||||
|
// n [op] value ? index : n [op] value ? index : ... : final_index
|
||||||
|
char_t *ptr = openParen + 1;
|
||||||
|
uint8_t pluralIndex = 0;
|
||||||
|
uint8_t definedCount = 0;
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Allow grouped subexpressions like:
|
||||||
|
// (n<7 ? 2 : 3)
|
||||||
|
// or
|
||||||
|
// (((3)))
|
||||||
|
uint8_t parenDepth = 0;
|
||||||
|
while(*ptr == '(') {
|
||||||
|
parenDepth++;
|
||||||
|
ptr++;
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final fallback: just an integer
|
||||||
|
if(*ptr != 'n') {
|
||||||
|
char_t *endPtr = NULL;
|
||||||
|
int32_t finalIndex = (int32_t)strtol(ptr, &endPtr, 10);
|
||||||
|
if(endPtr == ptr) {
|
||||||
|
errorThrow("Expected final plural index.");
|
||||||
|
}
|
||||||
|
ptr = endPtr;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
while(parenDepth > 0) {
|
||||||
|
if(*ptr != ')') {
|
||||||
|
errorThrow("Expected ')' after final plural index.");
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
parenDepth--;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*ptr != ')') {
|
||||||
|
errorThrow("Expected ')' at end of plural expression.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(finalIndex < 0 || finalIndex >= localeFile->pluralStateCount) {
|
||||||
|
errorThrow(
|
||||||
|
"Final plural expression index out of bounds: %d (nplurals: %d)",
|
||||||
|
finalIndex,
|
||||||
|
localeFile->pluralStateCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
localeFile->pluralDefaultIndex = (uint8_t)finalIndex;
|
||||||
|
definedCount++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pluralIndex >= localeFile->pluralStateCount - 1) {
|
||||||
|
errorThrow(
|
||||||
|
"Too many plural conditions. Expected %d conditional clauses for nplurals=%d.",
|
||||||
|
localeFile->pluralStateCount - 1,
|
||||||
|
localeFile->pluralStateCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr++; // skip 'n'
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Determine operator
|
||||||
|
assetlocalepluraloperation_t op;
|
||||||
|
if(strncmp(ptr, "==", 2) == 0) {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_EQUAL;
|
||||||
|
ptr += 2;
|
||||||
|
} else if(strncmp(ptr, "!=", 2) == 0) {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_NOT_EQUAL;
|
||||||
|
ptr += 2;
|
||||||
|
} else if(strncmp(ptr, "<=", 2) == 0) {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_LESS_EQUAL;
|
||||||
|
ptr += 2;
|
||||||
|
} else if(strncmp(ptr, ">=", 2) == 0) {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL;
|
||||||
|
ptr += 2;
|
||||||
|
} else if(*ptr == '<') {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_LESS;
|
||||||
|
ptr++;
|
||||||
|
} else if(*ptr == '>') {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_GREATER;
|
||||||
|
ptr++;
|
||||||
|
} else {
|
||||||
|
errorThrow("Unsupported plural operator.");
|
||||||
|
}
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Parse the comparitor value
|
||||||
|
char_t *endPtr = NULL;
|
||||||
|
int32_t value = (int32_t)strtol(ptr, &endPtr, 10);
|
||||||
|
if(endPtr == ptr) {
|
||||||
|
errorThrow("Expected value for plural expression.");
|
||||||
|
}
|
||||||
|
ptr = endPtr;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Parse ternary operator
|
||||||
|
if(*ptr != '?') {
|
||||||
|
errorThrow("Expected '?' after plural expression.");
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Parse the indice
|
||||||
|
endPtr = NULL;
|
||||||
|
int32_t index = (int32_t)strtol(ptr, &endPtr, 10);
|
||||||
|
if(endPtr == ptr) {
|
||||||
|
errorThrow("Expected index for plural expression.");
|
||||||
|
}
|
||||||
|
ptr = endPtr;
|
||||||
|
|
||||||
|
if(index < 0 || index >= localeFile->pluralStateCount) {
|
||||||
|
errorThrow(
|
||||||
|
"Plural expression index out of bounds: %d (nplurals: %d)",
|
||||||
|
index,
|
||||||
|
localeFile->pluralStateCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store plural expression.
|
||||||
|
localeFile->pluralIndices[pluralIndex] = (uint8_t)index;
|
||||||
|
localeFile->pluralOps[pluralIndex] = op;
|
||||||
|
localeFile->pluralValues[pluralIndex] = value;
|
||||||
|
pluralIndex++;
|
||||||
|
definedCount++;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Close any grouping parens that wrapped this conditional branch
|
||||||
|
while(parenDepth > 0) {
|
||||||
|
if(*ptr != ')') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
parenDepth--;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*ptr != ':') {
|
||||||
|
errorThrow("Expected ':' after plural expression.");
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must define exactly nplurals outcomes:
|
||||||
|
// (nplurals - 1) conditional results + 1 final fallback result
|
||||||
|
if(
|
||||||
|
pluralIndex != localeFile->pluralStateCount - 1 ||
|
||||||
|
definedCount != localeFile->pluralStateCount
|
||||||
|
) {
|
||||||
|
errorThrow("Plural expression count does not match nplurals.");
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t assetLocaleEvaluatePlural(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const int32_t pluralCount
|
||||||
|
) {
|
||||||
|
assertNotNull(file, "Locale file cannot be NULL.");
|
||||||
|
assertTrue(pluralCount >= 0, "Plural count cannot be negative.");
|
||||||
|
|
||||||
|
for(uint8_t i = 0; i < file->pluralStateCount - 1; i++) {
|
||||||
|
int32_t value = file->pluralValues[i];
|
||||||
|
switch(file->pluralOps[i]) {
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_EQUAL:
|
||||||
|
if(pluralCount == value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_NOT_EQUAL:
|
||||||
|
if(pluralCount != value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_LESS:
|
||||||
|
if(pluralCount < value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_LESS_EQUAL:
|
||||||
|
if(pluralCount <= value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_GREATER:
|
||||||
|
if(pluralCount > value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL:
|
||||||
|
if(pluralCount >= value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return file->pluralDefaultIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleLineSkipBlanks(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
uint8_t *lineBuffer
|
||||||
|
) {
|
||||||
|
while(!reader->eof) {
|
||||||
|
// Skip blank lines
|
||||||
|
if(lineBuffer[0] == '\0') {
|
||||||
|
errorChain(assetFileLineReaderNext(reader));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip comment lines
|
||||||
|
if(lineBuffer[0] == '#') {
|
||||||
|
errorChain(assetFileLineReaderNext(reader));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is line only spaces?
|
||||||
|
size_t lineLength = strlen((char_t *)lineBuffer);
|
||||||
|
size_t i;
|
||||||
|
bool_t onlySpaces = true;
|
||||||
|
for(i = 0; i < lineLength; i++) {
|
||||||
|
if(lineBuffer[i] != ' ') {
|
||||||
|
onlySpaces = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(onlySpaces) {
|
||||||
|
errorChain(assetFileLineReaderNext(reader));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleLineUnbuffer(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
uint8_t *lineBuffer,
|
||||||
|
uint8_t *stringBuffer,
|
||||||
|
const size_t stringBufferSize
|
||||||
|
) {
|
||||||
|
stringBuffer[0] = '\0';
|
||||||
|
|
||||||
|
// At the point this funciton is called, we are looking for an opening quote.
|
||||||
|
char_t *start = strchr((char_t *)lineBuffer, '"');
|
||||||
|
if(!start) {
|
||||||
|
errorThrow("Expected open (0) \"");
|
||||||
|
}
|
||||||
|
|
||||||
|
char *end = strchr(start + 1, '"');
|
||||||
|
if(!end) {
|
||||||
|
errorThrow("Expected close (0) \"");
|
||||||
|
}
|
||||||
|
*end = '\0';
|
||||||
|
|
||||||
|
if(strlen(start) >= stringBufferSize) {
|
||||||
|
errorThrow("String buffer overflow");
|
||||||
|
}
|
||||||
|
memoryCopy(stringBuffer, start + 1, strlen(start));
|
||||||
|
|
||||||
|
// Now start buffering lines out
|
||||||
|
while(!reader->eof) {
|
||||||
|
errorChain(assetFileLineReaderNext(reader));
|
||||||
|
|
||||||
|
// Skip blank lines
|
||||||
|
errorChain(assetLocaleLineSkipBlanks(reader, lineBuffer));
|
||||||
|
|
||||||
|
// Skip starting spaces
|
||||||
|
char_t *ptr = (char_t *)lineBuffer;
|
||||||
|
while(*ptr == ' ') {
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only consider lines starting with quote
|
||||||
|
if(*ptr != '"') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr++; // move past first quote
|
||||||
|
|
||||||
|
bool_t escaping = false;
|
||||||
|
char_t *dest = (char_t *)stringBuffer + strlen((char_t *)stringBuffer);
|
||||||
|
while(*ptr) {
|
||||||
|
if(escaping) {
|
||||||
|
// Handle escape sequences
|
||||||
|
switch(*ptr) {
|
||||||
|
case 'n': *dest++ = '\n'; break;
|
||||||
|
case 't': *dest++ = '\t'; break;
|
||||||
|
case '\\': *dest++ = '\\'; break;
|
||||||
|
case '"': *dest++ = '"'; break;
|
||||||
|
default:
|
||||||
|
errorThrow("Unknown escape sequence: \\%c", *ptr);
|
||||||
|
}
|
||||||
|
escaping = false;
|
||||||
|
} else if(*ptr == '\\') {
|
||||||
|
escaping = true;
|
||||||
|
} else if(*ptr == '"') {
|
||||||
|
// End of string
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
// Regular character
|
||||||
|
*dest++ = *ptr;
|
||||||
|
}
|
||||||
|
if((size_t)(dest - (char_t *)stringBuffer) >= stringBufferSize) {
|
||||||
|
errorThrow("String buffer overflow");
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
*dest = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleGetString(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralCount,
|
||||||
|
char_t *stringBuffer,
|
||||||
|
const size_t stringBufferSize
|
||||||
|
) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(messageId, "Message ID cannot be NULL.");
|
||||||
|
assertTrue(pluralCount >= 0, "Plural index cannot be negative.");
|
||||||
|
assertNotNull(stringBuffer, "String buffer cannot be NULL.");
|
||||||
|
assertTrue(stringBufferSize > 0, "String buffer size must be > 0");
|
||||||
|
assetfilelinereader_t reader;
|
||||||
|
|
||||||
|
bool_t msgidFound = false, msgidPluralFound = false, msgstrFound = false;
|
||||||
|
uint8_t msgidBuffer[256];
|
||||||
|
uint8_t msgidPluralBuffer[256];
|
||||||
|
uint8_t readBuffer[1024];
|
||||||
|
uint8_t lineBuffer[1024];
|
||||||
|
uint8_t pluralIndex = 0xFF;
|
||||||
|
|
||||||
|
msgidBuffer[0] = '\0';
|
||||||
|
msgidPluralBuffer[0] = '\0';
|
||||||
|
stringBuffer[0] = '\0';
|
||||||
|
|
||||||
|
// Rewind and start reading lines.
|
||||||
|
errorChain(assetFileRewind(&file->file));
|
||||||
|
assetFileLineReaderInit(
|
||||||
|
&reader,
|
||||||
|
&file->file,
|
||||||
|
readBuffer,
|
||||||
|
sizeof(readBuffer),
|
||||||
|
lineBuffer,
|
||||||
|
sizeof(lineBuffer)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Skip blanks, comments, etc and start looking for msgid's
|
||||||
|
errorChain(assetLocaleLineSkipBlanks(&reader, lineBuffer));
|
||||||
|
|
||||||
|
while(!reader.eof) {
|
||||||
|
// Is this msgid?
|
||||||
|
if(memoryCompare(lineBuffer, "msgid", 5) != 0) {
|
||||||
|
errorChain(assetFileLineReaderNext(&reader));
|
||||||
|
msgidBuffer[0] = '\0';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unbuffer the msgid
|
||||||
|
assetLocaleLineUnbuffer(
|
||||||
|
&reader, lineBuffer, (uint8_t *)msgidBuffer, sizeof(msgidBuffer)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Is this the needle?
|
||||||
|
if(memoryCompare(msgidBuffer, messageId, strlen(messageId)) != 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
msgidFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(!msgidFound) {
|
||||||
|
errorThrow("Failed to find message ID: %s", messageId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// We are either going to see a msgstr or a msgid_plural
|
||||||
|
while(!reader.eof) {
|
||||||
|
errorChain(assetLocaleLineSkipBlanks(&reader, lineBuffer));
|
||||||
|
|
||||||
|
// Is msgid_plural?
|
||||||
|
if(
|
||||||
|
!msgidPluralFound &&
|
||||||
|
memoryCompare(lineBuffer, "msgid_plural", 12) == 0
|
||||||
|
) {
|
||||||
|
// Yes, start reading plural ID
|
||||||
|
assetLocaleLineUnbuffer(
|
||||||
|
&reader,
|
||||||
|
lineBuffer,
|
||||||
|
(uint8_t *)msgidPluralBuffer,
|
||||||
|
sizeof(msgidPluralBuffer)
|
||||||
|
);
|
||||||
|
msgidPluralFound = true;
|
||||||
|
|
||||||
|
// At this point we determine the plural index to use by running the
|
||||||
|
// plural formula
|
||||||
|
pluralIndex = assetLocaleEvaluatePlural(
|
||||||
|
file,
|
||||||
|
pluralCount
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should be msgstr if not plural.
|
||||||
|
if(memoryCompare(lineBuffer, "msgstr", 6) != 0) {
|
||||||
|
errorThrow("Expected msgstr after msgid, found: %s", lineBuffer);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If plural we need an index
|
||||||
|
if(msgidPluralFound) {
|
||||||
|
// Skip blank chars
|
||||||
|
char_t *ptr = (char_t *)lineBuffer + 6;
|
||||||
|
while(*ptr == ' ') {
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*ptr != '[') {
|
||||||
|
errorThrow("Expected [ for plural form, found: %s", lineBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr++; // move past [
|
||||||
|
|
||||||
|
// Parse until ]
|
||||||
|
char *end = strchr(ptr, ']');
|
||||||
|
if(!end) {
|
||||||
|
errorThrow("Expected ] for plural form, found: %s", lineBuffer);
|
||||||
|
}
|
||||||
|
*end = '\0';
|
||||||
|
|
||||||
|
int32_t index = atoi(ptr);
|
||||||
|
if(index != pluralIndex) {
|
||||||
|
// Not the plural form we want, skip to the next useable line
|
||||||
|
while(!reader.eof) {
|
||||||
|
errorChain(assetFileLineReaderNext(&reader));
|
||||||
|
errorChain(assetLocaleLineSkipBlanks(&reader, lineBuffer));
|
||||||
|
if(
|
||||||
|
lineBuffer[0] == '\"' ||
|
||||||
|
lineBuffer[0] == '\0' ||
|
||||||
|
lineBuffer[0] == '#'
|
||||||
|
) continue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Undo damage to line buffer from unbuffering.
|
||||||
|
*end = ']';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse out msgstr
|
||||||
|
errorChain(assetLocaleLineUnbuffer(
|
||||||
|
&reader, lineBuffer, (uint8_t *)stringBuffer, stringBufferSize
|
||||||
|
));
|
||||||
|
msgstrFound = true;
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
if(!msgstrFound) {
|
||||||
|
errorThrow("Failed to find msgstr for message ID: %s", messageId);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleGetStringWithVA(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralIndex,
|
||||||
|
char_t *buffer,
|
||||||
|
const size_t bufferSize,
|
||||||
|
...
|
||||||
|
) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(messageId, "Message ID cannot be NULL.");
|
||||||
|
assertNotNull(buffer, "Buffer cannot be NULL.");
|
||||||
|
assertTrue(bufferSize > 0, "Buffer size must be > 0.");
|
||||||
|
assertTrue(pluralIndex >= 0, "Plural cannot be negative.");
|
||||||
|
|
||||||
|
char_t *tempBuffer;
|
||||||
|
tempBuffer = memoryAllocate(bufferSize);
|
||||||
|
errorret_t ret = assetLocaleGetString(
|
||||||
|
file,
|
||||||
|
messageId,
|
||||||
|
pluralIndex,
|
||||||
|
tempBuffer,
|
||||||
|
bufferSize
|
||||||
|
);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
memoryFree(tempBuffer);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
va_list args;
|
||||||
|
va_start(args, bufferSize);
|
||||||
|
int result = vsnprintf(buffer, bufferSize, tempBuffer, args);
|
||||||
|
va_end(args);
|
||||||
|
memoryFree(tempBuffer);
|
||||||
|
|
||||||
|
if(result < 0) {
|
||||||
|
errorThrow("Failed to format locale string for ID: %s", messageId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleGetStringWithArgs(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *id,
|
||||||
|
const int32_t plural,
|
||||||
|
char_t *buffer,
|
||||||
|
const size_t bufferSize,
|
||||||
|
const assetlocalearg_t *args,
|
||||||
|
const size_t argCount
|
||||||
|
) {
|
||||||
|
assertNotNull(id, "Message ID cannot be NULL.");
|
||||||
|
assertNotNull(buffer, "Buffer cannot be NULL.");
|
||||||
|
assertTrue(bufferSize > 0, "Buffer size must be > 0.");
|
||||||
|
assertTrue(plural >= 0, "Plural cannot be negative.");
|
||||||
|
assertTrue(
|
||||||
|
argCount == 0 || args != NULL,
|
||||||
|
"Args cannot be NULL when argCount > 0."
|
||||||
|
);
|
||||||
|
|
||||||
|
char_t *format = memoryAllocate(bufferSize);
|
||||||
|
if(format == NULL) {
|
||||||
|
errorThrow("Failed to allocate format buffer.");
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t ret = assetLocaleGetString(
|
||||||
|
file,
|
||||||
|
id,
|
||||||
|
plural,
|
||||||
|
format,
|
||||||
|
bufferSize
|
||||||
|
);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
memoryFree(format);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t inIndex = 0;
|
||||||
|
size_t outIndex = 0;
|
||||||
|
size_t nextArg = 0;
|
||||||
|
|
||||||
|
buffer[0] = '\0';
|
||||||
|
|
||||||
|
while(format[inIndex] != '\0') {
|
||||||
|
if(format[inIndex] != '%') {
|
||||||
|
if(outIndex + 1 >= bufferSize) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Formatted locale string buffer overflow for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[outIndex++] = format[inIndex++];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
inIndex++;
|
||||||
|
|
||||||
|
/* Escaped percent */
|
||||||
|
if(format[inIndex] == '%') {
|
||||||
|
if(outIndex + 1 >= bufferSize) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Formatted locale string buffer overflow for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[outIndex++] = '%';
|
||||||
|
inIndex++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(nextArg >= argCount) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Not enough locale arguments for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
char_t specBuffer[32];
|
||||||
|
char_t valueBuffer[256];
|
||||||
|
size_t specLength = 0;
|
||||||
|
int written = 0;
|
||||||
|
char_t specifier;
|
||||||
|
|
||||||
|
specBuffer[specLength++] = '%';
|
||||||
|
|
||||||
|
/* Allow flags / width / precision */
|
||||||
|
while(format[inIndex] != '\0') {
|
||||||
|
char_t ch = format[inIndex];
|
||||||
|
|
||||||
|
if(
|
||||||
|
ch == '-' || ch == '+' || ch == ' ' || ch == '#' || ch == '0' ||
|
||||||
|
ch == '.' || (ch >= '0' && ch <= '9')
|
||||||
|
) {
|
||||||
|
if(specLength + 1 >= sizeof(specBuffer)) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Format specifier too long for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
specBuffer[specLength++] = ch;
|
||||||
|
inIndex++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(format[inIndex] == '\0') {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Incomplete format specifier for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
specifier = format[inIndex++];
|
||||||
|
|
||||||
|
if(specifier != 's' && specifier != 'd' && specifier != 'f') {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow(
|
||||||
|
"Unsupported format specifier '%%%c' for ID: %s",
|
||||||
|
specifier,
|
||||||
|
id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
specBuffer[specLength++] = specifier;
|
||||||
|
specBuffer[specLength] = '\0';
|
||||||
|
|
||||||
|
switch(specifier) {
|
||||||
|
case 's':
|
||||||
|
if(args[nextArg].type != ASSET_LOCALE_ARG_STRING) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Expected string locale argument for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
written = snprintf(
|
||||||
|
valueBuffer,
|
||||||
|
sizeof(valueBuffer),
|
||||||
|
specBuffer,
|
||||||
|
args[nextArg].stringValue ? args[nextArg].stringValue : ""
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'd':
|
||||||
|
if(args[nextArg].type != ASSET_LOCALE_ARG_INT) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Expected int locale argument for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
written = snprintf(
|
||||||
|
valueBuffer,
|
||||||
|
sizeof(valueBuffer),
|
||||||
|
specBuffer,
|
||||||
|
args[nextArg].intValue
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'f':
|
||||||
|
if(
|
||||||
|
args[nextArg].type != ASSET_LOCALE_ARG_FLOAT &&
|
||||||
|
args[nextArg].type != ASSET_LOCALE_ARG_INT
|
||||||
|
) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Expected float or int locale argument for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
float_t floatValue = (
|
||||||
|
args[nextArg].type == ASSET_LOCALE_ARG_FLOAT ?
|
||||||
|
args[nextArg].floatValue :
|
||||||
|
(float_t)args[nextArg].intValue
|
||||||
|
);
|
||||||
|
|
||||||
|
written = snprintf(
|
||||||
|
valueBuffer,
|
||||||
|
sizeof(valueBuffer),
|
||||||
|
specBuffer,
|
||||||
|
floatValue
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
nextArg++;
|
||||||
|
|
||||||
|
if(written < 0) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Failed to format locale string for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(outIndex + (size_t)written >= bufferSize) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Formatted locale string buffer overflow for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryCopy(buffer + outIndex, valueBuffer, (size_t)written);
|
||||||
|
outIndex += (size_t)written;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[outIndex] = '\0';
|
||||||
|
memoryFree(format);
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -7,28 +7,155 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "asset/asset.h"
|
#include "asset/asset.h"
|
||||||
#include "locale/localemanager.h"
|
|
||||||
|
#define ASSET_LOCALE_FILE_PLURAL_FORM_COUNT 6
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_LOCALE_PLURAL_OP_EQUAL,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_NOT_EQUAL,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_LESS,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_LESS_EQUAL,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_GREATER,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL
|
||||||
|
} assetlocalepluraloperation_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_LOCALE_ARG_STRING,
|
||||||
|
ASSET_LOCALE_ARG_INT,
|
||||||
|
ASSET_LOCALE_ARG_FLOAT
|
||||||
|
} assetlocaleargtype_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
void *nothing;
|
assetlocaleargtype_t type;
|
||||||
} assetlocaleloaderparams_t;
|
union {
|
||||||
|
const char_t *stringValue;
|
||||||
|
int32_t intValue;
|
||||||
|
float_t floatValue;
|
||||||
|
};
|
||||||
|
} assetlocalearg_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
assetfile_t file;
|
||||||
|
assetlocalepluraloperation_t pluralOps[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||||
|
int32_t pluralValues[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||||
|
int32_t pluralIndices[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||||
|
uint8_t pluralStateCount;
|
||||||
|
uint8_t pluralDefaultIndex;
|
||||||
|
} assetlocalefile_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for locale assets.
|
* Initialize a locale asset file.
|
||||||
*
|
*
|
||||||
* @param file Asset file to load the locale from.
|
* @param localeFile The locale file to initialize.
|
||||||
* @return Any error that occurs during loading.
|
* @param path The path to the locale file.
|
||||||
|
* @return An error code if a failure occurs.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLocaleLoader(assetfile_t *file);
|
errorret_t assetLocaleFileInit(
|
||||||
|
assetlocalefile_t *localeFile,
|
||||||
|
const char_t *path
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a locale from the specified path.
|
* Dispose of a locale asset file.
|
||||||
*
|
*
|
||||||
* @param path Path to the locale asset.
|
* @param localeFile The locale file to dispose of.
|
||||||
* @param nothing Nothing yet.
|
* @return An error code if a failure occurs.
|
||||||
* @return Any error that occurs during loading.
|
|
||||||
*/
|
*/
|
||||||
errorret_t assetLocaleLoad(
|
errorret_t assetLocaleFileDispose(assetlocalefile_t *localeFile);
|
||||||
const char_t *path,
|
|
||||||
void *nothing
|
errorret_t assetLocaleParseHeader(
|
||||||
|
assetlocalefile_t *localeFile,
|
||||||
|
char_t *headerBuffer,
|
||||||
|
const size_t headerBufferSize
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skips blank lines and comment lines in the line reader.
|
||||||
|
*
|
||||||
|
* @param reader Line reader to read from.
|
||||||
|
* @param lineBuffer Buffer to use for reading lines.
|
||||||
|
* @return Any error that occurs during skipping.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleLineSkipBlanks(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
uint8_t *lineBuffer
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unbuffers a potentially multi-line quoted string from the line reader.
|
||||||
|
*
|
||||||
|
* This will read lines until it finds a line that starts with a quote, then
|
||||||
|
* read until the closing quote.
|
||||||
|
*
|
||||||
|
* @param reader Line reader to read from.
|
||||||
|
* @param lineBuffer Buffer to use for reading lines.
|
||||||
|
* @param stringBuffer Buffer to write the unbuffered string to.
|
||||||
|
* @param stringBufferSize Size of the string buffer.
|
||||||
|
* @return Any error that occurs during unbuffering.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleLineUnbuffer(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
uint8_t *lineBuffer,
|
||||||
|
uint8_t *stringBuffer,
|
||||||
|
const size_t stringBufferSize
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test function for locale asset loading.
|
||||||
|
*
|
||||||
|
* @param file Asset file to test loading from.
|
||||||
|
* @param messageId The message ID to retrieve.
|
||||||
|
* @param pluralCount Count for formulating the plural variant.
|
||||||
|
* @param stringBuffer Buffer to write the retrieved string to.
|
||||||
|
* @param stringBufferSize Size of the string buffer.
|
||||||
|
* @return Any error that occurs during testing.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleGetString(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralCount,
|
||||||
|
char_t *stringBuffer,
|
||||||
|
const size_t stringBufferSize
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test function for locale asset loading with a variable argument list.
|
||||||
|
*
|
||||||
|
* @param file Asset file to test loading from.
|
||||||
|
* @param messageId The message ID to retrieve.
|
||||||
|
* @param pluralCount Count for formulating the plural variant.
|
||||||
|
* @param buffer Buffer to write the retrieved string to.
|
||||||
|
* @param bufferSize Size of the buffer.
|
||||||
|
* @param ... Additional arguments for formatting the string.
|
||||||
|
* @return Any error that occurs during testing.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleGetStringWithVA(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralCount,
|
||||||
|
char_t *buffer,
|
||||||
|
const size_t bufferSize,
|
||||||
|
...
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test function for locale asset loading with a list of arguments.
|
||||||
|
*
|
||||||
|
* @param file Asset file to test loading from.
|
||||||
|
* @param messageId The message ID to retrieve.
|
||||||
|
* @param pluralCount Count for formulating the plural variant.
|
||||||
|
* @param buffer Buffer to write the retrieved string to.
|
||||||
|
* @param bufferSize Size of the buffer.
|
||||||
|
* @param args List of arguments for formatting the string.
|
||||||
|
* @param argCount Number of arguments in the list.
|
||||||
|
* @return Any error that occurs during testing.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleGetStringWithArgs(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralCount,
|
||||||
|
char_t *buffer,
|
||||||
|
const size_t bufferSize,
|
||||||
|
const assetlocalearg_t *args,
|
||||||
|
const size_t argCount
|
||||||
);
|
);
|
||||||
@@ -39,20 +39,6 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
|||||||
errorChain(sceneInit());
|
errorChain(sceneInit());
|
||||||
errorChain(gameInit());
|
errorChain(gameInit());
|
||||||
|
|
||||||
// JSON test.
|
|
||||||
yyjson_doc *doc;
|
|
||||||
assetJsonLoad("test.json", &doc);
|
|
||||||
|
|
||||||
yyjson_val *root = yyjson_doc_get_root(doc);
|
|
||||||
assertTrue(root, "JSON root is null.");
|
|
||||||
|
|
||||||
yyjson_val *name = yyjson_obj_get(root, "name");
|
|
||||||
assertTrue(name && yyjson_is_str(name), "JSON 'name' field is missing or not a string.");
|
|
||||||
const char *nameStr = yyjson_get_str(name);
|
|
||||||
printf("Name: %s\n", nameStr);
|
|
||||||
|
|
||||||
yyjson_doc_free(doc);
|
|
||||||
|
|
||||||
// Run the initial script.
|
// Run the initial script.
|
||||||
scriptcontext_t ctx;
|
scriptcontext_t ctx;
|
||||||
errorChain(scriptContextInit(&ctx));
|
errorChain(scriptContextInit(&ctx));
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2026 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "dusk.h"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
void *nothing;
|
|
||||||
} dusklocale_t;
|
|
||||||
@@ -9,5 +9,11 @@
|
|||||||
#include "dusk.h"
|
#include "dusk.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
const char_t *name;
|
||||||
const char_t *file;
|
const char_t *file;
|
||||||
} localeinfo_t;
|
} localeinfo_t;
|
||||||
|
|
||||||
|
static const localeinfo_t LOCALE_EN_US = {
|
||||||
|
.name = "en-US",
|
||||||
|
.file = "locale/en_US.po",
|
||||||
|
};
|
||||||
@@ -7,29 +7,34 @@
|
|||||||
|
|
||||||
#include "localemanager.h"
|
#include "localemanager.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
localemanager_t LOCALE;
|
localemanager_t LOCALE;
|
||||||
|
|
||||||
errorret_t localeManagerInit() {
|
errorret_t localeManagerInit() {
|
||||||
memoryZero(&LOCALE, sizeof(localemanager_t));
|
memoryZero(&LOCALE, sizeof(localemanager_t));
|
||||||
|
|
||||||
|
errorChain(localeManagerSetLocale(&LOCALE_EN_US));
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t localeManagerSetLocale(const dusklocale_t locale) {
|
errorret_t localeManagerSetLocale(const localeinfo_t *locale) {
|
||||||
errorThrow("Locale setting is not yet implemented.");
|
if(LOCALE.fileOpen) {
|
||||||
// assertTrue(locale < DUSK_LOCALE_COUNT, "Invalid locale.");
|
errorChain(assetLocaleFileDispose(&LOCALE.file));
|
||||||
// assertTrue(locale != DUSK_LOCALE_NULL, "Cannot set locale to NULL.");
|
LOCALE.fileOpen = false;
|
||||||
|
}
|
||||||
|
|
||||||
// LOCALE.locale = locale;
|
// Init the asset file
|
||||||
// char_t languageFile[FILENAME_MAX];
|
errorChain(assetLocaleFileInit(&LOCALE.file, locale->file));
|
||||||
// snprintf(
|
LOCALE.fileOpen = true;
|
||||||
// languageFile, FILENAME_MAX, "language/%s.dlf", LOCALE_INFOS[locale].file
|
|
||||||
// );
|
errorOk();
|
||||||
// assetLanguageDispose(&LOCALE.language);
|
|
||||||
// memoryZero(&LOCALE.language, sizeof(assetlanguage_t));
|
|
||||||
// errorChain(assetLoad(languageFile, &LOCALE.language));
|
|
||||||
// errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void localeManagerDispose() {
|
void localeManagerDispose() {
|
||||||
|
if(LOCALE.fileOpen) {
|
||||||
|
errorCatch(errorPrint(assetLocaleFileDispose(&LOCALE.file)));
|
||||||
|
LOCALE.fileOpen = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,11 +8,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.h"
|
#include "error/error.h"
|
||||||
#include "localemanager.h"
|
#include "localemanager.h"
|
||||||
#include "locale/locale.h"
|
#include "locale/localeinfo.h"
|
||||||
|
#include "asset/loader/locale/assetlocaleloader.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
dusklocale_t locale;
|
const localeinfo_t *locale;
|
||||||
// assetlanguage_t language;
|
assetlocalefile_t file;
|
||||||
|
bool_t fileOpen;
|
||||||
} localemanager_t;
|
} localemanager_t;
|
||||||
|
|
||||||
extern localemanager_t LOCALE;
|
extern localemanager_t LOCALE;
|
||||||
@@ -30,7 +32,51 @@ errorret_t localeManagerInit();
|
|||||||
* @param locale The locale to set.
|
* @param locale The locale to set.
|
||||||
* @return An error code if a failure occurs.
|
* @return An error code if a failure occurs.
|
||||||
*/
|
*/
|
||||||
errorret_t localeManagerSetLocale(const dusklocale_t locale);
|
errorret_t localeManagerSetLocale(const localeinfo_t *locale);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a localized string for the given message ID.
|
||||||
|
*
|
||||||
|
* @param id The message ID to retrieve.
|
||||||
|
* @param buffer Buffer to write the retrieved string to.
|
||||||
|
* @param bufferSize Size of the buffer.
|
||||||
|
* @param plural Plural index to retrieve.
|
||||||
|
* @param ... Additional arguments for formatting the string.
|
||||||
|
* @return An error code if a failure occurs.
|
||||||
|
*/
|
||||||
|
#define localeManagerGetText(id, buffer, bufferSize, plural, ...) \
|
||||||
|
assetLocaleGetStringWithVA( \
|
||||||
|
&LOCALE.file, \
|
||||||
|
id, \
|
||||||
|
plural, \
|
||||||
|
buffer, \
|
||||||
|
bufferSize, \
|
||||||
|
__VA_ARGS__ \
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a localized string for the given message ID with a list of arguments.
|
||||||
|
*
|
||||||
|
* @param id The message ID to retrieve.
|
||||||
|
* @param buffer Buffer to write the retrieved string to.
|
||||||
|
* @param bufferSize Size of the buffer.
|
||||||
|
* @param plural Plural index to retrieve.
|
||||||
|
* @param args List of arguments for formatting the string.
|
||||||
|
* @param argCount Number of arguments in the list.
|
||||||
|
* @return An error code if a failure occurs.
|
||||||
|
*/
|
||||||
|
#define localeManagerGetTextArgs( \
|
||||||
|
id, buffer, bufferSize, plural, args, argCount \
|
||||||
|
) \
|
||||||
|
assetLocaleGetStringWithArgs( \
|
||||||
|
&LOCALE.file, \
|
||||||
|
id, \
|
||||||
|
plural, \
|
||||||
|
buffer, \
|
||||||
|
bufferSize, \
|
||||||
|
args, \
|
||||||
|
argCount \
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispose of the locale system.
|
* Dispose of the locale system.
|
||||||
|
|||||||
@@ -13,66 +13,87 @@ void moduleLocale(scriptcontext_t *context) {
|
|||||||
assertNotNull(context, "Script context cannot be NULL");
|
assertNotNull(context, "Script context cannot be NULL");
|
||||||
|
|
||||||
// Execute the locale script definitions.
|
// Execute the locale script definitions.
|
||||||
// scriptContextExec(context, LOCALE_SCRIPT);
|
lua_register(context->luaState, "localeGetText", moduleLocaleGetText);
|
||||||
|
|
||||||
// lua_register(context->luaState, "localeGet", moduleLocaleGet);
|
|
||||||
// lua_register(context->luaState, "localeSet", moduleLocaleSet);
|
|
||||||
// lua_register(context->luaState, "localeGetName", moduleLocaleGetName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// int moduleLocaleGet(lua_State *L) {
|
int moduleLocaleGetText(lua_State *L) {
|
||||||
// assertNotNull(L, "Lua state cannot be NULL");
|
// Expect string param for the id
|
||||||
|
if(!lua_isstring(L, 1)) {
|
||||||
|
luaL_error(L, "Expected message ID as first argument");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// // No arguments expected
|
const char_t *id = lua_tostring(L, 1);
|
||||||
// dusklocale_t locale = LOCALE.locale;
|
if(id == NULL || strlen(id) == 0) {
|
||||||
// lua_pushnumber(L, (lua_Number)locale);
|
luaL_error(L, "Message ID cannot be NULL or empty");
|
||||||
// return 1;
|
return 0;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// int moduleLocaleSet(lua_State *L) {
|
// Optional plural param, default to 0
|
||||||
// assertNotNull(L, "Lua state cannot be NULL");
|
int32_t plural = 0;
|
||||||
|
int top = lua_gettop(L);
|
||||||
|
int argStart = 2;
|
||||||
|
|
||||||
// // Requires locale ID
|
if(top >= 2 && !lua_isnil(L, 2)) {
|
||||||
// if(!lua_isnumber(L, 1)) {
|
if(!lua_isnumber(L, 2)) {
|
||||||
// luaL_error(L, "localeSet: Expected locale ID as first argument");
|
luaL_error(L, "Expected plural as second argument");
|
||||||
// return 0;
|
return 0;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// errorret_t err;
|
plural = (int32_t)lua_tointeger(L, 2);
|
||||||
// dusklocale_t locale = (dusklocale_t)lua_tonumber(L, 1);
|
if(plural < 0) {
|
||||||
// if(locale >= DUSK_LOCALE_COUNT || locale == DUSK_LOCALE_NULL) {
|
luaL_error(L, "Plural cannot be negative");
|
||||||
// luaL_error(L, "localeSet: Invalid locale ID");
|
return 0;
|
||||||
// return 0;
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// err = localeManagerSetLocale(locale);
|
argStart = 3;
|
||||||
// if(err.code != ERROR_OK) {
|
}
|
||||||
// luaL_error(L, "localeSet: Failed to set locale");
|
|
||||||
// errorCatch(errorPrint(err));
|
|
||||||
// return 0;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return 0;
|
// Build structured arg list from remaining Lua args
|
||||||
// }
|
size_t argCount = (top >= argStart) ? (size_t)(top - argStart + 1) : 0;
|
||||||
|
#define MODULE_LOCALE_MAX_ARGS 16
|
||||||
|
assetlocalearg_t argsStack[MODULE_LOCALE_MAX_ARGS];
|
||||||
|
assetlocalearg_t *args = argsStack;
|
||||||
|
|
||||||
// int moduleLocaleGetName(lua_State *L) {
|
if(argCount > MODULE_LOCALE_MAX_ARGS) {
|
||||||
// assertNotNull(L, "Lua state cannot be NULL");
|
luaL_error(L, "Too many args (max %d)", MODULE_LOCALE_MAX_ARGS);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// // Optional ID, otherwise return current locale name
|
for(size_t i = 0; i < argCount; ++i) {
|
||||||
// dusklocale_t locale = LOCALE.locale;
|
int luaIndex = argStart + (int)i;
|
||||||
// if(lua_gettop(L) >= 1) {
|
|
||||||
// if(!lua_isnumber(L, 1)) {
|
|
||||||
// luaL_error(L, "localeGetName: Expected locale ID as first argument");
|
|
||||||
// return 0;
|
|
||||||
// }
|
|
||||||
// locale = (dusklocale_t)lua_tonumber(L, 1);
|
|
||||||
// if(locale >= DUSK_LOCALE_COUNT || locale == DUSK_LOCALE_NULL) {
|
|
||||||
// luaL_error(L, "localeGetName: Invalid locale ID");
|
|
||||||
// return 0;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const char_t *localeName = LOCALE_INFOS[locale].file;
|
if(lua_isinteger(L, luaIndex)) {
|
||||||
// lua_pushstring(L, localeName);
|
args[i].type = ASSET_LOCALE_ARG_INT;
|
||||||
// return 1;
|
args[i].intValue = (int32_t)lua_tonumber(L, luaIndex);
|
||||||
// }
|
} else if(lua_isnumber(L, luaIndex)) {
|
||||||
|
args[i].type = ASSET_LOCALE_ARG_FLOAT;
|
||||||
|
args[i].floatValue = lua_tonumber(L, luaIndex);
|
||||||
|
} else if(lua_isstring(L, luaIndex)) {
|
||||||
|
args[i].type = ASSET_LOCALE_ARG_STRING;
|
||||||
|
args[i].stringValue = lua_tostring(L, luaIndex);
|
||||||
|
} else {
|
||||||
|
luaL_error(L, "Unsupported localization argument type");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char_t buffer[1024];
|
||||||
|
errorret_t err = localeManagerGetTextArgs(
|
||||||
|
id,
|
||||||
|
buffer,
|
||||||
|
sizeof(buffer),
|
||||||
|
plural,
|
||||||
|
args,
|
||||||
|
argCount
|
||||||
|
);
|
||||||
|
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
errorCatch(errorPrint(err));
|
||||||
|
luaL_error(L, "Failed to get localized text for ID '%s'", id);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushstring(L, buffer);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -16,25 +16,9 @@
|
|||||||
void moduleLocale(scriptcontext_t *context);
|
void moduleLocale(scriptcontext_t *context);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Script binding for getting the current locale ID.
|
* Script binding for getting a localized string.
|
||||||
*
|
*
|
||||||
* @param L The Lua state.
|
* @param L The Lua state.
|
||||||
* @return Number of return values on the Lua stack.
|
* @return The number of return values on the Lua stack.
|
||||||
*/
|
*/
|
||||||
int moduleLocaleGet(lua_State *L);
|
int moduleLocaleGetText(lua_State *L);
|
||||||
|
|
||||||
/**
|
|
||||||
* Script binding for setting the current locale ID.
|
|
||||||
*
|
|
||||||
* @param L The Lua state.
|
|
||||||
* @return Number of return values on the Lua stack.
|
|
||||||
*/
|
|
||||||
int moduleLocaleSet(lua_State *L);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Script binding for getting the name of a locale by its ID.
|
|
||||||
*
|
|
||||||
* @param L The Lua state.
|
|
||||||
* @return Number of return values on the Lua stack.
|
|
||||||
*/
|
|
||||||
int moduleLocaleGetName(lua_State *L);
|
|
||||||
@@ -82,7 +82,7 @@ int_t memoryCompare(
|
|||||||
) {
|
) {
|
||||||
assertNotNull(a, "Cannot compare NULL memory.");
|
assertNotNull(a, "Cannot compare NULL memory.");
|
||||||
assertNotNull(b, "Cannot compare NULL memory.");
|
assertNotNull(b, "Cannot compare NULL memory.");
|
||||||
assertTrue(size > 0, "Cannot compare 0 bytes of memory.");
|
assertTrue(size >= 0, "Cannot compare negative bytes of memory.");
|
||||||
return memcmp(a, b, size);
|
return memcmp(a, b, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -313,8 +313,8 @@ static void test_memoryCompare(void **state) {
|
|||||||
assert_int_equal(memoryCompare(a, a, size), 0);
|
assert_int_equal(memoryCompare(a, a, size), 0);
|
||||||
assert_int_equal(memoryCompare(b, b, size), 0);
|
assert_int_equal(memoryCompare(b, b, size), 0);
|
||||||
|
|
||||||
// Cannot compare 0 bytes
|
// Comparison of 0 bytes is always true
|
||||||
expect_assert_failure(memoryCompare(a, b, 0));
|
assert_true(memoryCompare(a, b, 0));
|
||||||
|
|
||||||
// Compare different sizes (should assert, so we only test the API contract)
|
// Compare different sizes (should assert, so we only test the API contract)
|
||||||
// Not possible with current API, as size is explicit, but document intent.
|
// Not possible with current API, as size is explicit, but document intent.
|
||||||
|
|||||||
Reference in New Issue
Block a user