settings.cpp 4.4 KB

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