Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/HashMap.h>
30#include <AK/RefCounted.h>
31#include <AK/RefPtr.h>
32#include <AK/String.h>
33#include <AK/Vector.h>
34#include <LibGfx/Color.h>
35
36namespace Core {
37
38class ConfigFile : public RefCounted<ConfigFile> {
39public:
40 static NonnullRefPtr<ConfigFile> get_for_app(const String& app_name);
41 static NonnullRefPtr<ConfigFile> get_for_system(const String& app_name);
42 static NonnullRefPtr<ConfigFile> open(const String& path);
43 ~ConfigFile();
44
45 bool has_group(const String&) const;
46 bool has_key(const String& group, const String& key) const;
47
48 Vector<String> groups() const;
49 Vector<String> keys(const String& group) const;
50
51 String read_entry(const String& group, const String& key, const String& default_value = String()) const;
52 int read_num_entry(const String& group, const String& key, int default_value = 0) const;
53 bool read_bool_entry(const String& group, const String& key, bool default_value = false) const;
54
55 void write_entry(const String& group, const String& key, const String& value);
56 void write_num_entry(const String& group, const String& key, int value);
57 void write_bool_entry(const String& group, const String& key, bool value);
58 void write_color_entry(const String& group, const String& key, Color value);
59
60 void dump() const;
61
62 bool is_dirty() const { return m_dirty; }
63
64 bool sync();
65
66 void remove_group(const String& group);
67 void remove_entry(const String& group, const String& key);
68
69 String file_name() const { return m_file_name; }
70
71private:
72 explicit ConfigFile(const String& file_name);
73
74 void reparse();
75
76 String m_file_name;
77 HashMap<String, HashMap<String, String>> m_groups;
78 bool m_dirty { false };
79};
80
81}