diff options
| author | B. Watson <urchlay@slackware.uk> | 2025-11-18 01:36:46 -0500 |
|---|---|---|
| committer | B. Watson <urchlay@slackware.uk> | 2025-11-18 01:36:46 -0500 |
| commit | 8f14d6dd1293e0f1e2d3f65b596f0fa6f162a383 (patch) | |
| tree | 2aea8ca7b98f02da42a4013fefce74e433f4f3a7 /src/io.c | |
| parent | 409ec782e1a74cbd86b6e51b49c0785021be0fdc (diff) | |
| download | unalf-8f14d6dd1293e0f1e2d3f65b596f0fa6f162a383.tar.gz | |
Better text file detection for -aa; convert tabs with -a or -aa.
Diffstat (limited to 'src/io.c')
| -rw-r--r-- | src/io.c | 35 |
1 files changed, 25 insertions, 10 deletions
@@ -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); |
