2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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
|
|
|
|
|
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>
|
|
|
|
#include <AK/String.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 12:04:00 +01:00
|
|
|
#include <LibGfx/Color.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:
|
2020-08-17 19:13:15 +04:30
|
|
|
static NonnullRefPtr<ConfigFile> get_for_lib(const String& lib_name);
|
2020-02-02 12:34:39 +01:00
|
|
|
static NonnullRefPtr<ConfigFile> get_for_app(const String& app_name);
|
|
|
|
static NonnullRefPtr<ConfigFile> get_for_system(const String& app_name);
|
|
|
|
static NonnullRefPtr<ConfigFile> open(const String& path);
|
|
|
|
~ConfigFile();
|
2019-04-15 02:22:08 +02:00
|
|
|
|
|
|
|
bool has_group(const String&) const;
|
|
|
|
bool has_key(const String& group, const String& key) const;
|
|
|
|
|
|
|
|
Vector<String> groups() const;
|
|
|
|
Vector<String> keys(const String& group) const;
|
|
|
|
|
2020-01-07 22:42:28 +11:00
|
|
|
String read_entry(const String& group, const String& key, const String& default_value = String()) const;
|
2019-04-15 02:22:08 +02:00
|
|
|
int read_num_entry(const String& group, const String& key, int default_value = 0) const;
|
|
|
|
bool read_bool_entry(const String& group, const String& key, bool default_value = false) const;
|
|
|
|
|
2019-05-28 11:53:16 +02:00
|
|
|
void write_entry(const String& group, const String& key, const String& value);
|
2019-04-15 02:22:08 +02:00
|
|
|
void write_num_entry(const String& group, const String& key, int value);
|
|
|
|
void write_bool_entry(const String& group, const String& key, bool value);
|
2019-05-24 21:10:23 -07:00
|
|
|
void write_color_entry(const String& group, const String& key, Color value);
|
2019-04-15 02:22:08 +02:00
|
|
|
|
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; }
|
|
|
|
|
2019-05-28 11:53:16 +02:00
|
|
|
bool sync();
|
2019-04-15 02:22:08 +02:00
|
|
|
|
|
|
|
void remove_group(const String& group);
|
|
|
|
void remove_entry(const String& group, const String& key);
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
String filename() const { return m_filename; }
|
2019-05-24 21:10:23 -07:00
|
|
|
|
2019-04-15 02:22:08 +02:00
|
|
|
private:
|
2021-04-29 21:46:15 +02:00
|
|
|
explicit ConfigFile(const String& filename);
|
2019-04-15 02:22:08 +02:00
|
|
|
|
|
|
|
void reparse();
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
String m_filename;
|
2019-04-15 02:22:08 +02:00
|
|
|
HashMap<String, HashMap<String, String>> m_groups;
|
|
|
|
bool m_dirty { false };
|
|
|
|
};
|
2020-02-02 12:34:39 +01:00
|
|
|
|
|
|
|
}
|