/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, networkException * Copyright (c) 2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace Core { class ConfigFile : public RefCounted { public: enum class AllowWriting { Yes, No, }; static ErrorOr> open_for_lib(DeprecatedString const& lib_name, AllowWriting = AllowWriting::No); static ErrorOr> open_for_app(DeprecatedString const& app_name, AllowWriting = AllowWriting::No); static ErrorOr> open_for_system(DeprecatedString const& app_name, AllowWriting = AllowWriting::No); static ErrorOr> open(DeprecatedString const& filename, AllowWriting = AllowWriting::No); static ErrorOr> open(DeprecatedString const& filename, int fd); static ErrorOr> open(DeprecatedString const& filename, NonnullOwnPtr); ~ConfigFile(); bool has_group(DeprecatedString const&) const; bool has_key(DeprecatedString const& group, DeprecatedString const& key) const; Vector groups() const; Vector keys(DeprecatedString const& group) const; size_t num_groups() const { return m_groups.size(); } DeprecatedString read_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& default_value = DeprecatedString()) const; bool read_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool default_value = false) const; template T read_num_entry(DeprecatedString const& group, DeprecatedString const& key, T default_value = 0) const { if (!has_key(group, key)) return default_value; if constexpr (IsSigned) return read_entry(group, key).to_int().value_or(default_value); else return read_entry(group, key).to_uint().value_or(default_value); } void write_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value); void write_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool value); template void write_num_entry(DeprecatedString const& group, DeprecatedString const& key, T value) { write_entry(group, key, DeprecatedString::number(value)); } void dump() const; bool is_dirty() const { return m_dirty; } ErrorOr sync(); void add_group(DeprecatedString const& group); void remove_group(DeprecatedString const& group); void remove_entry(DeprecatedString const& group, DeprecatedString const& key); DeprecatedString const& filename() const { return m_filename; } private: ConfigFile(DeprecatedString const& filename, OwnPtr open_file); ErrorOr reparse(); DeprecatedString m_filename; OwnPtr m_file; HashMap> m_groups; bool m_dirty { false }; }; }