mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:27:35 +00:00
LibCore: Make ConfigFile parsing work for non-null-terminated strings
This is necessary for converting it to Core::Stream.
This commit is contained in:
parent
c7f8c20f8b
commit
d9fb1b8c2e
1 changed files with 26 additions and 17 deletions
|
@ -1,6 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
* Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
|
* Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
|
||||||
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -10,7 +11,6 @@
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/StandardPaths.h>
|
#include <LibCore/StandardPaths.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
@ -82,32 +82,41 @@ void ConfigFile::reparse()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto* cp = line.characters();
|
size_t i = 0;
|
||||||
|
|
||||||
while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n'))
|
while (i < line.length() && (line[i] == ' ' || line[i] == '\t' || line[i] == '\n'))
|
||||||
++cp;
|
++i;
|
||||||
|
|
||||||
switch (*cp) {
|
// EOL
|
||||||
case '\0': // EOL...
|
if (i >= line.length())
|
||||||
case '#': // Comment, skip entire line.
|
continue;
|
||||||
case ';': // -||-
|
|
||||||
|
switch (line[i]) {
|
||||||
|
case '#': // Comment, skip entire line.
|
||||||
|
case ';': // -||-
|
||||||
continue;
|
continue;
|
||||||
case '[': { // Start of new group.
|
case '[': { // Start of new group.
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
++cp; // Skip the '['
|
++i; // Skip the '['
|
||||||
while (*cp && (*cp != ']'))
|
while (i < line.length() && (line[i] != ']')) {
|
||||||
builder.append(*(cp++));
|
builder.append(line[i]);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
current_group = &m_groups.ensure(builder.to_string());
|
current_group = &m_groups.ensure(builder.to_string());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: { // Start of key{
|
default: { // Start of key
|
||||||
StringBuilder key_builder;
|
StringBuilder key_builder;
|
||||||
StringBuilder value_builder;
|
StringBuilder value_builder;
|
||||||
while (*cp && (*cp != '='))
|
while (i < line.length() && (line[i] != '=')) {
|
||||||
key_builder.append(*(cp++));
|
key_builder.append(line[i]);
|
||||||
++cp; // Skip the '='
|
++i;
|
||||||
while (*cp && (*cp != '\n'))
|
}
|
||||||
value_builder.append(*(cp++));
|
++i; // Skip the '='
|
||||||
|
while (i < line.length() && (line[i] != '\n')) {
|
||||||
|
value_builder.append(line[i]);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
if (!current_group) {
|
if (!current_group) {
|
||||||
// We're not in a group yet, create one with the name ""...
|
// We're not in a group yet, create one with the name ""...
|
||||||
current_group = &m_groups.ensure("");
|
current_group = &m_groups.ensure("");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue