home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 238_01 / swprt.c < prev    next >
Text File  |  1987-07-28  |  6KB  |  198 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdarg.h>
  4. #include <fcntl.h>
  5. #include <GRADIO.h>
  6. #include <GRADVAR.h>
  7.  
  8. #define TBUFSIZ 16384   /* Text buffer size */
  9. #define MAX_LPP 100     /* Max. number of line per page */
  10.  
  11. #define FALSE 0
  12. #define TRUE 1
  13. #define ERROR (-1)
  14.  
  15. int TEXT_EOF=0, REMAIN=0;
  16. char TBUF[TBUFSIZ+1], *PAGEPTR;
  17. char *PAGE[MAX_LPP];
  18.  
  19. /* global variables init. to default value */
  20. int SWWIDTH=792;        /* width of output, length of paper */
  21. int SWHEIGHT=960;       /* height of output, width of paper */
  22. int VERT_DEN=0, HORZ_DEN=4;  /* vertical and horizontal density */
  23. int LINE_SPACING=11;    /* number of pixels between 2 base lines */
  24. int TOPMARG=36, BOTMARG=36;     /* top and bottom margin */
  25. int LEN=1, LINEPERPAGE;
  26. char fontname[64]="S6X8D.fon";  /* default font name */
  27.  
  28. int textfile;
  29.  
  30. int sw_move();
  31.  
  32. main(argc,argv)
  33. int argc;
  34. char *argv[];
  35. {
  36.     char ch;
  37.     char **tpage, *ptr;
  38.     int ret, frame1, linepos, loop, linecnt, fontnu, tlcnt;
  39.  
  40.     /* Interpret the options if any */
  41.  
  42.     argv++;
  43.     while (argc-- > 1 && *(*argv)++=='-') {
  44.         if (**argv) {
  45.             ch= *(*argv)++;
  46.             if (ch!='f')
  47.                 ret=atoi(*argv);
  48.         } else {
  49.             ch=0;
  50.         }
  51.         switch(ch) {
  52.         case 'H':
  53.             SWHEIGHT=ret;
  54.             break;
  55.         case 'L':
  56.             if (ret>0) LEN=ret;
  57.             break;
  58.         case 'W':
  59.             SWWIDTH=ret;
  60.             break;
  61.         case 'b':
  62.             BOTMARG=ret;
  63.             break;
  64.         case 'd':
  65.             HORZ_DEN=ret & 0x0f; 
  66.             break;
  67.         case 'f':
  68.             strcpy(fontname,*argv);
  69.             break;
  70.         case 's':
  71.             LINE_SPACING=ret;
  72.             break;
  73.         case 't':
  74.             TOPMARG=ret;
  75.             break;
  76.         case 'v':
  77.             VERT_DEN=ret ? 0x10 : 0;
  78.             break;
  79.         default:
  80.             fprintf(stderr,"Unknow option -%c ** IGNORED **\n",ch);
  81.             break;
  82.         }
  83.         argv++;
  84.     }
  85.  
  86.     /* Any command line argument remain ? */
  87.     if (argc != 1) {
  88.         /* no more or too much */
  89.         fprintf(stderr,"Usage: swprt [options] textfile\n");
  90.         exit(1);
  91.     }
  92.     (*argv)--;
  93.  
  94.     /* Open text file */
  95.     if ((textfile=open(argv[0],O_RDONLY | O_BINARY)) == ERROR) {
  96.         fprintf(stderr,"swprt: text file not found\n");
  97.         exit(1);
  98.     }
  99.     LINEPERPAGE=(SWHEIGHT-TOPMARG-BOTMARG)/LINE_SPACING;
  100.     GRADinit();         /* Initialize GRAD environemnt */
  101.     if ((fontnu=LoadFont(fontname)) == 0) {
  102.         fprintf(stderr,"Font file %s not found\n",fontname);
  103.         cleanup(1);
  104.     }
  105.     SelectFont(fontnu);
  106.     SPACING_FUNC=sw_move;
  107. /*    setgraph(); */
  108.     frame1=CreateFrame(SWHEIGHT-TOPMARG, SWWIDTH);
  109.     SelectFrame(frame1);
  110.  
  111.     while ((linecnt=readpage()) > 0) {  /* while more to print */
  112.         for (loop=LEN; loop>0; loop--) {   /* loop LEN times for each page */
  113.             SetOrg(0,(loop-LEN)*SWWIDTH);
  114.             tpage=PAGE;
  115.             linepos=SWHEIGHT-TOPMARG-LINE_SPACING;   /* starting line pos */
  116.             tlcnt=linecnt;
  117.             while (tlcnt--) {
  118.                 WriteStr(TOP_LEFT,DOWN,linepos,0,*tpage++,0,0);
  119.                 linepos-=LINE_SPACING;
  120.             }
  121.             PrintFrame(HORZ_DEN+VERT_DEN,(char **) NULL,0,12,0);
  122. /*            getch(); */
  123.             PlotType(1);        /* erase the frame */
  124.             HorzLine(0,0,20000,20000);
  125.             PlotType(0);
  126.         }
  127.         ptr=TBUF;       /* move remaining data to begining of buffer */
  128.         for (loop=REMAIN; loop>0; loop--) {
  129.             *ptr++ = *PAGEPTR++;
  130.         }
  131.         PAGEPTR=TBUF;
  132.     }
  133.     close(textfile);
  134.     cleanup(0);
  135. /*    settext(); */
  136.     
  137.  
  138. /* read next page of text data */
  139. readpage()
  140. {
  141.     int temp1, linenu, temp2, inkline;
  142.  
  143.     inkline=0;
  144.     if (!TEXT_EOF) {
  145.         temp1=TBUFSIZ-REMAIN;
  146.         if ((temp2=read(textfile, &TBUF[REMAIN], temp1)) != temp1) {
  147.             TEXT_EOF=TRUE;
  148.         }
  149.         REMAIN+=temp2;
  150.     }
  151.     linenu=0;
  152.     if (REMAIN != 0) {
  153.         PAGEPTR=TBUF;
  154.         for (; (linenu<LINEPERPAGE) && (REMAIN>0); ) {
  155.             PAGE[linenu]=PAGEPTR;
  156.             linenu++;
  157.             for (; REMAIN>0 ;) {
  158.                 while (REMAIN-- > 0) {
  159.                     if (*PAGEPTR==12) {         /* is it a FORM FEED ? */
  160.                         *PAGEPTR++='\0';        /* yes, end of page */
  161.                         return(linenu);
  162.                     }
  163.                     if (*PAGEPTR==0) {
  164.                         *PAGEPTR++ = 0x80; /* change 0 in text file to 0x80 */
  165.                         continue;
  166.                     }
  167.                     if (*PAGEPTR++ == '\r')     /* is it a carriage return ? */
  168.                         break;                  /* yes, end of search */
  169.                     inkline=linenu;
  170.                 }
  171.                 if ((REMAIN>0) && (*PAGEPTR=='\n')) {   /* new line ? */
  172.                     *(PAGEPTR-1)='\0';  /* end of line */
  173.                     *PAGEPTR++;
  174.                     REMAIN--;
  175.                     break;
  176.                 }
  177.             }
  178.         }
  179.         if (REMAIN<=0) {        /* any data remain in the buffer ?*/
  180.             linenu=inkline;     /* No more! */
  181.             REMAIN=0;
  182.             *PAGEPTR='\0';
  183.         }
  184.     }
  185.     return(linenu);
  186. }
  187.  
  188. int sw_move(curx, cury, w, h, remaining)
  189. int *curx, *cury, w, h;
  190. char *remaining;
  191. {
  192.     *cury+=h;   /* increase to next position */
  193.     return(*cury+ORGY > WINY2);  /* if cury > upper limit of window */
  194.                                  /* no need to continue             */
  195. }
  196.  
  197.