summaryrefslogtreecommitdiff
path: root/libs/pbd/enumwriter.cc
blob: 7b3aba9053bafc4b956b7b4194cec809b4e97915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
    Copyright (C) 2006 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    $Id$
*/

#include <cctype>
#include <algorithm>

#include <cstring>
#include <cstdlib>

#include "pbd/enumwriter.h"
#include "pbd/error.h"
#include "pbd/compose.h"

using namespace std;
using namespace PBD;

#include "pbd/i18n.h"

EnumWriter* EnumWriter::_instance = 0;
map<string,string> EnumWriter::hack_table;

static int
nocase_cmp(const string & s1, const string& s2)
{
	string::const_iterator it1 = s1.begin();
	string::const_iterator it2 = s2.begin();

	while ((it1 != s1.end()) && (it2 != s2.end())) {
		if(::toupper(*it1) != ::toupper(*it2))  {//letters differ?
			// return -1 to indicate 'smaller than', 1 otherwise
			return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
		}

		++it1;
		++it2;
	}

	string::size_type size1 = s1.size();
	string::size_type size2 = s2.size();

	//return -1,0 or 1 according to strings' lengths

	if (size1 == size2) {
		return 0;
	}

	return (size1 < size2) ? -1 : 1;
}

EnumWriter&
EnumWriter::instance()
{
	if (_instance == 0) {
		_instance = new EnumWriter;
	}

	return *_instance;
}

void
EnumWriter::destroy ()
{
	delete _instance;
	_instance = 0;
}

EnumWriter::EnumWriter ()
{
}

EnumWriter::~EnumWriter ()
{
}

void
EnumWriter::register_distinct (string type, vector<int> v, vector<string> s)
{
	pair<string,EnumRegistration> newpair;
	pair<Registry::iterator,bool> result;

	newpair.first = type;
	newpair.second = EnumRegistration (v, s, false);

	result = registry.insert (newpair);

	if (!result.second) {
		warning << string_compose (_("enum type \"%1\" already registered with the enum writer"), type) << endmsg;
	}
}

void
EnumWriter::register_bits (string type, vector<int> v, vector<string> s)
{
	pair<string,EnumRegistration> newpair;
	pair<Registry::iterator,bool> result;

	newpair.first = type;
	newpair.second = EnumRegistration (v, s, true);

	result = registry.insert (newpair);

	if (!result.second) {
		warning << _("enum type \"%1\" already registered with the enum writer") << endmsg;
	}
}

string
EnumWriter::write (string type, int value)
{
	Registry::iterator x = registry.find (type);

	if (x == registry.end()) {
		error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
		throw unknown_enumeration (type);
	}

	if (x->second.bitwise) {
		return write_bits (x->second, value);
	} else {
		return write_distinct (x->second, value);
	}
}

int
EnumWriter::read (string type, string value)
{
	Registry::iterator x = registry.find (type);

	if (x == registry.end()) {
		error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
		throw unknown_enumeration (type);
	}

	if (x->second.bitwise) {
		return read_bits (x->second, value);
	} else {
		return read_distinct (x->second, value);
	}
}

string
EnumWriter::write_bits (EnumRegistration& er, int value)
{
	vector<int>::iterator i;
	vector<string>::iterator s;
	string result;

	for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
		if (value & (*i)) {
			if (!result.empty()) {
				result += ',';
			}
			result += (*s);
		}
	}

	return result;
}

string
EnumWriter::write_distinct (EnumRegistration& er, int value)
{
	vector<int>::iterator i;
	vector<string>::iterator s;

	for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
		if (value == (*i)) {
			return (*s);
		}
	}

	return string();
}

int
EnumWriter::validate (EnumRegistration& er, int val) const
{
        if (er.values.empty()) {
                return val;
        }

        if (val == 0) {
                /* zero is always a legal value for our enumerations, just about
                 */
                return val;
        }

        vector<int>::iterator i;
        string enum_name = _("unknown enumeration");

        for (Registry::const_iterator x = registry.begin(); x != registry.end(); ++x) {
                if (&er == &(*x).second) {
                        enum_name = (*x).first;
                }
        }


        for (i = er.values.begin(); i != er.values.end(); ++i) {
                if (*i == val) {
                        return val;
                }
        }

        warning << string_compose (_("Illegal value loaded for %1 (%2) - %3 used instead"),
                                   enum_name, val, er.names.front())
                << endmsg;
        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)
{
	vector<int>::iterator i;
	vector<string>::iterator s;
	int result = 0;
	bool found = false;
	string::size_type comma;

	/* catch old-style hex numerics */

	if (str.length() > 2 && str[0] == '0' && str[1] == 'x') {
		int val = strtol (str.c_str(), (char **) 0, 16);
                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_bitwise (er, val);
        }

	do {

		comma = str.find_first_of (',');
		string segment = str.substr (0, comma);

		for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
			if (segment == *s || nocase_cmp (segment, *s) == 0) {
				result |= (*i);
				found = true;
			}
		}

		if (comma == string::npos) {
			break;
		}

		str = str.substr (comma+1);

	} while (true);

	if (!found) {
		throw unknown_enumeration (str);
	}

	return result;
}

int
EnumWriter::read_distinct (EnumRegistration& er, string str)
{
	vector<int>::iterator i;
	vector<string>::iterator s;

	/* first, check to see if there a hack for the name we're looking up */

	map<string,string>::iterator x;

	if ((x  = hack_table.find (str)) != hack_table.end()) {

		cerr << "found hack for " << str << " = " << x->second << endl;

		str = x->second;

		for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
			if (str == (*s) || nocase_cmp (str, *s) == 0) {
				return (*i);
			}
		}
	}

	/* catch old-style hex numerics */

	if (str.length() > 2 && str[0] == '0' && str[1] == 'x') {
		int val = strtol (str.c_str(), (char **) 0, 16);
                return validate (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);
        }

	for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
		if (str == (*s) || nocase_cmp (str, *s) == 0) {
			return (*i);
		}
	}

	throw unknown_enumeration(str);
}

void
EnumWriter::add_to_hack_table (string str, string hacked)
{
	hack_table[str] = hacked;
}