From b66bf67030207b3a3a5e9e57731f21dfbf85e4ea Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 5 Sep 2015 00:00:16 +0200 Subject: [PATCH] fix bitwise enum parsing --- libs/pbd/enumwriter.cc | 23 +++++++++++++++++++---- libs/pbd/pbd/enumwriter.h | 4 ++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/libs/pbd/enumwriter.cc b/libs/pbd/enumwriter.cc index f67d305080..0cfe42a0c3 100644 --- a/libs/pbd/enumwriter.cc +++ b/libs/pbd/enumwriter.cc @@ -189,7 +189,7 @@ EnumWriter::write_distinct (EnumRegistration& er, int value) } int -EnumWriter::validate (EnumRegistration& er, int val) +EnumWriter::validate (EnumRegistration& er, int val) const { if (er.values.empty()) { return val; @@ -204,7 +204,7 @@ EnumWriter::validate (EnumRegistration& er, int val) vector::iterator i; string enum_name = _("unknown enumeration"); - for (Registry::iterator x = registry.begin(); x != registry.end(); ++x) { + for (Registry::const_iterator x = registry.begin(); x != registry.end(); ++x) { if (&er == &(*x).second) { enum_name = (*x).first; } @@ -223,6 +223,21 @@ EnumWriter::validate (EnumRegistration& er, int val) return er.values.front(); } +int +EnumWriter::validate_bitwise (EnumRegistration& er, int val) const +{ + int result = 0; + for (int p = 1; p <= val; p = p << 1) { + if (std::find (er.values.begin(), er.values.end(), p) == er.values.end()) { + continue; + } + if (p & val) { + result |= p; + } + } + return result; +} + int EnumWriter::read_bits (EnumRegistration& er, string str) { @@ -236,14 +251,14 @@ EnumWriter::read_bits (EnumRegistration& er, string str) if (str.length() > 2 && str[0] == '0' && str[1] == 'x') { int val = strtol (str.c_str(), (char **) 0, 16); - return validate (er, val); + return validate_bitwise (er, val); } /* catch old style dec numerics */ if (strspn (str.c_str(), "0123456789") == str.length()) { int val = strtol (str.c_str(), (char **) 0, 10); - return validate (er, val); + return validate_bitwise (er, val); } do { diff --git a/libs/pbd/pbd/enumwriter.h b/libs/pbd/pbd/enumwriter.h index 634767e7f2..0be9b7be19 100644 --- a/libs/pbd/pbd/enumwriter.h +++ b/libs/pbd/pbd/enumwriter.h @@ -88,8 +88,8 @@ class LIBPBD_API EnumWriter { static EnumWriter* _instance; static std::map hack_table; - - int validate (EnumRegistration& er, int value); + int validate (EnumRegistration& er, int value) const; + int validate_bitwise (EnumRegistration& er, int value) const; }; }