commit e020ca8

default  ·  2025-02-14 09:24:13 +0000 UTC
parent 78bd026
xs_po.h new file.
6 files changed,  +103, -15
M data.c
M snac.c
+7, -7
 1@@ -44,20 +44,20 @@ activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \
 2  xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h xs_unicode.h \
 3  snac.h http_codes.h
 4 data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \
 5- xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h snac.h \
 6- http_codes.h
 7+ xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h \
 8+ xs_po.h snac.h http_codes.h
 9 format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \
10  xs_time.h xs_match.h snac.h http_codes.h
11 html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \
12- xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h snac.h \
13- http_codes.h
14+ xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \
15+ snac.h http_codes.h
16 http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \
17  snac.h http_codes.h
18 httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \
19  xs_httpd.h xs_mime.h xs_time.h xs_openssl.h xs_fcgi.h xs_html.h snac.h \
20  http_codes.h
21-main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h snac.h \
22- http_codes.h
23+main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h xs_match.h \
24+ snac.h http_codes.h
25 mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \
26  xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \
27  snac.h http_codes.h
28@@ -65,7 +65,7 @@ sandbox.o: sandbox.c xs.h snac.h http_codes.h
29 snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \
30  xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \
31  xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \
32- xs_match.h xs_fcgi.h xs_html.h snac.h http_codes.h
33+ xs_match.h xs_fcgi.h xs_html.h xs_po.h snac.h http_codes.h
34 upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h
35 utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \
36  xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h
+7, -7
 1@@ -39,20 +39,20 @@ activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \
 2  xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h xs_unicode.h \
 3  snac.h http_codes.h
 4 data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \
 5- xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h snac.h \
 6- http_codes.h
 7+ xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h \
 8+ xs_po.h snac.h http_codes.h
 9 format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \
10  xs_time.h xs_match.h snac.h http_codes.h
11 html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \
12- xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h snac.h \
13- http_codes.h
14+ xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \
15+ snac.h http_codes.h
16 http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \
17  snac.h http_codes.h
18 httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \
19  xs_httpd.h xs_mime.h xs_time.h xs_openssl.h xs_fcgi.h xs_html.h snac.h \
20  http_codes.h
21-main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h snac.h \
22- http_codes.h
23+main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h xs_match.h \
24+ snac.h http_codes.h
25 mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \
26  xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \
27  snac.h http_codes.h
28@@ -60,7 +60,7 @@ sandbox.o: sandbox.c xs.h snac.h http_codes.h
29 snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \
30  xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \
31  xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \
32- xs_match.h xs_fcgi.h xs_html.h snac.h http_codes.h
33+ xs_match.h xs_fcgi.h xs_html.h xs_po.h snac.h http_codes.h
34 upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h
35 utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \
36  xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h
M data.c
+1, -0
1@@ -13,6 +13,7 @@
2 #include "xs_match.h"
3 #include "xs_unicode.h"
4 #include "xs_random.h"
5+#include "xs_po.h"
6 
7 #include "snac.h"
8 
M snac.c
+1, -0
1@@ -24,6 +24,7 @@
2 #include "xs_match.h"
3 #include "xs_fcgi.h"
4 #include "xs_html.h"
5+#include "xs_po.h"
6 
7 #include "snac.h"
8 
+86, -0
 1@@ -0,0 +1,86 @@
 2+/* copyright (c) 2025 grunfink et al. / MIT license */
 3+
 4+#ifndef _XS_PO_H
 5+
 6+#define _XS_PO_H
 7+
 8+xs_dict *xs_po_to_dict(const char *fn);
 9+
10+#ifdef XS_IMPLEMENTATION
11+
12+xs_dict *xs_po_to_dict(const char *fn)
13+/* converts a PO file to a dict */
14+{
15+    xs_dict *d = NULL;
16+    FILE *f;
17+
18+    if ((f = fopen(fn, "r")) != NULL) {
19+        d = xs_dict_new();
20+
21+        xs *k = NULL;
22+        xs *v = NULL;
23+        enum { IN_NONE, IN_K, IN_V } mode = IN_NONE;
24+
25+        while (!feof(f)) {
26+            xs *l = xs_strip_i(xs_readline(f));
27+
28+            /* discard empty lines and comments */
29+            if (*l == '\0' || *l == '#')
30+                continue;
31+
32+            if (xs_startswith(l, "msgid ")) {
33+                if (mode == IN_V) {
34+                    /* flush */
35+                    if (xs_is_string(k) && xs_is_string(v) && *v)
36+                        d = xs_dict_set(d, k, v);
37+
38+                    k = xs_free(k);
39+                    v = xs_free(v);
40+                }
41+
42+                l = xs_replace_i(l, "msgid ", "");
43+                mode = IN_K;
44+
45+                k = xs_str_new(NULL);
46+            }
47+            else
48+            if (xs_startswith(l, "msgstr ")) {
49+                if (mode != IN_K)
50+                    break;
51+
52+                l = xs_replace_i(l, "msgstr ", "");
53+                mode = IN_V;
54+
55+                v = xs_str_new(NULL);
56+            }
57+
58+            l = xs_replace_i(l, "\\n", "\n");
59+            l = xs_strip_chars_i(l, "\"");
60+
61+            switch (mode) {
62+            case IN_K:
63+                k = xs_str_cat(k, l);
64+                break;
65+
66+            case IN_V:
67+                v = xs_str_cat(v, l);
68+                break;
69+
70+            case IN_NONE:
71+                break;
72+            }
73+        }
74+
75+        /* final flush */
76+        if (xs_is_string(k) && xs_is_string(v) && *v)
77+            d = xs_dict_set(d, k, v);
78+
79+        fclose(f);
80+    }
81+
82+    return d;
83+}
84+
85+#endif /* XS_IMPLEMENTATION */
86+
87+#endif /* XS_PO_H */
+1, -1
1@@ -1 +1 @@
2-/* 2f43b93e9d2b63360c802e09f4c68adfef74c673 2025-01-28T07:40:50+01:00 */
3+/* d467dc71e518603250a55c8a67e26cf40e1710e9 2025-02-14T10:21:15+01:00 */