Options and Quit now route through main_menu.jsonc markers the same way New Game does, with a new QUIT_GAME cutscene item type ending the process directly once quit is confirmed via MODAL_OPTIONS_MARKERS (whose "message" field is now optional, defaulting to "" like "title"). Fixes two crashes surfaced along the way: - uiModalLocalize asserted on an empty locale key instead of treating it as "nothing to show" - hit as soon as a modal omitted a title or message. - assetLocaleGetString's new empty-message-ID guard broke the PO format's own header-read convention (messageId "") used internally by assetLocaleLoaderAsync, crashing on every boot. Extracted the shared lookup into assetLocaleGetStringLookup and added a dedicated assetLocaleGetHeader for that internal read, leaving assetLocaleGetString's validation untouched for every real caller. Also replaced every raw assetLocaleGetString/WithVA call across the UI with the locale/localemanager.h helpers (adding localeManagerGetString for the plain, non-formatting case), so app code no longer bypasses them. Co-Authored-By: Claude Sonnet 5 <[email protected]>
996 lines
26 KiB
C
996 lines
26 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "assetlocaleloader.h"
|
|
#include "util/memory.h"
|
|
#include "util/math.h"
|
|
#include "util/string.h"
|
|
#include "assert/assert.h"
|
|
|
|
#include "asset/loader/assetloading.h"
|
|
#include "asset/loader/assetentry.h"
|
|
|
|
errorret_t assetLocaleLoaderAsync(assetloading_t *loading) {
|
|
assertNotNull(loading, "Loading cannot be NULL");
|
|
assertNotMainThread("Async loader should not be on main thread.");
|
|
|
|
if(loading->loading.locale.state != ASSET_LOCALE_LOADER_STATE_LOAD_HEADER) {
|
|
errorOk();
|
|
}
|
|
|
|
assetlocalefile_t *localeFile = &loading->entry->data.locale;
|
|
memoryZero(localeFile, sizeof(assetlocalefile_t));
|
|
assetLoaderErrorChain(loading, assetFileInit(
|
|
&localeFile->file, loading->entry->name, NULL, NULL
|
|
));
|
|
assetLoaderErrorChain(loading, assetFileOpen(&localeFile->file));
|
|
|
|
char_t buffer[1024];
|
|
assetLoaderErrorChain(loading, assetLocaleGetHeader(
|
|
localeFile, buffer, sizeof(buffer)
|
|
));
|
|
assetLoaderErrorChain(loading, assetLocaleParseHeader(
|
|
localeFile, buffer, sizeof(buffer)
|
|
));
|
|
|
|
loading->loading.locale.state = ASSET_LOCALE_LOADER_STATE_DONE;
|
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
|
errorOk();
|
|
}
|
|
|
|
errorret_t assetLocaleLoaderSync(assetloading_t *loading) {
|
|
assertNotNull(loading, "Loading cannot be NULL");
|
|
assertTrue(loading->type == ASSET_LOADER_TYPE_LOCALE, "Invalid type.");
|
|
assertIsMainThread("Must be called from the main thread.");
|
|
|
|
switch(loading->loading.locale.state) {
|
|
case ASSET_LOCALE_LOADER_STATE_INITIAL:
|
|
loading->loading.locale.state = ASSET_LOCALE_LOADER_STATE_LOAD_HEADER;
|
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
|
errorOk();
|
|
break;
|
|
|
|
case ASSET_LOCALE_LOADER_STATE_DONE:
|
|
break;
|
|
|
|
default:
|
|
errorOk();
|
|
}
|
|
|
|
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
|
errorOk();
|
|
}
|
|
|
|
errorret_t assetLocaleDispose(assetentry_t *entry) {
|
|
assertNotNull(entry, "Asset entry cannot be NULL");
|
|
assertTrue(entry->type == ASSET_LOADER_TYPE_LOCALE, "Invalid type.");
|
|
assertIsMainThread("Must be called from the main thread.");
|
|
|
|
assetlocalefile_t *localeFile = &entry->data.locale;
|
|
|
|
if(localeFile->cache != NULL) {
|
|
memoryFree(localeFile->cache);
|
|
localeFile->cache = NULL;
|
|
}
|
|
|
|
errorChain(assetFileClose(&localeFile->file));
|
|
return assetFileDispose(&localeFile->file);
|
|
}
|
|
|
|
// These functions probably need some cleaning;
|
|
errorret_t assetLocaleParseHeader(
|
|
assetlocalefile_t *localeFile,
|
|
char_t *headerBuffer,
|
|
const size_t headerBufferSize
|
|
) {
|
|
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 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') {
|
|
errorret_t r = assetFileLineReaderNext(reader);
|
|
if(errorIsNotOk(r)) {
|
|
errorCatch(r);
|
|
break;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
// Skip comment lines
|
|
if(lineBuffer[0] == '#') {
|
|
errorret_t r = assetFileLineReaderNext(reader);
|
|
if(errorIsNotOk(r)) {
|
|
errorCatch(r);
|
|
break;
|
|
}
|
|
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) {
|
|
errorret_t r = assetFileLineReaderNext(reader);
|
|
if(errorIsNotOk(r)) {
|
|
errorCatch(r);
|
|
break;
|
|
}
|
|
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) {
|
|
errorret_t r = assetFileLineReaderNext(reader);
|
|
if(errorIsNotOk(r)) {
|
|
errorCatch(r);
|
|
break;
|
|
}
|
|
|
|
// Skip blank lines
|
|
r = assetLocaleLineSkipBlanks(reader, lineBuffer);
|
|
if(errorIsNotOk(r)) {
|
|
errorCatch(r);
|
|
break;
|
|
}
|
|
|
|
// 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 assetLocaleCacheFind(
|
|
assetlocalefile_t *file,
|
|
const char_t *messageId,
|
|
const int32_t pluralCount,
|
|
char_t *stringBuffer,
|
|
const size_t stringBufferSize,
|
|
bool_t *outHit
|
|
) {
|
|
assertNotNull(file, "Asset file cannot be NULL.");
|
|
assertNotNull(messageId, "Message ID cannot be NULL.");
|
|
assertNotNull(outHit, "outHit cannot be NULL.");
|
|
assertStrLenMin(messageId, 1, "Message ID cannot be empty.");
|
|
|
|
*outHit = false;
|
|
if(file->cache == NULL) errorOk();
|
|
|
|
assetlocalecache_t *cache = file->cache;
|
|
for(uint8_t i = 0; i < ASSET_LOCALE_CACHE_COUNT; i++) {
|
|
assetlocalecacheentry_t *entry = &cache->entries[i];
|
|
if(entry->messageId[0] == '\0') break;// unused tail - nothing further
|
|
if(entry->pluralCount != pluralCount) continue;
|
|
if(!stringEquals(entry->messageId, messageId)) continue;
|
|
|
|
// Move to the front of the LRU order (a no-op when already there).
|
|
if(i > 0) {
|
|
assetlocalecacheentry_t hit = *entry;
|
|
memoryMove(
|
|
&cache->entries[1], &cache->entries[0],
|
|
i * sizeof(assetlocalecacheentry_t)
|
|
);
|
|
cache->entries[0] = hit;
|
|
}
|
|
|
|
size_t len = strlen(cache->entries[0].value);
|
|
if(len >= stringBufferSize) errorThrow("String buffer overflow");
|
|
memoryCopy(stringBuffer, cache->entries[0].value, len + 1);
|
|
*outHit = true;
|
|
errorOk();
|
|
}
|
|
|
|
errorOk();
|
|
}
|
|
|
|
void assetLocaleCacheInsert(
|
|
assetlocalefile_t *file,
|
|
const char_t *messageId,
|
|
const int32_t pluralCount,
|
|
const char_t *value
|
|
) {
|
|
assertNotNull(file, "Asset file cannot be NULL.");
|
|
assertNotNull(messageId, "Message ID cannot be NULL.");
|
|
assertNotNull(value, "Value cannot be NULL.");
|
|
assertStrLenMin(messageId, 1, "Message ID cannot be empty.");
|
|
|
|
// Caching is a pure optimization - a message ID or value too long for a
|
|
// fixed cache slot is just never cached, not an error.
|
|
if(strlen(messageId) >= ASSET_LOCALE_CACHE_KEY_MAX) return;
|
|
if(strlen(value) >= ASSET_LOCALE_CACHE_VALUE_MAX) return;
|
|
|
|
if(file->cache == NULL) {
|
|
file->cache =
|
|
(assetlocalecache_t *)memoryAllocate(sizeof(assetlocalecache_t));
|
|
memoryZero(file->cache, sizeof(assetlocalecache_t));
|
|
}
|
|
|
|
// Shift every entry down one slot, dropping the least-recently-used one
|
|
// off the end, to make room for the new entry at the front. This assumes
|
|
// messageId is never already present elsewhere in the cache, which holds
|
|
// as long as callers only insert after a confirmed assetLocaleCacheFind
|
|
// miss (a hit would have returned before reaching this point).
|
|
memoryMove(
|
|
&file->cache->entries[1], &file->cache->entries[0],
|
|
(ASSET_LOCALE_CACHE_COUNT - 1) * sizeof(assetlocalecacheentry_t)
|
|
);
|
|
|
|
assetlocalecacheentry_t *entry = &file->cache->entries[0];
|
|
stringCopy(entry->messageId, messageId, ASSET_LOCALE_CACHE_KEY_MAX);
|
|
entry->pluralCount = pluralCount;
|
|
stringCopy(entry->value, value, ASSET_LOCALE_CACHE_VALUE_MAX);
|
|
}
|
|
|
|
// Shared by assetLocaleGetString (external callers, messageId required
|
|
// non-empty) and assetLocaleGetHeader (messageId "", the PO format's own
|
|
// sentinel for its header entry - see assetLocaleGetHeader). Caching is
|
|
// skipped entirely for an empty messageId: assetLocaleCacheFind/Insert
|
|
// both reject one outright (same guard as assetLocaleGetString's own),
|
|
// and it's a one-shot boot-time read anyway, not worth caching.
|
|
errorret_t assetLocaleGetStringLookup(
|
|
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");
|
|
|
|
bool_t cacheHit = false;
|
|
if(messageId[0] != '\0') {
|
|
errorChain(assetLocaleCacheFind(
|
|
file, messageId, pluralCount, stringBuffer, stringBufferSize, &cacheHit
|
|
));
|
|
if(cacheHit) errorOk();
|
|
}
|
|
|
|
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)
|
|
);
|
|
|
|
// Prime the reader with the first line before scanning; outBuffer holds
|
|
// uninitialized memory until the first Next() call fills it.
|
|
errorChain(assetFileLineReaderNext(&reader));
|
|
|
|
// Skip blanks, comments, etc and start looking for msgid's
|
|
errorChain(assetLocaleLineSkipBlanks(&reader, lineBuffer));
|
|
|
|
while(true) {
|
|
// Is this msgid?
|
|
if(memoryCompare(lineBuffer, "msgid", 5) != 0) {
|
|
if(reader.eof) break;
|
|
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(true) {
|
|
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);
|
|
}
|
|
|
|
if(messageId[0] != '\0') {
|
|
assetLocaleCacheInsert(file, messageId, pluralCount, stringBuffer);
|
|
}
|
|
|
|
errorOk();
|
|
}
|
|
|
|
errorret_t assetLocaleGetString(
|
|
assetlocalefile_t *file,
|
|
const char_t *messageId,
|
|
const int32_t pluralCount,
|
|
char_t *stringBuffer,
|
|
const size_t stringBufferSize
|
|
) {
|
|
assertNotNull(messageId, "Message ID cannot be NULL.");
|
|
assertStrLenMin(messageId, 1, "Message ID cannot be empty.");
|
|
|
|
return assetLocaleGetStringLookup(
|
|
file, messageId, pluralCount, stringBuffer, stringBufferSize
|
|
);
|
|
}
|
|
|
|
errorret_t assetLocaleGetHeader(
|
|
assetlocalefile_t *file,
|
|
char_t *stringBuffer,
|
|
const size_t stringBufferSize
|
|
) {
|
|
return assetLocaleGetStringLookup(
|
|
file, "", 0, stringBuffer, stringBufferSize
|
|
);
|
|
}
|
|
|
|
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(errorIsNotOk(ret)) {
|
|
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(errorIsNotOk(ret)) {
|
|
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();
|
|
} |