diff options
author | XANTRONIX Development | 2023-09-10 22:59:40 -0400 |
---|---|---|
committer | XANTRONIX Development | 2023-09-10 22:59:40 -0400 |
commit | 74095ec4d92c4e70352d199447790b3fe096d4a1 (patch) | |
tree | 596fc271fffc321137b76c999d53fbbd3faa97e3 | |
parent | 788e4546debb65839484112dae05a5e13c1070c6 (diff) | |
download | zxdump-74095ec4d92c4e70352d199447790b3fe096d4a1.tar.gz zxdump-74095ec4d92c4e70352d199447790b3fe096d4a1.tar.bz2 zxdump-74095ec4d92c4e70352d199447790b3fe096d4a1.zip |
SO CLOSE
-rw-r--r-- | main.c | 38 |
1 files changed, 23 insertions, 15 deletions
@@ -37,6 +37,22 @@ static void usage(int argc, char **argv, char *message, ...) { exit(1); } +static inline size_t utf8_encode(uint8_t *buf, uint16_t codepoint) { + if ((codepoint & 0x007f) == codepoint) { + buf[0] = codepoint & 0xff; + return 1; + } else if ((codepoint & 0x07ff) == codepoint) { + buf[0] = (codepoint & 0x07c0) >> 6; + buf[1] = codepoint & 0x003f; + return 2; + } else if ((codepoint & 0xffff) == codepoint) { + buf[0] = (codepoint & 0xf000) >> 12; + buf[1] = (codepoint & 0x0fc0) >> 6; + buf[2] = codepoint & 0x003f; + return 3; + } +} + static ssize_t dump_line(off_t offset, void *buf, size_t len) { size_t i; @@ -69,23 +85,15 @@ static ssize_t dump_line(off_t offset, void *buf, size_t len) { for (i=0; i<ZXDUMP_STRIDE_LINE; i++) { uint8_t c = ((uint8_t *)buf)[offset+i]; - if (c <= ZXDUMP_CHARSET_LEN) { - uint16_t printable = charset[c]; + if (c < ZXDUMP_CHARSET_LEN) { + uint16_t codepoint = charset[c]; + uint8_t sequence[4]; + + size_t len = utf8_encode(sequence, codepoint); /* XXX: Implement support for inverse colours */ - if (printable & 0xf800) { - uint8_t sequence[2] = { - 0xc0 | ((printable & 0x1f00) >> 8), - 0x80 | (printable & 0x003f) - }; - - if (fwrite(sequence, 2, 1, stdout) < 1) { - goto error_io; - } - } else { - if (putchar((uint8_t)(printable & 0x00ff)) < 0) { - goto error_io; - } + if (fwrite(sequence, len, 1, stdout) < 1) { + goto error_io; } } else { if (putchar('.') < 0) { |