GIniFile.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /** *************************************************************** **
  2. ** Implementation of the GIniFile class. **
  3. ** by S.B. **
  4. ** *************************************************************** **/
  5. #include "stdafx.h"
  6. #include "GIniFile.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. void GIniFile::SetFileName( const TCHAR *AFileName )
  11. {
  12. _tcscpy( rFName, AFileName );
  13. }
  14. void GIniFile::SetSectName( const TCHAR *ASectName )
  15. {
  16. _tcscpy( rSName, ASectName );
  17. }
  18. #define mc_GINIFILE_CheckDeflt()\
  19. const TCHAR *qDeflt;\
  20. if (ADeflt) qDeflt = ADeflt;\
  21. else qDeflt = AKeyName;
  22. int GIniFile::GetIniStr( const TCHAR *AKeyName, TCHAR *AResult, const TCHAR *ADeflt )
  23. {
  24. int qOk = GetPrivateProfileString( rSName, AKeyName, _T(""), AResult, 512, rFName );
  25. if (qOk) {
  26. rLastResult = TRUE;
  27. if (AResult[0]==_T('\\')) { // it allows to specify empty lines using back slash
  28. _tcscpy( AResult, &(AResult[1]) );
  29. qOk--;
  30. };
  31. }
  32. else {
  33. mc_GINIFILE_CheckDeflt();
  34. _tcscpy( AResult, qDeflt );
  35. rLastResult = FALSE;
  36. qOk = (int)_tcslen( ADeflt );
  37. };
  38. return qOk;
  39. }
  40. int GIniFile::SGetIniStr( const TCHAR *AKeyName, TCHAR *AResult, const TCHAR *ADeflt )
  41. {
  42. int qOk;
  43. mc_GINIFILE_CheckDeflt();
  44. qOk = GetIniStr( AKeyName, AResult, qDeflt );
  45. if (!rLastResult) SetIniStr( AKeyName, qDeflt );
  46. return qOk;
  47. }
  48. BOOL GIniFile::SetIniStr( const TCHAR *AKeyName, const TCHAR *AStr )
  49. {
  50. if (!AStr || !AStr[0]) AStr = _T("\\");
  51. return WritePrivateProfileString( rSName, AKeyName, AStr, rFName );
  52. }
  53. long GIniFile::GetIniInt( const TCHAR *AKeyName, long ADeflt )
  54. {
  55. TCHAR qS[32], qSD[32];
  56. _ltot( ADeflt, qSD, 0xA );
  57. GetIniStr( AKeyName, qS, qSD );
  58. if (!rLastResult) return ADeflt;
  59. else return _tcstol( qS, NULL, 0xA );
  60. }
  61. long GIniFile::SGetIniInt( const TCHAR *AKeyName, long ADeflt )
  62. {
  63. long qRes = GetIniInt( AKeyName, ADeflt );
  64. if (!rLastResult)
  65. SetIniInt( AKeyName, ADeflt );
  66. return qRes;
  67. }
  68. BOOL GIniFile::SetIniInt( const TCHAR *AKeyName, long AVal )
  69. {
  70. TCHAR qS[32];
  71. _stprintf( qS, _T("%d"), AVal );
  72. return SetIniStr( AKeyName, qS );
  73. }
  74. double GIniFile::GetIniFloat( const TCHAR *AKeyName, double ADeflt )
  75. {
  76. TCHAR qS[48], qSD[48];
  77. _stprintf( qSD, _T("%.4f"), ADeflt );
  78. GetIniStr( AKeyName, qS, qSD );
  79. if (!rLastResult) return ADeflt;
  80. else return _tcstod( qS, NULL );
  81. }
  82. double GIniFile::SGetIniFloat( const TCHAR *AKeyName, double ADeflt )
  83. {
  84. double qRes = GetIniFloat( AKeyName, ADeflt );
  85. if (!rLastResult)
  86. SetIniFloat( AKeyName, ADeflt );
  87. return qRes;
  88. }
  89. BOOL GIniFile::SetIniFloat( const TCHAR *AKeyName, double AVal )
  90. {
  91. TCHAR qS[32];
  92. _stprintf( qS, _T("%f"), AVal );
  93. return SetIniStr( AKeyName, qS );
  94. }