home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / Profile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  4.8 KB  |  210 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : profile.cpp                                                          //
  10. //  Description: .ini file access                                                    //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define NOCRYPT
  15.  
  16. #define STRICT
  17. #include <windows.h>
  18. #include <assert.h>
  19.  
  20. #include "Profile.h"
  21.  
  22.  
  23. BOOL KProfile::SetFileName(HINSTANCE hInstance, const TCHAR *filename)
  24. {
  25.     OFSTRUCT ofs;
  26.  
  27.     if (hInstance)
  28.     {
  29.         GetModuleFileName(hInstance, sFileName, sizeof(sFileName));
  30.  
  31.         TCHAR * p = sFileName;
  32.  
  33.         while ( strchr(p, ':') || strchr(p, '\\') )
  34.             if ( strchr(p, ':') )
  35.                 p = strchr(p, ':') + 1;
  36.             else
  37.                 p = strchr(p, '\\') + 1;
  38.  
  39.         strcpy(p, filename);
  40.     }
  41.     else
  42.         strcpy(sFileName, filename);
  43.  
  44.     return OpenFile(sFileName, &ofs, OF_EXIST) != HFILE_ERROR;
  45. }
  46.  
  47.  
  48. int KProfile::ReadString(const TCHAR *section, int no)
  49. {
  50.     TCHAR key_no[10];
  51.  
  52.     wsprintf(key_no, "%d", no);
  53.  
  54.     p = sLine;
  55.  
  56.     return GetPrivateProfileString(section, key_no, NULL, sLine, sizeof(sLine), sFileName);
  57. }
  58.  
  59.  
  60. int KProfile::ReadString(const TCHAR *section, const TCHAR *key)
  61. {
  62.     p = sLine;
  63.  
  64.     sLine[0] = 0;
  65.     return GetPrivateProfileString(section, key, NULL, sLine, sizeof(sLine), sFileName);
  66. }
  67.  
  68.  
  69. int KProfile::ReadInt(const TCHAR *section, const TCHAR *key, int dflt) const
  70. {
  71.     return GetPrivateProfileInt(section, key, dflt, sFileName);
  72. }
  73.  
  74.  
  75. BOOL KProfile::Write(const TCHAR *section, const TCHAR *key, int value) const
  76. {
  77.     TCHAR temp[10];
  78.  
  79.     wsprintf(temp, "%d", value);
  80.     
  81.     return WritePrivateProfileString(section, key, temp, sFileName);
  82. }
  83.  
  84.  
  85. BOOL KProfile::Write(const TCHAR *section, const TCHAR *key, const TCHAR *value) const
  86. {
  87.     return WritePrivateProfileString(section, key, value, sFileName);
  88. }
  89.  
  90.  
  91. BOOL KProfile::ReadDelimiter(TCHAR ch)
  92. {
  93.     // skip white space 
  94.     while ( p && isspace(*p) )
  95.         p ++;
  96.  
  97.     if ( p && (*p == ch) )
  98.     {
  99.         p ++;
  100.         return TRUE;
  101.     }
  102.     else
  103.         return FALSE;
  104. }
  105.  
  106.  
  107. // read the next <nondex> <hexdigit> 
  108. unsigned KProfile::ReadHex(void)
  109. {
  110.     unsigned rslt = 0;
  111.  
  112.     if ( p )
  113.     {
  114.         while ( *p && !isxdigit(*p) )
  115.             p ++;
  116.  
  117.         while ( isxdigit(*p) )
  118.         {
  119.             if (*p<='9')
  120.                 rslt = (rslt << 4) | (*p - '0');
  121.             else
  122.                 rslt = (rslt << 4) | ( (*p - 'A') % 32 + 10 );
  123.             p++;
  124.         }
  125.     }
  126.  
  127.     return rslt;
  128. }
  129.  
  130.  
  131. // read the next decimal number
  132. unsigned KProfile::ReadDec(void)
  133. {
  134.     unsigned rslt = 0;
  135.  
  136.     if ( p )
  137.     {
  138.         while ( *p && !isdigit(*p) )
  139.             p ++;
  140.  
  141.         while ( isdigit(*p) )
  142.         {
  143.             rslt = rslt * 10 + ( *p - '0' );
  144.             p ++;
  145.         }
  146.     }
  147.  
  148.     return rslt;
  149. }
  150.  
  151.  
  152. // read the next identifier
  153. int KProfile::ReadIdentifier(TCHAR *name, int maxlength, TCHAR extra)
  154. {
  155.     int len = 0;
  156.  
  157.     if ( p )
  158.     {
  159.         // skip until first identifier character
  160.         while ( *p && ! isalpha(*p) && (*p!='_') )
  161.             p ++;
  162.  
  163.         while ( isalpha(*p) || isdigit(*p) || (*p=='_') || (*p==extra) )
  164.         {
  165.             if (name)
  166.             {
  167.                 if ( len < maxlength )
  168.                     name[len++] = *p;
  169.             }
  170.             else
  171.                 len ++;
  172.             
  173.             p ++;
  174.         }
  175.     }
  176.  
  177.     if (name)
  178.       name[len] = 0;
  179.  
  180.     return len;
  181. }
  182.  
  183.  
  184. // read the next: {03B034D1-68AF-11D1-AB-9F-F4-1A-FD-C0-00-00}
  185. BOOL KProfile::ReadGuid(GUID & guid)
  186. {
  187.     if ( ReadDelimiter('{') )
  188.     {
  189.         guid.Data1 = ReadHex();   ReadDelimiter('-');
  190.         guid.Data2 = ReadHex();   ReadDelimiter('-');
  191.         guid.Data3 = ReadHex();   ReadDelimiter('-');
  192.  
  193.         ReadDelimiter('-');
  194.         
  195.         for (int i=0; i<8; i++)
  196.         {
  197.             guid.Data4[i] = ReadHex();
  198.  
  199.             if (i<7)
  200.                 ReadDelimiter('-');
  201.         }
  202.  
  203.         return ReadDelimiter('}');
  204.     }
  205.     else
  206.         return FALSE;
  207. }
  208.  
  209.  
  210.