home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / sharewar / vecad / source / Config.cpp next >
C/C++ Source or Header  |  2000-06-23  |  5KB  |  221 lines

  1. /********************************************************************
  2. * Project: VeCAD ver.5.1
  3. * Copyright (C) 1999-2000 by Oleg Kolbaskin.
  4. * All rights reserved.
  5. *
  6. * Application Configuraion
  7. ********************************************************************/
  8. #include <windows.h>
  9. #include <tchar.h>
  10. #include "config.h"
  11.  
  12. const CConfig* __pCfg = NULL;
  13.  
  14. int _cdecl CmpCfgItem (const void* e1, const void* e2);
  15.  
  16. //-------------------------------------
  17. CConfigItem::CConfigItem ()
  18. {
  19.   szKey = NULL;
  20.   szValue = NULL;
  21. }
  22.  
  23.  
  24. //-------------------------------------
  25. CConfigItem::~CConfigItem ()
  26. {
  27.   delete[] szKey;
  28.   delete[] szValue;
  29.   szKey = NULL;
  30.   szValue = NULL;
  31. }
  32.  
  33.  
  34. //-------------------------------------
  35. void CConfigItem::Set (LPCTSTR _szKey, LPCTSTR _szValue)
  36. {
  37.   int len;
  38.  
  39.   len = _tcslen( _szKey );
  40.   delete[] szKey;
  41.   szKey = new TCHAR[len+1];
  42.   ::ZeroMemory( szKey, len+1 );
  43.   _tcscpy( szKey, _szKey );
  44.  
  45.   len = _tcslen( _szValue );
  46.   delete[] szValue;
  47.   szValue = new TCHAR[len+1];
  48.   ::ZeroMemory( szValue, len+1 );
  49.   _tcscpy( szValue, _szValue );
  50. }
  51.  
  52.  
  53. //-------------------------------------
  54. void CConfigItem::Get (LPTSTR _szKey, LPTSTR _szValue) const
  55. {
  56.   _tcscpy( _szKey, szKey );
  57.   _tcscpy( _szValue, szValue );
  58. }
  59.  
  60.  
  61. //-------------------------------------
  62. bool CConfigItem::GetValue (LPCTSTR _szKey, LPTSTR _szValue) const
  63. {
  64.   if (szKey && szValue){
  65.     if (_tcsicmp( szKey, _szKey )==0){
  66.       _tcscpy( _szValue, szValue );
  67.       return true;
  68.     }
  69.   }
  70.   _szValue[0] = 0;
  71.   return false;
  72. }
  73.  
  74.  
  75. //-------------------------------------
  76. bool CConfigItem::SetValue (LPCTSTR _szKey, LPCTSTR _szValue)
  77. {
  78.   if (szKey && szValue){
  79.     if (_tcsicmp( szKey, _szKey )==0){
  80.       int len = _tcslen( _szValue );
  81.       delete[] szValue;
  82.       szValue = new TCHAR[len+1];
  83.       ::ZeroMemory( szValue, len+1 );
  84.       _tcscpy( szValue, _szValue );
  85.       return true;
  86.     }
  87.   }
  88.   return false;
  89. }
  90.  
  91.  
  92. //-------------------------------------
  93. bool CConfigItem::Write (FILE* df)
  94. {
  95.   if (szKey && szValue){
  96.     _ftprintf( df, _T("%s=%s\n"), szKey, szValue ); 
  97.     return true;
  98.   }
  99.   return false;
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. //*******************************************************************
  109. CConfig::CConfig ()
  110. {
  111.   ::ZeroMemory( szFileName, sizeof(szFileName) );
  112.   n_item = 0;
  113.   for (int i=0; i<MAX_CFGITEM; i++){
  114.     ISort[i] = i;
  115.   }
  116. }
  117.  
  118.  
  119. //-------------------------------------
  120. bool CConfig::Open  (LPCTSTR szFName)
  121. {
  122.   TCHAR  szBuf[320];
  123.   TCHAR* pd;
  124.   _TINT divider = '=';
  125.   _TINT endstr = '\n';
  126.   _tcscpy( szFileName, szFName);
  127.   n_item = 0;
  128.   FILE* df = _tfopen( szFileName, _T("rt") );
  129.   if (df){
  130.     while (1){
  131.       szBuf[0] = 0;
  132.       if (_fgetts( szBuf, 310, df )==NULL){
  133.         break;
  134.       }
  135.       pd = _tcschr( szBuf, endstr );
  136.       pd[0] = 0;
  137.       pd = _tcschr( szBuf, divider );
  138.       if (pd){
  139.         pd[0] = 0;
  140.         Item[n_item].Set( szBuf, pd+1 );
  141.         n_item++;
  142.         if (n_item>=MAX_CFGITEM){
  143.           break;
  144.         }
  145.       }
  146.     }
  147.     fclose( df );
  148.     return true;
  149.   }   
  150.   return false;
  151. }
  152.  
  153.  
  154. //-------------------------------------
  155. bool CConfig::Close ()
  156. {
  157.   FILE* df = _tfopen( szFileName, _T("wt") );
  158.   if (df){
  159.     __pCfg = this;
  160.     qsort( ISort, n_item, sizeof(int), CmpCfgItem );
  161.     __pCfg = NULL;
  162.     int i,j;
  163.     for (i=0; i<n_item; i++){
  164.       j = ISort[i];
  165.       Item[j].Write( df );
  166.     }
  167.     fclose( df );
  168.     return true;
  169.   }
  170.   return false;
  171. }
  172.  
  173.  
  174. //-------------------------------------
  175. bool CConfig::GetValue (LPCTSTR szKey, LPTSTR szValue) const
  176. {
  177.   for (int i=0; i<n_item; i++){
  178.     if (Item[i].GetValue( szKey, szValue )){
  179.       return true;
  180.     }
  181.   }
  182.   return false;  // key not found
  183. }
  184.  
  185.  
  186. //-------------------------------------
  187. bool CConfig::SetValue (LPCTSTR szKey, LPCTSTR szValue)
  188. {
  189.   int i;
  190.   for (i=0; i<n_item; i++){
  191.     if (Item[i].SetValue( szKey, szValue )){
  192.       return true;
  193.     }
  194.   }
  195.   // key not found, create it
  196.   if (n_item<MAX_CFGITEM){
  197.     i = n_item++;
  198.     Item[i].Set( szKey, szValue );
  199.     return true;  // added new key
  200.   }
  201.   return false; 
  202. }
  203.  
  204.  
  205. //-------------------------------------
  206. int CConfig::CmpItems (int i1, int i2) const
  207. {
  208.   return _tcsicmp( Item[i1].GetKey(), Item[i2].GetKey() );
  209. }
  210.  
  211.  
  212.  
  213. //***********************************************
  214. int _cdecl CmpCfgItem (const void* e1, const void* e2)
  215. {
  216.   int i1 = *(int*)e1;
  217.   int i2 = *(int*)e2;
  218.   return __pCfg->CmpItems( i1, i2 );
  219. }
  220.    
  221.