2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2022-09-12 14:06:24 +00:00
|
|
|
* Copyright (c) 2021, networkException <networkexception@serenityos.org>
|
2022-02-06 15:27:17 +00:00
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-04-15 00:22:08 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2022-04-09 10:35:21 +00:00
|
|
|
#include <AK/Forward.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/HashMap.h>
|
2023-02-09 02:11:50 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-06-21 16:58:45 +00:00
|
|
|
#include <AK/RefCounted.h>
|
2020-02-02 11:34:39 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Vector.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
class ConfigFile : public RefCounted<ConfigFile> {
|
2019-04-15 00:22:08 +00:00
|
|
|
public:
|
2021-08-18 23:23:31 +00:00
|
|
|
enum class AllowWriting {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_lib(ByteString const& lib_name, AllowWriting = AllowWriting::No);
|
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_app(ByteString const& app_name, AllowWriting = AllowWriting::No);
|
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_system(ByteString const& app_name, AllowWriting = AllowWriting::No);
|
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open(ByteString const& filename, AllowWriting = AllowWriting::No);
|
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open(ByteString const& filename, int fd);
|
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open(ByteString const& filename, NonnullOwnPtr<Core::File>);
|
2020-02-02 11:34:39 +00:00
|
|
|
~ConfigFile();
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
bool has_group(ByteString const&) const;
|
|
|
|
bool has_key(ByteString const& group, ByteString const& key) const;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
Vector<ByteString> groups() const;
|
|
|
|
Vector<ByteString> keys(ByteString const& group) const;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2021-06-28 02:04:11 +00:00
|
|
|
size_t num_groups() const { return m_groups.size(); }
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString read_entry(ByteString const& group, ByteString const& key, ByteString const& default_value = {}) const
|
2023-10-10 11:30:58 +00:00
|
|
|
{
|
|
|
|
return read_entry_optional(group, key).value_or(default_value);
|
|
|
|
}
|
2023-12-16 14:19:34 +00:00
|
|
|
Optional<ByteString> read_entry_optional(ByteString const& group, ByteString const& key) const;
|
|
|
|
bool read_bool_entry(ByteString const& group, ByteString const& key, bool default_value = false) const;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2022-12-20 12:12:21 +00:00
|
|
|
template<Integral T = int>
|
2023-12-16 14:19:34 +00:00
|
|
|
T read_num_entry(ByteString const& group, ByteString const& key, T default_value = 0) const
|
2022-12-20 12:12:21 +00:00
|
|
|
{
|
|
|
|
if (!has_key(group, key))
|
|
|
|
return default_value;
|
|
|
|
|
2023-12-23 02:59:14 +00:00
|
|
|
return read_entry(group, key, "").to_number<T>().value_or(default_value);
|
2022-12-20 12:12:21 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
void write_entry(ByteString const& group, ByteString const& key, ByteString const& value);
|
|
|
|
void write_bool_entry(ByteString const& group, ByteString const& key, bool value);
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2022-12-20 12:12:21 +00:00
|
|
|
template<Integral T = int>
|
2023-12-16 14:19:34 +00:00
|
|
|
void write_num_entry(ByteString const& group, ByteString const& key, T value)
|
2022-12-20 12:12:21 +00:00
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
write_entry(group, key, ByteString::number(value));
|
2022-12-20 12:12:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
void dump() const;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
|
|
|
bool is_dirty() const { return m_dirty; }
|
|
|
|
|
2022-02-06 14:26:33 +00:00
|
|
|
ErrorOr<void> sync();
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
void add_group(ByteString const& group);
|
|
|
|
void remove_group(ByteString const& group);
|
|
|
|
void remove_entry(ByteString const& group, ByteString const& key);
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString const& filename() const { return m_filename; }
|
2019-05-25 04:10:23 +00:00
|
|
|
|
2019-04-15 00:22:08 +00:00
|
|
|
private:
|
2023-12-16 14:19:34 +00:00
|
|
|
ConfigFile(ByteString const& filename, OwnPtr<InputBufferedFile> open_file);
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2022-02-06 15:27:17 +00:00
|
|
|
ErrorOr<void> reparse();
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString m_filename;
|
2023-05-03 22:45:18 +00:00
|
|
|
OwnPtr<InputBufferedFile> m_file;
|
2023-12-16 14:19:34 +00:00
|
|
|
HashMap<ByteString, HashMap<ByteString, ByteString>> m_groups;
|
2019-04-15 00:22:08 +00:00
|
|
|
bool m_dirty { false };
|
|
|
|
};
|
2020-02-02 11:34:39 +00:00
|
|
|
|
|
|
|
}
|