From 43ff48ff49c57f6c30795b7a9acadf1f9e638e8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 13 Dec 2020 00:42:23 +0100 Subject: typ.c: Re-design system It is now really solid. --- typ.c | 106 +++++++++++++++++++++++++++++------------------------------------- 1 file changed, 47 insertions(+), 59 deletions(-) diff --git a/typ.c b/typ.c index 8c6d808..3f28f18 100644 --- a/typ.c +++ b/typ.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -13,82 +14,69 @@ double atime() { } int main() { - double *wt; - int c, l, t, ttyfd, wi, *wl, ws; - struct termios orig, raw; - - ws = 100; /* allocated number of words */ - wl = malloc(ws * sizeof(int)); /* word lengths */ - if (wl == NULL) err(1, "malloc"); - wt = malloc(ws * sizeof(double)); /* word times */ - if (wt == NULL) err(1, "malloc"); - - for (int i = 0; i < ws; i++) - wt[i] = 0; - - l = 0; /* current word length */ - wi = -1; /* index of current word in order */ + int cs = 500; /* allocated number of characters */ + char *cb = malloc(cs * sizeof(char)); /* character buffer */ + if (cb == NULL) err(1, "malloc"); + double *ct = malloc(cs * sizeof(double)); /* character times */ + if (ct == NULL) err(1, "malloc"); /* enter "raw" mode */ - ttyfd = open("/dev/tty", O_RDWR); + struct termios orig, raw; + int ttyfd = open("/dev/tty", O_RDWR); if (ttyfd == -1) err(1, "open"); tcgetattr(ttyfd, &orig); raw = orig; raw.c_lflag &= ~(ECHO | ICANON); tcsetattr(ttyfd, TCSANOW, &raw); - /* read loop */ + int c; + int ci = 0; /* character index */ while (read(ttyfd, &c, 1)) { - /* backspace */ - if (c == 127) { - l--; + if (c == 127 && ci > 0) { /* backspace */ + ci--; write(ttyfd, &c, 1); - if (l == -1) { - wi--; - fprintf(stderr, "<\n"); - } continue; - } + } else if (iscntrl(c) && c != '\n') + continue; + write(ttyfd, &c, 1); + cb[ci] = c; + ct[ci] = atime(); + ci++; + if (c == '\n') + break; + } - /* back to previous word (after backspace) */ - if (l == -1) { - l = wl[wi]; - fprintf(stderr, "%d\n", wl[wi-1]); - } + double finish = atime(); /* finish time */ - /* new word */ - if (c != ' ' && c != '\n' && (l == 0 || l == -1)) { - wi++; - t = atime(); /* current word start time */ - fprintf(stderr, "*\n"); + double tt = 0; /* total time */ + double t1 = -1; /* start time of current word */ + int words = 0; /* number of words */ + int nonspace = 0; /* number of non-space characters */ + int i; + for (i = 0; i <= ci; i++) { + if (cb[i] == ' ' || cb[i] == '\n') { + if (t1 == -1) continue; + tt += ct[i - 1] - t1; + t1 = -1; + if (cb[i] == '\n') break; + else continue; } - - /* end word */ - if (c == ' ' || c == '\n') { - wl[wi] = l; - if (t > -1) { - wt[wi] += atime() - t; - // fprintf(stderr, "[+%f]", atime() - t); - } - l = 0; - t = -1; - } else - l++; - if (c == '\n') - break; - write(ttyfd, &c, 1); + if (t1 == -1) { + t1 = ct[i]; + words++; + } + nonspace++; } - write(ttyfd, "\n", 1); /* print statistics */ - double tt = 0; - int tl = 0; - for (int i = 0; i <= wi; i++) { - tt += wt[i]; - tl += wl[i]; - } - printf("%f seconds per word (%d)\n", tt / (wi + 1), wi + 1); - printf("%f seconds per word character (%d)\n", tt / tl, tl); + printf("%d\twords\n", words); + printf("%d\tcharacters\n", ci - 1); + printf("%d\tcharacters inside words\n", nonspace); + printf("%.4f\tseconds in total\n", finish - ct[0]); + printf("%.4f\tseconds per character\n", tt / (ci - 1)); + printf("%.4f\tseconds per character inside word\n", tt / nonspace); + printf("%.4f\tseconds per word\n", tt / words); + printf("%.4f\tseconds inside words\n", tt); /* restore original terminal settings */ tcsetattr(ttyfd, TCSANOW, &orig); -- cgit v1.2.3