home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qb / toolbox / disk2 / ctools2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-28  |  16.0 KB  |  490 lines

  1. #include <ctype.h>
  2. #include <conio.h>
  3.  
  4. #define VIDEO_START         0xb8000000
  5.  
  6. #define BLACK_ON_CYAN       48
  7. #define RED_ON_CYAN         52
  8. #define BRIGHT_WHITE_ON_RED 79
  9.  
  10. #define ENTER               13
  11. #define RIGHT_ARROW         77
  12. #define LEFT_ARROW          75
  13.  
  14. /* Definition of the QuickBASIC string descriptor structure */
  15. struct bas_str
  16.     {
  17.     int  sd_len;
  18.     char *sd_addr;
  19.     };
  20.  
  21. /***********************************************
  22. **  Name:         BitShiftLeft%               **
  23. **  Type:         Function                    **
  24. **  Module:       CTOOLS2.C                   **
  25. **  Language:     Microsoft QuickC/QuickBASIC **
  26. ************************************************
  27. *
  28. * Shifts all bits in a QuickBASIC string one bit
  29. * to the left.  The leftmost bit is returned, and
  30. * the rightmost bit is set to zero.
  31. *
  32. * EXAMPLE OF USE:  bit% = BitShiftLeft%(bit$)
  33. * PARAMETERS:      bit$       String containing a bit pattern
  34. * VARIABLES:       len        Length of the string (number of bytes)
  35. *                  str        Pointer to string contents
  36. *                  i          Looping index to each byte of the string
  37. *                  carry      Bit carried over from byte to byte
  38. *                  the_byte   Working copy of each byte of the string
  39. *
  40. * Definition of the QuickBASIC string descriptor structure
  41. *    struct bas_str
  42. *        {
  43. *        int  sd_len;
  44. *        char *sd_addr;
  45. *        };                                    */
  46.  
  47.  
  48. int bitshiftleft (basic_string)
  49. struct bas_str *basic_string;
  50.     {
  51.     int len = basic_string->sd_len;
  52.     unsigned char *str = basic_string->sd_addr;
  53.     int i, carry;
  54.     unsigned int the_byte;
  55.    
  56.     for (i=len-1, carry=0; i>=0; i--)
  57.         {
  58.         the_byte = *(str + i);
  59.         *(str + i) = (the_byte << 1) + carry;
  60.         carry = the_byte >> 7;
  61.         }
  62.    
  63.     return (carry);
  64.     }
  65.  
  66. /***********************************************
  67. **  Name:         BitShiftRight%              **
  68. **  Type:         Function                    **
  69. **  Module:       CTOOLS2.C                   **
  70. **  Language:     Microsoft QuickC/QuickBASIC **
  71. ************************************************
  72. *
  73. * Shifts all bits in a QuickBASIC string one bit to
  74. * the right.  The rightmost bit is returned, and the
  75. * leftmost bit is set to zero.
  76. *
  77. * EXAMPLE OF USE:  bit% = BitShiftRight%(bit$)
  78. * PARAMETERS:      bit$       String containing a bit pattern
  79. * VARIABLES:       len        Length of the string (number of bytes)
  80. *                  str        Pointer to string contents
  81. *                  i          Looping index to each byte of the string
  82. *                  carry      Bit carried over from byte to byte
  83. *                  the_byte   Working copy of each byte of the string
  84. *
  85. * Definition of the QuickBASIC string descriptor structure
  86. *    struct bas_str
  87. *        {
  88. *        int  sd_len;
  89. *        char *sd_addr;
  90. *        };                                             */
  91.  
  92.  
  93. int bitshiftright (basic_string)
  94. struct bas_str *basic_string;
  95.     {
  96.     int len = basic_string->sd_len;
  97.     unsigned char *str = basic_string->sd_addr;
  98.     int i, carry;
  99.     unsigned int the_byte;
  100.    
  101.     for (i=0, carry=0; i<len; i++)
  102.         {
  103.         the_byte = *(str + i);
  104.         *(str + i) = (the_byte >> 1) + carry;
  105.         carry = (the_byte & 1) << 7;
  106.         }
  107.    
  108.     if (carry)
  109.         return(1);
  110.     else
  111.         return(0);
  112.     }
  113.  
  114. /***********************************************
  115. **  Name:         MenuString%                 **
  116. **  Type:         Function                    **
  117. **  Module:       CTOOLS2.C                   **
  118. **  Language:     Microsoft QuickC/QuickBASIC **
  119. ************************************************
  120. *
  121. *  Displays a horizontal bar menu and waits for a
  122. *  response from the user.  Returns the number of
  123. *  the word selected from the string. 
  124. *
  125. * EXAMPLE OF USE:  choice% = MenuString%(row%, col%, menu$)
  126. * PARAMETERS:      row%       Row location to display the menu string
  127. *                  col%       Column location to display the menu string
  128. *                  menu$      String containing list of words representing
  129. *                             choices
  130. * VARIABLES:       len        Length of the menu string
  131. *                  str        Pointer to string contents
  132. *                  vidptr     Pointer to video memory
  133. *                  attribute  Index into string
  134. *                  character  Character from keyboard press
  135. *                  both       Combination of a character and its attribute
  136. *                  i          Looping index
  137. *                  j          Looping index
  138. *                  k          Looping index
  139. *                  c          Looping index
  140. *                  choice     Menu selection number
  141. *                  wordnum    Sequential count of each word in the menu string
  142. *                  refresh    Signals to redraw the menu string
  143. * #include <ctype.h>
  144. * #include <conio.h>
  145. * #define VIDEO_START         0xb8000000
  146. * #define BLACK_ON_CYAN       48
  147. * #define RED_ON_CYAN         52
  148. * #define BRIGHT_WHITE_ON_RED 79
  149. * #define ENTER               13
  150. * #define RIGHT_ARROW         77
  151. * #define LEFT_ARROW          75
  152. *
  153. * Definition of the QuickBASIC string descriptor structure
  154. *    struct bas_str
  155. *        {
  156. *        int  sd_len;
  157. *        char *sd_addr;
  158. *        };                                        */
  159.  
  160.  
  161. int menustring (row, col, basic_string)
  162. int *row, *col;
  163. struct bas_str *basic_string;
  164.     {
  165.     int len;
  166.     char * str;
  167.     int far * vidptr;
  168.     int attribute, character, both;
  169.     int i, j, k, c;
  170.     int choice, wordnum;
  171.     int refresh;
  172.     void packword();
  173.  
  174.     /* Initialize variables */
  175.     len = basic_string->sd_len;
  176.     str = basic_string->sd_addr;
  177.     vidptr = (int far *) VIDEO_START + (*row - 1) * 80 + (*col - 1);
  178.     choice = 1;
  179.     refresh = 1;
  180.    
  181.     /* Loop until return() statement */
  182.     while (1)
  183.         {
  184.        
  185.         /* Display the string only if refresh is non-zero */
  186.         if (refresh)
  187.             {
  188.             refresh = 0;
  189.            
  190.             /* Loop through each character of the string */
  191.             for (wordnum = 0, i=0; i<len; i++)
  192.                 {
  193.                
  194.                 /* Set the character and default attribute */
  195.                 character = str[i];
  196.                 attribute = BLACK_ON_CYAN;
  197.                
  198.                 /* Uppercase? */
  199.                 if (isupper(character))
  200.                     {
  201.                     wordnum++;
  202.                     attribute = RED_ON_CYAN;
  203.                     }
  204.                
  205.                 /* In the middle of the current selection? */
  206.                 if (wordnum == choice && character != ' ')
  207.                     attribute = BRIGHT_WHITE_ON_RED;
  208.                
  209.                 /* Move data to video */
  210.                 packword(&both, &attribute, &character);
  211.                 vidptr[i] = both;
  212.                 }
  213.             }
  214.        
  215.         /* Check for any key presses */
  216.         if (kbhit())
  217.             {
  218.            
  219.             /* Get the key code and process it */
  220.             switch (c = getch())
  221.                 {
  222.                
  223.                 /* Return the choice when Enter is pressed */
  224.                 case ENTER:
  225.                     return (choice);
  226.                
  227.                 /* Highlight next choice if Right arrow is pressed */
  228.                 case RIGHT_ARROW:
  229.                     if (choice < wordnum)
  230.                         {
  231.                         choice++;
  232.                         refresh = 1;
  233.                         }
  234.                     break;
  235.                
  236.                 /* Highlight previous choice if Left arrow is pressed */
  237.                 case LEFT_ARROW:
  238.                     if (choice > 1)
  239.                         {
  240.                         choice--;
  241.                         refresh = 1;
  242.                         }
  243.                     break;
  244.                
  245.                 /* Check for match on first character of each word */
  246.                 default:
  247.                     c = _toupper(c);
  248.                     for (k=0, j=0; j<len; j++)
  249.                         {
  250.                        
  251.                         /* Each choice starts at an uppercase char */
  252.                         if (isupper(str[j]))
  253.                             k++;
  254.                    
  255.                         /* Match if same char and not current choice */
  256.                         if (str[j] == c && k != choice)
  257.                             {
  258.                             choice = k;
  259.                             refresh = 1;
  260.                             break;
  261.                             }
  262.                         }
  263.                     break;
  264.                 }
  265.             }
  266.         }
  267.     }
  268.  
  269. /***********************************************
  270. **  Name:         NumberOfBits&               **
  271. **  Type:         Function                    **
  272. **  Module:       CTOOLS2.C                   **
  273. **  Language:     Microsoft QuickC/QuickBASIC **
  274. ************************************************
  275. *
  276. * Counts the 1 bits in a QuickBASIC string.
  277. *
  278. * EXAMPLE OF USE:  count& = NumberOfBits&(a$)
  279. * PARAMETERS:      a$         String containing bits to be counted
  280. * VARIABLES:       len        Length of the string
  281. *                  str        Pointer to string contents
  282. *                  i          Looping index to each byte
  283. *                  the_byte   Working copy of each byte of the string
  284. *                  count      Count of the bits
  285. *
  286. * Definition of the QuickBASIC string descriptor structure
  287. *    struct bas_str
  288. *        {
  289. *        int  sd_len;
  290. *        char *sd_addr;
  291. *        };                                           */
  292.  
  293.  
  294. long numberofbits (basic_string)
  295. struct bas_str *basic_string;
  296.     {
  297.     int len = basic_string->sd_len;
  298.     unsigned char *str = basic_string->sd_addr;
  299.     int i,the_byte;
  300.     long count = 0;
  301.  
  302.     for (i=0; i<len; i++)
  303.         {
  304.         the_byte = *(str+i);
  305.         while (the_byte)
  306.             {
  307.             count += (the_byte & 1);
  308.             the_byte >>= 1;
  309.             }
  310.         }
  311.     return (count);
  312.     }
  313.  
  314. /***********************************************
  315. **  Name:         PackWord                    **
  316. **  Type:         Subprogram                  **
  317. **  Module:       CTOOLS2.C                   **
  318. **  Language:     Microsoft QuickC/QuickBASIC **
  319. ************************************************
  320. *
  321. *  Packs two byte values into the high and low
  322. *  bytes of an integer (word).
  323. *
  324. * EXAMPLE OF USE:  PackWord hiloword%, hibyte%, lobyte%
  325. * PARAMETERS:      hiloword%  Integer word to pack the two bytes into
  326. *                  hibyte%    Integer value of the most significant byte
  327. *                  lobyte%    Integer value of the least significant byte
  328. * VARIABLES:       both       A union of a two-byte structure and an integer *  
  329. *                             variable                                */
  330.  
  331.  
  332. void packword (hiloword, hibyte, lobyte)
  333. int *hiloword, *hibyte, *lobyte;
  334.     {
  335.     union
  336.         {
  337.         struct
  338.             {
  339.             unsigned char lo;
  340.             unsigned char hi;
  341.             } bytes;
  342.         int hilo;
  343.         } both;
  344.    
  345.     both.bytes.hi = *hibyte;
  346.     both.bytes.lo = *lobyte;
  347.     *hiloword = both.hilo;
  348.     }
  349.  
  350. /***********************************************
  351. **  Name:         TextGet                     **
  352. **  Type:         Subprogram                  **
  353. **  Module:       CTOOLS2.C                   **
  354. **  Language:     Microsoft QuickC/QuickBASIC **
  355. ************************************************
  356. *
  357. * Saves characters and attributes from a rectangular
  358. * area of the screen.
  359. *
  360. * EXAMPLE OF USE:  TextGet r1%, c1%, r2%, c2%, a$
  361. * PARAMETERS:      r1%        Pointer to row at upper left corner
  362. *                  c1%        Pointer to column at upper left corner
  363. *                  r2%        Pointer to row at lower right corner
  364. *                  c2%        Pointer to column at lower right corner
  365. *                  a$         String descriptor, where screen contents
  366. *                             will be stored
  367. * VARIABLES:       len        Length of string
  368. *                  str        Pointer to string contents
  369. *                  video      Pointer to video memory
  370. *                  i          Index into string
  371. *                  row        Looping index
  372. *                  col        Looping index
  373. * #define VIDEO_START         0xb8000000
  374. *
  375. * Definition of the QuickBASIC string descriptor structure
  376. *
  377. *  struct bas_str
  378. *      {
  379. *      int  sd_len;
  380. *      char *sd_addr;
  381. *      };                                           */
  382.  
  383.  
  384. void textget (r1,c1,r2,c2,basic_string)
  385. int *r1,*c1,*r2,*c2;
  386. struct bas_str *basic_string;
  387.     {
  388.     int len;
  389.     int * str;
  390.     int far * video;
  391.     int i,row,col;
  392.    
  393.     len = basic_string->sd_len;
  394.     str = (int *) basic_string->sd_addr;
  395.     video = (int far *) VIDEO_START;
  396.    
  397.     if (len == (*r2 - *r1 + 1) * (*c2 - *c1 + 1) * 2)
  398.         for (row = *r1 - 1, i = 0; row < *r2; row++)
  399.             for (col = *c1 - 1; col < *c2; col++)
  400.                 str[i++] = video[row * 80 + col];
  401.     }
  402.  
  403. /***********************************************
  404. **  Name:         TextPut                     **
  405. **  Type:         Subprogram                  **
  406. **  Module:       CTOOLS2.C                   **
  407. **  Language:     Microsoft QuickC/QuickBASIC **
  408. ************************************************
  409. *
  410. * Restores characters and attributes to a rectangular
  411. * area of the screen.
  412. *
  413. * EXAMPLE OF USE:  TextPut r1%, c1%, r2%, c2%, a$
  414. * PARAMETERS:      r1%        Pointer to row at upper left corner
  415. *                  c1%        Pointer to column at upper left corner
  416. *                  r2%        Pointer to row at lower right corner
  417. *                  c2%        Pointer to column at lower right corner
  418. *                  a$         String descriptor where screen contents are stored
  419. * VARIABLES:       len        Length of string
  420. *                  str        Pointer to string contents
  421. *                  video      Pointer to video memory
  422. *                  i          Index into string
  423. *                  row        Looping index
  424. *                  col        Looping index
  425. * #define VIDEO_START         0xb8000000
  426. *
  427. * Definition of the QuickBASIC string descriptor structure
  428. *    struct bas_str
  429. *        {
  430. *        int  sd_len;
  431. *        char *sd_addr;
  432. *        };                                        */
  433.  
  434.  
  435. void textput (r1,c1,r2,c2,basic_string)
  436. int *r1,*c1,*r2,*c2;
  437. struct bas_str *basic_string;
  438.     {
  439.     int len;
  440.     int * str;
  441.     int far * video;
  442.     int i,row,col;
  443.    
  444.     len = basic_string->sd_len;
  445.     str = (int *) basic_string->sd_addr;
  446.     video = (int far *) VIDEO_START;
  447.    
  448.     if (len == (*r2 - *r1 + 1) * (*c2 - *c1 + 1) * 2)
  449.         for (row = *r1 - 1, i = 0; row < *r2; row++)
  450.             for (col = *c1 - 1; col < *c2; col++)
  451.                 video[row * 80 + col] = str[i++];
  452.     }
  453.  
  454. /***********************************************
  455. **  Name:         UnPackWord                  **
  456. **  Type:         Subprogram                  **
  457. **  Module:       CTOOLS2.C                   **
  458. **  Language:     Microsoft QuickC/QuickBASIC **
  459. ************************************************
  460. *
  461. *  Unpacks two byte values from the high and low
  462. *  bytes of an integer (word).
  463. *
  464. * EXAMPLE OF USE:  UnPackWord hiloword%, hibyte%, lobyte%
  465. * PARAMETERS:      hiloword%  Integer word containing the two bytes
  466. *                  hibyte%    Integer value of the most significant byte
  467. *                  lobyte%    Integer value of the least significant byte
  468. * VARIABLES:       both       A union of a two-byte structure and an integer
  469. *                             variable                               */
  470.  
  471.  
  472. void unpackword (hiloword, hibyte, lobyte)
  473. int *hiloword, *hibyte, *lobyte;
  474.     {
  475.     union
  476.         {
  477.         struct
  478.             {
  479.             unsigned char lo;
  480.             unsigned char hi;
  481.             } bytes;
  482.         int hilo;
  483.         } both;
  484.    
  485.     both.hilo = *hiloword;
  486.     *hibyte = both.bytes.hi;
  487.     *lobyte = both.bytes.lo;
  488.     }
  489.  
  490.