CIni.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Ini.h: Schnittstelle für die Klasse CIni.
  2. // Autor: Michael Schikora
  3. // Mail: schiko@schikos.de
  4. //
  5. // If you found this code useful,
  6. // please let me know
  7. //
  8. // How to use:
  9. //
  10. //
  11. //void CMyClass::UpdateFromIni(BOOL bFromIni)
  12. //{
  13. // CIni ini(m_strFileName,m_strSection);
  14. // ini.SER_GET(bFromIni,m_nValueXY);
  15. // ini.SER_GET(bFromIni,m_strValue);
  16. // ini.SER_ARR(bFromIni,m_arValue,MAX_AR);
  17. // ini.SER_ARR(bFromIni,m_ar3D,3);
  18. // //ore with default values
  19. // ini.SER_GETD(bFromIni,m_nValueXY,5);
  20. // ini.SER_GETD(bFromIni,m_strValue,"Hello");
  21. // ini.SER_ARRD(bFromIni,m_arValue,MAX_AR,10);
  22. // ini.SER_ARRD(bFromIni,m_ar3D,3,5);
  23. //}
  24. #if !defined(AFX_INI_H__EEBAF800_182A_11D3_B51F_00104B4A13B4__INCLUDED_)
  25. #define AFX_INI_H__EEBAF800_182A_11D3_B51F_00104B4A13B4__INCLUDED_
  26. #if _MSC_VER >= 1000
  27. #pragma once
  28. #endif // _MSC_VER >= 1000
  29. #define SER_GET(bGet,value) SerGet(bGet,value,#value)
  30. #define SER_ARR(bGet,value,n) SerGet(bGet,value,n,#value)
  31. #define SER_GETD(bGet,value,default) SerGet(bGet,value,#value,NULL,default)
  32. #define SER_ARRD(bGet,value,n,default) SerGet(bGet,value,n,#value,default)
  33. class CIni
  34. {
  35. public:
  36. #ifdef __NEVER_DEFINED__
  37. // MAKRO: SerGet(bGet,value,#value)
  38. int SER_GET(BOOL bGet,int value);
  39. // MAKRO: SerGet(bGet,value,n,#value)
  40. int SER_ARR(bGet,int* value,int n);
  41. #endif
  42. // If the IniFilename contains no path,
  43. // the module-directory will be add to the FileName,
  44. // to avoid storing in the windows-directory
  45. // bModulPath=TRUE: ModulDir, bModulPath=FALSE: CurrentDir
  46. static void AddModulPath(DString& strFileName,BOOL bModulPath = TRUE);
  47. static DString GetDefaultSection();
  48. static DString GetDefaultIniFile(BOOL bModulPath = TRUE);
  49. CIni( BOOL bModulPath = TRUE);
  50. CIni(CIni const& Ini, BOOL bModulPath = TRUE);
  51. CIni(DString const& strFileName, BOOL bModulPath = TRUE);
  52. CIni(DString const& strFileName, DString const& strSection, BOOL bModulPath = TRUE);
  53. virtual ~CIni();
  54. void SetFileName(DString const& strFileName);
  55. void SetSection(DString const& strSection);
  56. DString const& GetFileName() const;
  57. DString const& GetSection() const;
  58. private:
  59. void Init(LPCSTR strIniFile, LPCSTR strSection = NULL);
  60. public:
  61. DString GetString(DString strEntry, LPCSTR strDefault=NULL, LPCSTR strSection = NULL);
  62. double GetDouble(DString strEntry, double fDefault = 0.0, LPCSTR strSection = NULL);
  63. float GetFloat(DString strEntry, float fDefault = 0.0, LPCSTR strSection = NULL);
  64. int GetInt(DString strEntry, int nDefault = 0, LPCSTR strSection = NULL);
  65. DWORD GetDWORD(DString strEntry, DWORD nDefault = 0, LPCSTR strSection = NULL);
  66. BOOL GetBool(DString strEntry, BOOL bDefault = FALSE, LPCSTR strSection = NULL);
  67. void WriteString(DString strEntry,DString str, LPCSTR strSection = NULL);
  68. void WriteDouble(DString strEntry,double f, LPCSTR strSection = NULL);
  69. void WriteFloat(DString strEntry,float f, LPCSTR strSection = NULL);
  70. void WriteInt(DString strEntry,int n, LPCSTR strSection = NULL);
  71. void WriteDWORD(DString strEntry,DWORD n, LPCSTR strSection = NULL);
  72. void WriteBool(DString strEntry,BOOL b, LPCSTR strSection = NULL);
  73. void SerGetString( BOOL bGet,DString & str, DString strEntry, LPCSTR strSection = NULL, LPCSTR strDefault=NULL);
  74. void SerGetDouble( BOOL bGet,double & f, DString strEntry, LPCSTR strSection = NULL, double fDefault = 0.0);
  75. void SerGetFloat( BOOL bGet,float & f, DString strEntry, LPCSTR strSection = NULL, float fDefault = 0.0);
  76. void SerGetInt( BOOL bGet,int & n, DString strEntry, LPCSTR strSection = NULL, int nDefault = 0);
  77. void SerGetDWORD( BOOL bGet,DWORD & n, DString strEntry, LPCSTR strSection = NULL, DWORD nDefault = 0);
  78. void SerGetBool( BOOL bGet,BOOL & b, DString strEntry, LPCSTR strSection = NULL, BOOL bDefault = FALSE);
  79. void SerGet( BOOL bGet,DString & str, DString strEntry, LPCSTR strSection = NULL, LPCSTR strDefault=NULL);
  80. void SerGet( BOOL bGet,double & f, DString strEntry, LPCSTR strSection = NULL, double fDefault = 0.0);
  81. void SerGet( BOOL bGet,float & f, DString strEntry, LPCSTR strSection = NULL, float fDefault = 0.0);
  82. void SerGet( BOOL bGet,int & n, DString strEntry, LPCSTR strSection = NULL, int nDefault = 0);
  83. void SerGet( BOOL bGet,short & n, DString strEntry, LPCSTR strSection = NULL, int nDefault = 0);
  84. void SerGet( BOOL bGet,DWORD & n, DString strEntry, LPCSTR strSection = NULL, DWORD nDefault = 0);
  85. void SerGet( BOOL bGet,WORD & n, DString strEntry, LPCSTR strSection = NULL, DWORD nDefault = 0);
  86. // void SerGet( BOOL bGet,BOOL & b, DString strEntry, LPCSTR strSection = NULL, BOOL bDefault = FALSE);
  87. //ARRAYs
  88. void SerGet( BOOL bGet,DString * str, int nCount, DString strEntry, LPCSTR strSection = NULL, LPCSTR strDefault=NULL);
  89. void SerGet( BOOL bGet,double * f, int nCount, DString strEntry, LPCSTR strSection = NULL, double fDefault = 0.0);
  90. void SerGet( BOOL bGet,float * f, int nCount, DString strEntry, LPCSTR strSection = NULL, float fDefault = 0.0);
  91. void SerGet( BOOL bGet,int * n, int nCount, DString strEntry, LPCSTR strSection = NULL, int nDefault = 0);
  92. void SerGet( BOOL bGet,short * n, int nCount, DString strEntry, LPCSTR strSection = NULL, int nDefault = 0);
  93. void SerGet( BOOL bGet,DWORD * n, int nCount, DString strEntry, LPCSTR strSection = NULL, DWORD nDefault = 0);
  94. void SerGet( BOOL bGet,WORD * n, int nCount, DString strEntry, LPCSTR strSection = NULL, DWORD nDefault = 0);
  95. //MAKRO :
  96. //SERGET(bGet,value) SerGet(bGet,value,#value)
  97. private:
  98. char* GetLPCSTR(DString strEntry,LPCSTR strSection,LPCSTR strDefault);
  99. BOOL m_bModulPath; //TRUE: Filenames without path take the Modulepath
  100. //FALSE: Filenames without path take the CurrentDirectory
  101. #define MAX_INI_BUFFER 256
  102. char m_chBuffer[MAX_INI_BUFFER];
  103. DString m_strFileName;
  104. DString m_strSection;
  105. //////////////////////////////////////////////////////////////////////
  106. // statische Methoden
  107. //////////////////////////////////////////////////////////////////////
  108. public:
  109. static DString Read( DString const& strFileName, DString const& strSection, DString const& strEntry, DString const& strDefault);
  110. static void Write(DString const& strFileName, DString const& strSection, DString const& strEntry, DString const& strValue);
  111. };
  112. #endif // !defined(AFX_INI_H__EEBAF800_182A_11D3_B51F_00104B4A13B4__INCLUDED_)