config.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <windows.h>
  2. #include <strsafe.h>
  3. #include "config.h"
  4. #include "../nu/AutoCharFn.h"
  5. C_Config::C_Config( wchar_t *ini )
  6. {
  7. memset( m_strbuf, 0, sizeof( m_strbuf ) );
  8. memset( m_strbufA, 0, sizeof( m_strbufA ) );
  9. m_inifile = _wcsdup( ini );
  10. m_inifileA = _strdup( AutoCharFn( m_inifile ) );
  11. m_section = 0;
  12. }
  13. C_Config::~C_Config()
  14. {
  15. if ( m_inifile )
  16. free( m_inifile );
  17. if ( m_inifileA )
  18. free( m_inifileA );
  19. if ( m_section )
  20. free( m_section );
  21. }
  22. void C_Config::WriteInt( wchar_t *name, int value )
  23. {
  24. wchar_t buf[ 32 ] = { 0 };
  25. StringCchPrintfW( buf, 32, L"%d", value );
  26. WriteString( name, buf );
  27. }
  28. int C_Config::ReadInt( wchar_t *name, int defvalue )
  29. {
  30. return GetPrivateProfileIntW( L"gen_ml_config", name, defvalue, m_inifile );
  31. }
  32. wchar_t *C_Config::WriteString( wchar_t *name, wchar_t *string )
  33. {
  34. WritePrivateProfileStringW( L"gen_ml_config", name, string, m_inifile );
  35. return name;
  36. }
  37. char *C_Config::WriteString( char *name, char *string )
  38. {
  39. WritePrivateProfileStringA( "gen_ml_config", name, string, m_inifileA );
  40. return name;
  41. }
  42. wchar_t *C_Config::ReadString( wchar_t *name, wchar_t *defstr )
  43. {
  44. static wchar_t foobuf[] = L"___________gen_ml_lameness___________";
  45. m_strbuf[ 0 ] = 0;
  46. GetPrivateProfileStringW( L"gen_ml_config", name, foobuf, m_strbuf, ARRAYSIZE( m_strbuf ), m_inifile );
  47. if ( !wcscmp( foobuf, m_strbuf ) )
  48. return defstr;
  49. m_strbuf[ ARRAYSIZE( m_strbuf ) - 1 ] = 0;
  50. return m_strbuf;
  51. }
  52. bool C_Config::ReadString( const wchar_t *name, const wchar_t *defvalue, wchar_t *storage, size_t len )
  53. {
  54. static wchar_t foobuf[] = L"___________gen_ml_lameness___________";
  55. storage[ 0 ] = 0;
  56. GetPrivateProfileStringW( L"gen_ml_config", name, foobuf, storage, (DWORD)len, m_inifile );
  57. if ( !wcscmp( foobuf, storage ) )
  58. {
  59. if ( defvalue )
  60. lstrcpynW( storage, defvalue, (int)len );
  61. return false;
  62. }
  63. return true;
  64. }
  65. char *C_Config::ReadString( const char *name, char *defstr )
  66. {
  67. static char foobuf[] = "___________gen_ml_lameness___________";
  68. m_strbufA[ 0 ] = 0;
  69. GetPrivateProfileStringA( "gen_ml_config", name, foobuf, m_strbufA, ARRAYSIZE( m_strbufA ), m_inifileA );
  70. if ( !strcmp( foobuf, m_strbufA ) )
  71. return defstr;
  72. m_strbufA[ ARRAYSIZE( m_strbufA ) - 1 ] = 0;
  73. return m_strbufA;
  74. }
  75. char *C_Config::ReadString( const char *section_name, const char *key_name, char *defvalue )
  76. {
  77. static char foobuf[] = "___________lameness___________";
  78. m_strbufA[ 0 ] = 0;
  79. GetPrivateProfileStringA( section_name, key_name, foobuf, m_strbufA, ARRAYSIZE( m_strbufA ), m_inifileA );
  80. if ( !strcmp( foobuf, m_strbufA ) )
  81. return defvalue;
  82. m_strbufA[ ARRAYSIZE( m_strbufA ) - 1 ] = 0;
  83. return m_strbufA;
  84. }
  85. bool C_Config::ReadString( const char *name, const char *defvalue, char *storage, size_t len )
  86. {
  87. static char foobuf[] = "___________gen_ml_lameness___________";
  88. storage[ 0 ] = 0;
  89. GetPrivateProfileStringA( "gen_ml_config", name, foobuf, storage, (DWORD)len, m_inifileA );
  90. if ( !strcmp( foobuf, storage ) )
  91. {
  92. if ( defvalue )
  93. lstrcpynA( storage, defvalue, (int)len );
  94. return false;
  95. }
  96. return true;
  97. }