ploot

plot images or real-time plain text from a .csv file
Log | Files | Refs | README

commit c9c242228362e465b83fc106675981fd45419228
parent b22b1b174646f828a28144c010961a9c57f54dbd
Author: Josuah Demangeon <mail@josuah.net>
Date:   Wed,  2 May 2018 05:09:18 +0200

fix buffer size calculation

Diffstat:
Mmain.c | 17+++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/main.c b/main.c @@ -78,14 +78,15 @@ eatol(char *str) } void -add_value(double **v, int *bufsiz, int nval, char *field) +add_value(Vlist *v, int *bufsiz, int nval, char *field) { if (nval >= *bufsiz) { - *bufsiz *= 2; - if ((*v = realloc(*v, *bufsiz)) == NULL) + *bufsiz = *bufsiz * 2 + 1; + if ((v->v = realloc(v->v, *bufsiz)) == NULL) perror("reallocating values buffer"), exit(1); } - (*v)[nval] = eatol(field); + v->v[nval] = eatol(field); + v->n = nval; } /* @@ -106,7 +107,7 @@ add_each_value(Vlist *v, int *bufsiz, int ncol, int nval, char *line) for (n = 0; (field = strsep(&line, ",")) != NULL; n++, v++) { if (n > ncol) fprintf(stderr, "%d: too many fields\n", nval), exit(0); - add_value(&v->v, bufsiz, nval, field); + add_value(v, bufsiz, nval, field); } if (n < ncol) fprintf(stderr, "%d: too few fields\n", nval), exit(0); @@ -121,15 +122,15 @@ add_each_value(Vlist *v, int *bufsiz, int ncol, int nval, char *line) void read_values(Vlist *v, int ncol) { - int n, nval, bufsiz; + int nval, bufsiz; char line[LINE_MAX]; + bufsiz = 0; for (nval = 0; fgets(line, sizeof(line), stdin); nval++) { estriplf(line); add_each_value(v, &bufsiz, ncol, nval, line); } - for (n = 0; n < ncol; n++, v++) - v->n = nval; + fprintf(stderr, "nval: %d, bufsiz: %d\n", nval, bufsiz), fflush(stderr); } static void