2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.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
|
|
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
#include <AK/DeprecatedString.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>
|
2023-02-08 23:11:50 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-06-21 13:58:45 -03:00
|
|
|
#include <AK/RefCounted.h>
|
2020-02-02 08:34:39 -03:00
|
|
|
#include <AK/RefPtr.h>
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <AK/Vector.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 {
|
|
|
|
|
|
|
|
|
|
class 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,
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_lib(DeprecatedString const& lib_name, AllowWriting = AllowWriting::No);
|
|
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_app(DeprecatedString const& app_name, AllowWriting = AllowWriting::No);
|
|
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_system(DeprecatedString const& app_name, AllowWriting = AllowWriting::No);
|
|
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, AllowWriting = AllowWriting::No);
|
|
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, int fd);
|
2023-02-08 23:02:46 -03:00
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, NonnullOwnPtr<Core::File>);
|
2020-02-02 08:34:39 -03:00
|
|
|
~ConfigFile();
|
2019-04-14 21:22:08 -03:00
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
bool has_group(DeprecatedString const&) const;
|
|
|
|
|
bool has_key(DeprecatedString const& group, DeprecatedString const& key) const;
|
2019-04-14 21:22:08 -03:00
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
Vector<DeprecatedString> groups() const;
|
|
|
|
|
Vector<DeprecatedString> keys(DeprecatedString 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(); }
|
|
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
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;
|
2019-04-14 21:22:08 -03:00
|
|
|
|
2022-12-20 09:12:21 -03:00
|
|
|
template<Integral T = int>
|
|
|
|
|
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<T>)
|
|
|
|
|
return read_entry(group, key).to_int<T>().value_or(default_value);
|
|
|
|
|
else
|
|
|
|
|
return read_entry(group, key).to_uint<T>().value_or(default_value);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
void write_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value);
|
|
|
|
|
void write_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool value);
|
2019-04-14 21:22:08 -03:00
|
|
|
|
2022-12-20 09:12:21 -03:00
|
|
|
template<Integral T = int>
|
|
|
|
|
void write_num_entry(DeprecatedString const& group, DeprecatedString const& key, T value)
|
|
|
|
|
{
|
|
|
|
|
write_entry(group, key, DeprecatedString::number(value));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
void add_group(DeprecatedString const& group);
|
|
|
|
|
void remove_group(DeprecatedString const& group);
|
|
|
|
|
void remove_entry(DeprecatedString const& group, DeprecatedString const& key);
|
2019-04-14 21:22:08 -03:00
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
DeprecatedString const& filename() const { return m_filename; }
|
2019-05-25 01:10:23 -03:00
|
|
|
|
2019-04-14 21:22:08 -03:00
|
|
|
private:
|
2023-05-03 19:45:18 -03:00
|
|
|
ConfigFile(DeprecatedString 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
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
DeprecatedString m_filename;
|
2023-05-03 19:45:18 -03:00
|
|
|
OwnPtr<InputBufferedFile> m_file;
|
2022-12-04 15:02:33 -03:00
|
|
|
HashMap<DeprecatedString, HashMap<DeprecatedString, DeprecatedString>> m_groups;
|
2019-04-14 21:22:08 -03:00
|
|
|
bool m_dirty { false };
|
|
|
|
|
};
|
2020-02-02 08:34:39 -03:00
|
|
|
|
|
|
|
|
}
|