home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / plot / plot2ps.sha / plot2ps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  3.2 KB  |  166 lines

  1. /*
  2.     plot2ps --- convert Unix plot(5) data to PostScript
  3.  
  4.     Copyright (C) 1989 Rene' Seindal
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     My current address is: seindal@diku.dk
  21. */
  22.  
  23. #include <stdio.h>
  24.  
  25. /* routines for reading plot data */
  26. int  getch();            /* get a byte */
  27. int  getsi();            /* get a signed short */
  28. char *getstr();            /* get a string */
  29.  
  30. #include "bytesex.h"
  31.  
  32. main(argc,argv)
  33.     int    argc;
  34.     char **argv;
  35. {
  36.     int cmd;
  37.     int x, y, x0, y0, x1, y1;    /* (x, y) coordinates */
  38.     int r;            /* circle radius */
  39.     char *l;            /* label/linemods */
  40.  
  41.     openpl();
  42.     while ( (cmd = getch()) != EOF ) {
  43.     switch (cmd) {
  44.     case 'm':
  45.         x = getsi();
  46.         y = getsi();
  47.         move( x, y );
  48.         break;
  49.     case 'n':
  50.         x = getsi();
  51.         y = getsi();
  52.         cont( x, y );
  53.         break;
  54.     case 'p':
  55.         x = getsi();
  56.         y = getsi();
  57.         point( x, y );
  58.         break;
  59.     case 'l':
  60.         x = getsi();
  61.         y = getsi();
  62.         x0 = getsi();
  63.         y0 = getsi();
  64.         line( x, y, x0, y0 );
  65.         break;
  66.     case 't':
  67.         l = getstr();
  68.         label( l );
  69.         break;
  70.     case 'a':
  71.         x = getsi();
  72.         y = getsi();
  73.         x0 = getsi();
  74.         y0 = getsi();
  75.         x1 = getsi();
  76.         y1 = getsi();
  77.         arc( x,y, x0, y0, x1, y1 );
  78.         break;
  79.     case 'c':
  80.         x = getsi();
  81.         y = getsi();
  82.         r = getsi();
  83.         circle( x, y, r );
  84.         break;
  85.     case 'e':
  86.         erase();
  87.         break;
  88.     case 'f':
  89.         l = getstr();
  90.         linemod( l );
  91.         break;
  92.     case 's':
  93.         x = getsi();
  94.         y = getsi();
  95.         x0 = getsi();
  96.         y0 = getsi();
  97.         space( x, y, x0, y0 );
  98.         break;
  99.     default:
  100.         fprintf( stderr, "Undefinde plot command 0%o\n", cmd );
  101.     }
  102.     }
  103.     closepl();
  104. }
  105.  
  106. /* getch() reads a single byte from stdin, not reading EOF more than once,
  107.  * and not returning more than 8 bits.
  108.  */
  109. int getch()
  110. {
  111.     int ch;
  112.     static char eof = '\0';
  113.  
  114.     if (eof) return EOF;
  115.     
  116.     if ( (ch = getchar()) == EOF ) {
  117.     eof = '\1';
  118.     return EOF;
  119.     }
  120.     return ch&0377;
  121. }
  122.  
  123. /* getsi() return a signed short integer, big endian and sign extending.
  124.  * Returns EOF on end of file.
  125.  */
  126. int getsi()
  127. {
  128.     int m, n;
  129.     int val;
  130.  
  131.     n = getch();
  132.     if ( n == EOF )
  133.     return EOF;
  134.  
  135.     m = getch();
  136.     if ( m == EOF )
  137.     return EOF;
  138.  
  139. #ifdef BIG_ENDIAN
  140.     val = ((m << 8) | n);
  141. #else
  142.     val = ((n << 8) | m);
  143. #endif
  144.     if ( val & 0x8000 )
  145.     val |= ~0x7fff;
  146.     return val;
  147. }
  148.  
  149. /* getstr() reads a string from stdin.  The string is terminated by EOF,
  150.  * newline or '\0'.
  151.  */
  152. char *getstr()
  153. {
  154.     char str[1024];
  155.     char *s = str;
  156.     int ch;
  157.  
  158.     while ( (ch = getch()) != EOF && ch != '\n' && ch != '\0' )
  159.     *s++ = ch;
  160.     *s = '\0';
  161.  
  162.     return str;
  163. }
  164.  
  165.     
  166.