diff options
author | John Ankarström <john@ankarstrom.se> | 2020-12-13 00:42:23 +0100 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2020-12-13 00:42:23 +0100 |
commit | 43ff48ff49c57f6c30795b7a9acadf1f9e638e8d (patch) | |
tree | e0ad182043c18c83c0c232254645e6b543d739a3 | |
parent | 533fd864a564943beba3b0d311eb7e0f990051e2 (diff) | |
download | typ-43ff48ff49c57f6c30795b7a9acadf1f9e638e8d.tar.gz |
typ.c: Re-design system
It is now really solid.
-rw-r--r-- | typ.c | 106 |
1 files changed, 47 insertions, 59 deletions
@@ -1,3 +1,4 @@ +#include <ctype.h> #include <err.h> #include <fcntl.h> #include <stdio.h> @@ -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); |