2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-09-12 16:06:24 +02: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 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-04-15 02:22:08 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2022-04-09 12:35:21 +02:00
|
|
|
#include <AK/Forward.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/HashMap.h>
|
2019-06-21 18:58:45 +02:00
|
|
|
#include <AK/RefCounted.h>
|
2020-02-02 12:34:39 +01:00
|
|
|
#include <AK/RefPtr.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/Vector.h>
|
2023-02-09 03:02:46 +01:00
|
|
|
#include <LibCore/File.h>
|
2022-02-06 15:27:17 +00:00
|
|
|
#include <LibCore/Stream.h>
|
2019-04-15 02:22:08 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
class ConfigFile : public RefCounted<ConfigFile> {
|
2019-04-15 02:22:08 +02:00
|
|
|
public:
|
2021-08-19 01:23:31 +02:00
|
|
|
enum class AllowWriting {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
2022-12-04 18:02:33 +00: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-09 03:02:46 +01:00
|
|
|
static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, NonnullOwnPtr<Core::File>);
|
2020-02-02 12:34:39 +01:00
|
|
|
~ConfigFile();
|
2019-04-15 02:22:08 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
bool has_group(DeprecatedString const&) const;
|
|
|
|
bool has_key(DeprecatedString const& group, DeprecatedString const& key) const;
|
2019-04-15 02:22:08 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> groups() const;
|
|
|
|
Vector<DeprecatedString> keys(DeprecatedString const& group) const;
|
2019-04-15 02:22:08 +02:00
|
|
|
|
2021-06-27 20:04:11 -06:00
|
|
|
size_t num_groups() const { return m_groups.size(); }
|
|
|
|
|
2022-12-04 18:02:33 +00: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-15 02:22:08 +02:00
|
|
|
|
2022-12-20 07:12:21 -05: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 18:02:33 +00: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-15 02:22:08 +02:00
|
|
|
|
2022-12-20 07:12:21 -05: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 11:53:16 +02:00
|
|
|
void dump() const;
|
2019-04-15 02:22:08 +02:00
|
|
|
|
|
|
|
bool is_dirty() const { return m_dirty; }
|
|
|
|
|
2022-02-06 14:26:33 +00:00
|
|
|
ErrorOr<void> sync();
|
2019-04-15 02:22:08 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void add_group(DeprecatedString const& group);
|
|
|
|
void remove_group(DeprecatedString const& group);
|
|
|
|
void remove_entry(DeprecatedString const& group, DeprecatedString const& key);
|
2019-04-15 02:22:08 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString const& filename() const { return m_filename; }
|
2019-05-24 21:10:23 -07:00
|
|
|
|
2019-04-15 02:22:08 +02:00
|
|
|
private:
|
2023-02-09 03:02:46 +01:00
|
|
|
ConfigFile(DeprecatedString const& filename, OwnPtr<BufferedFile> open_file);
|
2019-04-15 02:22:08 +02:00
|
|
|
|
2022-02-06 15:27:17 +00:00
|
|
|
ErrorOr<void> reparse();
|
2019-04-15 02:22:08 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString m_filename;
|
2023-02-09 03:02:46 +01:00
|
|
|
OwnPtr<BufferedFile> m_file;
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<DeprecatedString, HashMap<DeprecatedString, DeprecatedString>> m_groups;
|
2019-04-15 02:22:08 +02:00
|
|
|
bool m_dirty { false };
|
|
|
|
};
|
2020-02-02 12:34:39 +01:00
|
|
|
|
|
|
|
}
|