From 8f14d6dd1293e0f1e2d3f65b596f0fa6f162a383 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Tue, 18 Nov 2025 01:36:46 -0500 Subject: Better text file detection for -aa; convert tabs with -a or -aa. --- src/io.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'src/io.c') diff --git a/src/io.c b/src/io.c index 2323700..1896df9 100644 --- a/src/io.c +++ b/src/io.c @@ -5,6 +5,7 @@ #include "addrs.h" static int headers_read = 0; +static int convert_eols = 0; static void die_arc(void) { fprintf(stderr, "%s: this is an ARC file, not ALF\n", self); @@ -104,27 +105,41 @@ void readblock(void) { dpoke(buf_len_l, bytes); } -int is_text_file(char *fn) { - if(globmatch("*.txt", fn)) return 1; - if(globmatch("*.doc", fn)) return 1; - if(globmatch("*.lst", fn)) return 1; - return 0; +static int is_printable(u8 c) { + return (c == 0x9b || (c >= ' ' && c <= 124)); +} + +static int is_text_file(u8 *buf) { + return is_printable(buf[0]) && is_printable(buf[1]); } -/* mirror of readblock() */ +/* mirror of readblock(), plus EOL conversion if needed. With -a, + a file is considered text if its first 2 bytes are printable ATASCII, + including EOLs. With -aa, all files are converted. */ void writeblock(void) { int i, bytes, len, bufadr; u8 *buf; - extern char *out_filename; bufadr = dpeek(buf_adr_l); buf = mem + bufadr; len = dpeek(buf_len_l); - if(opts.txtconv) { - if(opts.txtconv > 1 || is_text_file(out_filename)) - for(i = 0; i < len; i++) + if(new_file) { + if(opts.txtconv > 1) { + convert_eols = 1; + } else if(opts.txtconv == 1 && len > 1) { + convert_eols = is_text_file(buf); + } else { + convert_eols = 0; + } + } + new_file = 0; + + if(convert_eols) { + for(i = 0; i < len; i++) { if(buf[i] == 0x9b) buf[i] = '\n'; + if(buf[i] == 0x7f) buf[i] = '\t'; + } } // fprintf(stderr, "writeblock, bufadr = $%04x, len = $%04x\n", bufadr, len); -- cgit v1.2.3