home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / cnews019.zip / BLOB.C next >
C/C++ Source or Header  |  1990-08-05  |  4KB  |  174 lines

  1. /*  BLOB.C -- A program to print out a four line label.  Written for an
  2.     ALPS P2000G printer, it should work on any Epson-compatible printer.
  3.     This program accompanies an article in issue 20 of the C News.  */
  4.  
  5. #include <string.h>
  6. #include <conio.h>
  7. #include <stdio.h>
  8. #include <dos.h>
  9.  
  10. char *get_date();
  11.  
  12. union REGS regs;
  13.  
  14. #define YES 1
  15. #define NO  0
  16. #define LINELEN 129         /* DOS max line length + 1 */
  17.  
  18.  
  19. FILE *printer;
  20.  
  21. void main()
  22. {
  23.     /*  Declare variables for name, title, and the two comment lines
  24.         to be character arrays.  Declare the character array NLQ for
  25.         elite print and initialize with the escape codes for elite
  26.         printing on the ALPS P2000 printer.  Declare an integer for
  27.         use with yorn() to determine whether elite printer is desired.  */
  28.  
  29.     char name[15], title[28], comment1[28], comment2[28];
  30.     char NLQ[] = {0x1B, 0x4D};
  31.     int elite;
  32.  
  33.     /*  Clear the screen and reset the cursor to the upper left
  34.         corner.  */
  35.  
  36.     clearscr(24,79);
  37.     set_cursor(0,0);
  38.  
  39.     /*  Open the stream to the printer.  */
  40.  
  41.     printer = fopen("LPT1", "w");
  42.  
  43.     /*  Check whether the printer is online.  If not, notify the user
  44.         before proceeding.  (NOTE:  The program could be designed to
  45.         exit if the printer is not ready.)  */
  46.  
  47.     if(!prncheck())
  48.         puts("Please check the printer.");
  49.  
  50.     /*  Ask the user if elite printing is desired using yorn().  */
  51.  
  52.     elite = yorn("Would you like elite printing?", YES);
  53.  
  54.     /*  If the user has requested elite printing, send the escape codes
  55.         for elite printing, NLQ, to the printer.  */
  56.  
  57.     if (elite)
  58.         fputs(NLQ, printer);
  59.  
  60.     /*  Use a do-while loop to request the data and print the labels.  */
  61.  
  62.     do
  63.     {
  64.         printf("Please enter the title:  ");
  65.         gets(title);
  66.  
  67.         printf("Please enter a name:  ");
  68.         gets(name);
  69.  
  70.         printf("Please enter the first comment:  ");
  71.         gets(comment1);
  72.  
  73.         printf("Please enter the second comment:  ");
  74.         gets(comment2);
  75.  
  76.         fprintf(printer,"%s\n", title);
  77.         fprintf(printer,"%s", name);
  78.         fprintf(printer,"                  %s\n", get_date());
  79.         fprintf(printer,"%s\n", comment1);
  80.         fprintf(printer,"%s\n\n", comment2);
  81.  
  82.         printf("\n");
  83.  
  84.     } while(yorn("Would you like to print another label?", YES));
  85.  
  86.     /*  Flush the buffer and close the stream to the printer.  */
  87.  
  88.     fflush(printer);
  89.     fclose(printer);
  90.  
  91. }
  92.  
  93. /*  Get the system date using interrupt 21, function 2A.  See accompanying
  94.     text for details.  */
  95.  
  96. char *get_date()
  97. {
  98.     static char date[9];
  99.  
  100.     regs.h.ah= 0x2a;
  101.     intdos(®s, ®s);
  102.  
  103.     sprintf(date, "%02.2d/%02.2d/%02.2d", regs.h.dh, regs.h.dl, regs.x.cx-1900);
  104.  
  105.     return(date);
  106. }
  107.  
  108. /*  Get user response to a question, allowing the user to hit enter for
  109.     the requested default.  */
  110.  
  111. int yorn(char *prompt, int dfault)
  112. {
  113.     char s[LINELEN], c;
  114.     int len;
  115.  
  116.     printf("%s (%c) ", prompt, dfault ? 'Y' : 'N');
  117.     fgets(s, LINELEN, stdin);
  118.     len = strspn(s, " \t\n");    /* skip whitespace */
  119.  
  120.     if(len < strlen(s))
  121.     {
  122.         c = *(s + len);
  123.         if(c == 'Y' || c == 'y') return YES;
  124.         else if(c == 'N' || c == 'n') return NO;
  125.     }
  126.  
  127.     return dfault;
  128. }
  129.  
  130. /*  Clear the area of the screen from 0,0 to the specified coordinates using
  131.     interrupt 10, function 6.  See accompanying text for details.  */
  132.  
  133. int clearscr(int rows, int columns)
  134. {
  135.     regs.h.ah = 6;
  136.     regs.h.al = 0;
  137.     regs.h.bh = 7;    /*  Attribute to write to screen.  */
  138.     regs.h.ch = 0;
  139.     regs.h.cl = 0;
  140.     regs.h.dh = rows;
  141.     regs.h.dl = columns;
  142.  
  143.     int86(0x10, ®s, ®s);
  144. }
  145.  
  146. /*  Return the cursor to the desired position using interrupt 10, function 2.
  147.     See accompanying text for details.  */
  148.  
  149. int set_cursor(int rows, int columns)
  150. {
  151.     regs.h.ah = 2;
  152.     regs.h.bh = 0;
  153.     regs.h.dh = rows;           /*  Return cursor to this row.  */
  154.     regs.h.dl = columns;        /*  Retun cursor to this column.  */
  155.  
  156.     int86(0x10, ®s, ®s);
  157. }
  158.  
  159.  
  160. /*  Check to see if the printer is online using interrupt 17, function 2.
  161.     See accompanying text for details.  */
  162.  
  163. int prncheck()
  164. {
  165.     union REGS regs;
  166.  
  167.     regs.h.ah = 0x02;        /*  AH = 02 for printer status  */
  168.     regs.x.dx = 0;           /*  DX = 00 for LPT1  */
  169.  
  170.     int86(0x17, ®s, ®s);
  171.  
  172.     return(((regs.h.ah & 0x80) == 0x80) ? 1 : 0);
  173. }
  174.