config.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <memory.h>
  6. #include "config.h"
  7. C_Config::~C_Config()
  8. {
  9. free(m_inifile);
  10. free(m_section);
  11. }
  12. C_Config::C_Config(wchar_t *ini, wchar_t *section, C_Config * globalWrite) : globalWrite(globalWrite)
  13. {
  14. m_strbuf[0]=0;
  15. m_inifile=_wcsdup(ini);
  16. m_section=_wcsdup(section);
  17. }
  18. void C_Config::WriteInt(wchar_t *name, int value, wchar_t *section)
  19. {
  20. wchar_t buf[32] = {0};
  21. wsprintf(buf,L"%d",value);
  22. WriteString(name,buf,section);
  23. }
  24. int C_Config::ReadInt(wchar_t *name, int defvalue, wchar_t *section)
  25. {
  26. return GetPrivateProfileInt(section?section:m_section,name,defvalue,m_inifile);
  27. }
  28. wchar_t *C_Config::WriteString(wchar_t *name, wchar_t *string, wchar_t *section)
  29. {
  30. if(globalWrite && !section) globalWrite->WriteString(name,string,m_section);
  31. WritePrivateProfileString(section?section:m_section,name,string,m_inifile);
  32. return name;
  33. }
  34. wchar_t *C_Config::ReadString(wchar_t *name, wchar_t *defstr, wchar_t *section)
  35. {
  36. static wchar_t foobuf[] = L"___________config_lameness___________";
  37. m_strbuf[0]=0;
  38. GetPrivateProfileString(section?section:m_section,name,foobuf,m_strbuf,sizeof(m_strbuf)/sizeof(wchar_t),m_inifile);
  39. if (!lstrcmp(foobuf,m_strbuf)) return defstr;
  40. m_strbuf[sizeof(m_strbuf)/sizeof(wchar_t)-1]=0;
  41. return m_strbuf;
  42. }