2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ConfigFile.h>
|
|
|
|
#include <LibCore/File.h>
|
2020-04-19 17:57:05 +00:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2019-04-15 00:22:08 +00:00
|
|
|
#include <pwd.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <stdio.h>
|
2019-04-15 00:22:08 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2020-08-17 14:43:15 +00:00
|
|
|
NonnullRefPtr<ConfigFile> ConfigFile::get_for_lib(const String& lib_name)
|
|
|
|
{
|
|
|
|
String directory = StandardPaths::config_directory();
|
|
|
|
auto path = String::format("%s/lib/%s.ini", directory.characters(), lib_name.characters());
|
|
|
|
|
|
|
|
return adopt(*new ConfigFile(path));
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
NonnullRefPtr<ConfigFile> ConfigFile::get_for_app(const String& app_name)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2020-08-05 15:18:59 +00:00
|
|
|
String directory = StandardPaths::config_directory();
|
|
|
|
auto path = String::format("%s/%s.ini", directory.characters(), app_name.characters());
|
2020-02-02 11:34:39 +00:00
|
|
|
return adopt(*new ConfigFile(path));
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
NonnullRefPtr<ConfigFile> ConfigFile::get_for_system(const String& app_name)
|
2019-05-25 04:10:23 +00:00
|
|
|
{
|
|
|
|
auto path = String::format("/etc/%s.ini", app_name.characters());
|
2020-02-02 11:34:39 +00:00
|
|
|
return adopt(*new ConfigFile(path));
|
2019-05-25 04:10:23 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
NonnullRefPtr<ConfigFile> ConfigFile::open(const String& path)
|
2019-11-11 08:39:01 +00:00
|
|
|
{
|
2020-02-02 11:34:39 +00:00
|
|
|
return adopt(*new ConfigFile(path));
|
2019-11-11 08:39:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
ConfigFile::ConfigFile(const String& file_name)
|
2019-04-15 00:22:08 +00:00
|
|
|
: m_file_name(file_name)
|
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
reparse();
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
ConfigFile::~ConfigFile()
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
sync();
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void ConfigFile::reparse()
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
m_groups.clear();
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
auto file = File::construct(m_file_name);
|
|
|
|
if (!file->open(IODevice::OpenMode::ReadOnly))
|
2019-04-15 00:22:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
HashMap<String, String>* current_group = nullptr;
|
|
|
|
|
2019-09-21 18:50:06 +00:00
|
|
|
while (file->can_read_line()) {
|
|
|
|
auto line = file->read_line(BUFSIZ);
|
2019-09-30 06:57:01 +00:00
|
|
|
auto* cp = (const char*)line.data();
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n'))
|
|
|
|
++cp;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
|
|
|
switch (*cp) {
|
2019-05-28 09:53:16 +00:00
|
|
|
case '\0': // EOL...
|
|
|
|
case '#': // Comment, skip entire line.
|
|
|
|
case ';': // -||-
|
|
|
|
continue;
|
|
|
|
case '[': { // Start of new group.
|
|
|
|
StringBuilder builder;
|
|
|
|
++cp; // Skip the '['
|
|
|
|
while (*cp && (*cp != ']'))
|
|
|
|
builder.append(*(cp++));
|
|
|
|
current_group = &m_groups.ensure(builder.to_string());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: { // Start of key{
|
|
|
|
StringBuilder key_builder;
|
|
|
|
StringBuilder value_builder;
|
|
|
|
while (*cp && (*cp != '='))
|
|
|
|
key_builder.append(*(cp++));
|
|
|
|
++cp; // Skip the '='
|
|
|
|
while (*cp && (*cp != '\n'))
|
|
|
|
value_builder.append(*(cp++));
|
|
|
|
if (!current_group) {
|
|
|
|
// We're not in a group yet, create one with the name ""...
|
|
|
|
current_group = &m_groups.ensure("");
|
|
|
|
}
|
|
|
|
current_group->set(key_builder.to_string(), value_builder.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
String ConfigFile::read_entry(const String& group, const String& key, const String& default_value) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
if (!has_key(group, key)) {
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
auto it = m_groups.find(group);
|
|
|
|
auto jt = it->value.find(key);
|
|
|
|
return jt->value;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
int ConfigFile::read_num_entry(const String& group, const String& key, int default_value) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
if (!has_key(group, key)) {
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
|
2020-06-12 19:07:52 +00:00
|
|
|
return read_entry(group, key).to_int().value_or(default_value);
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool ConfigFile::read_bool_entry(const String& group, const String& key, bool default_value) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2020-04-22 18:10:54 +00:00
|
|
|
auto value = read_entry(group, key, default_value ? "1" : "0");
|
|
|
|
if (value == "1" || value.to_lowercase() == "true")
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void ConfigFile::write_entry(const String& group, const String& key, const String& value)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
m_groups.ensure(group).ensure(key) = value;
|
2019-05-28 09:53:16 +00:00
|
|
|
m_dirty = true;
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void ConfigFile::write_num_entry(const String& group, const String& key, int value)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2019-07-03 12:56:27 +00:00
|
|
|
write_entry(group, key, String::number(value));
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
void ConfigFile::write_bool_entry(const String& group, const String& key, bool value)
|
2019-07-01 05:54:30 +00:00
|
|
|
{
|
|
|
|
write_entry(group, key, value ? "1" : "0");
|
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
void ConfigFile::write_color_entry(const String& group, const String& key, Color value)
|
2019-05-25 04:10:23 +00:00
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
write_entry(group, key, String::format("%d,%d,%d,%d", value.red(), value.green(), value.blue(), value.alpha()));
|
2019-05-25 04:10:23 +00:00
|
|
|
}
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool ConfigFile::sync()
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
if (!m_dirty)
|
2019-05-28 09:53:16 +00:00
|
|
|
return true;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
FILE* fp = fopen(m_file_name.characters(), "wb");
|
2019-04-15 00:22:08 +00:00
|
|
|
if (!fp)
|
2019-05-28 09:53:16 +00:00
|
|
|
return false;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
|
|
|
for (auto& it : m_groups) {
|
|
|
|
fprintf(fp, "[%s]\n", it.key.characters());
|
|
|
|
for (auto& jt : it.value)
|
|
|
|
fprintf(fp, "%s=%s\n", jt.key.characters(), jt.value.characters());
|
|
|
|
fprintf(fp, "\n");
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-04-15 00:22:08 +00:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
m_dirty = false;
|
|
|
|
return true;
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void ConfigFile::dump() const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
for (auto& it : m_groups) {
|
|
|
|
printf("[%s]\n", it.key.characters());
|
|
|
|
for (auto& jt : it.value)
|
|
|
|
printf("%s=%s\n", jt.key.characters(), jt.value.characters());
|
|
|
|
printf("\n");
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Vector<String> ConfigFile::groups() const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
return m_groups.keys();
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Vector<String> ConfigFile::keys(const String& group) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
auto it = m_groups.find(group);
|
|
|
|
if (it == m_groups.end())
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-04-15 00:22:08 +00:00
|
|
|
return it->value.keys();
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool ConfigFile::has_key(const String& group, const String& key) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
auto it = m_groups.find(group);
|
|
|
|
if (it == m_groups.end())
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-04-15 00:22:08 +00:00
|
|
|
return it->value.contains(key);
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool ConfigFile::has_group(const String& group) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
return m_groups.contains(group);
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void ConfigFile::remove_group(const String& group)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
m_groups.remove(group);
|
2019-05-28 09:53:16 +00:00
|
|
|
m_dirty = true;
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void ConfigFile::remove_entry(const String& group, const String& key)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
auto it = m_groups.find(group);
|
|
|
|
if (it == m_groups.end())
|
|
|
|
return;
|
|
|
|
it->value.remove(key);
|
2019-05-28 09:53:16 +00:00
|
|
|
m_dirty = true;
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
|
|
|
}
|