| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- /*
- * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
- * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
- */
- #include <bx/settings.h>
- namespace
- {
- #define INI_MALLOC(_ctx, _size) (bx::alloc(reinterpret_cast<bx::AllocatorI*>(_ctx), _size) )
- #define INI_FREE(_ctx, _ptr) (bx::free(reinterpret_cast<bx::AllocatorI*>(_ctx), _ptr) )
- #define INI_MEMCPY(_dst, _src, _count) (bx::memCopy(_dst, _src, _count) )
- #define INI_STRLEN(_str) (bx::strLen(_str) )
- #define INI_STRNICMP(_s1, _s2, _len) (bx::strCmpI(_s1, _s2, _len) )
- #define INI_IMPLEMENTATION
- BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-function");
- BX_PRAGMA_DIAGNOSTIC_PUSH();
- BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wsign-compare");
- #include <ini/ini.h>
- BX_PRAGMA_DIAGNOSTIC_POP();
- }
- namespace bx
- {
- Settings::Settings(AllocatorI* _allocator, const void* _data, uint32_t _len)
- : m_allocator(_allocator)
- , m_ini(NULL)
- {
- load(_data, _len);
- }
- #define INI_T(_ptr) reinterpret_cast<ini_t*>(_ptr)
- Settings::~Settings()
- {
- ini_destroy(INI_T(m_ini) );
- }
- void Settings::clear()
- {
- load(NULL, 0);
- }
- void Settings::load(const void* _data, uint32_t _len)
- {
- if (NULL != m_ini)
- {
- ini_destroy(INI_T(m_ini) );
- }
- if (NULL == _data)
- {
- m_ini = ini_create(m_allocator);
- }
- else
- {
- m_ini = ini_load( (const char*)_data, _len, m_allocator);
- }
- }
- StringView Settings::get(const StringView& _name) const
- {
- ini_t* ini = INI_T(m_ini);
- FilePath uri(_name);
- const StringView path(strTrim(uri.getPath(), "/") );
- const StringView& fileName(uri.getFileName() );
- int32_t section = INI_GLOBAL_SECTION;
- if (!path.isEmpty() )
- {
- section = ini_find_section(ini, path.getPtr(), path.getLength() );
- if (INI_NOT_FOUND == section)
- {
- section = INI_GLOBAL_SECTION;
- }
- }
- int32_t property = ini_find_property(ini, section, fileName.getPtr(), fileName.getLength() );
- if (INI_NOT_FOUND == property)
- {
- return StringView();
- }
- return ini_property_value(ini, section, property);
- }
- void Settings::set(const StringView& _name, const StringView& _value)
- {
- ini_t* ini = INI_T(m_ini);
- FilePath uri(_name);
- const StringView path(strTrim(uri.getPath(), "/") );
- const StringView& fileName(uri.getFileName() );
- int32_t section = INI_GLOBAL_SECTION;
- if (!path.isEmpty() )
- {
- section = ini_find_section(ini, path.getPtr(), path.getLength() );
- if (INI_NOT_FOUND == section)
- {
- section = ini_section_add(ini, path.getPtr(), path.getLength() );
- }
- }
- int32_t property = ini_find_property(ini, section, fileName.getPtr(), fileName.getLength() );
- if (INI_NOT_FOUND == property)
- {
- ini_property_add(
- ini
- , section
- , fileName.getPtr()
- , fileName.getLength()
- , _value.getPtr()
- , _value.getLength()
- );
- }
- else
- {
- ini_property_value_set(
- ini
- , section
- , property
- , _value.getPtr()
- , _value.getLength()
- );
- }
- }
- void Settings::remove(const StringView& _name) const
- {
- ini_t* ini = INI_T(m_ini);
- FilePath uri(_name);
- const StringView path = strTrim(uri.getPath(), "/");
- const StringView& fileName = uri.getFileName();
- int32_t section = INI_GLOBAL_SECTION;
- if (!path.isEmpty() )
- {
- section = ini_find_section(ini, path.getPtr(), path.getLength() );
- if (INI_NOT_FOUND == section)
- {
- section = INI_GLOBAL_SECTION;
- }
- }
- int32_t property = ini_find_property(ini, section, fileName.getPtr(), fileName.getLength() );
- if (INI_NOT_FOUND == property)
- {
- return;
- }
- ini_property_remove(ini, section, property);
- if (INI_GLOBAL_SECTION != section
- && 0 == ini_property_count(ini, section) )
- {
- ini_section_remove(ini, section);
- }
- }
- int32_t Settings::read(ReaderSeekerI* _reader, Error* _err)
- {
- int32_t size = int32_t(getRemain(_reader) );
- void* data = bx::alloc(m_allocator, size);
- int32_t total = bx::read(_reader, data, size, _err);
- load(data, size);
- bx::free(m_allocator, data);
- return total;
- }
- int32_t Settings::write(WriterI* _writer, Error* _err) const
- {
- ini_t* ini = INI_T(m_ini);
- int32_t size = ini_save(ini, NULL, 0);
- void* data = bx::alloc(m_allocator, size);
- ini_save(ini, (char*)data, size);
- int32_t total = bx::write(_writer, data, size-1, _err);
- bx::free(m_allocator, data);
- return total;
- }
- #undef INI_T
- int32_t read(ReaderSeekerI* _reader, Settings& _settings, Error* _err)
- {
- BX_ERROR_SCOPE(_err);
- return _settings.read(_reader, _err);
- }
- int32_t write(WriterI* _writer, const Settings& _settings, Error* _err)
- {
- BX_ERROR_SCOPE(_err);
- return _settings.write(_writer, _err);
- }
- } // namespace bx
|