cfg_var.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #define STRICT
  2. #include <windows.h>
  3. #include "cfg_var.h"
  4. #include "string_unicode.h"
  5. static const char *m_inifile, *m_section;
  6. int cfg_var::reg_read_int(HKEY hk,int def)
  7. {
  8. return GetPrivateProfileIntA(m_section,var_get_name(),def,m_inifile);
  9. }
  10. void cfg_var::reg_write_int(HKEY hk,int val)
  11. {
  12. /* long temp=val;
  13. RegSetValueEx(hk,var_get_name(),0,REG_DWORD,(const BYTE*)&temp,4);*/
  14. char tmp[512] = {0};
  15. wsprintfA(tmp,"%d",val);
  16. WritePrivateProfileStringA(m_section,var_get_name(),tmp,m_inifile);
  17. }
  18. void cfg_var::reg_write_struct(HKEY hk,const void * ptr,UINT size)
  19. {
  20. WritePrivateProfileStructA(m_section,var_get_name(),(void *)ptr,size,m_inifile);
  21. }
  22. bool cfg_var::reg_read_struct(HKEY hk,void * ptr,UINT size)
  23. {
  24. GetPrivateProfileStructA(m_section,var_get_name(),ptr,size,m_inifile);
  25. return 1;
  26. }
  27. int cfg_var::reg_get_struct_size(HKEY hk)
  28. {
  29. DWORD sz=0,t=0;
  30. if (RegQueryValueExA(hk,var_get_name(),0,&t,0,&sz)!=ERROR_SUCCESS) return 0;
  31. return sz;
  32. }
  33. bool string_a::reg_read(HKEY hk,const char * name)
  34. {
  35. char tmp[4096] = {0};
  36. GetPrivateProfileStringA(m_section,name,"|||",tmp,sizeof(tmp)-1,m_inifile);
  37. if(strstr(tmp,"|||")==tmp) return 0;
  38. lstrcpyA(buffer_get(strlen(tmp)+1),tmp);
  39. buffer_done();
  40. return 1;
  41. }
  42. void string_a::reg_write(HKEY hk,const char * name)
  43. {
  44. WritePrivateProfileStringA(m_section,name,(const char*)*this,m_inifile);
  45. }
  46. cfg_var * cfg_var::list=0;
  47. /*HKEY cfg_var::reg_open(const char * regname)
  48. {
  49. HKEY hk;
  50. RegCreateKey(HKEY_CURRENT_USER,regname,&hk);
  51. return hk;
  52. }*/
  53. void cfg_var::config_read(const char *inifile, const char *section)
  54. {
  55. HKEY hk = 0; //reg_open(regname);
  56. m_inifile=inifile;
  57. m_section=section;
  58. cfg_var * ptr;
  59. for(ptr = list; ptr; ptr=ptr->next) ptr->read(hk);
  60. //RegCloseKey(hk);
  61. }
  62. void cfg_var::config_write(const char *inifile, const char *section)
  63. {
  64. HKEY hk = 0; //reg_open(regname);
  65. m_inifile=inifile;
  66. m_section=section;
  67. cfg_var * ptr;
  68. for(ptr = list; ptr; ptr=ptr->next) ptr->write(hk);
  69. //RegCloseKey(hk);
  70. }
  71. void cfg_var::config_reset()
  72. {
  73. cfg_var * ptr;
  74. for(ptr = list; ptr; ptr=ptr->next) ptr->reset();
  75. }
  76. void cfg_int::read(HKEY hk)
  77. {
  78. val = reg_read_int(hk,def);
  79. }
  80. void cfg_int::write(HKEY hk)
  81. {
  82. if (val!=reg_read_int(hk,def))
  83. reg_write_int(hk,val);
  84. }
  85. void cfg_string::read(HKEY hk)
  86. {
  87. string_a temp;
  88. if (temp.reg_read(hk,var_get_name())) val=temp;
  89. }
  90. void cfg_string::write(HKEY hk)
  91. {
  92. string_a temp = def;
  93. string_a name = var_get_name();
  94. if (!temp.reg_read(hk,name) || lstrcmpA(val,temp))
  95. val.reg_write(hk,name);
  96. }
  97. #ifdef PFC_UNICODE
  98. void cfg_string_w::read(HKEY hk)
  99. {
  100. string_w temp;
  101. if (temp.reg_read(hk,string_w(var_get_name()))) val=temp;
  102. }
  103. void cfg_string_w::write(HKEY hk)
  104. {
  105. string_w temp = def;
  106. string_w name = var_get_name();
  107. string_w val_w = val;
  108. if (!temp.reg_read(hk,name) || wcscmp(val_w,temp))
  109. val_w.reg_write(hk,name);
  110. }
  111. #endif