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.
26 #include "pbd/enumwriter.h"
27 #include "pbd/error.h"
28 #include "pbd/compose.h"
35 EnumWriter* EnumWriter::_instance = 0;
36 map<string,string> EnumWriter::hack_table;
39 nocase_cmp(const string & s1, const string& s2)
41 string::const_iterator it1 = s1.begin();
42 string::const_iterator it2 = s2.begin();
44 while ((it1 != s1.end()) && (it2 != s2.end())) {
45 if(::toupper(*it1) != ::toupper(*it2)) {//letters differ?
46 // return -1 to indicate 'smaller than', 1 otherwise
47 return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
54 string::size_type size1 = s1.size();
55 string::size_type size2 = s2.size();
57 //return -1,0 or 1 according to strings' lengths
63 return (size1 < size2) ? -1 : 1;
66 EnumWriter::EnumWriter ()
73 EnumWriter::~EnumWriter ()
78 EnumWriter::register_distinct (string type, vector<int> v, vector<string> s)
80 pair<string,EnumRegistration> newpair;
81 pair<Registry::iterator,bool> result;
84 newpair.second = EnumRegistration (v, s, false);
86 result = registry.insert (newpair);
89 warning << string_compose (_("enum type \"%1\" already registered with the enum writer"), type) << endmsg;
94 EnumWriter::register_bits (string type, vector<int> v, vector<string> s)
96 pair<string,EnumRegistration> newpair;
97 pair<Registry::iterator,bool> result;
100 newpair.second = EnumRegistration (v, s, true);
102 result = registry.insert (newpair);
104 if (!result.second) {
105 warning << _("enum type \"%1\" already registered with the enum writer") << endmsg;
110 EnumWriter::write (string type, int value)
112 Registry::iterator x = registry.find (type);
114 if (x == registry.end()) {
115 error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
116 throw unknown_enumeration();
119 if (x->second.bitwise) {
120 return write_bits (x->second, value);
122 return write_distinct (x->second, value);
127 EnumWriter::read (string type, string value)
129 Registry::iterator x = registry.find (type);
131 if (x == registry.end()) {
132 error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
133 throw unknown_enumeration();
136 if (x->second.bitwise) {
137 return read_bits (x->second, value);
139 return read_distinct (x->second, value);
144 EnumWriter::write_bits (EnumRegistration& er, int value)
146 vector<int>::iterator i;
147 vector<string>::iterator s;
150 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
152 if (!result.empty()) {
163 EnumWriter::write_distinct (EnumRegistration& er, int value)
165 vector<int>::iterator i;
166 vector<string>::iterator s;
168 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
178 EnumWriter::read_bits (EnumRegistration& er, string str)
180 vector<int>::iterator i;
181 vector<string>::iterator s;
184 string::size_type comma;
186 /* catch old-style hex numerics */
188 if (str.length() > 2 && str[0] == '0' && str[1] == 'x') {
189 return strtol (str.c_str(), (char **) 0, 16);
192 /* catch old style dec numerics */
194 if (strspn (str.c_str(), "0123456789") == str.length()) {
195 return strtol (str.c_str(), (char **) 0, 10);
200 comma = str.find_first_of (',');
201 string segment = str.substr (0, comma);
203 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
204 if (segment == *s || nocase_cmp (segment, *s) == 0) {
210 if (comma == string::npos) {
214 str = str.substr (comma+1);
219 throw unknown_enumeration();
226 EnumWriter::read_distinct (EnumRegistration& er, string str)
228 vector<int>::iterator i;
229 vector<string>::iterator s;
231 /* catch old-style hex numerics */
233 if (str.length() > 2 && str[0] == '0' && str[1] == 'x') {
234 return strtol (str.c_str(), (char **) 0, 16);
237 /* catch old style dec numerics */
239 if (strspn (str.c_str(), "0123456789") == str.length()) {
240 return strtol (str.c_str(), (char **) 0, 10);
243 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
244 if (str == (*s) || nocase_cmp (str, *s) == 0) {
249 /* failed to find it as-is. check to see if there a hack for the name we're looking up */
251 map<string,string>::iterator x;
253 if ((x = hack_table.find (str)) != hack_table.end()) {
255 cerr << "found hack for " << str << " = " << x->second << endl;
259 for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
260 if (str == (*s) || nocase_cmp (str, *s) == 0) {
266 throw unknown_enumeration();
270 EnumWriter::add_to_hack_table (string str, string hacked)
272 hack_table[str] = hacked;