File: Convert.cpp

package info (click to toggle)
basic256 2.0.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 15,076 kB
  • sloc: cpp: 16,791; yacc: 3,979; lex: 1,446; makefile: 25
file content (321 lines) | stat: -rwxr-xr-x 9,405 bytes parent folder | download
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
#include "Convert.h"
#include "Error.h"

#include <string>


int Convert::e = ERROR_NONE;

Convert::Convert(QLocale *applocale) {
	SETTINGS;
	decimaldigits = settings.value(SETTINGSDECDIGS, SETTINGSDECDIGSDEFAULT).toInt();
	if(decimaldigits < SETTINGSDECDIGSMIN || decimaldigits > SETTINGSDECDIGSMAX)
		decimaldigits = SETTINGSDECDIGSDEFAULT;
	floattail = settings.value(SETTINGSFLOATTAIL, SETTINGSFLOATTAILDEFAULT).toBool();
	replaceDecimalPoint = settings.value(SETTINGSFLOATLOCALE, SETTINGSFLOATLOCALEDEFAULT).toBool();

	// build international safe regular expression for numbers
	locale = applocale;
	replaceDecimalPoint = replaceDecimalPoint && locale->decimalPoint()!='.'; //use locale decimal point only if !="."
	decimalPoint = (replaceDecimalPoint?locale->decimalPoint():'.');
	isnumericexpression = QString("^[-+]?[0-9]*") + decimalPoint + QString("?[0-9]+([eE][-+]?[0-9]+)?$");
	musicalnote.setPattern("^(do|re|mi|fa|sol|la|si|c|d|e|f|g|a|b|h|ni|pa|vu|ga|di|ke|zo)([-]?[0-9]+)?(#{1,2}|b{1,2})?$");
	musicalnote.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
}

Convert::~Convert() {
}


bool Convert::isNumeric(DataElement *d) {
	// return true if this is a number or can convert to one
	// if there is white space around the string number IT IS NOT NUMERIC
	if (d) {
		if (d->type == T_INT || d->type == T_FLOAT) {
			return true;
		} else if (d->type == T_STRING) {
			if (d->stringval.length()!=0) {
				// using isnumeric as private (like musicalnote) increase time for all functions
				// eg. getBool() increased time elapsed from 10200ms to 12500ms
				// Benchmark: convert->getBool() for 2.700.000.000 times
				// ...I have no explanation for this...
				QRegularExpression isnumeric(isnumericexpression);
				QRegularExpressionMatch match = isnumeric.match(d->stringval);
				return match.hasMatch();
			}
		}
	}
	return false;
}


int Convert::getBool(DataElement *d) {
	// BASIC56 uses 1 for true and 0 for false
	// a number not zero is 1 otherwise 0
	// a non empty string is 1 otherwise 0
	// an unassigned variable is 0
	// an array throws an error and is 0
	if (d) {
		if (d->type == T_INT) {
			return (d->intval!=0l);
		} else if (d->type == T_FLOAT) {
			return ((d->floatval < -EPSILON || d->floatval > EPSILON)?1:0);
		} else if (d->type == T_STRING) {
			return ((d->stringval.length()!=0)?1:0);
		} else {
			// anything else  = false
			e = ERROR_BOOLEANCONV;
			return 0;
		}
	}
	return 0;
}

int Convert::getInt(DataElement *d) {
	long l=getLong(d);
	if (l<INT_MIN||l>INT_MAX) {
		e = ERROR_INTEGERRANGE;
		l = 0;
	}
	return (int) l;
}

long Convert::getLong(DataElement *d) {
	long i=0;
	if (d) {
		if (d->type == T_INT) {
			i = d->intval;
		} else if (d->type == T_FLOAT) {
			double f = d->floatval + (d->floatval>0.0?EPSILON:-EPSILON);
			if (f<LONG_MIN||f>LONG_MAX) {
				e = ERROR_LONGRANGE;
			} else {
				i = (long) f;
			}
		} else if (d->type == T_STRING) {
			if (d->stringval.length()!=0) {
				bool ok;
				i = d->stringval.toLong(&ok);
				if(!ok) {
					i = 0;
					e = ERROR_NUMBERCONV;
				}
			} else {
				e = ERROR_NUMBERCONV;
			}
		} else{
			e = ERROR_NUMBERCONV;
		}
	}
	return i;
}

double Convert::getFloat(DataElement *d) {
	double f = 0.0;
	if (d) {
		if (d->type == T_FLOAT) {
			return(d->floatval);
		} else if (d->type == T_INT) {
			return(d->intval);
		} else if (d->type == T_STRING) {
			if (d->stringval.length()!=0) {
				bool ok;
				if(replaceDecimalPoint){
					f = locale->toDouble(d->stringval, &ok);
				}else{
					f = d->stringval.toDouble(&ok);
				}
				if(!ok) {
					f = 0.0;
					e = ERROR_NUMBERCONV;
				}
			}else {
				e = ERROR_NUMBERCONV;
			}
		} else {
			e = ERROR_NUMBERCONV;
		}
	}
	return f;
}

double Convert::getMusicalNote(DataElement *d) {
	double f = 0.0;
	if (d) {
		if (d->type == T_FLOAT) {
			f = d->floatval;
		} else if (d->type == T_INT) {
			f = d->intval;
		}else if (d->type == T_STRING) {
			QRegularExpressionMatch match = musicalnote.match(d->stringval);
			if (match.hasMatch()) {
				QString note, octave, accidentals;
				int oct = 4, acc = 0, n;
				double s;
				note = match.captured(1).toLower();
				n = notesmap[note];
				octave = match.captured(2);
				if(octave!="") oct = octave.toInt();
				accidentals = match.captured(3).toLower();
				if(accidentals=="#") acc = 1;
				if(accidentals=="##") acc = 2;
				if(accidentals=="b") acc = -1;
				if(accidentals=="bb") acc = -2;

				s=oct*12+n+acc-57; //if s==0 then s==440Hz
				f=pow(2.0,s/12.0)*440;
			}else{
				if (d->stringval.length()!=0) {
					bool ok;
					if(replaceDecimalPoint){
						f = locale->toDouble(d->stringval, &ok);
					}else{
						f = d->stringval.toDouble(&ok);
					}
					if(!ok) {
						f = 0.0;
						e = ERROR_STRING2NOTE;
					}
				}else {
					e = ERROR_STRING2NOTE;
				}
			}
			} else {
				e = ERROR_STRINGCONV;
			}
		}
	return f;
}

QString Convert::getString(DataElement *d) {
	return getString(d, decimaldigits);
}

QString Convert::getString(DataElement *d, int ddigits) {
	QString s = "";
	if (d) {
		if (d->type == T_STRING) {
			s = d->stringval;
		} else if (d->type == T_INT) {
			s = QString::number(d->intval);
		} else if (d->type == T_FLOAT) {
			if(d->floatval >= -EPSILON && d->floatval <= EPSILON){
				//this is almost 0.0
				//If you add 0.1/-0.1 you will never get a perfect 0.0 again
				if(floattail)
					s = QStringLiteral("0") + decimalPoint + QStringLiteral("0");
				else
					s = QStringLiteral("0");
			}else{
				double xp = log10(d->floatval*(d->floatval<0.0?-1.0:1.0)); // size in powers of 10
				//check if adding of ".0" will exceed the number of digits to print numbers
				if(((int)xp)==ddigits-1 && floattail){
					s.setNum(d->floatval,'e',ddigits - 1);
					s.replace(QRegExp(QStringLiteral("0+e")), QStringLiteral("e"));
					s.replace(QStringLiteral(".e"), QStringLiteral(".0e"));
					if(replaceDecimalPoint){
						s.replace('.', decimalPoint);
					}
				}else if (xp*2<-ddigits || (int)xp>=(ddigits-(floattail?1:0))) { //number is too small or too big to show in original form
					s.setNum(d->floatval,'g',ddigits);
					if(replaceDecimalPoint){
						s.replace('.', decimalPoint);
					}
				} else {
					int dig = ddigits - (xp>0.0?(int)xp+1:1);
					//double precision sucks
					//if we need to print more than 7 decimals there are chances to print numbers as 2.300000001 instead of 2.3
					if(dig>7){
						s.setNum(d->floatval,'f',7);
						if(!s.endsWith(QStringLiteral("0"))) // we cannot reduce it
							s.setNum(d->floatval,'f',dig);
					}else{
						s.setNum(d->floatval,'f',dig);
					}
					if (s.contains('.',Qt::CaseInsensitive)) {
						while(s.endsWith(QStringLiteral("0"))) s.chop(1);
						if(s.endsWith(QStringLiteral("."))){
							s.chop(1);
							if(floattail)
								s.append(decimalPoint + '0');
						}else if(replaceDecimalPoint){
							s.replace('.', decimalPoint);
						}
					}else if(floattail){
						s.append(decimalPoint + '0');
					}
				}
			}
		} else {
			e = ERROR_STRINGCONV;
		}
	}
	return s;
}


int Convert::compareFloats(double fone, double ftwo) {
	// return 1 if one>two  0 if one==two or -1 if one<two
	// USE FOR ALL COMPARISON WITH FLOATS
	// used a small number (epsilon) to make sure that
	// decimal precission errors are ignored
	if (fabs(fone - ftwo)<=EPSILON) return 0;
	else if (fone < ftwo) return -1;
	else return 1;
}

int Convert::compare(DataElement *one, DataElement* two) {
	// complex compare logic - compare two stack types with each other
	// return 1 if one>two  0 if one==two or -1 if one<two
	//
	if (one&&two) {
		if (one->type == T_INT && two->type == T_INT) {
			// if both are integers comapre as integers
			long ans = one->intval - two->intval;
			if (ans==0) return 0;
			else if (ans<0) return -1;
			else return 1;
		} else if (one->type == T_STRING && two->type == T_STRING) {
			// if both are strings - [compare them as strings] strcmp
			int ans = one->stringval.compare(two->stringval);
			if (ans==0) return 0;
			else if (ans<0) return -1;
			else return 1;
		} else {
			// if either a float number then compare as floats
			double ftwo = getFloat(two);
			double fone = getFloat(one);
			return compareFloats(fone, ftwo);
		}
	} else {
		return 0;
	}
}

QPolygonF *Convert::getPolygonF(DataElement *d) {
	// returns a QPolygonF - used in several graphics OPcodes
	QPolygonF *poly = new QPolygonF();
	if (d->type==T_ARRAY) {
		if (d->arrayRows()==1 || d->arrayCols()==2) {
			if (d->arrayRows()*d->arrayCols()>=6) {
				if (d->arrayRows()==1) {
					for(int col = 0; col < d->arrayCols(); col+=2){
						poly->append(QPointF(getFloat(d->arrayGetData(0,col)), getFloat(d->arrayGetData(0,col+1))));
					}
				} else {
					for(int row = 0; row < d->arrayRows(); row++){
						poly->append(QPointF(getFloat(d->arrayGetData(row,0)), getFloat(d->arrayGetData(row,1))));
					}
				}
			} else {
				e = ERROR_POLYPOINTS;
			}
		} else {
			e = ERROR_ARRAYEVEN;
		}
	} else {
		e = ERROR_ARRAYEXPR;
	}
	//fprintf(stderr,"getPolygon %d %d %d\n", d->arrayRows(), d->arrayCols(), poly->size());
	return poly;
}