Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, networkException <networkexception@serenityos.org>
4 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 */
8
9#pragma once
10
11#include <AK/DeprecatedString.h>
12#include <AK/Forward.h>
13#include <AK/HashMap.h>
14#include <AK/OwnPtr.h>
15#include <AK/RefCounted.h>
16#include <AK/RefPtr.h>
17#include <AK/Vector.h>
18#include <LibCore/File.h>
19
20namespace Core {
21
22class ConfigFile : public RefCounted<ConfigFile> {
23public:
24 enum class AllowWriting {
25 Yes,
26 No,
27 };
28
29 static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_lib(DeprecatedString const& lib_name, AllowWriting = AllowWriting::No);
30 static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_app(DeprecatedString const& app_name, AllowWriting = AllowWriting::No);
31 static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_system(DeprecatedString const& app_name, AllowWriting = AllowWriting::No);
32 static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, AllowWriting = AllowWriting::No);
33 static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, int fd);
34 static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, NonnullOwnPtr<Core::File>);
35 ~ConfigFile();
36
37 bool has_group(DeprecatedString const&) const;
38 bool has_key(DeprecatedString const& group, DeprecatedString const& key) const;
39
40 Vector<DeprecatedString> groups() const;
41 Vector<DeprecatedString> keys(DeprecatedString const& group) const;
42
43 size_t num_groups() const { return m_groups.size(); }
44
45 DeprecatedString read_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& default_value = DeprecatedString()) const;
46 bool read_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool default_value = false) const;
47
48 template<Integral T = int>
49 T read_num_entry(DeprecatedString const& group, DeprecatedString const& key, T default_value = 0) const
50 {
51 if (!has_key(group, key))
52 return default_value;
53
54 if constexpr (IsSigned<T>)
55 return read_entry(group, key).to_int<T>().value_or(default_value);
56 else
57 return read_entry(group, key).to_uint<T>().value_or(default_value);
58 }
59
60 void write_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value);
61 void write_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool value);
62
63 template<Integral T = int>
64 void write_num_entry(DeprecatedString const& group, DeprecatedString const& key, T value)
65 {
66 write_entry(group, key, DeprecatedString::number(value));
67 }
68
69 void dump() const;
70
71 bool is_dirty() const { return m_dirty; }
72
73 ErrorOr<void> sync();
74
75 void add_group(DeprecatedString const& group);
76 void remove_group(DeprecatedString const& group);
77 void remove_entry(DeprecatedString const& group, DeprecatedString const& key);
78
79 DeprecatedString const& filename() const { return m_filename; }
80
81private:
82 ConfigFile(DeprecatedString const& filename, OwnPtr<BufferedFile> open_file);
83
84 ErrorOr<void> reparse();
85
86 DeprecatedString m_filename;
87 OwnPtr<BufferedFile> m_file;
88 HashMap<DeprecatedString, HashMap<DeprecatedString, DeprecatedString>> m_groups;
89 bool m_dirty { false };
90};
91
92}