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