home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 376.lha / DWPrint_v1.2 / src / DWPrint.c < prev    next >
C/C++ Source or Header  |  1990-05-01  |  8KB  |  325 lines

  1. /*
  2.  *
  3.  * Module : dwprint.c
  4.  *
  5.  * Description : Print program for daisy wheels
  6.  *
  7.  * Author : Simon Raybould
  8.  *
  9.  * Date : January 1990
  10.  *
  11.  * Compiler switches :
  12.  *
  13.  *        TEST - Send output to screen, for testing.
  14.  *
  15.  */
  16.  
  17. #include <stdio.h>            /* Yawn */
  18. #include <intuition/intuition.h>    /* For the window struct stuff */
  19. #include <workbench/startup.h>        /* For the filename from WB */
  20. #include <workbench/workbench.h>    /* For the filename from WB */
  21.  
  22. #define PRINTER        "PRT:"        /* Printer filename */
  23. #define DEFAULTLPP    66        /* Default lines per page */
  24. #define CTRLL        12        /* Control L */
  25. #define MAXLINELEN    128
  26. #define TAB        0x09        /* ASCii code for a horizontal tab */
  27. #define TABSTOP        8        /* Tab stop every TABSTOP columns */
  28.  
  29. /*
  30.  *    Globals
  31.  */
  32. char titlebuffer[80];
  33. short LinesPerPage = DEFAULTLPP;
  34.  
  35. struct IntuitionBase *IntuitionBase;
  36. struct GfxBase *GfxBase;
  37. struct Window *LittleWindow;
  38. struct IntuiMessage *Message;
  39. struct RastPort *RP;
  40. ULONG IconBase = 0;
  41.  
  42. /* WB Startup stuff */
  43. extern struct WBStartup *WBenchMsg;
  44. struct FileLock *startlock, *newlock;
  45.  
  46. /*** The Newwindow Structure. ***/
  47.  
  48. char TITLETEXT[] = "DWPrint V1.2 S.J.Raybould 1990";
  49.  
  50. struct TextAttr Font = {
  51.     (STRPTR)"topaz.font",
  52.     TOPAZ_EIGHTY, 0, 0
  53. };
  54.  
  55.     /* Gadget to do NEXT PAGE */
  56.  
  57. USHORT NextVectors[] = {
  58.     0, 0, 80, 0, 80, 12, 0, 12, 0, 1,
  59.     79, 1, 79, 11, 1, 11, 1, 1
  60. };
  61.  
  62. struct Border NextBorder = {
  63.     -2, -2,        /* Initial offsets, gadget relative */
  64.     1, 0, JAM1,    /* Frontpen, Backpen, Drawmode    */
  65.     9,        /* Number of vectors*/
  66.     NextVectors,    /* Pointer to array of vectors */
  67.     NULL        /* No next border */
  68. };
  69.  
  70. struct IntuiText NextText = {
  71.     1, 0,                /* Frontpen, Backpen */
  72.     JAM1,                /* Drawmode */
  73.     3, 1,                /* Leftedge, Topedge (relative to gadget) */
  74.     &Font,                /* Pointer to font structure */
  75.     "NEXT PAGE",            /* Actual text */
  76.     NULL                /* Next text */
  77. };
  78.  
  79. struct Gadget Next = {
  80.     NULL,            /* Pointer to next gadget */
  81.     130, 60, 77, 9,        /* Left, Top, Width, Height */
  82.     GADGHCOMP,        /* Flags */
  83.     RELVERIFY|GADGIMMEDIATE,/* Activation Flags */
  84.     BOOLGADGET,        /* Type */
  85.     (APTR)&NextBorder,    /* Pointer to border border image */
  86.     NULL,            /* No pointer to select render */
  87.     &NextText,        /* Pointer to Ituitext structure */
  88.     0,            /* Mutual exclude */
  89.     NULL,            /* Pointer to Special info */
  90.     0,            /* no ID */
  91.     NULL            /* Pointer to special data */
  92. };
  93.  
  94. struct NewWindow NewLittleWindow = {
  95.     40,    /* Left edge */
  96.     15,    /* Top edge */
  97.     330,    /* Width */
  98.     80,    /* Height */
  99.     0,    /* Front pen */    
  100.     1,    /* Back pen */
  101. CLOSEWINDOW | GADGETUP,    /* IDCMP Flags */
  102. WINDOWDRAG | WINDOWDEPTH | RMBTRAP | WINDOWCLOSE | NOCAREREFRESH, /* Windo Flagz */    
  103.     &Next,        /* First Gadget */
  104.     NULL,        /* Custom menu image */
  105.     TITLETEXT,    /* Window title */
  106.     NULL,        /* Custom screen */
  107.     NULL,        /* Custom bitmap */
  108.     10,        /* Minimum Width */
  109.     10,        /* Minimum Height */
  110.     500,        /* Maximum Width */
  111.     160,        /* Maximum Height */
  112.     WBENCHSCREEN,    /* Type of screen */
  113. };
  114.  
  115. void main(argc, argv)
  116. int argc;
  117. char **argv;
  118. {
  119.     register int Class;
  120.     struct Window *OpenWindow();
  121.     struct IntuiMessage *GetMsg();
  122.     struct WBArg *arg;
  123.     char *filename, sbuffer[80], buffer[MAXLINELEN], *buf_ptr, line;
  124.     unsigned char FromWB = FALSE;
  125.     int page=1;
  126.     FILE *fp;
  127.     void ChkToolTypes();
  128.     FILE *pfp;
  129.     char *chstr = "CHANGE PAPER AND CLICK NEXT PAGE";
  130.     char *rchstr= "                                ";
  131.     char ContLine = FALSE;
  132.     char wbuffer[MAXLINELEN], *rptr;
  133.     short wptr;
  134.  
  135.     newlock = startlock = (struct FileLock *)NULL;
  136.  
  137.     switch(argc) {
  138.     case 0:
  139.         FromWB = TRUE;
  140.         /* Check if user has set lines per page in tool types */
  141.         ChkToolTypes();
  142. #ifdef TRACE
  143.         printf("NumArgs = %d\n", WBenchMsg->sm_NumArgs);
  144.         arg = WBenchMsg->sm_ArgList;
  145.         printf("Arg0 = %s\n", (char *)arg->wa_Name);
  146.         arg++;
  147.         printf("Arg1 = %s\n", (char *)arg->wa_Name);
  148. #endif /* TRACE */
  149.         if(WBenchMsg->sm_NumArgs != 2) {
  150.             fprintf(stderr, "No file selected.\n");
  151.             Delay(300);
  152.             exit(1);
  153.         } else {
  154.             arg = WBenchMsg->sm_ArgList;
  155.             arg++;
  156.             filename = (char *)arg->wa_Name;
  157.             newlock = (struct FileLock *)arg->wa_Lock;
  158.             startlock = (struct FileLock *)CurrentDir(newlock);
  159.         }
  160.         break;
  161.     case 2:
  162.         filename = argv[1];
  163.         break;
  164.     case 3:
  165.         filename = argv[1];
  166.         LinesPerPage = atoi(argv[2]);
  167.         if(LinesPerPage > 0)
  168.             break;
  169.         /* ELSE FALL THROUGH TO USAGE */
  170.     default:
  171.         fprintf(stderr, "Usage: %s <filename> [n]\n", argv[0]);
  172.         exit(1);
  173.     }
  174.  
  175.     IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);
  176.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  177.  
  178.     LittleWindow = OpenWindow(&NewLittleWindow);
  179.     if (LittleWindow == NULL) exit(400L);/* No memory for little window! */
  180.  
  181.     RP = LittleWindow->RPort;
  182.  
  183.     SetAPen(RP, 1);
  184.     SetBPen(RP, 0);
  185.     SetDrMd(RP, JAM2);
  186.  
  187.     /*
  188.      *    OK it's about time we actually did somthing dwprint'ish.
  189.      */
  190.  
  191.     if((fp = fopen(filename, "r")) == (FILE *)NULL) {
  192.         fprintf(stderr, "Failed to open input file \"%s\"\n", filename);
  193.         perror("Reason");
  194.         if(FromWB)
  195.             Delay(300);
  196.         CloseWindow(LittleWindow);
  197.         if(newlock != startlock)
  198.             CurrentDir(startlock);
  199.         exit(1);
  200.     }
  201. #ifndef TEST
  202.     if((pfp = fopen(PRINTER, "w")) == (FILE *)NULL) {
  203.         fprintf(stderr, "Failed to open printer\n");
  204.         perror("Reason");
  205.         if(FromWB)
  206.             Delay(300);
  207.         CloseWindow(LittleWindow);
  208.         if(newlock != startlock)
  209.             CurrentDir(startlock);
  210.         exit(1);
  211.     }
  212.     if(FromWB) {
  213.         close(0);
  214.         close(1);
  215.         close(2);
  216.     }
  217. #else    /* If TEST switch set */
  218.     pfp = stdout;
  219. #endif /* TEST */
  220.  
  221.     SetAPen(RP, 3);
  222.     Move(RP, 50, 20);
  223.     Text(RP, "Printing:", strlen("Printing:"));
  224.     SetAPen(RP, 1);
  225.     Move(RP, 130, 20);
  226.     Text(RP, filename, strlen(filename));
  227.  
  228.     /* Wait for event */
  229.     for (;;) {
  230.         for(line=1;line<(LinesPerPage+1);line++) {
  231.             sprintf(sbuffer, "Page: %-3d         Line: %-2d", page, line);
  232.             Move(RP, 50, 35);
  233.             Text(RP, sbuffer, strlen(sbuffer));
  234.             if(!ContLine) {
  235.                 if(fgets(buffer, MAXLINELEN, fp) == (FILE *)NULL) {
  236.                     CloseWindow(LittleWindow);
  237.                     if(newlock != startlock)
  238.                         CurrentDir(startlock);
  239.                     fclose(fp);
  240.                     fclose(pfp);
  241.                     exit(0);
  242.                 }
  243.                 buf_ptr = buffer;    /* Reset ptr to start of buffer */
  244.             }
  245.             /* If CTRL-L then end page */
  246.             if(*buf_ptr == CTRLL) {
  247.                 ContLine = TRUE;
  248.                 fputc(*buf_ptr++, pfp);        /* Send Cntrl L */
  249.                 fflush(pfp);
  250.                 if(!*buf_ptr)    /* End of line reached. */
  251.                     ContLine = FALSE;    /* Don't put NULL on its own page */
  252.                 break;
  253.             } else {
  254.                 ContLine = FALSE;
  255.                 for(wptr = 0;wptr < MAXLINELEN;wptr++)
  256.                     wbuffer[wptr] = ' ';
  257.                 for(wptr = 0, rptr = buf_ptr;*rptr != '\0';rptr++) {
  258.                     if(*rptr == TAB)
  259.                         wptr = ((wptr/TABSTOP) + 1) * TABSTOP;
  260.                     else
  261.                         wbuffer[wptr++] = *rptr;
  262.                 }
  263.                 wbuffer[wptr] = *rptr;
  264.                 fputs(wbuffer, pfp);            /* Output line */
  265.                 fflush(pfp);
  266.             }
  267.         }
  268.         Move(RP, 40, 50);
  269.         Text(RP, chstr, strlen(chstr));
  270.         page++;
  271.         SetWindowTitles(LittleWindow, -1, titlebuffer);
  272.         Message = GetMsg(LittleWindow->UserPort);
  273.         while (Message == NULL) {
  274.             /* Lets be nice to other tasks!  Ya! */
  275.             Wait(1<<LittleWindow->UserPort->mp_SigBit);
  276.             Message = GetMsg(LittleWindow->UserPort);
  277.         };
  278.         Class = Message->Class;
  279.         if (Message != NULL) ReplyMsg(Message);
  280.         
  281.         switch(Class) {
  282.         case CLOSEWINDOW:
  283.             CloseWindow(LittleWindow);
  284.             if(newlock != startlock)
  285.                 CurrentDir(startlock);
  286.             fclose(fp);
  287.             fclose(pfp);
  288.             exit(0);
  289.         case GADGETUP:
  290.             Move(RP, 40, 50);
  291.             Text(RP, rchstr, strlen(rchstr));
  292.             break;    /* Just go round again */
  293.         default:
  294. #ifdef TRACE
  295.             fprintf(stderr, "Unkown message (0x%x) recieved\n", Class);
  296. #endif /* TRACE */
  297.             break;
  298.         }
  299.     }
  300. }
  301.  
  302. void ChkToolTypes()
  303. {
  304.     struct DiskObject *diskobj;
  305.     struct WBArg *arg;
  306.     char **toolarray, *s, *progname;
  307.  
  308.     arg = WBenchMsg->sm_ArgList;
  309.     progname = (char *)arg->wa_Name;
  310.  
  311.     if(IconBase = OpenLibrary("icon.library", 0)) {
  312.         diskobj = (struct DiskObject *)GetDiskObject(progname);
  313.         if(diskobj) {
  314.             toolarray = diskobj->do_ToolTypes;
  315.             if(s=(char *)FindToolType(toolarray, "LINESPERPAGE")) {
  316.                 LinesPerPage = atoi(s);
  317. #ifdef TRACE
  318.                 printf("Found it, got %d\n", LinesPerPage);
  319. #endif /* TRACE */
  320.             }
  321.         }
  322.         FreeDiskObject(diskobj);
  323.     }
  324. }
  325.