Set a 50-character limit on the saved size of option section and name.

Allows use of istream::getline() to retrieve the values, simplifying
delimiter detection.
This commit is contained in:
John Ralls 2019-12-05 17:54:02 -08:00
parent d7a2a0ffff
commit 6ab5618b76
3 changed files with 13 additions and 9 deletions

View File

@ -119,6 +119,9 @@ enum GncOptionUIType
static const char* commodity_scm_intro{"'(commodity-scm "};
size_t constexpr classifier_size_max{50};
size_t constexpr sort_tag_size_max{10};
struct OptionClassifier
{
std::string m_section;

View File

@ -124,13 +124,13 @@ GncOptionDB::set_option_from_ui(const char* section, const char* name,
std::optional<std::reference_wrapper<GncOptionSection>>
GncOptionDB::find_section(const char* section)
GncOptionDB::find_section(const std::string& section)
{
auto db_section = std::find_if(
m_sections.begin(), m_sections.end(),
[section](GncOptionSection sect) -> bool
[&section](GncOptionSection sect) -> bool
{
return sect.first == std::string{section};
return section.compare(0, classifier_size_max, sect.first) == 0;
});
if (db_section == m_sections.end())
return std::nullopt;
@ -138,16 +138,17 @@ GncOptionDB::find_section(const char* section)
}
std::optional<std::reference_wrapper<GncOption>>
GncOptionDB::find_option(const char* section, const char* name) const
GncOptionDB::find_option(const std::string& section, const std::string& name) const
{
auto db_section = const_cast<GncOptionDB*>(this)->find_section(section);
if (!db_section)
return std::nullopt;
auto db_opt = std::find_if(
db_section->get().second.begin(), db_section->get().second.end(),
[name](GncOption& option) -> bool
[&name](GncOption& option) -> bool
{
return option.get_name() == std::string{name};
return name.compare(0, classifier_size_max - 1,
option.get_name()) == 0;
});
if (db_opt == db_section->get().second.end())
return std::nullopt;

View File

@ -83,13 +83,13 @@ public:
// void set_selectable(const char* section, const char* name);
void make_internal(const char* section, const char* name);
void commit();
std::optional<std::reference_wrapper<GncOptionSection>> find_section(const char* section);
std::optional<std::reference_wrapper<GncOption>> find_option(const char* section, const char* name) {
std::optional<std::reference_wrapper<GncOptionSection>> find_section(const std::string& section);
std::optional<std::reference_wrapper<GncOption>> find_option(const std::string& section, const std::string& name) {
return static_cast<const GncOptionDB&>(*this).find_option(section, name);
}
std::optional<std::reference_wrapper<GncOption>> find_option(const char* section, const char* name) const;
private:
std::ostream& serialize_option_scheme(std::ostream& oss,
std::optional<std::reference_wrapper<GncOption>> find_option(const std::string& section, const std::string& name) const;
const char* option_prolog,
const char* section, const char* name) const noexcept;
std::ostream& serialize_option_key_value(std::ostream& oss,