File: DataElement.cpp

package info (click to toggle)
basic256 2.0.99.10-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,888 kB
  • sloc: cpp: 17,185; yacc: 4,025; lex: 1,466; java: 1,091; sh: 39; xml: 33; makefile: 20
file content (360 lines) | stat: -rw-r--r-- 8,236 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "DataElement.h"
#include "Settings.h"

#include <string>

int DataElement::e = ERROR_NONE;

void DataElement::init() {
	// initialize dataelement common stuff
	e=0;
	type = T_UNASSIGNED;
	arr = NULL;
	map = NULL;
}

DataElement::DataElement() {
	// create an empty dataelement
	init();
}

DataElement::DataElement(QString s) {
	init();
	type = T_STRING;
	stringval = s;
}

DataElement::DataElement(int i) {
	// 32 bit integer stored on stack as a 64 bit long
	init();
    type = T_INT;
    intval = i;
}


DataElement::DataElement(unsigned int i) {
	// 32 bit integer stored on stack as a 64 bit long
	init();
    type = T_INT;
    intval = i;
}

DataElement::DataElement(long l) {
	init();
	type = T_INT;
	intval = l;
}

DataElement::DataElement(double d) {
	init();
	type = T_FLOAT;
	floatval = d;
}


DataElement::DataElement(DataElement *de) {
	init();
	copy(de);
}

DataElement::~DataElement() {
	clear();
}

void DataElement::copy(DataElement *source) {
	// fill an existing from as a copy of another
	// it is as fast as it can be
	// unused values of strings can be found as garbage but this is the cost of speed
	// source->stringval.clear() is too expensive to be used for each assignment/copy
	clear();
	if (source) {
		type = source->type;
		switch (source->type){
			case T_UNASSIGNED:
				break;
			case T_FLOAT:
				floatval = source->floatval;
				break;
			case T_STRING:
				stringval = source->stringval;
				break;
			case T_ARRAY:
				{
					// copy array elements from one old array to a new one
					if (!arr)
						arr = new DataElementArray;
					int i = source->arr->xdim * source->arr->ydim;
					arr->xdim = source->arr->xdim;
					arr->ydim = source->arr->ydim;
					arr->data.resize(i);
					while(i-- > 0) {
						arr->data[i] = new DataElement(source->arr->data[i]);
					}
				}
				break;
			case T_MAP:
				{
#ifdef DEBUG
fprintf(stderr,"de copy map source len %d\n",source->map->data.size());
#endif
					if (!map)
						map = new DataElementMap;
					for (std::map<std::string, DataElement*>::iterator it = source->map->data.begin(); it != source->map->data.end(); it++ ) {
						mapSetData(it->first, it->second);
					}
				}
				break;
			case T_REF:
				intval = source->intval;
				level = source->level;
				break;
			case T_INT:
				intval = source->intval;
		}
	}
}

void DataElement::clear() {
	//fprintf(stderr, "DataElement clear %s\n", debug().toStdString().c_str());
	switch (type){
		//case T_STRING:
		//	stringval.clear();
		//	break;
		case T_ARRAY:
			if (arr) {
				int i = arr->xdim * arr->ydim;
				while(i-- > 0) {
					if (arr->data[i]) delete arr->data[i];
					arr->data[i] = NULL;
				}
				arr->data.clear();
				delete(arr);
				arr = NULL;
			}
			break;
		case T_MAP:
			if (map) {
				for (std::map<std::string, DataElement*>::iterator it = map->data.begin(); it != map->data.end(); it++ ) {
					if (it->second) delete it->second;
					it->second = NULL;
				}
				map->data.clear();
				delete(map);
				map = NULL;
			}
			break;
	}
	type = T_UNASSIGNED;
}

int DataElement::getType(DataElement* e) {
	if (e) {
		return e->type;
	} else {
		return T_UNASSIGNED;
	}
}

int DataElement::getError() {
	return getError(false);
}

int DataElement::getError(int clear) {
	int olde = e;
	if (clear) e = ERROR_NONE;
	return olde;
}

QString DataElement::debug() {
	// return a string representing the DataElement contents
	if(type==T_INT) return("int(" + QString::number(intval) + ") ");
	if(type==T_REF) return("varref(" + QString::number(intval) + ", level: " + QString::number(level) + ") ");
	if(type==T_FLOAT) return("float(" + QString::number(floatval) + ") ");
	if(type==T_STRING) return("string(" + stringval + ") ");
	if(type==T_ARRAY) return("array(" + QString::number(intval) + ")");
	if(type==T_MAP) return("MAP("+ QString::number(map->data.size()) + ")");
	if(type==T_UNASSIGNED) return("unused ");
	return("badtype(" + QString::number(type) + ")");
}

void DataElement::arrayDim(const int xdim, const int ydim, const bool redim) {
	const int size = xdim * ydim;
	
	if (size <= MAXARRAYSIZE) {
		if (size >= 1) {
			if (type != T_ARRAY || !redim) {
				// if array data is dim or redim without a dim then create a new one (clear the old)
				clear();
				arr = new DataElementArray;
				arr->data.resize(size, NULL);
				type = T_ARRAY;
			}else{
				// redim - resize the vector
				arr->data.resize(size, NULL);
			}

			arr->xdim = xdim;
			arr->ydim = ydim;
		} else {
			e = ERROR_ARRAYSIZESMALL;
		}
	} else {
		e = ERROR_ARRAYSIZELARGE;
	}
}

int DataElement::arrayRows() {
	// return number of columns of array as if it was a two dimensional array - 0 = not an array
	if (type == T_ARRAY) {
		return(arr->xdim);
	} else if (type==T_UNASSIGNED){
		e = ERROR_VARNOTASSIGNED;
	} else {
		e = ERROR_NOTARRAY;
	}
	return(0);
}

int DataElement::arrayCols() {
	// return number of rows of array as if it was a two dimensional array - 0 = not an array
	if (type == T_ARRAY) {
		return(arr->ydim);
	} else if (type==T_UNASSIGNED){
		e = ERROR_VARNOTASSIGNED;
	} else {
		e = ERROR_NOTARRAY;
	}
	return(0);
}

DataElement* DataElement::arrayGetData(const int x, const int y) {
	// get data from array elements from map (using x, y)
	// if there is an error return an unassigned value
	// DO NOT DELETE ****** COPY OF THE DATAELEMENT INTERNAL STORAGE
	DataElement *d;
	if (type == T_ARRAY) {
		if (x >=0 && x < arr->xdim && y >=0 && y < arr->ydim) {
			const int i = x * arr->ydim + y;
			if (arr->data[i]) {
				return arr->data[i];
			} else {
				e = ERROR_VARNOTASSIGNED;
			}
		} else {
			e = ERROR_ARRAYINDEX;
		}
	} else {
		e = ERROR_NOTARRAY;
	}
	return NULL;
}

void DataElement::arraySetData(const int x, const int y, DataElement *d) {
	// DataElement's data is copied and it should be deleted
	// by whomever created it
	if (type == T_ARRAY) {
		if (x >=0 && x < arr->xdim && y >=0 && y < arr->ydim) {
			const int i = x * arr->ydim + y;
			if (!arr->data[i]) arr->data[i] = new DataElement();
			arr->data[i]->copy(d);
		} else {
			e = ERROR_ARRAYINDEX;
		}
	} else {
		e = ERROR_NOTARRAY;
	}
}

void DataElement::arrayUnassign(const int x, const int y) {
	if (type == T_ARRAY) {
		if (x >=0 && x < arr->xdim && y >=0 && y < arr->ydim) {
			const int i = x * arr->ydim + y;
			if (arr->data[i])  {
				arr->data[i]->clear();
			}
		} else {
			e = ERROR_ARRAYINDEX;
		}
	} else {
		e = ERROR_NOTARRAY;
	}
}



void DataElement::mapDim(){
	clear();
	map = new DataElementMap();
	type=T_MAP;
}

DataElement* DataElement::mapGetData(QString qkey){
	// DO NOT DELETE ****** COPY OF THE DATAELEMENT INTERNAL STORAGE
	DataElement *d;
	if (type==T_MAP) {
		std::string key = qkey.toUtf8().constData();
		//fprintf(stdout, "mapGetData key = %s\n", key.c_str());
		if (map->data.count(key)) {
			return map->data[key];
		} else {
			e = ERROR_MAPKEY;
		}
	} else {
		e = ERROR_NOTMAP;
	}
	return NULL;
}

void DataElement::mapSetData(QString qkey, DataElement *d){
	mapSetData(qkey.toStdString(), d);
}

void DataElement::mapSetData(std::string key, DataElement *d){
	if (type==T_MAP) {
		if (!map->data.count(key)) {
			map->data[key] = new DataElement();
		}
		map->data[key]->copy(d);
	} else {
		e = ERROR_NOTMAP;
	}
}

void DataElement::mapUnassign(QString qkey){
	if (type==T_MAP) {
		std::string key = qkey.toUtf8().constData();
		if (map->data.count(key)) {
			delete map->data[key];
			map->data.erase(key);
		} else {
			e = ERROR_MAPKEY;
		}
	} else {
		e = ERROR_NOTMAP;
	}
}

int DataElement::mapLength(){
	int l = 0;
	if (type==T_MAP) {
		l = map->data.size();
	} else {
		e = ERROR_NOTMAP;
	}
	return l;
}

bool DataElement::mapKey(QString qkey){
	// is the key in the map
	if (type==T_MAP) {
		std::string key = qkey.toUtf8().constData();
		if (map->data.count(key)) {
			return true;
		} else {
			e = ERROR_MAPKEY;
		}
	} else {
		e = ERROR_NOTMAP;
	}
	return false;
}