Add some more tests, prepping for asset testing
This commit is contained in:
@@ -184,6 +184,7 @@ errorret_t assetUpdate(void) {
|
||||
loading->entry->state == ASSET_ENTRY_STATE_ERROR,
|
||||
"Loader did not set entry state to error on failed load."
|
||||
);
|
||||
errorCatch(errorPrint(ret));
|
||||
}
|
||||
|
||||
threadMutexUnlock(&loading->mutex);
|
||||
|
||||
@@ -303,16 +303,24 @@ errorret_t assetLocaleLineSkipBlanks(
|
||||
while(!reader->eof) {
|
||||
// Skip blank lines
|
||||
if(lineBuffer[0] == '\0') {
|
||||
errorChain(assetFileLineReaderNext(reader));
|
||||
errorret_t r = assetFileLineReaderNext(reader);
|
||||
if(errorIsNotOk(r)) {
|
||||
errorCatch(r);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip comment lines
|
||||
if(lineBuffer[0] == '#') {
|
||||
errorChain(assetFileLineReaderNext(reader));
|
||||
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;
|
||||
@@ -325,7 +333,11 @@ errorret_t assetLocaleLineSkipBlanks(
|
||||
}
|
||||
|
||||
if(onlySpaces) {
|
||||
errorChain(assetFileLineReaderNext(reader));
|
||||
errorret_t r = assetFileLineReaderNext(reader);
|
||||
if(errorIsNotOk(r)) {
|
||||
errorCatch(r);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
@@ -361,10 +373,18 @@ errorret_t assetLocaleLineUnbuffer(
|
||||
|
||||
// Now start buffering lines out
|
||||
while(!reader->eof) {
|
||||
errorChain(assetFileLineReaderNext(reader));
|
||||
errorret_t r = assetFileLineReaderNext(reader);
|
||||
if(errorIsNotOk(r)) {
|
||||
errorCatch(r);
|
||||
break;
|
||||
}
|
||||
|
||||
// Skip blank lines
|
||||
errorChain(assetLocaleLineSkipBlanks(reader, lineBuffer));
|
||||
r = assetLocaleLineSkipBlanks(reader, lineBuffer);
|
||||
if(errorIsNotOk(r)) {
|
||||
errorCatch(r);
|
||||
break;
|
||||
}
|
||||
|
||||
// Skip starting spaces
|
||||
char_t *ptr = (char_t *)lineBuffer;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
@@ -11,11 +11,22 @@
|
||||
typedef struct assetloading_s assetloading_t;
|
||||
typedef struct assetentry_s assetentry_t;
|
||||
|
||||
/** Input passed to the locale loader — currently unused. */
|
||||
typedef struct { void *nothing; } assetlocaleloaderinput_t;
|
||||
|
||||
/** Per-slot scratch data used while the locale file is loading. */
|
||||
typedef struct { void *nothing; } assetlocaleloaderloading_t;
|
||||
|
||||
/** Maximum number of distinct plural forms a locale file may declare. */
|
||||
#define ASSET_LOCALE_FILE_PLURAL_FORM_COUNT 6
|
||||
|
||||
/**
|
||||
* Comparison operator used in a plural-form expression.
|
||||
*
|
||||
* Each condition in the plural-form header is evaluated as
|
||||
* `n <op> value`
|
||||
* where `n` is the runtime plural count.
|
||||
*/
|
||||
typedef enum {
|
||||
ASSET_LOCALE_PLURAL_OP_EQUAL,
|
||||
ASSET_LOCALE_PLURAL_OP_NOT_EQUAL,
|
||||
@@ -25,13 +36,24 @@ typedef enum {
|
||||
ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL
|
||||
} assetlocalepluraloperation_t;
|
||||
|
||||
/**
|
||||
* Discriminator tag for a locale string argument.
|
||||
* @see assetlocalearg_t
|
||||
*/
|
||||
typedef enum {
|
||||
ASSET_LOCALE_ARG_STRING,
|
||||
ASSET_LOCALE_ARG_INT,
|
||||
ASSET_LOCALE_ARG_FLOAT
|
||||
} assetlocaleargtype_t;
|
||||
|
||||
/**
|
||||
* A single typed argument for locale string substitution.
|
||||
*
|
||||
* Used with @ref assetLocaleGetStringWithArgs to fill `%s`, `%d`, or `%f`
|
||||
* placeholders inside a translated string.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Which union member is active. */
|
||||
assetlocaleargtype_t type;
|
||||
union {
|
||||
const char_t *stringValue;
|
||||
@@ -40,20 +62,72 @@ typedef struct {
|
||||
};
|
||||
} assetlocalearg_t;
|
||||
|
||||
/**
|
||||
* Runtime state for an open locale file.
|
||||
*
|
||||
* Loaded once by @ref assetLocaleLoaderSync and kept alive for the lifetime
|
||||
* of the asset entry. The plural-form fields are populated from the PO header
|
||||
* by @ref assetLocaleParseHeader.
|
||||
*
|
||||
* Plural evaluation works as a linear scan over `pluralStateCount - 1`
|
||||
* conditions; if none match, `pluralDefaultIndex` is returned.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Underlying file handle used to rewind and re-read the PO data. */
|
||||
assetfile_t file;
|
||||
|
||||
/** Comparison operator for each conditional plural clause. */
|
||||
assetlocalepluraloperation_t pluralOps[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||
|
||||
/** Right-hand value for each conditional plural clause. */
|
||||
int32_t pluralValues[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||
|
||||
/** Form index returned when the corresponding condition is true. */
|
||||
int32_t pluralIndices[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||
|
||||
/** Total number of plural forms declared by `nplurals=`. */
|
||||
uint8_t pluralStateCount;
|
||||
|
||||
/** Form index used when no conditional clause matches. */
|
||||
uint8_t pluralDefaultIndex;
|
||||
} assetlocalefile_t;
|
||||
|
||||
/** Convenience alias — the loaded output type of a locale asset entry. */
|
||||
typedef assetlocalefile_t assetlocaleoutput_t;
|
||||
|
||||
/**
|
||||
* Synchronous loader callback. Opens and validates the locale file, reads the
|
||||
* PO header, and parses plural-form rules. Sets entry state to
|
||||
* `ASSET_ENTRY_STATE_LOADED` on success or `ASSET_ENTRY_STATE_ERROR` on
|
||||
* failure.
|
||||
*
|
||||
* @param loading The loading slot for this asset entry.
|
||||
* @return OK on success, error otherwise.
|
||||
*/
|
||||
errorret_t assetLocaleLoaderSync(assetloading_t *loading);
|
||||
|
||||
/**
|
||||
* Dispose callback. Closes the open file handle and zeros the locale data.
|
||||
*
|
||||
* @param entry The asset entry to dispose.
|
||||
* @return OK on success, error otherwise.
|
||||
*/
|
||||
errorret_t assetLocaleDispose(assetentry_t *entry);
|
||||
|
||||
/**
|
||||
* Parses the `Plural-Forms:` line from a PO header string and populates the
|
||||
* plural-form fields of `localeFile`. If no `Plural-Forms:` key is present
|
||||
* the function returns OK without modifying the struct.
|
||||
*
|
||||
* Supports the `nplurals=N; plural=(<expr>);` syntax where `<expr>` is a
|
||||
* chain of `n <op> value ? index : ...` ternary conditions ending with a
|
||||
* fallback index.
|
||||
*
|
||||
* @param localeFile Struct whose plural fields will be filled in.
|
||||
* @param headerBuffer The raw PO header string (msgstr of msgid "").
|
||||
* @param headerBufferSize Size of `headerBuffer` in bytes.
|
||||
* @return OK on success, error if the plural expression is malformed.
|
||||
*/
|
||||
errorret_t assetLocaleParseHeader(
|
||||
assetlocalefile_t *localeFile,
|
||||
char_t *headerBuffer,
|
||||
@@ -61,11 +135,29 @@ errorret_t assetLocaleParseHeader(
|
||||
);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Evaluates which plural form index to use for a given count.
|
||||
*
|
||||
* Walks the conditions stored in `file` in order; returns the index for the
|
||||
* first condition that matches `pluralCount`. Falls back to
|
||||
* `file->pluralDefaultIndex` if none match.
|
||||
*
|
||||
* @param file Locale file with parsed plural rules.
|
||||
* @param pluralCount The runtime count (e.g. number of items).
|
||||
* @return Zero-based plural form index.
|
||||
*/
|
||||
uint8_t assetLocaleEvaluatePlural(
|
||||
assetlocalefile_t *file,
|
||||
const int32_t pluralCount
|
||||
);
|
||||
|
||||
/**
|
||||
* Advances the line reader past blank lines, comment lines (starting with
|
||||
* `#`), and lines containing only spaces. Stops at the first content line or
|
||||
* end of file.
|
||||
*
|
||||
* @param reader Line reader positioned at the current line.
|
||||
* @param lineBuffer Output buffer that the reader writes each line into.
|
||||
* @return OK on success, error if reading fails.
|
||||
*/
|
||||
errorret_t assetLocaleLineSkipBlanks(
|
||||
assetfilelinereader_t *reader,
|
||||
@@ -73,16 +165,19 @@ errorret_t assetLocaleLineSkipBlanks(
|
||||
);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Reads a PO quoted string value from the current line and any immediately
|
||||
* following continuation lines that begin with `"`.
|
||||
*
|
||||
* Handles standard C escape sequences (`\n`, `\t`, `\\`, `\"`).
|
||||
*
|
||||
* @param reader Line reader positioned at the line containing the opening
|
||||
* quote (e.g. `msgstr "..."`).
|
||||
* @param lineBuffer Buffer the reader fills on each @ref assetFileLineReaderNext
|
||||
* call; also used to detect continuation lines.
|
||||
* @param stringBuffer Destination for the unescaped string content.
|
||||
* @param stringBufferSize Capacity of `stringBuffer` in bytes.
|
||||
* @return OK on success, error if a quote or escape sequence is malformed or
|
||||
* the buffer would overflow.
|
||||
*/
|
||||
errorret_t assetLocaleLineUnbuffer(
|
||||
assetfilelinereader_t *reader,
|
||||
@@ -92,14 +187,19 @@ errorret_t assetLocaleLineUnbuffer(
|
||||
);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Looks up a translated string by message ID from the open locale file.
|
||||
*
|
||||
* Rewinds the file and scans from the beginning on every call. For plural
|
||||
* entries (`msgid_plural`) the `pluralCount` is evaluated against the loaded
|
||||
* plural rules to select the correct `msgstr[N]` form.
|
||||
*
|
||||
* @param file Locale file to search. Must be open.
|
||||
* @param messageId PO message ID to find (`""` retrieves the header entry).
|
||||
* @param pluralCount Count used to select the plural form (ignored for
|
||||
* singular entries).
|
||||
* @param stringBuffer Buffer to receive the translated string.
|
||||
* @param stringBufferSize Capacity of `stringBuffer` in bytes.
|
||||
* @return OK on success, error if the message ID is not found or I/O fails.
|
||||
*/
|
||||
errorret_t assetLocaleGetString(
|
||||
assetlocalefile_t *file,
|
||||
@@ -110,15 +210,21 @@ errorret_t assetLocaleGetString(
|
||||
);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Looks up a translated string and formats it with a `printf`-style variadic
|
||||
* argument list.
|
||||
*
|
||||
* Retrieves the raw format string via @ref assetLocaleGetString then applies
|
||||
* `vsnprintf` with the provided arguments.
|
||||
*
|
||||
* @param file Locale file to search. Must be open.
|
||||
* @param messageId PO message ID to find.
|
||||
* @param pluralCount Count used to select the plural form.
|
||||
* @param buffer Buffer to receive the formatted string.
|
||||
* @param bufferSize Capacity of `buffer` in bytes.
|
||||
* @param ... Format arguments matching the specifiers in the translated
|
||||
* string.
|
||||
* @return OK on success, error if the message is not found or formatting
|
||||
* fails.
|
||||
*/
|
||||
errorret_t assetLocaleGetStringWithVA(
|
||||
assetlocalefile_t *file,
|
||||
@@ -130,16 +236,22 @@ errorret_t assetLocaleGetStringWithVA(
|
||||
);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Looks up a translated string and substitutes typed arguments into its
|
||||
* `%s`, `%d`, and `%f` placeholders.
|
||||
*
|
||||
* Unlike @ref assetLocaleGetStringWithVA this variant accepts an explicit
|
||||
* array of @ref assetlocalearg_t values, which is safer to use from
|
||||
* generated or scripted call sites.
|
||||
*
|
||||
* @param file Locale file to search. Must be open.
|
||||
* @param messageId PO message ID to find.
|
||||
* @param pluralCount Count used to select the plural form.
|
||||
* @param buffer Buffer to receive the formatted string.
|
||||
* @param bufferSize Capacity of `buffer` in bytes.
|
||||
* @param args Array of typed arguments; may be NULL when `argCount` is 0.
|
||||
* @param argCount Number of elements in `args`.
|
||||
* @return OK on success, error if the message is not found, an argument type
|
||||
* mismatches the specifier, or the buffer would overflow.
|
||||
*/
|
||||
errorret_t assetLocaleGetStringWithArgs(
|
||||
assetlocalefile_t *file,
|
||||
@@ -149,4 +261,4 @@ errorret_t assetLocaleGetStringWithArgs(
|
||||
const size_t bufferSize,
|
||||
const assetlocalearg_t *args,
|
||||
const size_t argCount
|
||||
);
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user