summaryrefslogtreecommitdiff
path: root/src/catalog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/catalog.c')
-rw-r--r--src/catalog.c338
1 files changed, 160 insertions, 178 deletions
diff --git a/src/catalog.c b/src/catalog.c
index 31aef9a..d543864 100644
--- a/src/catalog.c
+++ b/src/catalog.c
@@ -10,37 +10,6 @@
#include "logerr.h"
#include "catalog.h"
-#ifdef TEST
-#include "testing.h"
-#include "../tests/slurp.h"
-#endif
-
-
-#ifdef TEST
-static const char *const
-FNAME = __FILE__ ".txt";
-
-enum TEST_MSGCATALOG_ID {
- MSG_X_FIRST = 1,
- MSG_X_1,
- MSG_X_2,
- MSG_X_LAST,
- MSG_STANDALONE,
-};
-
-static const char *const
-TEST_MSGS[] = {
- "",
- [MSG_X_FIRST]="First line\n",
- [MSG_X_1]="a second\n",
- [MSG_X_2]="a third\n",
- [MSG_X_LAST]="and the last one\n",
- [MSG_STANDALONE]="single line message\n",
- NULL
-};
-#endif
-
-
static const char *const
CATALOG_NAME = NAME;
@@ -80,43 +49,6 @@ out:
return rc;
}
-#ifdef TEST
-static int
-test_i18n_init(void) {
- int rc = 0;
-
- test_start("i18n_init()");
-
- {
- testing("simple call without touching the environment");
-
- const int should_overwrite = 1;
- if (setenv(NLSPATH_KEY, "src/%N.en.cat", should_overwrite)) {
- logerr("setenv(\"%s\", \"src/%%N.en.cat\", 1): %s\n",
- NLSPATH_KEY, strerror(errno));
- rc = -1;
- goto out;
- }
-
- if (i18n_init()) {
- logerr("i18n_init()\n");
- rc = -1;
- goto out;
- }
-
- test_ok();
- }
-
-out:
- if (i18n_destroy()) {
- logerr("i18n_destroy()\n");
- rc = -1;
- }
- return rc;
-}
-#endif
-
-
int
i18n_destroy(void) {
int rc = 0;
@@ -136,31 +68,6 @@ out:
return rc;
}
-#ifdef TEST
-static int
-test_i18n_destroy(void) {
- int rc = 0;
-
- test_start("i18n_destroy()");
-
- {
- testing("simple call without init first");
-
- if (i18n_destroy()) {
- logerr("i18n_destroy()\n");
- rc = -1;
- goto out;
- }
-
- test_ok();
- }
-
-out:
- return rc;
-}
-#endif
-
-
/**
* Infallible: always returns a valid string, no matter what.
*/
@@ -183,70 +90,137 @@ s(const char* const MSGS[], const int msg_id) {
return ret;
}
-#ifdef TEST
-static int
-test_s(void) {
+int
+s_print_msgs(
+ const char *const MSGS[],
+ FILE *restrict stream,
+ const int msg_begin,
+ const int msg_end
+) {
int rc = 0;
- test_start("_()");
- FILE *file = NULL;
- char *str = NULL;
-
- {
- testing("empty string");
-
- file = fopen(FNAME, "w");
- if (!file) {
- perror("fopen(FNAME, \"w\")");
+ for (int i = msg_begin; i <= msg_end; i++) {
+ if (fprintf(stream, "%s", s(MSGS, i)) < 0) {
+ logerr("fprintf(stream, \"%%s\", _(%d)): %s\n", i,
+ strerror(errno));
rc = -1;
goto out;
}
+ }
- // FIXME: implement correct test
+out:
+ return rc;
+}
+
+int
+s_print_msg(const char *const MSGS[], FILE *const fd, const int msg_id) {
+ return s_print_msgs(MSGS, fd, msg_id, msg_id);
+}
+int
+dump_translatable_strings(const char *const MSGS[]) {
+ int rc = 0;
+ for (size_t i = 1; MSGS[i]; i++) {
+ if (printf("%ld ", i) < 0) {
+ logerr("printf(\"%%ld\", %d): %s\n", i);
+ rc = -1;
+ goto out;
+ }
- test_ok();
- }
+ for (size_t j = 0; MSGS[i][j]; j++) {
+ if (MSGS[i][j] == '\n') {
+ if (printf("\\n") < 0) {
+ logerr("printf(\"\\\\n\"): %s\n",
+ strerror(errno));
+ rc = -1;
+ goto out;
+ }
+ } else {
+ if (printf("%c", MSGS[i][j]) < 0) {
+ logerr("printf(\"%%c\", "
+ "MSGS[%ld][%ld]): %s\n",
+ i, j, strerror(errno));
+ rc = -1;
+ goto out;
+ }
+ }
+ }
-out:
- if (str) {
- free(str);
- }
- if (file) {
- if (fclose(file)) {
- logerr("fclose(file): %s\n", strerror(errno));
+ if (printf("\n\n") < 0) {
+ logerr("printf(\"\\n\\n\"): %s\n", strerror(errno));
rc = -1;
+ goto out;
}
}
+
+out:
return rc;
}
-#endif
-int
-s_print_msgs(
- const char *const MSGS[],
- FILE *restrict stream,
- const int msg_begin,
- const int msg_end
-) {
+
+#ifdef TEST
+#include "testing.h"
+#include "../tests/slurp.h"
+
+
+static const char *const
+FNAME = __FILE__ ".txt";
+
+enum TEST_MSGCATALOG_ID {
+ MSG_X_FIRST = 1,
+ MSG_X_1,
+ MSG_X_2,
+ MSG_X_LAST,
+ MSG_STANDALONE,
+};
+
+static const char *const
+TEST_MSGS[] = {
+ "",
+ [MSG_X_FIRST]="First line\n",
+ [MSG_X_1]="a second\n",
+ [MSG_X_2]="a third\n",
+ [MSG_X_LAST]="and the last one\n",
+ [MSG_STANDALONE]="single line message\n",
+ NULL
+};
+
+static int
+test_i18n_init(void) {
int rc = 0;
- for (int i = msg_begin; i <= msg_end; i++) {
- if (fprintf(stream, "%s", s(MSGS, i)) < 0) {
- logerr("fprintf(stream, \"%%s\", _(%d)): %s\n", i,
- strerror(errno));
+ test_start("i18n_init()");
+
+ {
+ testing("simple call without touching the environment");
+
+ const int should_overwrite = 1;
+ if (setenv(NLSPATH_KEY, "src/%N.en.cat", should_overwrite)) {
+ logerr("setenv(\"%s\", \"src/%%N.en.cat\", 1): %s\n",
+ NLSPATH_KEY, strerror(errno));
rc = -1;
goto out;
}
+
+ if (i18n_init()) {
+ logerr("i18n_init()\n");
+ rc = -1;
+ goto out;
+ }
+
+ test_ok();
}
out:
+ if (i18n_destroy()) {
+ logerr("i18n_destroy()\n");
+ rc = -1;
+ }
return rc;
}
-#ifdef TEST
static int
test_s_print_msgs(void) {
int rc = 0;
@@ -352,15 +326,67 @@ out:
}
return rc;
}
-#endif
+static int
+test_s(void) {
+ int rc = 0;
-int
-s_print_msg(const char *const MSGS[], FILE *const fd, const int msg_id) {
- return s_print_msgs(MSGS, fd, msg_id, msg_id);
+ test_start("_()");
+ FILE *file = NULL;
+ char *str = NULL;
+
+ {
+ testing("empty string");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ perror("fopen(FNAME, \"w\")");
+ rc = -1;
+ goto out;
+ }
+
+ // FIXME: implement correct test
+
+
+
+ test_ok();
+ }
+
+out:
+ if (str) {
+ free(str);
+ }
+ if (file) {
+ if (fclose(file)) {
+ logerr("fclose(file): %s\n", strerror(errno));
+ rc = -1;
+ }
+ }
+ return rc;
+}
+
+static int
+test_i18n_destroy(void) {
+ int rc = 0;
+
+ test_start("i18n_destroy()");
+
+ {
+ testing("simple call without init first");
+
+ if (i18n_destroy()) {
+ logerr("i18n_destroy()\n");
+ rc = -1;
+ goto out;
+ }
+
+ test_ok();
+ }
+
+out:
+ return rc;
}
-#ifdef TEST
static int
test_s_print_msg(void) {
int rc = 0;
@@ -422,51 +448,7 @@ out:
}
return rc;
}
-#endif
-
-
-int
-dump_translatable_strings(const char *const MSGS[]) {
- int rc = 0;
-
- for (size_t i = 1; MSGS[i]; i++) {
- if (printf("%ld ", i) < 0) {
- logerr("printf(\"%%ld\", %d): %s\n", i);
- rc = -1;
- goto out;
- }
-
- for (size_t j = 0; MSGS[i][j]; j++) {
- if (MSGS[i][j] == '\n') {
- if (printf("\\n") < 0) {
- logerr("printf(\"\\\\n\"): %s\n",
- strerror(errno));
- rc = -1;
- goto out;
- }
- } else {
- if (printf("%c", MSGS[i][j]) < 0) {
- logerr("printf(\"%%c\", "
- "MSGS[%ld][%ld]): %s\n",
- i, j, strerror(errno));
- rc = -1;
- goto out;
- }
- }
- }
-
- if (printf("\n\n") < 0) {
- logerr("printf(\"\\n\\n\"): %s\n", strerror(errno));
- rc = -1;
- goto out;
- }
- }
-
-out:
- return rc;
-}
-#ifdef TEST
int
main(void) {
int rc = 0;