2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "pbd/enumwriter.h"
28 #include "pbd/error.h"
29 #include "pbd/compose.h"
36 EnumWriter* EnumWriter::_instance = 0;
37 map<string,string> EnumWriter::hack_table;
40 nocase_cmp(const string & s1, const string& s2)
42 string::const_iterator it1 = s1.begin();
43 string::const_iterator it2 = s2.begin();
45 while ((it1 != s1.end()) && (it2 != s2.end())) {
46 if(::toupper(*it1) != ::toupper(*it2)) {//letters differ?
47 // return -1 to indicate 'smaller than', 1 otherwise
48 return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
55 string::size_type size1 = s1.size();
56 string::size_type size2 = s2.size();
58 //return -1,0 or 1 according to strings' lengths
64 return (size1 < size2) ? -1 : 1;
68 EnumWriter::instance()
71 _instance = new EnumWriter;
78 EnumWriter::destroy ()
84 EnumWriter::EnumWriter ()
88 EnumWriter::~EnumWriter ()
93 EnumWriter::register_distinct (string type, vector<int> v, vector<string> s)
95 pair<string,EnumRegistration> newpair;
96 pair<Registry::iterator,bool> result;
99 newpair.second = EnumRegistration (v, s, false);
101 result = registry.insert (newpair);
103 if (!result.second) {
104 warning << string_compose (_("enum type \"%1\" already registered with the enum writer"), type) << endmsg;
109 EnumWriter::register_bits (string type, vector<int> v, vector<string> s)
111 pair<string,EnumRegistration> newpair;
112 pair<Registry::iterator,bool> result;
114 newpair.first = type;
115 newpair.second = EnumRegistration (v, s, true);
117 result = registry.insert (newpair);
119 if (!result.second) {
120 warning << _("enum type \"%1\" already registered with the enum writer") << endmsg;
125 EnumWriter::write (string type, int value)
127 Registry::iterator x = registry.find (type);
129 if (x == registry.end()) {
130 error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
131 throw unknown_enumeration (type);
134 if (x->second.bitwise) {
135 return write_bits (x->second, value);
137 return write_distinct (x->second, value);
142 EnumWriter::read (string type, string value)
144 Registry::iterator x = registry.find (type);
146 if (x == registry.end()) {
147 error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
148 throw unknown_enumeration (type);
151 if (x->second.bitwise) {
152 return read_bits (x->second, value);
154 return read_distinct (x->second, value);
159 EnumWriter::write_bits (EnumRegistration& er, int value)
161 vector<int>::iterator i;
162 vector<string>::iterator s;
165 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
167 if (!result.empty()) {
178 EnumWriter::write_distinct (EnumRegistration& er, int value)
180 vector<int>::iterator i;
181 vector<string>::iterator s;
183 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
193 EnumWriter::validate (EnumRegistration& er, int val) const
195 if (er.values.empty()) {
200 /* zero is always a legal value for our enumerations, just about
205 vector<int>::iterator i;
206 string enum_name = _("unknown enumeration");
208 for (Registry::const_iterator x = registry.begin(); x != registry.end(); ++x) {
209 if (&er == &(*x).second) {
210 enum_name = (*x).first;
215 for (i = er.values.begin(); i != er.values.end(); ++i) {
221 warning << string_compose (_("Illegal value loaded for %1 (%2) - %3 used instead"),
222 enum_name, val, er.names.front())
224 return er.values.front();
228 EnumWriter::validate_bitwise (EnumRegistration& er, int val) const
231 for (int p = 1; p <= val; p = p << 1) {
232 if (std::find (er.values.begin(), er.values.end(), p) == er.values.end()) {
243 EnumWriter::read_bits (EnumRegistration& er, string str)
245 vector<int>::iterator i;
246 vector<string>::iterator s;
249 string::size_type comma;
251 /* catch old-style hex numerics */
253 if (str.length() > 2 && str[0] == '0' && str[1] == 'x') {
254 int val = strtol (str.c_str(), (char **) 0, 16);
255 return validate_bitwise (er, val);
258 /* catch old style dec numerics */
260 if (strspn (str.c_str(), "0123456789") == str.length()) {
261 int val = strtol (str.c_str(), (char **) 0, 10);
262 return validate_bitwise (er, val);
267 comma = str.find_first_of (',');
268 string segment = str.substr (0, comma);
270 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
271 if (segment == *s || nocase_cmp (segment, *s) == 0) {
277 if (comma == string::npos) {
281 str = str.substr (comma+1);
286 throw unknown_enumeration (str);
293 EnumWriter::read_distinct (EnumRegistration& er, string str)
295 vector<int>::iterator i;
296 vector<string>::iterator s;
298 /* first, check to see if there a hack for the name we're looking up */
300 map<string,string>::iterator x;
302 if ((x = hack_table.find (str)) != hack_table.end()) {
304 cerr << "found hack for " << str << " = " << x->second << endl;
308 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
309 if (str == (*s) || nocase_cmp (str, *s) == 0) {
315 /* catch old-style hex numerics */
317 if (str.length() > 2 && str[0] == '0' && str[1] == 'x') {
318 int val = strtol (str.c_str(), (char **) 0, 16);
319 return validate (er, val);
322 /* catch old style dec numerics */
324 if (strspn (str.c_str(), "0123456789") == str.length()) {
325 int val = strtol (str.c_str(), (char **) 0, 10);
326 return validate (er, val);
329 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
330 if (str == (*s) || nocase_cmp (str, *s) == 0) {
335 throw unknown_enumeration(str);
339 EnumWriter::add_to_hack_table (string str, string hacked)
341 hack_table[str] = hacked;