File: value.cc

package info (click to toggle)
littlewizard 1.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,436 kB
  • sloc: sh: 9,389; cpp: 7,685; xml: 420; makefile: 380; awk: 40; ansic: 38
file content (629 lines) | stat: -rw-r--r-- 13,733 bytes parent folder | download | duplicates (4)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
 *  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.
 */

 /* (C) Marcin Kwadrans <quarter@users.sourceforge.net> */

#include <cstdlib>
#include <cstring>
#include "include/support.h"
#include "include/variable.h"
#include "include/environment.h"

#define MAKE_TWIN(v) LWValue *twin = (FALSE == (v)->isSpecial()) ? v : (v)->get(); \
					 const gboolean needRelease = (TRUE == v->isSpecial());

#define FREE_TWIN if (needRelease == TRUE) delete twin;

#define MAKE_TWIN2(v) LWValue *twin2 = (FALSE == (v)->isSpecial()) ? v : (v)->get(); \
					  const gboolean needRelease2 = (TRUE == (v)->isSpecial());

#define FREE_TWINS  if (needRelease == TRUE) delete twin; \
					if (needRelease2 == TRUE) delete twin2;

static int pixmapToInt (LWPixmap *pixmap)
{
	g_return_val_if_fail (pixmap != NULL, 0);

	gchar *s = pixmap->getName ();
	
	if (!strcmp(s, "true"))
		return 1;
	
	if (!strcmp (s, "false"))
		return 0;
	
	return atoi (s);
}

static int _isInt (LWPixmap *pixmap)
{
	g_return_val_if_fail (pixmap != NULL, 0);
	
	gchar *s = pixmap->getName ();
	return (s[0] >= '0' && s[0] <= '9') ? TRUE : FALSE;
}

static LWPixmap *intToPixmap (gint n)
{
	g_return_val_if_fail (NULL != LWEnvironment::getPixmapSet(), NULL);
	g_return_val_if_fail (n >= 0, NULL);
	
	gchar *s = g_strdup_printf ("%i", n);
	LWPixmap *pixmap = LWEnvironment::getPixmapSet()->getPixmap(s);
	g_free (s);
	return pixmap;
}

static LWPixmap *getMinusSign ()
{
	g_return_val_if_fail (NULL != LWEnvironment::getPixmapSet(), NULL);
	return LWEnvironment::getPixmapSet()->getPixmap("-");
}

void LWValue::convertToList ()
{
	g_return_if_fail (TRUE == canBeIndexed());
	
	if (is_list == FALSE) {
		list = g_slist_append (NULL, new LWValue (pixmap));
		is_list = TRUE;
	}
}

	
/*! \brief Konstruktor pustej wartości

	Tworzy pustą wartość
*/
LWValue::LWValue () : is_list(TRUE), list(NULL)
{
}

/*! \brief Konstruktor wartości z ikoną

	Tworzy wartość zawierającą pojedynczą ikonę
*/
LWValue::LWValue (LWPixmap *a_pixmap): is_list(FALSE), pixmap(a_pixmap)
{
}

/*! \brief Konstruktor kopiujący

	Konstruktor kopiujący wartości

	\param value Wartość na podstawie której ma powstać duplikat
*/
LWValue::LWValue (const LWValue *value): is_list(TRUE), list(NULL)
{
	set ( (LWValue *) value);
}

/*! \brief Konstruktor wartości na podstawie liczby

	Tworzy wartość zawierającą listę ikon będącą odpowiednikiem liczby
	\param ivalue Liczba 
*/
LWValue::LWValue (gint ivalue) : is_list(TRUE), list(NULL)
{
	gboolean hasminus = (ivalue < 0) ? TRUE : FALSE;
	
	if (hasminus == TRUE)
		ivalue = - ivalue;
	
	do {
		LWPixmap *pixmap = intToPixmap(ivalue % 10);
		list = g_slist_prepend (list, (gpointer) new LWValue(pixmap));
		ivalue /= 10;
	} while (ivalue > 0);
	
	if (hasminus == TRUE) {
		LWPixmap *pixmap = getMinusSign ();
		list = g_slist_prepend (list, (gpointer) new LWValue(pixmap));
	}
}

/*! \brief Destruktor

	Niszczy wartość
*/	
LWValue::~LWValue ()
{
	clear();
}

/*! \brief Czyszczenie wartości

	Czyści wartość niszcząc przy tym wszystkie ikony wchodzące w jej skład
*/	
void LWValue::clear ()
{
	if (TRUE == isSpecial()) return;

	if (is_list == TRUE) {
		
		for (GSList *l=list; l != NULL; l = l->next) {
			LWValue *value = (LWValue *) l->data;
			delete value;
		}
		
		g_slist_free (list);
	}
	
	is_list = TRUE;
	list = NULL;
}

/*! \brief Dołączenie ikony

	Dołącza ikonę do końca listy przechywowanej w wartości
	\param pixmap Ikona
*/
void LWValue::append (LWPixmap *pixmap)
{
	g_return_if_fail (FALSE == isSpecial());
	
	if (TRUE == canBeIndexed ())
		convertToList();
	
	if (is_list == TRUE)
		list = g_slist_append (list, 
			(TRUE == isVariable()) ? 
				new LWVariable(pixmap) : new LWValue(pixmap));
}

/*! \brief Łączenie wartości

	Dołącza listę przechywaną w value na koniec listy przechywowanej w wartości
	\param value Warość dołączana
*/
void LWValue::concat (LWValue *value)
{
	g_return_if_fail (value != NULL);
	MAKE_TWIN (value)
	
	if (TRUE == canBeIndexed ())
		convertToList();
	
	if (is_list == FALSE) 
		return;

	if (twin->is_list == FALSE)
		list = g_slist_append (list, (TRUE == isVariable()) ?
			new LWVariable(twin->pixmap) : new LWValue(twin->pixmap));
	else	
	for (GSList *l= twin->list; l != NULL; l = l->next) {
		LWValue *v = (LWValue *) l->data;
		
		if (TRUE == isVariable())
			list = (v->is_list == TRUE) ? 
				g_slist_append (list, new LWValue (v->pixmap)) :
					g_slist_append (list, new LWValue (v));
		else
			list = (v->is_list == TRUE) ? 
				g_slist_append (list, new LWVariable (v->pixmap)) :
					g_slist_append (list, new LWVariable (v));
	}
		
	FREE_TWIN
}

/*! \brief Interpretacja jako liczba

	Interpretuje wartość jako liczbę
	\return Liczba
*/
gint LWValue::toInt ()
{
	MAKE_TWIN(this)
	
	if (twin->is_list == FALSE)
		return (twin->pixmap != NULL) ? pixmapToInt (twin->pixmap) : 0;
			
	gboolean isminus = FALSE;
	gint ivalue = 0;
	GSList *l;
	
	for (l = twin->list; l != NULL; l = l->next) {
		LWValue *v = (LWValue *) l->data;
		if (v->is_list == TRUE) return 0;
		if (v->pixmap == NULL) continue;
		if (getMinusSign () == v->pixmap) isminus = TRUE; 
		else if (pixmapToInt( (LWPixmap *) v->pixmap) > 0) break;
		}
		
	for (; l != NULL; l = l->next) {
		LWValue *v = (LWValue *) l->data;
		if (v->is_list == TRUE || v->pixmap == NULL || FALSE == _isInt(v->pixmap)) break;
		ivalue *= 10; 
		ivalue += pixmapToInt( (LWPixmap *) v->pixmap);
	}

	FREE_TWIN
	
	return (isminus == FALSE) ? ivalue : -ivalue;
}

/*! \brief Czy wartość indeksowana

	Czy wartość może być indeksowana,
	zwraca prawdę. Metoda może być przeciążona
	w implementacji specjalnych wartości 
	\return Czy wartość może być indeksowana
*/
gboolean LWValue::canBeIndexed ()
{
	return TRUE;
}

/*! \brief Pobierz indeks

	Pobiera wartość znajdującą sie pod indeksem
	\param n Indeks do pobrania (liczony od 1)
	\return Pobrany indeks
*/
LWValue *LWValue::getIndex (guint n)
{
	g_return_val_if_fail (n > 0, new LWValue());
	g_return_val_if_fail (TRUE == canBeIndexed(), new LWValue());
	
	if (is_list == FALSE) 
		return (n == 1) ? new LWValue (pixmap) : new LWValue (); //this);
		
	LWValue *value = (LWValue *) g_slist_nth_data (list, n-1);
	return (value != NULL) ? new LWValue (value) : new LWValue();
}

void LWValue::deleteIndex (guint n)
{
	g_return_if_fail (n > 0);
	g_return_if_fail (TRUE == canBeIndexed());
	g_return_if_fail (FALSE == isSpecial());
		
	if (is_list == FALSE) {
		convertToList ();
	}
		
	GSList *l = g_slist_nth (list, n-1);
	
	if (l != NULL) {
		if (l->data)
			delete (LWValue *) l->data;
		
		list = g_slist_delete_link (list, l);
	}
}

/*! \brief Pobiera listę ikon

	Pobiera listę ikon znajdującą się w wartości
	\return Pobrana lista
*/
GSList *LWValue::getListPixmap ()
{
	MAKE_TWIN(this)
	
	if (twin->is_list == FALSE) {
		return g_slist_append (NULL, twin->pixmap);
	}
	
	GSList *list_pixmap=NULL;
	
	for (GSList *l = twin->list; l != NULL; l = l->next) {
		LWValue *v = (LWValue *) l->data;
		if (v->is_list == TRUE) break;
		list_pixmap = g_slist_append (list_pixmap, v->pixmap);
	}
	
	FREE_TWIN
	
	return list_pixmap;	
}


/*! \brief Pobranie piksmapy

	Pobranie piksmapy zawartej w wartości
	Wartość nie może być jest listą, lecz pojedynczą piksamapą,
	ewentualnie wartość może przechowywać listę jednoelementową
	\return Pobrana piksamapa
*/
LWPixmap *LWValue::getPixmap ()
{
LWPixmap *p=NULL;
	
	MAKE_TWIN(this)
	
	if (FALSE == twin->is_list) {
		p = twin->pixmap;
	} else
	if (twin->list != NULL)
		if (twin->list->next == NULL) {
			LWValue *v = (LWValue *) twin->list->data;
			if (FALSE == v->is_list)
				p = v->pixmap;
		}

	FREE_TWIN
	return p;
}

/*! \brief Set the value 

	Set the value basing on another value
	\param value The value that should be used for assigment
*/
void LWValue::set (LWValue *value)
{
	g_return_if_fail (value != NULL);
	
	if (this == value) return;
	
	MAKE_TWIN(value)
	
	if (FALSE == twin->is_list) {
		pixmap = twin->pixmap;
		is_list = FALSE;
		FREE_TWIN
		return;
	}

	if (twin->list != NULL)
		if (twin->list->next == NULL) {
			LWValue *v = (LWValue *) twin->list->data;
			if (FALSE == v->is_list) {
				pixmap = v->pixmap;
				is_list = FALSE;
				FREE_TWIN
				return;
			}
		}
				
	clear();
	
	for (GSList *l = twin->list; l != NULL; l = l->next) {
		LWValue *v = (LWValue *) l->data;
		list = g_slist_append (list, 
			(gpointer) ((TRUE == isVariable()) ?
				new LWVariable(v) : new LWValue (v)));
	}
	
	FREE_TWIN
}

/*! \brief Get the copy of the value

	It gets the copy of the value
	\return Copy of a value
*/
LWValue *LWValue::get()
{
	return new LWValue(this);
}

/*! \brief Counting elements in a list
	
	Counts elements in a list, if value is a pixmap it returns 1.
	\return Number of elements in a list
*/
guint LWValue::count ()
{
	MAKE_TWIN(this)
	
	guint count = (twin->is_list == TRUE) ? g_slist_length (twin->list) : 1;
	
	FREE_TWIN
	
	return count;
}

/*! \brief Checking if value is a integer
	
	Checks if value is a integer (string with only digits and optional minus sign at first position).
	\return Result of of test.
*/
gboolean LWValue::isInt ()
{
	MAKE_TWIN(this)
	
	if (twin->is_list == FALSE) {
		gboolean r = (twin->pixmap != NULL)	? _isInt (twin->pixmap) : FALSE;
		FREE_TWIN
		return r;
	}
			
	GSList *l = twin->list;
		
	if (l == NULL) {
		FREE_TWIN
		return FALSE;
	}
				
	LWValue *v = (LWValue *) l->data;
	
	if (v->is_list == FALSE)	
		if (getMinusSign () == v->pixmap)
			l = l->next;
		
	for (; l != NULL; l = l->next) {
		v = (LWValue *) l->data;
		
		if (v->is_list == TRUE || v->pixmap == NULL || _isInt((LWPixmap *) v->pixmap) == FALSE) {
			FREE_TWIN	
			return FALSE;
		}
	}

	FREE_TWIN
	
	return TRUE;
}

/*! \brief Checking if the value fits in the integer range
	
	Checks if the value can be converted to "gint" without an overflow.
	\return True if can fit in integer range
*/
gboolean LWValue::checkRange ()
{
	MAKE_TWIN(this)
	
	if (twin->is_list == FALSE) {
		FREE_TWIN 
		return TRUE;
	}

	GSList *l = twin->list;

	LWValue *v = (LWValue *) l->data;
	
	if (v->is_list == TRUE) {
		FREE_TWIN 
		return TRUE;
	}

	guint limit;

	if (getMinusSign () == v->pixmap) {
		gint min = G_MININT;
		limit = (guint) -min;
		l = l->next;
	} else {
		limit = (guint) G_MAXINT;
	}
	
	for (; l != NULL && limit > 0; l = l->next, limit /= 10) {
		v = (LWValue *) l->data;
				
		if (v->is_list == TRUE || v->pixmap == NULL || _isInt((LWPixmap *) v->pixmap) == FALSE) {
			FREE_TWIN	
			return TRUE;
		}
			
		if (limit < 10 && limit < (guint) pixmapToInt((LWPixmap *) v->pixmap)) {
			FREE_TWIN	
			return FALSE;
		}
	}

	FREE_TWIN

	return (limit > 0) ? TRUE : FALSE;
}

/*! \brief Comparation of the values
	
	The smart way of comparing of the values. It can be used to numerical or
	lexigraphical sorting depends on the contet of values.
	It can be used for comparing single values or whole arrays.

	\param value The compared value
	\return -1, 0 or 1 depends on the result of comparation.
*/
gint LWValue::compare (LWValue *value)
{
	if (this == value) return 0;
	
	MAKE_TWIN(this)
	MAKE_TWIN2(value)
	
	if (twin->isInt() == TRUE && twin2->isInt() == TRUE) {
		gint i1 = twin->toInt();
		gint i2 = twin2->toInt();
		FREE_TWINS
		return (i1 == i2) ? 0 : ((i1 < i2) ? -1 : 1);
	}
	
	if (FALSE == twin->is_list && FALSE == twin2->is_list) {
		gchar *s1 = (twin->pixmap != NULL) ? twin->pixmap->getName() : (gchar *) " ";
		gchar *s2 = (twin2->pixmap != NULL) ? twin2->pixmap->getName() : (gchar *) " ";
		gint r = strcmp (s1, s2);
		FREE_TWINS
		return r;
	}
	
	if (TRUE == twin->is_list && FALSE == twin2->is_list) {
		gint r;
		
		if (twin->list != NULL) {
			LWValue *v = twin->getIndex(1);
			r = v->compare(twin2);
			if (r == 0) r = 1;
			if (!v->isVariable()) delete v;
		} else 
			r = -1;
			
		FREE_TWINS
		return r;
	}
	
	if (FALSE == twin->is_list && TRUE == twin2->is_list) {
		gint r;
	
		if (twin2->list != NULL) {
			LWValue *v = twin2->getIndex(1);
			r = twin->compare(v);
			if (r == 0) r = -1;
			if (!v->isVariable()) delete v;
		} else r = 1;
					
		FREE_TWINS
		return r;
	}
		
	GSList *l = twin->list;
	GSList *l2 = twin2->list;	

	for (; l != NULL && l2 != NULL; l = l->next, l2 = l2->next) {
		LWValue *t1 = (LWValue *) l->data;
		LWValue *t2 = (LWValue *) l2->data;
		
		guint r = t1->compare (t2);
		
		if (r == 0) continue;
			
		FREE_TWINS
		
		return r;
	}
	
	FREE_TWINS
	
	return (l == NULL && l2 == NULL) ? 0 : ((l != NULL) ? 1 : -1);
}

/*! \brief Is it a variable

	Is it a variable ?
	\return Not overwritten method returns always FALSE
*/
gboolean LWValue::isVariable ()
{
	return FALSE;
}

/*! \brief Is it a special value
	
	Is it a special value?
	Method overwritten in implementation of special values (f.i world variable)
	The "special" means that the content is not stored directly in a value,
	but the value class is only some kind of interface that behaves like an
	value, but the real content is stored somewhere else. For special value 
	at least "set" and "get" methods should work correctly, other methods
	internally should use copy generated by "get" method, 
	which is no longer special value.

	\return Not overwritten method returns always FALSE
*/
gboolean LWValue::isSpecial ()
{
	return FALSE;
}