Read file quick

This commit is contained in:
Shav Kinderlehrer 2023-04-14 22:42:55 -04:00
parent ca8bb9e1c3
commit 6eadc6dd06
3 changed files with 23 additions and 19 deletions

View File

@ -7,6 +7,7 @@ struct filedata {
size_t len; size_t len;
int binary; int binary;
char *buf; char *buf;
char *altbuf;
}; };
struct filedata readfile(FILE *fp); struct filedata readfile(FILE *fp);

View File

@ -25,7 +25,7 @@ struct filedata readfile(FILE *fp) {
die("fread"); die("fread");
} }
f.lc = 1000; f.lc = 10000;
// guess if printable // guess if printable
// from https://github.com/sharkdp/content_inspector/blob/master/src/lib.rs // from https://github.com/sharkdp/content_inspector/blob/master/src/lib.rs

View File

@ -30,28 +30,30 @@ void run(FILE *fp, char *filename, bool tty) {
int lcpad = intlen(f.lc); int lcpad = intlen(f.lc);
f.lc = 0; // f.lc = 0;
char pc = '\0'; // char pc = '\0';
char c; // char c;
for (size_t i = 0; i < f.len; i++) { // for (size_t i = 0; i < f.len; i++) {
c = f.buf[i]; // c = f.buf[i];
if ((conf.lines && tty && !f.binary) && (pc == '\n' || i == 0)) { // if ((conf.lines && tty && !f.binary) && (pc == '\n' || i == 0)) {
f.lc++; // f.lc++;
int padlen = lcpad - intlen(f.lc); // int padlen = lcpad - intlen(f.lc);
char padding[padlen]; // char padding[padlen];
if (padlen) // if (padlen)
memset(padding, ' ', padlen); // memset(padding, ' ', padlen);
fprintf(stderr, "\r%s%s%d:%s ", grey, padlen > 0 ? padding : "", f.lc, // fprintf(stderr, "\r%s%s%d:%s ", grey, padlen > 0 ? padding : "", f.lc,
reset); // padlen < 1 causes undefined // reset); // padlen < 1 causes undefined
} // }
pc = c; // pc = c;
printf("%c", c); // printf("%c", c);
} // }
printf("%s", f.buf);
fflush(stdout); // prevent timing inconsistencies between stdout and stderr fflush(stdout); // prevent timing inconsistencies between stdout and stderr
@ -59,7 +61,8 @@ void run(FILE *fp, char *filename, bool tty) {
float rounded; float rounded;
char *format = formatbytes(f.len, &rounded); char *format = formatbytes(f.len, &rounded);
char *cnewline = c == '\n' ? "" : "\n"; // char *cnewline = c == '\n' ? "" : "\n";
char *cnewline = "";
fprintf(stderr, "\r%s%s%.2f %s%s\r\n", cnewline, invert_t, rounded, format, fprintf(stderr, "\r%s%s%.2f %s%s\r\n", cnewline, invert_t, rounded, format,
uinvert_t); uinvert_t);
} }