#include #include #include #include #include #include #include #include "logerr.h" #include "meta.h" #include "msgs.h" static const char CATALOG_NAME[] = NAME; static nl_catd catalog_descriptor = NULL; static size_t msgs_length = 0U; static const char NLSPATH[] = LOCALEDIR "/%l_%t/LC_MESSAGES/%N.cat" ":" LOCALEDIR "/%l/LC_MESSAGES/%N.cat"; static const char NLSPATH_KEY[] = "NLSPATH"; int msgs_init(const char *const MSGS[]) { int rc = -1; static const int should_overwrite = 0; if (setenv(NLSPATH_KEY, NLSPATH, should_overwrite)) { logerr("setenv(): %s", strerror(errno)); goto out; } catalog_descriptor = catopen(CATALOG_NAME, 0); if (catalog_descriptor != NULL && catalog_descriptor == (nl_catd)-1) { logerr("catopen(): %s", strerror(errno)); catalog_descriptor = NULL; goto out; } msgs_length = 0U; while (MSGS[msgs_length] != NULL) { msgs_length++; } rc = 0; out: return rc; } int msgs_end(void) { int rc = -1; if (catalog_descriptor != NULL) { if (catclose(catalog_descriptor)) { logerr("catclose(): %s", strerror(errno)); goto out; } } rc = 0; out: if (catalog_descriptor != NULL) { catalog_descriptor = NULL; } return rc; } const char * msgs_string(const char* const MSGS[], const int msg_id) { assert(msg_id >= 0); if (msgs_length != 0U) { assert((size_t)msg_id < msgs_length); } if (catalog_descriptor == NULL) { return MSGS[msg_id]; } errno = 0; const char *const ret = catgets(catalog_descriptor, NL_SETD, msg_id, MSGS[msg_id]); if (errno) { logerr("catgets(): %s", strerror(errno)); } return ret; } int msgs_print_ids( const char *const MSGS[], FILE *restrict stream, const int msg_begin, const int msg_end ) { int rc = -1; for (int i = msg_begin; i <= msg_end; i++) { if (fprintf(stream, "%s", msgs_string(MSGS, i)) < 0) { logerr("fprintf(): %s", strerror(errno)); goto out; } } rc = 0; out: return rc; } int msgs_print_id(const char *const MSGS[], FILE *const stream, const int msg_id) { return msgs_print_ids(MSGS, stream, msg_id, msg_id); } int msgs_dump_translatable_strings(const char *const MSGS[]) { int rc = -1; for (size_t i = 0U; MSGS[i] != NULL; i++) { if (printf("%ld ", i) < 0) { logerr("printf(): %s", strerror(errno)); goto out; } for (size_t j = 0U; MSGS[i][j]; j++) { if (MSGS[i][j] == '\n') { if (printf("\\n") < 0) { logerr("printf(): %s", strerror(errno)); goto out; } } else { if (printf("%c", MSGS[i][j]) < 0) { logerr("printf(): %s", strerror(errno)); goto out; } } } if (printf("\n\n") < 0) { logerr("printf(): %s", strerror(errno)); goto out; } } rc = 0; out: return rc; }