config/Data: add methods AddParam(), AddBlock()
This commit is contained in:
parent
95481dda86
commit
8df98932b1
@ -42,6 +42,26 @@ ConfigData::Clear()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gcc_nonnull_all
|
||||||
|
static void
|
||||||
|
Append(ConfigParam *&head, ConfigParam *p)
|
||||||
|
{
|
||||||
|
assert(p->next == nullptr);
|
||||||
|
|
||||||
|
auto **i = &head;
|
||||||
|
while (*i != nullptr)
|
||||||
|
i = &(*i)->next;
|
||||||
|
|
||||||
|
*i = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ConfigData::AddParam(ConfigOption option,
|
||||||
|
std::unique_ptr<ConfigParam> param) noexcept
|
||||||
|
{
|
||||||
|
Append(params[size_t(option)], param.release());
|
||||||
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
ConfigData::GetString(ConfigOption option,
|
ConfigData::GetString(ConfigOption option,
|
||||||
const char *default_value) const noexcept
|
const char *default_value) const noexcept
|
||||||
@ -123,6 +143,26 @@ ConfigData::GetBool(ConfigOption option, bool default_value) const
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gcc_nonnull_all
|
||||||
|
static void
|
||||||
|
Append(ConfigBlock *&head, ConfigBlock *p)
|
||||||
|
{
|
||||||
|
assert(p->next == nullptr);
|
||||||
|
|
||||||
|
auto **i = &head;
|
||||||
|
while (*i != nullptr)
|
||||||
|
i = &(*i)->next;
|
||||||
|
|
||||||
|
*i = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ConfigData::AddBlock(ConfigBlockOption option,
|
||||||
|
std::unique_ptr<ConfigBlock> block) noexcept
|
||||||
|
{
|
||||||
|
Append(blocks[size_t(option)], block.release());
|
||||||
|
}
|
||||||
|
|
||||||
const ConfigBlock *
|
const ConfigBlock *
|
||||||
ConfigData::FindBlock(ConfigBlockOption option,
|
ConfigData::FindBlock(ConfigBlockOption option,
|
||||||
const char *key, const char *value) const
|
const char *key, const char *value) const
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
struct ConfigParam;
|
struct ConfigParam;
|
||||||
struct ConfigBlock;
|
struct ConfigBlock;
|
||||||
@ -35,6 +36,9 @@ struct ConfigData {
|
|||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
|
|
||||||
|
void AddParam(ConfigOption option,
|
||||||
|
std::unique_ptr<ConfigParam> param) noexcept;
|
||||||
|
|
||||||
gcc_pure
|
gcc_pure
|
||||||
const ConfigParam *GetParam(ConfigOption option) const noexcept {
|
const ConfigParam *GetParam(ConfigOption option) const noexcept {
|
||||||
return params[size_t(option)];
|
return params[size_t(option)];
|
||||||
@ -77,6 +81,9 @@ struct ConfigData {
|
|||||||
|
|
||||||
bool GetBool(ConfigOption option, bool default_value) const;
|
bool GetBool(ConfigOption option, bool default_value) const;
|
||||||
|
|
||||||
|
void AddBlock(ConfigBlockOption option,
|
||||||
|
std::unique_ptr<ConfigBlock> block) noexcept;
|
||||||
|
|
||||||
gcc_pure
|
gcc_pure
|
||||||
const ConfigBlock *GetBlock(ConfigBlockOption option) const noexcept {
|
const ConfigBlock *GetBlock(ConfigBlockOption option) const noexcept {
|
||||||
return blocks[size_t(option)];
|
return blocks[size_t(option)];
|
||||||
|
@ -74,7 +74,7 @@ config_read_name_value(ConfigBlock &block, char *input, unsigned line)
|
|||||||
block.AddBlockParam(name, std::move(value), line);
|
block.AddBlockParam(name, std::move(value), line);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConfigBlock *
|
static std::unique_ptr<ConfigBlock>
|
||||||
config_read_block(BufferedReader &reader)
|
config_read_block(BufferedReader &reader)
|
||||||
{
|
{
|
||||||
std::unique_ptr<ConfigBlock> block(new ConfigBlock(reader.GetLineNumber()));
|
std::unique_ptr<ConfigBlock> block(new ConfigBlock(reader.GetLineNumber()));
|
||||||
@ -96,7 +96,7 @@ config_read_block(BufferedReader &reader)
|
|||||||
if (*line != 0 && *line != CONF_COMMENT)
|
if (*line != 0 && *line != CONF_COMMENT)
|
||||||
throw std::runtime_error("Unknown tokens after '}'");
|
throw std::runtime_error("Unknown tokens after '}'");
|
||||||
|
|
||||||
return block.release();
|
return block;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse name and value */
|
/* parse name and value */
|
||||||
@ -106,19 +106,6 @@ config_read_block(BufferedReader &reader)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gcc_nonnull_all
|
|
||||||
static void
|
|
||||||
Append(ConfigBlock *&head, ConfigBlock *p)
|
|
||||||
{
|
|
||||||
assert(p->next == nullptr);
|
|
||||||
|
|
||||||
auto **i = &head;
|
|
||||||
while (*i != nullptr)
|
|
||||||
i = &(*i)->next;
|
|
||||||
|
|
||||||
*i = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ReadConfigBlock(ConfigData &config_data, BufferedReader &reader,
|
ReadConfigBlock(ConfigData &config_data, BufferedReader &reader,
|
||||||
const char *name, ConfigBlockOption o,
|
const char *name, ConfigBlockOption o,
|
||||||
@ -126,15 +113,13 @@ ReadConfigBlock(ConfigData &config_data, BufferedReader &reader,
|
|||||||
{
|
{
|
||||||
const unsigned i = unsigned(o);
|
const unsigned i = unsigned(o);
|
||||||
const ConfigTemplate &option = config_block_templates[i];
|
const ConfigTemplate &option = config_block_templates[i];
|
||||||
ConfigBlock *&head = config_data.blocks[i];
|
|
||||||
|
|
||||||
if (head != nullptr && !option.repeatable) {
|
if (!option.repeatable)
|
||||||
ConfigBlock *block = head;
|
if (const auto *block = config_data.GetBlock(o))
|
||||||
throw FormatRuntimeError("config parameter \"%s\" is first defined "
|
throw FormatRuntimeError("config parameter \"%s\" is first defined "
|
||||||
"on line %d and redefined on line %u\n",
|
"on line %d and redefined on line %u\n",
|
||||||
name, block->line,
|
name, block->line,
|
||||||
reader.GetLineNumber());
|
reader.GetLineNumber());
|
||||||
}
|
|
||||||
|
|
||||||
/* now parse the block or the value */
|
/* now parse the block or the value */
|
||||||
|
|
||||||
@ -145,22 +130,7 @@ ReadConfigBlock(ConfigData &config_data, BufferedReader &reader,
|
|||||||
if (*line != 0 && *line != CONF_COMMENT)
|
if (*line != 0 && *line != CONF_COMMENT)
|
||||||
throw std::runtime_error("Unknown tokens after '{'");
|
throw std::runtime_error("Unknown tokens after '{'");
|
||||||
|
|
||||||
auto *param = config_read_block(reader);
|
config_data.AddBlock(o, config_read_block(reader));
|
||||||
assert(param != nullptr);
|
|
||||||
Append(head, param);
|
|
||||||
}
|
|
||||||
|
|
||||||
gcc_nonnull_all
|
|
||||||
static void
|
|
||||||
Append(ConfigParam *&head, ConfigParam *p)
|
|
||||||
{
|
|
||||||
assert(p->next == nullptr);
|
|
||||||
|
|
||||||
auto **i = &head;
|
|
||||||
while (*i != nullptr)
|
|
||||||
i = &(*i)->next;
|
|
||||||
|
|
||||||
*i = p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -170,21 +140,18 @@ ReadConfigParam(ConfigData &config_data, BufferedReader &reader,
|
|||||||
{
|
{
|
||||||
const unsigned i = unsigned(o);
|
const unsigned i = unsigned(o);
|
||||||
const ConfigTemplate &option = config_param_templates[i];
|
const ConfigTemplate &option = config_param_templates[i];
|
||||||
auto *&head = config_data.params[i];
|
|
||||||
|
|
||||||
if (head != nullptr && !option.repeatable) {
|
if (!option.repeatable)
|
||||||
auto *param = head;
|
if (const auto *param = config_data.GetParam(o))
|
||||||
throw FormatRuntimeError("config parameter \"%s\" is first defined "
|
throw FormatRuntimeError("config parameter \"%s\" is first defined "
|
||||||
"on line %d and redefined on line %u\n",
|
"on line %d and redefined on line %u\n",
|
||||||
name, param->line,
|
name, param->line,
|
||||||
reader.GetLineNumber());
|
reader.GetLineNumber());
|
||||||
}
|
|
||||||
|
|
||||||
/* now parse the block or the value */
|
/* now parse the block or the value */
|
||||||
|
|
||||||
auto *param = new ConfigParam(ExpectValueAndEnd(tokenizer),
|
config_data.AddParam(o, std::make_unique<ConfigParam>(ExpectValueAndEnd(tokenizer),
|
||||||
reader.GetLineNumber());
|
reader.GetLineNumber()));
|
||||||
Append(head, param);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
Loading…
Reference in New Issue
Block a user