home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap14 / attrib.c next >
Encoding:
C/C++ Source or Header  |  1988-04-07  |  2.8 KB  |  94 lines

  1. /* attrib.c -- this program illustrates attributes */
  2. /* program list: attrib.c, scrfun.c                */
  3. /* user include files: scrn.h                      */
  4. /* Note: activate Screen Swapping On in Debug menu */
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include "scrn.h"
  8. #define PAGE 0
  9. #define ESC '\033'
  10. char *Format = "This message is displayed using an "
  11.                "attribute value of %2X hex (%s).";
  12. int Get_attrib(char *);
  13. void Print_attr(char *, unsigned char, unsigned char);
  14.  
  15. main()
  16. {
  17.  
  18.     int attribute;       /* value of attribute */
  19.     char attr_str[9];    /* attr. in string form */
  20.     char mesg[80];
  21.  
  22.     Clearscr();
  23.     Home();
  24.     printf("Enter an attribute as an 8-digit binary "
  25.            "number, such as 00000111, and see a\n"
  26.            "message displayed using that attribute."
  27.            "Hit <Esc> to quit.\n"
  28.            "Attribute = ");
  29.     while ((attribute = Get_attrib(attr_str)) != -1)
  30.         {
  31.         Setcurs(10,0,PAGE);
  32.         sprintf(mesg, Format, attribute, attr_str);
  33.         Print_attr(mesg, attribute, PAGE);
  34.         Setcurs(2, 12, PAGE);
  35.         printf("         ");  /* clear old display */
  36.         Setcurs(2, 12, PAGE);
  37.         }
  38.     Clearscr();
  39. }
  40.  
  41. /* The following function reads in a binary number    */
  42. /* as a sequence of 1s and 0s. It places the 1 and 0  */
  43. /* characters in a string whose address is passed as  */
  44. /* an argument. It returns the numeric value of the   */
  45. /* binary number. Bad input is summarily rejected.    */
  46. /* The function returns -1 when you press Esc.        */
  47.  
  48. int Get_attrib(a_str)
  49. char a_str[];     /* attribute as binary string */
  50. {
  51.     int attrib[8];
  52.     int index = 7;
  53.     int ch;
  54.     int attribute = 0; /* attrib. as numeric value */
  55.     int pow;
  56.  
  57.     a_str[8] = '\0';  /* terminate string */
  58.     while ((index >= 0) &&  (ch = getch()) != ESC)
  59.         {
  60.         if (ch != '0' && ch != '1')  /* bad input */
  61.             putch('\a');
  62.         else
  63.             {
  64.             putch(ch);
  65.             a_str[index] = ch;      /* string form */
  66.             attrib[index--] = ch - '0'; /* numeric */
  67.             }
  68.         }
  69.     if (ch == ESC)
  70.         return (-1);
  71.     else            /* convert numeric array to a number */
  72.         {
  73.         for(index = 0, pow = 1; index < 8;
  74.                                   index++, pow *= 2)
  75.             attribute += attrib[index] * pow;
  76.         return attribute;
  77.         }
  78. }
  79.  
  80. /* The following function prints the string str using */
  81. /* attribute attr on the indicated page.              */
  82. /* It uses functions from the scrfun.c file.          */
  83.  
  84. void Print_attr(str, attr, page)
  85. char *str;
  86. unsigned char attr, page;
  87. {
  88.     while (*str != '\0')
  89.         {
  90.         Write_ch_atr(*str++, attr , page, 1);
  91.         Cursrt();
  92.         }
  93. }
  94.