Browse Source

xs_po.h new file.

default 1 month ago
parent
commit
e020ca8471
6 changed files with 103 additions and 15 deletions
  1. 7 7
      Makefile
  2. 7 7
      Makefile.NetBSD
  3. 1 0
      data.c
  4. 1 0
      snac.c
  5. 86 0
      xs_po.h
  6. 1 1
      xs_version.h

+ 7 - 7
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

+ 7 - 7
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

+ 1 - 0
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"
 

+ 1 - 0
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"
 

+ 86 - 0
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 */

+ 1 - 1
xs_version.h

@@ -1 +1 @@
-/* 2f43b93e9d2b63360c802e09f4c68adfef74c673 2025-01-28T07:40:50+01:00 */
+/* d467dc71e518603250a55c8a67e26cf40e1710e9 2025-02-14T10:21:15+01:00 */