Skip to content

Commit

Permalink
Set a 50-character limit on the saved size of option section and name.
Browse files Browse the repository at this point in the history
Allows use of istream::getline() to retrieve the values, simplifying
delimiter detection.
  • Loading branch information
jralls committed Jul 11, 2021
1 parent d7a2a0f commit 6ab5618
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
3 changes: 3 additions & 0 deletions libgnucash/app-utils/gnc-option.hpp
Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions libgnucash/app-utils/gnc-optiondb.cpp
Expand Up @@ -124,30 +124,31 @@ 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;
return *db_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;
Expand Down
6 changes: 3 additions & 3 deletions libgnucash/app-utils/gnc-optiondb.hpp
Expand Up @@ -83,13 +83,13 @@ class GncOptionDB
// 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,
Expand Down

0 comments on commit 6ab5618

Please sign in to comment.