commit e020ca84718d99401a99af0363992b4661a1d16e
parent 78bd0263ce56ce8d8544389971daa3d6cdd5c25e
Author: default <nobody@localhost>
Date: Fri, 14 Feb 2025 10:24:13 +0100
xs_po.h new file.
Diffstat:
6 files changed, 103 insertions(+), 15 deletions(-)
diff --git a/Makefile b/Makefile
@@ -44,20 +44,20 @@ activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \
xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h xs_unicode.h \
snac.h http_codes.h
data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \
- xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h snac.h \
- http_codes.h
+ xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h \
+ xs_po.h snac.h http_codes.h
format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \
xs_time.h xs_match.h snac.h http_codes.h
html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \
- xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h snac.h \
- http_codes.h
+ xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \
+ snac.h http_codes.h
http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \
snac.h http_codes.h
httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \
xs_httpd.h xs_mime.h xs_time.h xs_openssl.h xs_fcgi.h xs_html.h snac.h \
http_codes.h
-main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h snac.h \
- http_codes.h
+main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h xs_match.h \
+ snac.h http_codes.h
mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \
xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \
snac.h http_codes.h
@@ -65,7 +65,7 @@ sandbox.o: sandbox.c xs.h snac.h http_codes.h
snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \
xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \
xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \
- xs_match.h xs_fcgi.h xs_html.h snac.h http_codes.h
+ xs_match.h xs_fcgi.h xs_html.h xs_po.h snac.h http_codes.h
upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h
utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \
xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h
diff --git a/Makefile.NetBSD b/Makefile.NetBSD
@@ -39,20 +39,20 @@ activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \
xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h xs_unicode.h \
snac.h http_codes.h
data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \
- xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h snac.h \
- http_codes.h
+ xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h \
+ xs_po.h snac.h http_codes.h
format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \
xs_time.h xs_match.h snac.h http_codes.h
html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \
- xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h snac.h \
- http_codes.h
+ xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \
+ snac.h http_codes.h
http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \
snac.h http_codes.h
httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \
xs_httpd.h xs_mime.h xs_time.h xs_openssl.h xs_fcgi.h xs_html.h snac.h \
http_codes.h
-main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h snac.h \
- http_codes.h
+main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h xs_match.h \
+ snac.h http_codes.h
mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \
xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \
snac.h http_codes.h
@@ -60,7 +60,7 @@ sandbox.o: sandbox.c xs.h snac.h http_codes.h
snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \
xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \
xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \
- xs_match.h xs_fcgi.h xs_html.h snac.h http_codes.h
+ xs_match.h xs_fcgi.h xs_html.h xs_po.h snac.h http_codes.h
upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h
utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \
xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h
diff --git a/data.c b/data.c
@@ -13,6 +13,7 @@
#include "xs_match.h"
#include "xs_unicode.h"
#include "xs_random.h"
+#include "xs_po.h"
#include "snac.h"
diff --git a/snac.c b/snac.c
@@ -24,6 +24,7 @@
#include "xs_match.h"
#include "xs_fcgi.h"
#include "xs_html.h"
+#include "xs_po.h"
#include "snac.h"
diff --git a/xs_po.h b/xs_po.h
@@ -0,0 +1,86 @@
+/* copyright (c) 2025 grunfink et al. / MIT license */
+
+#ifndef _XS_PO_H
+
+#define _XS_PO_H
+
+xs_dict *xs_po_to_dict(const char *fn);
+
+#ifdef XS_IMPLEMENTATION
+
+xs_dict *xs_po_to_dict(const char *fn)
+/* converts a PO file to a dict */
+{
+ xs_dict *d = NULL;
+ FILE *f;
+
+ if ((f = fopen(fn, "r")) != NULL) {
+ d = xs_dict_new();
+
+ xs *k = NULL;
+ xs *v = NULL;
+ enum { IN_NONE, IN_K, IN_V } mode = IN_NONE;
+
+ while (!feof(f)) {
+ xs *l = xs_strip_i(xs_readline(f));
+
+ /* discard empty lines and comments */
+ if (*l == '\0' || *l == '#')
+ continue;
+
+ if (xs_startswith(l, "msgid ")) {
+ if (mode == IN_V) {
+ /* flush */
+ if (xs_is_string(k) && xs_is_string(v) && *v)
+ d = xs_dict_set(d, k, v);
+
+ k = xs_free(k);
+ v = xs_free(v);
+ }
+
+ l = xs_replace_i(l, "msgid ", "");
+ mode = IN_K;
+
+ k = xs_str_new(NULL);
+ }
+ else
+ if (xs_startswith(l, "msgstr ")) {
+ if (mode != IN_K)
+ break;
+
+ l = xs_replace_i(l, "msgstr ", "");
+ mode = IN_V;
+
+ v = xs_str_new(NULL);
+ }
+
+ l = xs_replace_i(l, "\\n", "\n");
+ l = xs_strip_chars_i(l, "\"");
+
+ switch (mode) {
+ case IN_K:
+ k = xs_str_cat(k, l);
+ break;
+
+ case IN_V:
+ v = xs_str_cat(v, l);
+ break;
+
+ case IN_NONE:
+ break;
+ }
+ }
+
+ /* final flush */
+ if (xs_is_string(k) && xs_is_string(v) && *v)
+ d = xs_dict_set(d, k, v);
+
+ fclose(f);
+ }
+
+ return d;
+}
+
+#endif /* XS_IMPLEMENTATION */
+
+#endif /* XS_PO_H */
diff --git a/xs_version.h b/xs_version.h
@@ -1 +1 @@
-/* 2f43b93e9d2b63360c802e09f4c68adfef74c673 2025-01-28T07:40:50+01:00 */
+/* d467dc71e518603250a55c8a67e26cf40e1710e9 2025-02-14T10:21:15+01:00 */