/*----------------------------------------------------------------------------*\ * Sample C program to read a SpinDoctor plot file. \*----------------------------------------------------------------------------*/ #include #include typedef enum { EndPlot = 0, /* End plot; ignore (x,y) */ Unused = 1, /* Unused; ignore */ PenDown = 2, /* Pen down (draw) to (x,y) */ PenUp = 3, /* Pen up (move) to (x,y) */ Pen1 = 4, /* Select pen #1; ignore (x,y) */ Pen2 = 5, /* Select pen #2; ignore (x,y) */ Pen3 = 6, /* Select pen #3; ignore (x,y) */ Pen4 = 7, /* Select pen #4; ignore (x,y) */ Pen5 = 8, /* Select pen #5; ignore (x,y) */ Pen6 = 9, /* Select pen #6; ignore (x,y) */ Pen7 = 10, /* Select pen #7; ignore (x,y) */ Pen8 = 11 /* Select pen #8; ignore (x,y) */ } Pcode ; void plot_point ( float x, float y, Pcode i) ; /*----------------------------------------------------------------------------*\ * FUNCTION: main * PURPOSE: To open the plot file, process the data, and close the file. * * REMARKS: If argc > 1, then the program reads plot data from the file * named by argv[1]. Else, it reads from stdin. * * DATE: 2000/09/06 T.W.H. \*----------------------------------------------------------------------------*/ int main ( int argc, char *argv[]) { FILE *plot ; float x, y ; Pcode i ; int n, rcode ; char xstr[9], ystr[8], istr[2] ; /* * Open the input file. */ if (argc > 1) { if (! (plot = fopen (argv[1], "r"))) { fprintf (stderr, "%s: failed to open input %s\n", argv[0], argv[1]) ; exit (EXIT_FAILURE) ; } } else plot = stdin ; /* * To parse fields strictly by column alignment without relying on * whitespace (which may or may not occur within or between fields), read * literal characters into separate strings with %c, then convert those * strings. Note that %f and %s would attempt to parse the plot file on * whitespace, which we don't want. */ xstr[8] = ystr[7] = istr[1] = '\0' ; rcode = 0 ; do { /* * Parse 5 (x,y,i) triples per input line. */ for (n = 0 ; n < 5 ; n++) { if ((rcode = fscanf (plot, "%8c%7c%1c", xstr, ystr, istr)) != 3) break ; sscanf (xstr, "%f", &x) ; sscanf (ystr, "%f", &y) ; sscanf (istr, "%x", &i) ; plot_point (x, y, i) ; if (i == EndPlot) break ; } /* * End of input line. Skip over trailing '\r' and '\n' characters. */ fscanf (plot, "%*[\r\n]") ; } while ((rcode == 3) && (i != EndPlot)) ; /* * Close the input file. */ if (plot != stdin) fclose (plot) ; exit (EXIT_SUCCESS) ; } /*----------------------------------------------------------------------------*\ * FUNCTION: plot_point * PURPOSE: To process an (x,y,i) triple from the plot file. * * ARGS: x,y (float) x and y plot coordinates, in inches. * i (Pcode) command code. * * REMARKS: Replace this function with something useful. * * DATE: 2000/09/06 T.W.H. \*----------------------------------------------------------------------------*/ void plot_point ( float x, float y, Pcode i) { static int n = 0 ; /* * This simply echos the input to the output in the same plot file format. * The second fprintf() inserts '\n' after each set of 5 triples. */ fprintf (stdout, "%8.3f%7.3f%1x", x, y, i) ; n = (n + 1) % 5 ; if ((n == 0) || (i == EndPlot)) fprintf (stdout, "\n") ; }