summaryrefslogtreecommitdiff
path: root/src/lang.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-03-22 14:57:22 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-03-22 14:57:22 +0200
commit157f0be146bf8122a70dcf5940f1033a6462c34c (patch)
treeccd92876acb4f3ae42937b28a2c89e13666eede6 /src/lang.c
parent06156b3034353cc1d62b4f825b1fcad799e944eb (diff)
Lang: Loading a set of NULL-terminated strings
Diffstat (limited to 'src/lang.c')
-rw-r--r--src/lang.c40
1 files changed, 31 insertions, 9 deletions
diff --git a/src/lang.c b/src/lang.c
index b5f80fd1..3e99c09c 100644
--- a/src/lang.c
+++ b/src/lang.c
@@ -1,4 +1,5 @@
1#include "lang.h" 1#include "lang.h"
2#include "embedded.h"
2 3
3#include <the_Foundation/sortedarray.h> 4#include <the_Foundation/sortedarray.h>
4#include <the_Foundation/string.h> 5#include <the_Foundation/string.h>
@@ -7,8 +8,8 @@ iDeclareType(Lang)
7iDeclareType(MsgStr) 8iDeclareType(MsgStr)
8 9
9struct Impl_MsgStr { 10struct Impl_MsgStr {
10 const char *id; 11 const char *id; /* these point to null-terminated strings in embedded data */
11 iString str; 12 const char *str;
12}; 13};
13 14
14int cmp_MsgStr_(const void *e1, const void *e2) { 15int cmp_MsgStr_(const void *e1, const void *e2) {
@@ -24,6 +25,24 @@ struct Impl_Lang {
24 25
25static iLang lang_; 26static iLang lang_;
26 27
28static void clear_Lang_(iLang *d) {
29 clear_SortedArray(d->messages);
30}
31
32static void load_Lang_(iLang *d, const char *id) {
33 /* Load compiled language strings from an embedded blob. */
34 const iBlock *data = NULL; // &blobLangEn_Embedded;
35 iMsgStr msg;
36 for (const char *ptr = constBegin_Block(data); ptr != constEnd_Block(data); ptr++) {
37 msg.id = ptr;
38 while (*++ptr) {}
39 msg.str = ++ptr;
40 while (*++ptr) {}
41 /* Allocate the string. */
42 insert_SortedArray(d->messages, &msg);
43 }
44}
45
27void init_Lang(void) { 46void init_Lang(void) {
28 iLang *d = &lang_; 47 iLang *d = &lang_;
29 d->messages = new_SortedArray(sizeof(iMsgStr), cmp_MsgStr_); 48 d->messages = new_SortedArray(sizeof(iMsgStr), cmp_MsgStr_);
@@ -32,25 +51,28 @@ void init_Lang(void) {
32 51
33void deinit_Lang(void) { 52void deinit_Lang(void) {
34 iLang *d = &lang_; 53 iLang *d = &lang_;
54 clear_Lang_(d);
35 delete_SortedArray(d->messages); 55 delete_SortedArray(d->messages);
36} 56}
37 57
38void setCurrent_Lang(const char *language) { 58void setCurrent_Lang(const char *language) {
39 /* TODO: Load compiled language strings from an embedded blob. */ 59 iLang *d = &lang_;
60 clear_Lang_(d);
61 load_Lang_(d, language);
40} 62}
41 63
42const iString *string_Lang(const char *msgId) { 64const char *cstr_Lang(const char *msgId) {
43 const iLang *d = &lang_; 65 const iLang *d = &lang_;
44 size_t pos; 66 size_t pos;
45 const iMsgStr key = { .id = msgId }; 67 const iMsgStr key = { .id = iConstCast(char *, msgId) };
46 if (locate_SortedArray(d->messages, &key, &pos)) { 68 if (locate_SortedArray(d->messages, &key, &pos)) {
47 return &((const iMsgStr *) at_SortedArray(d->messages, pos))->str; 69 return ((const iMsgStr *) at_SortedArray(d->messages, pos))->str;
48 } 70 }
49 //iAssert(iFalse); 71 //iAssert(iFalse);
50 fprintf(stderr, "[Lang] missing: %s\n", msgId); 72 fprintf(stderr, "[Lang] missing: %s\n", msgId);
51 return collectNewCStr_String(msgId); 73 return msgId;
52} 74}
53 75
54const char *cstr_Lang(const char *msgId) { 76const iString *string_Lang(const char *msgId) {
55 return cstr_String(string_Lang(msgId)); 77 return collectNewCStr_String(cstr_Lang(msgId));
56} 78}