home *** CD-ROM | disk | FTP | other *** search
/ The Education Master 1994 (4th Edition) / EDUCATIONS_MASTER_4TH_EDITION.bin / files / progmisc / qparser2 / lib / respf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-31  |  3.1 KB  |  139 lines

  1. /*  respf.c
  2.  
  3.     QCAD Systems, Inc. 1990
  4.  
  5.     Provides single-character response to a query.
  6.  
  7.     This works well under Microsoft's C, Turbo C++, and only so-so 
  8.       under Unix.  The Unix implementation requires typing the character
  9.       then a RETURN.
  10.  
  11.     NOTE: the F1 key for printing a screen only functions under Microsoft C
  12.       and Turbo.
  13.  
  14.     */
  15.  
  16. #include <stdio.h>
  17. #include "respf.h"
  18.  
  19. #define SCRSIZE  24*80*2
  20. #define MONOCHROME 7
  21. #define PFFILE  "screen.txt"
  22. #define F1KEY   59
  23.  
  24. #ifdef MSDOS
  25. /* Microsoft C 5.XX */
  26. #include <malloc.h>
  27. #include <memory.h>
  28. #include <dos.h>
  29.  
  30. /* ............. */
  31. static void pf(filename)
  32.   char *filename;
  33. { int far *membuf;
  34.   int row, column;
  35.   char line[82];
  36.   FILE *outfile;
  37.  
  38.   if ((outfile= fopen(filename, "a"))==NULL) {
  39.     fprintf(stderr, "E %s", filename);
  40.     return;
  41.     }
  42.   fprintf(outfile, "=== NEW SCREEN ===\n");
  43.   membuf= (int far *) _fmalloc(SCRSIZE);
  44.   movedata(0x40, 0x49, FP_SEG(membuf), FP_OFF(membuf), 2);
  45.   if ((*membuf & 0xFF)==MONOCHROME)
  46.     movedata(0xB000, 0, FP_SEG(membuf), FP_OFF(membuf), SCRSIZE);
  47.   else movedata(0xB800, 0, FP_SEG(membuf), FP_OFF(membuf), SCRSIZE);
  48.   for (row= 0; row < 24*80; row += 80) {
  49.     for (column= 0; column < 80; column++)
  50.       line[column]= (char) (membuf[row + column] & 0x7F);
  51.     while (column>0 && line[column-1]==' ') column--;
  52.     line[column]= '\0';
  53.     fprintf(outfile, "%s\n", line);
  54.     }
  55.   fclose(outfile);
  56.   }
  57. #endif
  58.  
  59. #ifdef TCC
  60. /* Turbo C++ */
  61. #include <conio.h>
  62. #include <alloc.h>
  63.  
  64. /* ............. */
  65. static void pf(filename)
  66.   char *filename;
  67. { int row, column, last_col;
  68.   char line[2*82];
  69.   FILE *outfile;
  70.  
  71.   if ((outfile= fopen(filename, "a"))==NULL) {
  72.     fprintf(stderr, "E %s", filename);
  73.     return;
  74.     }
  75.   fprintf(outfile, "=== NEW SCREEN ===\n");
  76.   for (row= 1; row <= 24; row++) {
  77.     gettext(1, row, 80, row, line);
  78.     last_col= 2*80;
  79.     while (last_col > 0 && (line[last_col-2] & 0x7F) ==' ') 
  80.       last_col -= 2;
  81.     for (column= 0; column < last_col; column += 2)
  82.       fputc((char)(line[column] & 0x7F), outfile);
  83.     fputc('\n', outfile);
  84.     }
  85.   fclose(outfile);
  86.   }
  87. #endif
  88.  
  89. /* .................... */
  90. char resp(msg)
  91.   char *msg;
  92. { char line[80];
  93.   int more= 1;
  94.  
  95.   if (*msg) {
  96.     printf("%s ", msg);
  97.     fflush(stdout);
  98.     }
  99. #ifdef UNIX
  100.   read(0, line, 5);
  101.   if (*line=='\0') *line= '\n';
  102.   /* NOTE: no F1 key screen supported under Unix */
  103. #else    /* Microsoft or Turbo on PC */
  104.   while (more) {
  105.     *line= getch();
  106.     more= 0;
  107.     if (*line=='\r') *line= '\n';
  108.     else if (*line==('C' & 0x1F)) exit(0);   /* control-C */
  109.     else if (*line == '\0') {
  110.       *line= getch();
  111.       if (*line == F1KEY) pf(PFFILE);
  112.       more= 1;  /* keep going */
  113.       }
  114.     }
  115.   printf("%c\n", *line);
  116. #endif
  117.   return *line;
  118.   }
  119.  
  120. /* ................... */
  121. int yesresp(msg)
  122.   char *msg;
  123. { char yresp= resp(msg);
  124.  
  125.   return (yresp=='y' || yresp=='Y');
  126.   }
  127.  
  128. #ifdef TEST
  129. void main()
  130. { char ch;
  131.  
  132.   while (1) {
  133.     ch= resp("Type a character: ");
  134.     if (ch=='0') break;
  135.     }
  136.   exit(0);
  137.   }
  138. #endif
  139.