This commit is contained in:
XANTRONIX Development 2023-09-10 22:59:40 -04:00
parent 788e4546de
commit 74095ec4d9

38
main.c
View file

@ -37,6 +37,22 @@ static void usage(int argc, char **argv, char *message, ...) {
exit(1); 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) { static ssize_t dump_line(off_t offset, void *buf, size_t len) {
size_t i; 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++) { for (i=0; i<ZXDUMP_STRIDE_LINE; i++) {
uint8_t c = ((uint8_t *)buf)[offset+i]; uint8_t c = ((uint8_t *)buf)[offset+i];
if (c <= ZXDUMP_CHARSET_LEN) { if (c < ZXDUMP_CHARSET_LEN) {
uint16_t printable = charset[c]; uint16_t codepoint = charset[c];
uint8_t sequence[4];
size_t len = utf8_encode(sequence, codepoint);
/* XXX: Implement support for inverse colours */ /* XXX: Implement support for inverse colours */
if (printable & 0xf800) { if (fwrite(sequence, len, 1, stdout) < 1) {
uint8_t sequence[2] = { goto error_io;
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;
}
} }
} else { } else {
if (putchar('.') < 0) { if (putchar('.') < 0) {