Breakup monolithic code

This commit is contained in:
Shav Kinderlehrer 2023-04-10 20:14:18 -04:00
parent aeafcc6891
commit 0e59c5f27c
5 changed files with 76 additions and 45 deletions

12
include/file.h Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#ifndef FILE_H
#define FILE_H
struct filedata {
int lc;
unsigned len;
char *buf;
};
struct filedata readfile(FILE *fp);
#endif

View File

@ -2,7 +2,7 @@
#define LIB_H #define LIB_H
void die(const char *message); void die(const char *message);
char* formatBytes(double *bytes); char *formatBytes(unsigned bytes, float *rounded);
int intlen(unsigned i); int intlen(unsigned i);
#endif #endif

41
src/lib/file.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include "file.h"
#include "lib.h"
struct filedata readfile(FILE *fp) {
struct filedata f;
f.lc = 0;
f.len = 0;
unsigned bufsize = 4;
f.buf = malloc(bufsize);
if (f.buf == NULL)
die("malloc");
char c;
while (fread(&c, sizeof(char), 1, fp) > 0) {
if (f.len == bufsize - 1) {
bufsize *= 2;
char *new_buf = realloc(f.buf, bufsize);
if (f.buf == NULL) {
free(f.buf);
die("realloc");
}
f.buf = new_buf;
}
if (c == '\n') {
f.lc++;
}
f.buf[f.len++] = c;
}
return f;
}

View File

@ -7,12 +7,12 @@ void die(const char *message) {
exit(1); exit(1);
} }
char *formatBytes(double *bytes) { char *formatBytes(unsigned bytes, float *rounded) {
char *SIZES[] = {"bytes", "kB", "MB", "GB"}; char *SIZES[] = {"bytes", "kB", "MB", "GB"};
size_t size = *bytes; unsigned size = bytes;
size_t div = 0; unsigned div = 0;
size_t rem = 0; unsigned rem = 0;
while (size >= 1024 && div < (sizeof SIZES / sizeof *SIZES)) { while (size >= 1024 && div < (sizeof SIZES / sizeof *SIZES)) {
rem = (size % 1024); rem = (size % 1024);
@ -20,7 +20,7 @@ char *formatBytes(double *bytes) {
size /= 1024; size /= 1024;
} }
*bytes = (float)size + (float)rem / 1024.0; *rounded = (float)size + (float)rem / 1024.0;
return SIZES[div]; return SIZES[div];
} }

View File

@ -1,8 +1,10 @@
#include <libgen.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "file.h"
#include "lib.h" #include "lib.h"
#define INVERT_T "\x1b[7m" #define INVERT_T "\x1b[7m"
@ -19,64 +21,40 @@ int run(char *filename) {
die("fopen"); die("fopen");
if (tty) if (tty)
fprintf(stderr, "%s%s%s\r\n", INVERT_T, filename, UINVERT_T); fprintf(stderr, "%s%s%s\r\n", INVERT_T, basename(filename), UINVERT_T);
int bufsize = 4; struct filedata f;
char *buf; f = readfile(fp);
buf = malloc(bufsize);
if (buf == NULL)
die("malloc");
double fsize = 0;
unsigned offset = 0;
unsigned lc = 0;
char c;
while (fread(&c, sizeof(char), 1, fp) > 0) {
if (fsize == bufsize - 1) {
bufsize *= 2;
char *new_buf = realloc(buf, bufsize);
if (buf == NULL) {
free(buf);
die("realloc");
}
buf = new_buf;
}
if (c == '\n')
lc++;
buf[offset++] = c;
fsize++;
}
fclose(fp); fclose(fp);
int lcpad = intlen(lc); int lcpad = intlen(f.lc);
lc = 0; f.lc = 0;
char pc = '\0'; char pc = '\0';
for (int i = 0; (unsigned)i < offset; i++) { char c;
c = buf[i]; for (unsigned i = 0; i < f.len; i++) {
c = f.buf[i];
if (pc == '\n' || i == 0) { if (pc == '\n' || i == 0) {
lc++; f.lc++;
int padlen = lcpad - intlen(lc); int padlen = lcpad - intlen(f.lc);
char padding[padlen]; char padding[padlen];
memset(padding, ' ', padlen); memset(padding, ' ', padlen);
if (tty) if (tty)
fprintf(stderr, "%s%s%d:%s ", GREY, padding, lc, RESET); fprintf(stderr, "%s%s%d:%s ", GREY, padding, f.lc, RESET);
} }
pc = c; pc = c;
printf("%c", c); printf("%c", c);
} }
char *format = formatBytes(&fsize); float rounded;
char *format = formatBytes(f.len, &rounded);
if (tty) if (tty)
fprintf(stderr, "%s%.2f %s%s\r\n", INVERT_T, fsize, format, UINVERT_T); fprintf(stderr, "%s%.2f %s%s\r\n", INVERT_T, rounded, format, UINVERT_T);
return 0; return 0;
} }