/* #includes */ /*{{{C}}}*//*{{{*/ #undef _POSIX_SOURCE #define _POSIX_SOURCE 1 #undef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 2 #define _GNU_SOURCE 1 #include #include #include #include #include double strtod(const char *nptr, char **endptr); /* SunOS 4 hack */ #ifdef OLD_REALLOC #define realloc(s,l) myrealloc(s,l) #endif extern char *optarg; extern int optind,opterr,optopt; int getopt(int argc, char * const *argv, const char *optstring); #ifdef DMALLOC #include "dmalloc.h" #endif #include "csv.h" /*}}}*/ int main(int argc, char *argv[]) /*{{{*/ { /* variables */ /*{{{*/ struct Pair { const char *label; double value; int special; } *pair; int pairs; double height,width; int box; int nl; /*}}}*/ /* parse options */ /*{{{*/ { int c; height=width=2.0; box=0; nl=1; while ((c=getopt(argc,argv,"bnw:h:?"))!=EOF) switch (c) { /* w width */ /*{{{*/ case 'w': { char *end; width=strtod(optarg,&end); if (*end!='\0') { fprintf(stderr,"graph: invalid width\n"); exit(1); } break; } /*}}}*/ /* h height */ /*{{{*/ case 'h': { char *end; height=strtod(optarg,&end); if (*end!='\0') { fprintf(stderr,"graph: invalid height\n"); exit(1); } break; } /*}}}*/ /* n */ case 'n': { nl=0; break; } /* b */ case 'b': { box=1; break; } default: { fprintf(stderr,"Usage: graph [-b][-h height][-w width]\n"); exit(1); } } } /*}}}*/ /* get data */ /*{{{*/ { char ln[256]; int line,pairsz; pairs=pairsz=0; pair=(struct Pair*)0; for (line=1; fgets(ln,sizeof(ln),stdin); ++line) { const char *s; const char *end; if (pairs==pairsz) { if ((pair=realloc(pair,sizeof(struct Pair)*(pairsz+=128)))==(struct Pair*)0) { fprintf(stderr,"graph:%d:out of memory\n",line); exit(1); } } s=ln; pair[pairs].label=csv_string(s,&end); if (s==end) fprintf(stderr,"graph:%d:invalid string, ignoring record\n",line); else { s=end; pair[pairs].value=csv_double(s,&end); if (s==end) fprintf(stderr,"graph:%d:invalid value, ignoring record\n",line); else { s=end; pair[pairs].special=csv_double(s,&end); if (s==end && *s!='\n') fprintf(stderr,"graph:%d:invalid mark value, ignoringrecord\n",line); else { s=end; if (*s!='\n') fprintf(stderr,"graph:%d:trailing garbage\n",line); } ++pairs; } } } } /*}}}*/ if (box) /* make box graph */ /*{{{*/ { double height,boxwid,min,max,scale; int i; height=2; boxwid=width/pairs; for (min=max=0.0,i=0; imax) max=pair[i].value; } scale=height/(max-min); printf("[\nFRAME: box invis wid %f ht %f\n",width,height); for (i=0; i0 ? "sw" : "nw", i*boxwid, -min*scale, pair[i].label ); } printf("]"); if (nl) printf("\n"); } /*}}}*/ else /* make pie graph */ /*{{{*/ { int anyspecial,i; double sum,scale,extra,arc; for (sum=0.0,i=0,anyspecial=0; i