home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / mc / mcutil.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  5KB  |  258 lines

  1. /*++
  2.  
  3. Copyright (c) 1991-1997 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     mcutil.c
  8.  
  9. Abstract:
  10.  
  11.     This file contains utility functions for the Win32 Message Compiler (MC)
  12.  
  13. --*/
  14.  
  15. #include "mc.h"
  16.  
  17.  
  18. /*++
  19.  
  20. Routine Description:
  21.  
  22.     This routine simply writes/overwrites Name, ID and Value
  23.     to a link list of NAME_INFO structures.
  24.  
  25. Arguments:
  26.  
  27.     PNAME_INFO
  28.     Keyword such as "Facility", "Serverity", ...
  29.     Lauguage Identifier; a combination of a Primary Language ID,
  30.     Sublanguage ID pair such as LANG_NEUTRAL, SUBLANG_NEUTRAL
  31.     An option value specfic to the keyword.
  32.  
  33. Return Value:
  34.  
  35.     The list PNAME_INFO
  36.  
  37. --*/
  38.  
  39. PNAME_INFO
  40. McAddName(
  41.     PNAME_INFO *NameListHead,
  42.     char *Name,
  43.     ULONG Id,
  44.     PVOID Value
  45.     )
  46. {
  47.     PNAME_INFO p;
  48.     int n;
  49.  
  50.     while (p = *NameListHead) {
  51.         if (!(n = stricmp( p->Name, Name ))) {
  52.             if (p->Id != Id) {
  53.                 McInputError( "Redefining value of %s", FALSE, Name );
  54.                 }
  55.  
  56.             p->Id = Id;
  57.             p->Value = Value;
  58.             p->Used = FALSE;
  59.             return( p );
  60.             }
  61.         else
  62.         if (n < 0) {
  63.             break;
  64.             }
  65.  
  66.         NameListHead = &p->Next;
  67.         }
  68.  
  69.     p = malloc( sizeof( *p ) + strlen( Name ) );
  70.     p->LastId = 0;
  71.     p->Id = Id;
  72.     p->Value = Value;
  73.     p->Used = FALSE;
  74.     strcpy( p->Name, Name );
  75.     p->Next = *NameListHead;
  76.     *NameListHead = p;
  77.     return( p );
  78. }
  79.  
  80.  
  81.  
  82. /*++
  83.  
  84. Routine Description:
  85.  
  86.     This routine returns the NAME_INFO structure cooresponding to the
  87.     given Keyword.
  88.  
  89. Arguments:
  90.  
  91.     PNAME_INFO
  92.     Keyword such as "Facility", "Serverity", ...
  93.  
  94. Return Value:
  95.  
  96.     Either the NAME_INFO structure cooresponding to the keyname or NULL
  97.  
  98. --*/
  99.  
  100.  
  101. PNAME_INFO
  102. McFindName(
  103.     PNAME_INFO NameListHead,
  104.     char *Name
  105.     )
  106. {
  107.     PNAME_INFO p;
  108.  
  109.     p = NameListHead;
  110.     while (p) {
  111.         if (!stricmp( p->Name, Name )) {
  112.             p->Used = TRUE;
  113.             break;
  114.             }
  115.  
  116.         p = p->Next;
  117.         }
  118.  
  119.     return( p );
  120. }
  121.  
  122.  
  123.  
  124. /*++
  125.  
  126. Routine Description:
  127.  
  128.     This converts a string value to the corresponding integer value
  129.     in the specified base.
  130.  
  131. Arguments:
  132.  
  133.     String image of an integer
  134.     Base of resultant integer
  135.     Pointer to a unsigned long
  136.  
  137.  
  138. Return Value:
  139.  
  140.     A unsigned long
  141.  
  142. --*/
  143.  
  144. BOOLEAN
  145. McCharToInteger(
  146.     PCHAR String,
  147.     int Base,
  148.     PULONG Value
  149.     )
  150. {
  151.     CHAR c;
  152.     ULONG Result, Digit, Shift;
  153.  
  154.  
  155.     c = *String;
  156.     String = CharNext(String);
  157.     if (!Base) {
  158.         Base = 10;
  159.         Shift = 0;
  160.         if (c == '0') {
  161.             c = *String++;
  162.             if (c == 'x') {
  163.                 Base = 16;
  164.                 Shift = 4;
  165.                 }
  166.             else
  167.             if (c == 'o') {
  168.                 Base = 8;
  169.                 Shift = 3;
  170.                 }
  171.             else
  172.             if (c == 'b') {
  173.                 Base = 2;
  174.                 Shift = 1;
  175.                 }
  176.             else {
  177.                 String--;
  178.                 }
  179.  
  180.             c = *String++;
  181.             }
  182.         }
  183.     else {
  184.         switch( Base ) {
  185.             case 16:    Shift = 4;  break;
  186.             case  8:    Shift = 3;  break;
  187.             case  2:    Shift = 1;  break;
  188.             case 10:    Shift = 0;  break;
  189.             default:    return( FALSE );
  190.             }
  191.         }
  192.  
  193.     Result = 0;
  194.     while (c) {
  195.         if (c >= '0' && c <= '9') {
  196.             Digit = c - '0';
  197.             }
  198.         else
  199.         if (c >= 'A' && c <= 'F') {
  200.             Digit = c - 'A' + 10;
  201.             }
  202.         else
  203.         if (c >= 'a' && c <= 'f') {
  204.             Digit = c - 'a' + 10;
  205.             }
  206.         else {
  207.             break;
  208.             }
  209.  
  210.         if ((int)Digit >= Base) {
  211.             break;
  212.             }
  213.  
  214.         if (Shift == 0) {
  215.             Result = (Base * Result) + Digit;
  216.             }
  217.         else {
  218.             Result = (Result << Shift) | Digit;
  219.             }
  220.  
  221.  
  222.         c = *String;
  223.         String = CharNext(String);
  224.     }
  225.  
  226.     *Value = Result;
  227.     return( TRUE );
  228. }
  229.  
  230. /*++
  231.  
  232. Routine Description:
  233.  
  234.     Duplicates a string
  235.  
  236. Arguments:
  237.  
  238.     A string value
  239.  
  240. Return Value:
  241.  
  242.     The duplicated string value
  243.  
  244. --*/
  245.  
  246.  
  247. char *
  248. McMakeString(
  249.     char *String
  250.     )
  251. {
  252.     char *s;
  253.  
  254.     s = malloc( strlen( String ) + 1 );
  255.     strcpy( s, String );
  256.     return( s );
  257. }
  258.