summaryrefslogtreecommitdiffstats
path: root/gtk/src/values.c
blob: b6be28af4adf86f6631b19f971b42234527f2f58 (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
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * presets.c
 * Copyright (C) John Stebbins 2008 <stebbins@stebbins>
 * 
 * presets.c is free software.
 * 
 * You may 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.
 * 
 */

#include <glib.h>
#include <glib-object.h>
#include <string.h>
#include "values.h"

static void dict_delete_key(gpointer data);
static void dict_delete_value(gpointer data);

GValue*
ghb_value_new(GType gtype)
{
	GValue *gval = g_malloc0(sizeof(GValue));
	g_value_init(gval, gtype);
	return gval;
}

void
ghb_value_free(GValue *gval)
{
	if (gval == NULL) return;
	g_value_unset(gval);
	g_free(gval);
}

GValue*
ghb_value_dup(const GValue *val)
{
	if (val == NULL) return NULL;
	GValue *copy = ghb_value_new(G_VALUE_TYPE(val));
	g_value_copy(val, copy);
	return copy;
}

gint
ghb_value_int(const GValue *val)
{
	if (val == NULL) return 0;
	const GValue *gval;
	GValue xform = {0,};
	if (G_VALUE_TYPE(val) != G_TYPE_INT64)
	{
		g_value_init(&xform, G_TYPE_INT64);
		if (!g_value_transform(val, &xform))
			return 0;
		gval = &xform;
	}
	else
	{
		gval = val;
	}
	return (gint)g_value_get_int64(gval);
}

gint64
ghb_value_int64(const GValue *val)
{
	if (val == NULL) return 0;
	const GValue *gval;
	GValue xform = {0,};
	if (G_VALUE_TYPE(val) != G_TYPE_INT64)
	{
		g_value_init(&xform, G_TYPE_INT64);
		if (!g_value_transform(val, &xform))
			return 0;
		gval = &xform;
	}
	else
	{
		gval = val;
	}
	return g_value_get_int64(gval);
}

gdouble
ghb_value_double(const GValue *val)
{
	if (val == NULL) return 0;
	const GValue *gval;
	GValue xform = {0,};
	if (G_VALUE_TYPE(val) != G_TYPE_DOUBLE)
	{
		g_value_init(&xform, G_TYPE_DOUBLE);
		if (!g_value_transform(val, &xform))
			return 0;
		gval = &xform;
	}
	else
	{
		gval = val;
	}
	return g_value_get_double(gval);
}

gchar*
ghb_value_string(const GValue *val)
{
	if (val == NULL) return 0;
	const GValue *gval;
	GValue xform = {0,};
	if (G_VALUE_TYPE(val) != G_TYPE_STRING)
	{
		g_value_init(&xform, G_TYPE_STRING);
		if (!g_value_transform(val, &xform))
			return NULL;
		gval = &xform;
	}
	else
	{
		gval = val;
	}
	return g_strdup(g_value_get_string(gval));
}

gboolean
ghb_value_boolean(const GValue *val)
{
	if (val == NULL) return FALSE;
	const GValue *gval;
	GValue xform = {0,};
	if (G_VALUE_TYPE(val) != G_TYPE_BOOLEAN)
	{
		g_value_init(&xform, G_TYPE_BOOLEAN);
		if (!g_value_transform(val, &xform))
			return FALSE;
		gval = &xform;
	}
	else
	{
		gval = val;
	}
	return g_value_get_boolean(gval);
}

gint
ghb_value_cmp(const GValue *vala, const GValue *valb)
{
	GType typa;
	GType typb;

	typa = G_VALUE_TYPE(vala);
	typb = G_VALUE_TYPE(valb);
	if (typa == ghb_combodata_get_type()) typa = G_TYPE_STRING;
	if (typb == ghb_combodata_get_type()) typb = G_TYPE_STRING;
	if (typa != typb)
	{
		return 1;
	}
	
	if (typa == G_TYPE_STRING)
	{
		char *stra, *strb;
		gint res;
		stra = ghb_value_string(vala);
		strb = ghb_value_string(valb);
		if (stra == NULL && strb == NULL)
			return 0;
		if (stra == NULL)
		{
			g_free(strb);
			return -1;
		}
		if (strb == NULL)
		{
			g_free(stra);
			return 1;
		}
		res =  strcmp(stra, strb);
		g_free(stra);
		g_free(strb);
		return res;
	}
	else if (typa == G_TYPE_INT64 || typa == G_TYPE_INT || 
			typa == G_TYPE_BOOLEAN)
	{
		return ghb_value_int64(vala) - ghb_value_int64(valb);
	}
	else if (typa == G_TYPE_DOUBLE || typa == G_TYPE_FLOAT)
	{
		return ghb_value_double(vala) - ghb_value_double(valb);
	}
	else if (typa == ghb_array_get_type())
	{
		// Cheating here.  Just assume they are different.
		// Maybe later I'll recurse through these
		return 1;
	}
	else if (typa == ghb_dict_get_type())
	{
		// Cheating here.  Just assume they are different.
		// Maybe later I'll recurse through these
		return 1;
	}
	else
	{
		g_warning("ghb_value_cmp: unrecognized type");
		return 1;
	}
	return 0;
}

GValue*
ghb_string_value(const gchar *str)
{
	static GValue gval = {0,};
	if (!G_IS_VALUE(&gval))
		g_value_init(&gval, G_TYPE_STRING);
	g_value_set_string(&gval, str);
	return &gval;
}

GValue*
ghb_int64_value(gint64 ival)
{
	static GValue gval = {0,};
	if (!G_IS_VALUE(&gval))
		g_value_init(&gval, G_TYPE_INT64);
	g_value_set_int64(&gval, ival);
	return &gval;
}

GValue*
ghb_int_value(gint ival)
{
	static GValue gval = {0,};
	if (!G_IS_VALUE(&gval))
		g_value_init(&gval, G_TYPE_INT64);
	g_value_set_int64(&gval, (gint64)ival);
	return &gval;
}

GValue*
ghb_double_value(gdouble dval)
{
	static GValue gval = {0,};
	if (!G_IS_VALUE(&gval))
		g_value_init(&gval, G_TYPE_DOUBLE);
	g_value_set_double(&gval, dval);
	return &gval;
}

GValue*
ghb_boolean_value(gboolean bval)
{
	static GValue gval = {0,};
	if (!G_IS_VALUE(&gval))
		g_value_init(&gval, G_TYPE_BOOLEAN);
	g_value_set_boolean(&gval, bval);
	return &gval;
}

GValue*
ghb_string_value_new(const gchar *str)
{
	if (str == NULL) str = "";
	GValue *gval = ghb_value_new(G_TYPE_STRING);
	g_value_set_string(gval, str);
	return gval;
}

GValue*
ghb_int64_value_new(gint64 ival)
{
	GValue *gval = ghb_value_new(G_TYPE_INT64);
	g_value_set_int64(gval, ival);
	return gval;
}

GValue*
ghb_int_value_new(gint ival)
{
	GValue *gval = ghb_value_new(G_TYPE_INT64);
	g_value_set_int64(gval, ival);
	return gval;
}

GValue*
ghb_double_value_new(gdouble dval)
{
	GValue *gval = ghb_value_new(G_TYPE_DOUBLE);
	g_value_set_double(gval, dval);
	return gval;
}

GValue*
ghb_boolean_value_new(gboolean bval)
{
	GValue *gval = ghb_value_new(G_TYPE_BOOLEAN);
	g_value_set_boolean(gval, bval);
	return gval;
}

GValue*
ghb_combo_value_new(
	gint index,
	const gchar *option,
	const gchar *shortOpt,
	const gchar *svalue,
	gint ivalue)
{
	GValue *gval = ghb_value_new(ghb_combodata_get_type());
    ghb_value_set_combodata(gval, index, option, shortOpt, svalue, ivalue);
	return gval;
}

GValue*
ghb_dict_value_new()
{
	GHashTable *dict;
	GValue *gval = ghb_value_new(ghb_dict_get_type());
	dict = g_hash_table_new_full(g_str_hash, g_str_equal,
								dict_delete_key, dict_delete_value);
	g_value_take_boxed(gval, dict);
	return gval;
}

GValue*
ghb_array_value_new(guint size)
{
	GValue *gval = ghb_value_new(ghb_array_get_type());
	GArray *array;

	array = g_array_sized_new(FALSE, FALSE, sizeof(GValue*), size);
	g_value_take_boxed(gval, array);
	return gval;
}

void
ghb_array_value_reset(GValue *gval, guint size)
{
	GArray *array;
	g_value_reset(gval);
	array = g_array_sized_new(FALSE, FALSE, sizeof(GValue*), size);
	g_value_take_boxed(gval, array);
}

GValue* 
ghb_date_value_new(GDate *date)
{
	GValue *gval = ghb_value_new(g_date_get_type());
	g_value_set_boxed(gval, date);
	return gval;
}

GValue* 
ghb_rawdata_value_new(ghb_rawdata_t *data)
{
	GValue *gval = ghb_value_new(ghb_rawdata_get_type());
	g_value_take_boxed(gval, data);
	return gval;
}

static gpointer
combodata_copy(gpointer boxed)
{
	const ghb_combodata_t *combodata = (const ghb_combodata_t*)boxed;
	ghb_combodata_t *copy = g_malloc0(sizeof(ghb_combodata_t));
	if (combodata->option)
		copy->option = g_strdup(combodata->option);
	if (combodata->shortOpt)
		copy->shortOpt = g_strdup(combodata->shortOpt);
	if (combodata->svalue)
		copy->svalue = g_strdup(combodata->svalue);

	copy->index = combodata->index;
	copy->ivalue = combodata->ivalue;
	return copy;
}

static void
combodata_free(gpointer boxed)
{
	ghb_combodata_t *combodata = (ghb_combodata_t*)boxed;
	if (combodata->option) 
		g_free(combodata->option);
	if (combodata->shortOpt) 
		g_free(combodata->shortOpt);
	if (combodata->svalue) 
		g_free(combodata->svalue);
	g_free(combodata);
}


static void
xform_combodata_to_string(const GValue *combo, GValue *sval)
{
	const ghb_combodata_t *combodata = g_value_get_boxed(combo);
	g_value_set_string(sval, combodata->shortOpt);
}

static void
xform_combodata_to_int64(const GValue *combo, GValue *ival)
{
	const ghb_combodata_t *combodata = g_value_get_boxed(combo);
	g_value_set_int64(ival, combodata->ivalue);
}

static void
xform_combodata_to_double(const GValue *combo, GValue *dval)
{
	const ghb_combodata_t *combodata = g_value_get_boxed(combo);
	g_value_set_double(dval, (gdouble)combodata->ivalue);
}

GType
ghb_combodata_get_type(void)
{
	static GType type_id = 0;
	if (!type_id)
	{
		type_id = g_boxed_type_register_static(
					g_intern_static_string("GHBCombo"),
					(GBoxedCopyFunc) combodata_copy,
					(GBoxedFreeFunc) combodata_free);
		g_value_register_transform_func(type_id, G_TYPE_STRING, 
									xform_combodata_to_string);
		g_value_register_transform_func(type_id, G_TYPE_INT64, 
									xform_combodata_to_int64);
		g_value_register_transform_func(type_id, G_TYPE_DOUBLE, 
									xform_combodata_to_double);
	}
	return type_id;
}

void
ghb_value_set_combodata(
	GValue *gval, 
	gint index,
	const gchar *option,
	const gchar *shortOpt,
	const gchar *svalue,
	gint ivalue)
{
	ghb_combodata_t combodata;
	combodata.index = index;
	combodata.option = g_strdup(option);
	combodata.shortOpt = g_strdup(shortOpt);
	combodata.svalue = g_strdup(svalue);
	combodata.ivalue = ivalue;
	g_value_set_boxed(gval, &combodata);
}

static gpointer
rawdata_copy(gpointer boxed)
{
	const ghb_rawdata_t *data = (const ghb_rawdata_t*)boxed;
	ghb_rawdata_t *copy = g_malloc(sizeof(ghb_rawdata_t));
	copy->size = data->size;
	if (data->data)
	{
		copy->data = g_malloc(data->size);
		memcpy(copy->data, data->data, data->size);
	}
	else
	{
		copy->data = NULL;
		copy->size = 0;
	}
	return copy;
}

static void
rawdata_free(gpointer boxed)
{
	ghb_rawdata_t *data = (ghb_rawdata_t*)boxed;
	if (data->data) g_free(data->data);
	g_free(data);
}


GType
ghb_rawdata_get_type(void)
{
	static GType type_id = 0;
	if (!type_id)
		type_id = g_boxed_type_register_static(g_intern_static_string("GHBData"),
			(GBoxedCopyFunc) rawdata_copy,
			(GBoxedFreeFunc) rawdata_free);
	return type_id;
}

static void
dict_delete_key(gpointer data)
{
	if (data == NULL)
	{
		g_warning("dict frees null key");
		return;
	}
	g_free(data);
}

static void
dict_delete_value(gpointer data)
{
	GValue *gval = (GValue*)data;
	if (gval == NULL)
	{
		g_warning("dict frees null value");
		return;
	}
	ghb_value_free(gval);
}

static gpointer
dict_copy(gpointer boxed)
{
	GHashTable *dict = (GHashTable*)boxed;
	GHashTable *copy;
	GHashTableIter iter;
	gchar *key;
	GValue *gval;

	copy = g_hash_table_new_full(g_str_hash, g_str_equal,
								dict_delete_key, dict_delete_value);

	g_hash_table_iter_init(&iter, dict);
	// middle (void*) cast prevents gcc warning "defreferencing type-punned
	// pointer will break strict-aliasing rules"
	while (g_hash_table_iter_next(
			&iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
	{
		g_hash_table_insert(copy, g_strdup(key), ghb_value_dup(gval));
	}
	return copy;
}

static void
dict_free(gpointer boxed)
{
	GHashTable *dict = (GHashTable*)boxed;
	g_hash_table_destroy(dict);
}


GType
ghb_dict_get_type(void)
{
	static GType type_id = 0;
	if (!type_id)
		type_id = g_boxed_type_register_static(
						g_intern_static_string("GHBDict"),
						(GBoxedCopyFunc) dict_copy,
						(GBoxedFreeFunc) dict_free);
	return type_id;
}

void
ghb_dict_insert(GValue *gval, gchar *key, GValue *val)
{
	GHashTable *dict = g_value_get_boxed(gval);
	g_hash_table_insert(dict, key, val);
}

void
ghb_dict_iter_init(GHashTableIter *iter, GValue *gval)
{
	GHashTable *dict = g_value_get_boxed(gval);
	g_hash_table_iter_init(iter, dict);
}

GValue*
ghb_dict_lookup(GValue *gval, const gchar *key)
{
	GHashTable *dict = g_value_get_boxed(gval);
	return g_hash_table_lookup(dict, key);
}

gboolean
ghb_dict_remove(GValue *gval, const gchar *key)
{
	GHashTable *dict = g_value_get_boxed(gval);
	return g_hash_table_remove(dict, key);
}

static gpointer
array_copy(gpointer boxed)
{
	const GArray *array = (const GArray*)boxed;
	GArray *copy = g_array_new(FALSE, FALSE, sizeof(GValue*));

	GValue *gval, *gval_copy;
	gint ii;

	for (ii = 0; ii < array->len; ii++)
	{
		gval = g_array_index(array, GValue*, ii);
		if (gval)
		{
			gval_copy = ghb_value_dup(gval);
			g_array_append_val(copy, gval_copy);
		}
	}
	return copy;
}

static void
array_free(gpointer boxed)
{
	GArray *array = (GArray*)boxed;
	GValue *gval;
	gint ii;

	for (ii = 0; ii < array->len; ii++)
	{
		gval = g_array_index(array, GValue*, ii);
		if (gval)
		{
			ghb_value_free(gval);
		}
	}
	g_array_free(array, TRUE);
}


GType
ghb_array_get_type(void)
{
	static GType type_id = 0;
	if (!type_id)
		type_id = g_boxed_type_register_static(
						g_intern_static_string("GHBArray"),
						(GBoxedCopyFunc) array_copy,
						(GBoxedFreeFunc) array_free);
	return type_id;
}

GValue*
ghb_array_get_nth(const GValue *gval, gint ii)
{
	GArray *arr = g_value_get_boxed(gval);
	return g_array_index(arr, GValue*, ii);
}

void
ghb_array_append(GValue *gval, GValue *val)
{
	GArray *arr = g_value_get_boxed(gval);
	// A little nastyness here.  The array pointer
	// can change when the array changes size.  So
	// I must re-box it in the GValue each time.
	arr = g_array_append_val(arr, val);
	memset(gval, 0, sizeof(GValue));
	g_value_init(gval, ghb_array_get_type());
	g_value_take_boxed(gval, arr);
}

void
ghb_array_remove(GValue *gval, guint ii)
{
	GArray *arr = g_value_get_boxed(gval);
	// A little nastyness here.  The array pointer
	// can change when the array changes size.  So
	// I must re-box it in the GValue each time.
	arr = g_array_remove_index(arr, ii);
	memset(gval, 0, sizeof(GValue));
	g_value_init(gval, ghb_array_get_type());
	g_value_take_boxed(gval, arr);
}

gint
ghb_array_len(const GValue *gval)
{
	if (gval == NULL) return 0;
	GArray *arr = g_value_get_boxed(gval);
	return arr->len;
}