From 017a503f34a0f8ce2245f1b5dd78894f65326a90 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Sat, 29 Jun 2024 04:49:12 -0400 Subject: a8cat: added (will eventually replace both a8utf8 and a8eol). --- wtable.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 wtable.c (limited to 'wtable.c') diff --git a/wtable.c b/wtable.c new file mode 100644 index 0000000..3c008b3 --- /dev/null +++ b/wtable.c @@ -0,0 +1,140 @@ +/* ref: +https://stackoverflow.com/questions/21737906/how-to-read-write-utf8-text-files-in-c +*/ + +#include +#include +#include "wtable.h" + +/* +#define WSEARCH_DEBUG +*/ + +wint_t wchar2ata[][2] = { + /* Unicode, ATASCII */ + { 0x2190, 0x1e }, + { 0x2191, 0x1c }, + { 0x2192, 0x1f }, + { 0x2193, 0x1d }, + { 0x21b0, 0x7d }, + { 0x241b, 0x1b }, + { 0x2501, 0x12 }, + { 0x2503, 0x02 }, + { 0x250f, 0x11 }, + { 0x2513, 0x05 }, + { 0x2517, 0x1a }, + { 0x251b, 0x03 }, + { 0x2523, 0x01 }, + { 0x252b, 0x04 }, + { 0x2533, 0x17 }, + { 0x253b, 0x18 }, + { 0x254b, 0x13 }, + { 0x2571, 0x06 }, + { 0x2572, 0x07 }, + { 0x2581, 0x0e }, + { 0x2584, 0x15 }, + { 0x258c, 0x19 }, + { 0x258e, 0x16 }, + { 0x2594, 0x0d }, + { 0x2596, 0x0f }, + { 0x2597, 0x09 }, + { 0x2598, 0x0c }, + { 0x259d, 0x0b }, + { 0x25b6, 0x7f }, + { 0x25c0, 0x7e }, + { 0x25c6, 0x60 }, + { 0x25cf, 0x14 }, + { 0x25e2, 0x08 }, + { 0x25e3, 0x0a }, + { 0x2660, 0x7b }, + { 0x2663, 0x10 }, + { 0x2665, 0x00 }, +}; + +wint_t wchar2ics[][2] = { + /* Unicode, ATASCII */ + { 0x00a1, 0x60 }, + { 0x00a3, 0x08 }, + { 0x00c4, 0x7b }, + { 0x00c9, 0x03 }, + { 0x00d1, 0x02 }, + { 0x00d6, 0x0c }, + { 0x00dc, 0x10 }, + { 0x00e0, 0x19 }, + { 0x00e1, 0x00 }, + { 0x00e2, 0x11 }, + { 0x00e4, 0x0b }, + { 0x00e7, 0x04 }, + { 0x00e8, 0x15 }, + { 0x00e9, 0x14 }, + { 0x00ea, 0x17 }, + { 0x00ec, 0x07 }, + { 0x00ee, 0x13 }, + { 0x00ef, 0x09 }, + { 0x00f1, 0x16 }, + { 0x00f2, 0x06 }, + { 0x00f3, 0x0e }, + { 0x00f4, 0x05 }, + { 0x00f6, 0x0f }, + { 0x00f9, 0x01 }, + { 0x00fa, 0x0d }, + { 0x00fb, 0x12 }, + { 0x00fc, 0x0a }, + { 0x0226, 0x1a }, + { 0x0227, 0x18 }, + { 0x2190, 0x1e }, + { 0x2191, 0x1c }, + { 0x2192, 0x1f }, + { 0x2193, 0x1d }, + { 0x21b0, 0x7d }, + { 0x241b, 0x1b }, + { 0x25b6, 0x7f }, + { 0x25c0, 0x7e }, +}; + +static int tblsize = sizeof(wchar2ata) / sizeof(wchar2ata[0]); + +static wint_t wsearch(wint_t table[][2], wint_t target, int start, int end) { + wint_t *elem; + int center; + +#ifdef WSEARCH_DEBUG + fprintf(stderr, "wsearch(0x%04x, %d, %d)\n", target, start, end); +#endif + + if(start == end) { + if(table[start][0] == target) + return table[start][1]; + else + return -1; + } else { + center = (start + end) / 2; + elem = table[center]; + +#ifdef WSEARCH_DEBUG + fprintf(stderr, "elem = 0x%04x, 0x%02x\n", elem[0], elem[1]); +#endif + + if(elem[0] == target) + return elem[1]; + else if(elem[0] > target) + return wsearch(table, target, start, center); + else + return wsearch(table, target, center + 1, end); + } +} + +int wchar2atascii(wint_t wc, int ics) { + return wsearch((ics ? wchar2ics : wchar2ata), wc, 0, tblsize - 1); +} + +#ifdef WSEARCH_DEBUG +int main(int argc, char **argv) { + printf("%02x\n", wchar2atascii(0x2190, 0)); + printf("%02x\n", wchar2atascii(0x2571, 0)); + printf("%02x\n", wchar2atascii(0x25c6, 0)); + printf("%02x\n", wchar2atascii(0x2665, 0)); + printf("%02x\n", wchar2atascii(0x2510, 0)); + return 0; +} +#endif -- cgit v1.2.3