diff options
-rw-r--r-- | src/catalog.c | 21 | ||||
-rw-r--r-- | src/catalog.h | 2 | ||||
-rw-r--r-- | src/i18n.c | 5 | ||||
-rw-r--r-- | src/i18n.h | 3 | ||||
-rw-r--r-- | tests/catalog.c | 20 |
5 files changed, 34 insertions, 17 deletions
diff --git a/src/catalog.c b/src/catalog.c index 3e40bf2..9550631 100644 --- a/src/catalog.c +++ b/src/catalog.c @@ -11,12 +11,16 @@ #include "catalog.h" + static const char *const CATALOG_NAME = NAME; static nl_catd catalog_descriptor = NULL; +static size_t +msgs_length = 0U; + static const char *const NLSPATH = LOCALEDIR "/%l_%t/LC_MESSAGES/%N.cat" ":" LOCALEDIR "/%l/LC_MESSAGES/%N.cat"; @@ -26,7 +30,7 @@ NLSPATH_KEY = "NLSPATH"; int -i18n_init(void) { +i18n_init_msgs(const char *const MSGS[]) { int rc = -1; static const int should_overwrite = 0; @@ -42,6 +46,11 @@ i18n_init(void) { goto out; } + msgs_length = 0U; + while (MSGS[msgs_length] != NULL) { + msgs_length++; + } + rc = 0; out: return rc; @@ -66,14 +75,12 @@ out: return rc; } -/** - * Infallible: always returns a valid string, no matter what. - */ const char * s(const char* const MSGS[], const int msg_id) { - assert(msg_id > 0); - // FIXME: assert within bounds! - // printf("sizeof(MSGS): %ld\n", sizeof(MSGS)); + assert(msg_id >= 0); + if (msgs_length != 0U) { + assert((size_t)msg_id < msgs_length); + } if (catalog_descriptor == NULL) { return MSGS[msg_id]; } diff --git a/src/catalog.h b/src/catalog.h index 25adbeb..3acb24d 100644 --- a/src/catalog.h +++ b/src/catalog.h @@ -1,5 +1,5 @@ int -i18n_init(void); +i18n_init_msgs(const char *const MSGS[]); int i18n_destroy(void); @@ -54,3 +54,8 @@ const char * _(const int msg_id) { return s(MSGS, msg_id); } + +int +i18n_init(void) { + return i18n_init_msgs(MSGS); +} @@ -45,3 +45,6 @@ MSGS[]; const char * _(const int msg_id); + +int +i18n_init(void); diff --git a/tests/catalog.c b/tests/catalog.c index 197d06b..fbccfe5 100644 --- a/tests/catalog.c +++ b/tests/catalog.c @@ -8,16 +8,17 @@ static const char *const FNAME = __FILE__ ".txt"; enum TEST_MSGCATALOG_ID { - MSG_X_FIRST = 1, + MSG_X_FIRST, MSG_X_1, MSG_X_2, MSG_X_LAST, MSG_STANDALONE, }; +#define TEST_MSGS_LEN 6U + static const char *const -TEST_MSGS[] = { - "", +TEST_MSGS[TEST_MSGS_LEN] = { [MSG_X_FIRST]="First line\n", [MSG_X_1]="a second\n", [MSG_X_2]="a third\n", @@ -27,10 +28,10 @@ TEST_MSGS[] = { }; static int -test_i18n_init(void) { +test_i18n_init_msgs(void) { int rc = -1; - test_start("i18n_init()"); + test_start("i18n_init_msgs()"); { testing("simple call without touching the environment"); @@ -41,10 +42,11 @@ test_i18n_init(void) { goto out; } - if (i18n_init()) { - logerr("i18n_init()"); + if (i18n_init_msgs(TEST_MSGS)) { + logerr("i18n_init_msgs()"); goto out; } + assert(msgs_length == TEST_MSGS_LEN - 1U); test_ok(); } @@ -280,8 +282,8 @@ int main(void) { int rc = -1; - if (test_i18n_init()) { - logerr("test_i18n_init()"); + if (test_i18n_init_msgs()) { + logerr("test_i18n_init_msgs()"); goto out; } |