From aaed751bf4430bf4b9b30cef532b8753b9f639ce Mon Sep 17 00:00:00 2001 From: untodesu Date: Thu, 11 Sep 2025 13:48:31 +0500 Subject: Replace most of C strings with string_view --- core/io/cmdline.cc | 31 +++++++++++++++++++++---------- core/io/cmdline.hh | 8 +++++--- core/io/config_map.cc | 33 ++++++++++++++++----------------- core/io/config_map.hh | 12 ++++++------ 4 files changed, 48 insertions(+), 36 deletions(-) (limited to 'core/io') diff --git a/core/io/cmdline.cc b/core/io/cmdline.cc index 963f67c..7a00f37 100644 --- a/core/io/cmdline.cc +++ b/core/io/cmdline.cc @@ -57,19 +57,30 @@ void io::cmdline::create(int argc, char** argv) } } -void io::cmdline::insert(const char* option, const char* argument) +void io::cmdline::insert(std::string_view option) { - if(argument == nullptr) { - options.insert_or_assign(option, std::string()); - } - else { - options.insert_or_assign(option, argument); + options.insert_or_assign(std::string(option), std::string()); +} + +void io::cmdline::insert(std::string_view option, std::string_view argument) +{ + options.insert_or_assign(std::string(option), std::string(argument)); +} + +std::string_view io::cmdline::get(std::string_view option, std::string_view fallback) +{ + auto it = options.find(std::string(option)); + + if(it == options.cend()) { + return fallback; } + + return it->second; } -const char* io::cmdline::get(const char* option, const char* fallback) +const char* io::cmdline::get_cstr(std::string_view option, const char* fallback) { - auto it = options.find(option); + auto it = options.find(std::string(option)); if(it == options.cend()) { return fallback; @@ -78,7 +89,7 @@ const char* io::cmdline::get(const char* option, const char* fallback) return it->second.c_str(); } -bool io::cmdline::contains(const char* option) +bool io::cmdline::contains(std::string_view option) { - return options.count(option); + return options.count(std::string(option)); } diff --git a/core/io/cmdline.hh b/core/io/cmdline.hh index d68d9ef..8b2b4ee 100644 --- a/core/io/cmdline.hh +++ b/core/io/cmdline.hh @@ -5,9 +5,11 @@ namespace io::cmdline { void create(int argc, char** argv); -void insert(const char* option, const char* argument = nullptr); -const char* get(const char* option, const char* fallback = nullptr); -bool contains(const char* option); +void insert(std::string_view option); +void insert(std::string_view option, std::string_view argument); +std::string_view get(std::string_view option, std::string_view fallback = ""); +const char* get_cstr(std::string_view option, const char* fallback = nullptr); +bool contains(std::string_view option); } // namespace io::cmdline #endif // CORE_IO_CMDLINE_HH diff --git a/core/io/config_map.cc b/core/io/config_map.cc index 2afd2a8..9fd8ff7 100644 --- a/core/io/config_map.cc +++ b/core/io/config_map.cc @@ -10,15 +10,15 @@ void io::ConfigMap::load_cmdline(void) { for(auto it : m_values) { - if(auto value = io::cmdline::get(it.first.c_str())) { + if(auto value = io::cmdline::get_cstr(it.first.c_str())) { it.second->set(value); } } } -bool io::ConfigMap::load_file(const char* path) +bool io::ConfigMap::load_file(std::string_view path) { - if(auto file = PHYSFS_openRead(path)) { + if(auto file = PHYSFS_openRead(std::string(path).c_str())) { auto source = std::string(PHYSFS_fileLength(file), char(0x00)); PHYSFS_readBytes(file, source.data(), source.size()); PHYSFS_close(file); @@ -68,13 +68,13 @@ bool io::ConfigMap::load_file(const char* path) return false; } -bool io::ConfigMap::save_file(const char* path) const +bool io::ConfigMap::save_file(std::string_view path) const { std::ostringstream stream; auto curtime = std::time(nullptr); - stream << "# Voxelius " << project_version_string << " configuration file" << std::endl; + stream << "# Voxelius " << version::semver << " configuration file" << std::endl; stream << "# Generated at: " << std::put_time(std::gmtime(&curtime), "%Y-%m-%d %H:%M:%S %z") << std::endl << std::endl; for(const auto& it : m_values) { @@ -83,7 +83,7 @@ bool io::ConfigMap::save_file(const char* path) const stream << std::endl; } - if(auto file = PHYSFS_openWrite(path)) { + if(auto file = PHYSFS_openWrite(std::string(path).c_str())) { auto source = stream.str(); PHYSFS_writeBytes(file, source.data(), source.size()); PHYSFS_close(file); @@ -93,9 +93,9 @@ bool io::ConfigMap::save_file(const char* path) const return false; } -bool io::ConfigMap::set_value(const char* name, const char* value) +bool io::ConfigMap::set_value(std::string_view name, std::string_view value) { - auto kv_pair = m_values.find(name); + auto kv_pair = m_values.find(std::string(name)); if(kv_pair != m_values.cend()) { kv_pair->second->set(value); @@ -105,25 +105,24 @@ bool io::ConfigMap::set_value(const char* name, const char* value) return false; } -const char* io::ConfigMap::get_value(const char* name) const +std::string_view io::ConfigMap::get_value(std::string_view name) const { - auto kv_pair = m_values.find(name); + auto kv_pair = m_values.find(std::string(name)); if(kv_pair != m_values.cend()) { return kv_pair->second->get(); } - else { - return nullptr; - } + + return std::string_view(); } -void io::ConfigMap::add_value(const char* name, config::IValue& vref) +void io::ConfigMap::add_value(std::string_view name, config::IValue& vref) { - m_values.insert_or_assign(name, &vref); + m_values.insert_or_assign(std::string(name), &vref); } -const config::IValue* io::ConfigMap::find(const char* name) const +const config::IValue* io::ConfigMap::find(std::string_view name) const { - auto kv_pair = m_values.find(name); + auto kv_pair = m_values.find(std::string(name)); if(kv_pair != m_values.cend()) { return kv_pair->second; diff --git a/core/io/config_map.hh b/core/io/config_map.hh index 8ff92e1..7ab7543 100644 --- a/core/io/config_map.hh +++ b/core/io/config_map.hh @@ -15,15 +15,15 @@ public: virtual ~ConfigMap(void) = default; void load_cmdline(void); - bool load_file(const char* path); - bool save_file(const char* path) const; + bool load_file(std::string_view path); + bool save_file(std::string_view path) const; - bool set_value(const char* name, const char* value); - const char* get_value(const char* name) const; + bool set_value(std::string_view name, std::string_view value); + std::string_view get_value(std::string_view name) const; - void add_value(const char* name, config::IValue& vref); + void add_value(std::string_view name, config::IValue& vref); - const config::IValue* find(const char* name) const; + const config::IValue* find(std::string_view name) const; private: std::unordered_map m_values; -- cgit