settings.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2011-2020 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "bx_p.h"
  6. #include <bx/settings.h>
  7. namespace
  8. {
  9. #define INI_MALLOC(_ctx, _size) (BX_ALLOC(reinterpret_cast<bx::AllocatorI*>(_ctx), _size) )
  10. #define INI_FREE(_ctx, _ptr) (BX_FREE(reinterpret_cast<bx::AllocatorI*>(_ctx), _ptr) )
  11. #define INI_MEMCPY(_dst, _src, _count) (bx::memCopy(_dst, _src, _count) )
  12. #define INI_STRLEN(_str) (bx::strLen(_str) )
  13. #define INI_STRNICMP(_s1, _s2, _len) (bx::strCmpI(_s1, _s2, _len) )
  14. #define INI_IMPLEMENTATION
  15. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-function");
  16. BX_PRAGMA_DIAGNOSTIC_PUSH();
  17. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wsign-compare");
  18. #include <ini/ini.h>
  19. BX_PRAGMA_DIAGNOSTIC_POP();
  20. }
  21. namespace bx
  22. {
  23. Settings::Settings(AllocatorI* _allocator, const void* _data, uint32_t _len)
  24. : m_allocator(_allocator)
  25. , m_ini(NULL)
  26. {
  27. load(_data, _len);
  28. }
  29. #define INI_T(_ptr) reinterpret_cast<ini_t*>(_ptr)
  30. Settings::~Settings()
  31. {
  32. ini_destroy(INI_T(m_ini) );
  33. }
  34. void Settings::clear()
  35. {
  36. load(NULL, 0);
  37. }
  38. void Settings::load(const void* _data, uint32_t _len)
  39. {
  40. if (NULL != m_ini)
  41. {
  42. ini_destroy(INI_T(m_ini) );
  43. }
  44. if (NULL == _data)
  45. {
  46. m_ini = ini_create(m_allocator);
  47. }
  48. else
  49. {
  50. m_ini = ini_load( (const char*)_data, _len, m_allocator);
  51. }
  52. }
  53. StringView Settings::get(const StringView& _name) const
  54. {
  55. ini_t* ini = INI_T(m_ini);
  56. FilePath uri(_name);
  57. const StringView path(strTrim(uri.getPath(), "/") );
  58. const StringView& fileName(uri.getFileName() );
  59. int32_t section = INI_GLOBAL_SECTION;
  60. if (!path.isEmpty() )
  61. {
  62. section = ini_find_section(ini, path.getPtr(), path.getLength() );
  63. if (INI_NOT_FOUND == section)
  64. {
  65. section = INI_GLOBAL_SECTION;
  66. }
  67. }
  68. int32_t property = ini_find_property(ini, section, fileName.getPtr(), fileName.getLength() );
  69. if (INI_NOT_FOUND == property)
  70. {
  71. return StringView();
  72. }
  73. return ini_property_value(ini, section, property);
  74. }
  75. void Settings::set(const StringView& _name, const StringView& _value)
  76. {
  77. ini_t* ini = INI_T(m_ini);
  78. FilePath uri(_name);
  79. const StringView path(strTrim(uri.getPath(), "/") );
  80. const StringView& fileName(uri.getFileName() );
  81. int32_t section = INI_GLOBAL_SECTION;
  82. if (!path.isEmpty() )
  83. {
  84. section = ini_find_section(ini, path.getPtr(), path.getLength() );
  85. if (INI_NOT_FOUND == section)
  86. {
  87. section = ini_section_add(ini, path.getPtr(), path.getLength() );
  88. }
  89. }
  90. int32_t property = ini_find_property(ini, section, fileName.getPtr(), fileName.getLength() );
  91. if (INI_NOT_FOUND == property)
  92. {
  93. ini_property_add(
  94. ini
  95. , section
  96. , fileName.getPtr()
  97. , fileName.getLength()
  98. , _value.getPtr()
  99. , _value.getLength()
  100. );
  101. }
  102. else
  103. {
  104. ini_property_value_set(
  105. ini
  106. , section
  107. , property
  108. , _value.getPtr()
  109. , _value.getLength()
  110. );
  111. }
  112. }
  113. void Settings::remove(const StringView& _name) const
  114. {
  115. ini_t* ini = INI_T(m_ini);
  116. FilePath uri(_name);
  117. const StringView path = strTrim(uri.getPath(), "/");
  118. const StringView& fileName = uri.getFileName();
  119. int32_t section = INI_GLOBAL_SECTION;
  120. if (!path.isEmpty() )
  121. {
  122. section = ini_find_section(ini, path.getPtr(), path.getLength() );
  123. if (INI_NOT_FOUND == section)
  124. {
  125. section = INI_GLOBAL_SECTION;
  126. }
  127. }
  128. int32_t property = ini_find_property(ini, section, fileName.getPtr(), fileName.getLength() );
  129. if (INI_NOT_FOUND == property)
  130. {
  131. return;
  132. }
  133. ini_property_remove(ini, section, property);
  134. if (INI_GLOBAL_SECTION != section
  135. && 0 == ini_property_count(ini, section) )
  136. {
  137. ini_section_remove(ini, section);
  138. }
  139. }
  140. int32_t Settings::read(ReaderSeekerI* _reader, Error* _err)
  141. {
  142. int32_t size = int32_t(getRemain(_reader) );
  143. void* data = BX_ALLOC(m_allocator, size);
  144. int32_t total = bx::read(_reader, data, size, _err);
  145. load(data, size);
  146. BX_FREE(m_allocator, data);
  147. return total;
  148. }
  149. int32_t Settings::write(WriterI* _writer, Error* _err) const
  150. {
  151. ini_t* ini = INI_T(m_ini);
  152. int32_t size = ini_save(ini, NULL, 0);
  153. void* data = BX_ALLOC(m_allocator, size);
  154. ini_save(ini, (char*)data, size);
  155. int32_t total = bx::write(_writer, data, size-1, _err);
  156. BX_FREE(m_allocator, data);
  157. return total;
  158. }
  159. #undef INI_T
  160. int32_t read(ReaderSeekerI* _reader, Settings& _settings, Error* _err)
  161. {
  162. BX_ERROR_SCOPE(_err);
  163. return _settings.read(_reader, _err);
  164. }
  165. int32_t write(WriterI* _writer, const Settings& _settings, Error* _err)
  166. {
  167. BX_ERROR_SCOPE(_err);
  168. return _settings.write(_writer, _err);
  169. }
  170. } // namespace bx