File: myx_grt_value.c

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (1828 lines) | stat: -rw-r--r-- 53,946 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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
/* Copyright (C) 2004 MySQL AB

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


#include "myx_grt_private.h"

/**
 * @file  myx_grt_value.c
 * @brief GRT value handling 
 * 
 * See also: <a href="../grt.html#GRTValue">GRT Value</a>
 */


/*
 ****************************************************************************
 * @brief Frees a list struct
 *
 *  Frees a list value and all of its contents.
 *****************************************************************************/
int myx_grt_free_list(MYX_GRT_LIST *list)
{
  unsigned int i;
  for (i= 0; i < list->items_num; i++)
  {
    myx_grt_value_release(list->items[i]);
  }
  g_free(list->content_struct_name);
  g_free(list->items);
  g_free(list);
  
  return 0;
}


/*
 ****************************************************************************
 * @brief Frees a dictionary struct
 *
 * Frees a dictionary value and all of its contents.
 *****************************************************************************/
int myx_grt_free_dict(MYX_GRT_DICT *dict)
{
  unsigned int i;

  for (i= 0; i < dict->items_num; i++)
  {
    //if the grt member has been set, and there is a _id dict item of type string try to free the cache reference
    if ((dict->grt) && (strcmp2(dict->items[i].key, "_id") == 0) && 
      myx_grt_value_get_type(dict->items[i].value) == MYX_STRING_VALUE)
      g_hash_table_remove(dict->grt->reference_cache, dict->items[i].value->value.s);

    g_free(dict->items[i].key);
    myx_grt_value_release(dict->items[i].value);
  }
  g_free(dict->object_path);
  g_free(dict->struct_name);
  g_free(dict->items);
  g_free(dict);
  
  return 0;
}


/**
 ****************************************************************************
 * @brief Assigns a MYX_GRT_STRUCT to a dict value
 *
 *  This will assign a MYX_GRT_STRUCT to a list value. 
 *
 * @param dict a dict value
 * @param gstruct the name struct to assign or NULL
 *
 * @return 0 if ok, -1 if the parameters are invalid.
 *****************************************************************************/
int myx_grt_dict_struct_set_name(MYX_GRT_VALUE *dict, const char *struct_name)
{
  g_return_val_if_fail(dict != NULL, -1);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, -1);

  if (dict->value.d->struct_name)
    g_free(dict->value.d->struct_name);
  dict->value.d->struct_name= g_strdup(struct_name);

  //check if _id has been set for GrtObjects
  
  return 0;
}


/**
 ****************************************************************************
 * @brief Assigns a MYX_GRT_STRUCT to a list value
 *
 *  This will assign a MYX_GRT_STRUCT to a list value. The list should then
 * only accept values that match the given struct.
 * 
 * @param list a list value containing dictionaries
 * @param struct_name the name struct to assign or NULL. 
 *
 * @return 0 if ok, -1 if the parameters are invalid.
 *****************************************************************************/
int myx_grt_list_content_set_struct_name(MYX_GRT_VALUE *list, const char *struct_name)
{
  g_return_val_if_fail(list != NULL, -1);
  g_return_val_if_fail(list->type == MYX_LIST_VALUE, -1);

  if (list->value.l->content_struct_name)
    g_free(list->value.l->content_struct_name);
  list->value.l->content_struct_name= g_strdup(struct_name);

  return 0;
}


/**
 ****************************************************************************
 * @brief Returns the struct name associated with the value
 * 
 *    This will return the name of the MYX_GRT_STRUCT associated with the 
 *  dictionary or list which was previously set with 
 *  \c myx_grt_dict_struct_set_name or \c myx_grt_list_content_set_struct_name.
 *
 * @param dict a dict value
 *
 * @return A reference to the struct name or NULL if there's nothing associated.
 *****************************************************************************/
const char *myx_grt_dict_struct_get_name(MYX_GRT_VALUE *dict)
{
  g_return_val_if_fail(dict != NULL, NULL);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);
  
  return dict->value.d->struct_name;
}

/**
 ****************************************************************************
 * @brief Returns the struct name associated with the value
 * 
 *    This will return the name of the MYX_GRT_STRUCT associated with the 
 *  dictionary or list which was previously set with 
 *  \c myx_grt_dict_struct_set_name or \c myx_grt_list_content_set_struct_name.
 *
 * @param grt the GRT environment
 *
 * @param dict a dict value
 *
 * @return A reference to the struct name or NULL if there's nothing associated.
 *****************************************************************************/
MYX_GRT_STRUCT * myx_grt_dict_struct_get(MYX_GRT *grt, MYX_GRT_VALUE *dict)
{
  g_return_val_if_fail(dict != NULL, NULL);

  return myx_grt_struct_get(grt, myx_grt_dict_struct_get_name(dict));
}

/**
 ****************************************************************************
 * @brief Checks if the given dict's struct inherits from a parent struct
 *
 * @param grt the GRT environment
 *
 * @param dict a dict value
 *
 * @param parent_struct_name name of the parent struct
 *
 * @return 1 is the dict's struct inherits from the parent struct, 0 if not
 *****************************************************************************/
MYX_PUBLIC_FUNC int myx_grt_dict_struct_inherits_from(MYX_GRT *grt, MYX_GRT_VALUE *dict, const char *parent_struct_name)
{
  return myx_grt_struct_check_inherits_from(grt, myx_grt_dict_struct_get_name(dict), parent_struct_name);
}


static void print_value(MYX_GRT *grt, MYX_GRT_VALUE *value, int depth)
{
  unsigned int i;
  char spaces[1024];
  char buffer[200];

  g_return_if_fail(depth < (int)sizeof(spaces));

  switch (value->type)
  {
  case MYX_INT_VALUE:
    g_snprintf(buffer, sizeof(buffer), "%i"NL, value->value.i);
    MYX_PRINT(grt, buffer);
    break;
  case MYX_REAL_VALUE:
    g_snprintf(buffer, sizeof(buffer), "%f"NL, value->value.r);
    MYX_PRINT(grt, buffer);
    break;
  case MYX_STRING_VALUE:
    MYX_PRINT(grt, value->value.s);
    MYX_PRINT(grt, NL);
    break;
  case MYX_LIST_VALUE:
    MYX_PRINT(grt, "["NL);
    for (i= 0; i < value->value.l->items_num; i++)
    {
      g_snprintf(spaces, sizeof(spaces), "%*s", depth, "");
      MYX_PRINT(grt, spaces);
      print_value(grt, value->value.l->items[i], depth+1);
    }
    g_snprintf(spaces, sizeof(spaces), "%*s", depth, "");
    MYX_PRINT(grt, spaces);
    MYX_PRINT(grt, "]"NL);
    break;
  case MYX_DICT_VALUE:
    MYX_PRINT(grt, "{"NL);
    for (i= 0; i < value->value.d->items_num; i++)
    {
      g_snprintf(spaces, sizeof(spaces), "%*s", depth+1, "");
      MYX_PRINT(grt, spaces);
      myx_grt_printf(grt, "%s = ", value->value.d->items[i].key);
      print_value(grt, value->value.d->items[i].value, depth+1);
    }
    g_snprintf(spaces, sizeof(spaces), "%*s", depth, "");
    MYX_PRINT(grt, spaces);
    MYX_PRINT(grt, "}"NL);
    break;
  default:
    MYX_PRINT(grt, "Invalid value"NL);
    break;
  }
}

/**
 ****************************************************************************
 * @brief Prints the contents of a value to stdout
 *
 * @param grt the GRT environment
 * @param value the value to be printed
 *
 * @return 0
 *****************************************************************************/
int myx_grt_value_print(MYX_GRT *grt, MYX_GRT_VALUE *value)
{
  print_value(grt, value, 0);
  return 0;
}


/**
 ****************************************************************************
 * @brief Decrements the reference count of the value
 * 
 *   Will decrease the reference count of the value by one. When it reaches 0
 *   it will recursively release (and free) the whole contents of the value.
 *
 * @param value the value to be released
 *
 * @return 0
 * 
 * @see myx_grt_value_retain
 *****************************************************************************/
int myx_grt_value_release(MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(value != NULL, -1);
  g_return_val_if_fail(value->refcount > 0, -1);

  value->refcount--;
  if (value->refcount == 0)
  {
    switch (value->type)
    {
    case MYX_INT_VALUE:
    case MYX_REAL_VALUE:
      break;
    case MYX_STRING_VALUE:
      g_free(value->value.s);
      break;
    case MYX_LIST_VALUE:
      myx_grt_free_list(value->value.l);
      break;
    case MYX_DICT_VALUE:
      myx_grt_free_dict(value->value.d);
      break;
    }

    g_free(value);
  }

  return 0;
}

/**
 ****************************************************************************
 * @brief Returns the current reference count of the value
 * 
 *   Will return the current reference count of the value.
 *
 * @param value the value to be queried
 *
 * @return the currente reference count
 * 
 * @see myx_grt_value_retain
 *****************************************************************************/
int myx_grt_value_get_current_reference_count(MYX_GRT_VALUE *value)
{
  return value->refcount;
}


/** 
 ****************************************************************************
 * @brief Increments the reference count of the value.
 *
 *  Increments the reference count of the value by one. Newly created values
 * start with a reference count of 1, so you normally don't need to retain them.
 *
 * @param value - GRT value
 *
 * @return the GRT value passed as argument
 *
 * @see myx_grt_value_release
 ****************************************************************************
 */
MYX_GRT_VALUE *myx_grt_value_retain(MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(value != NULL, value);

  value->refcount++;

  return value;
}


/**
 ****************************************************************************
 * @brief create a dict value from a list of key/type/value tuples.
 *
 *   If value is a list of dict, you must pass a MYX_GRT_LIST or
 *   MYX_GRT_DICT. These must not be freed. But usually you will give this
 *   function a MYX_GRT_VALUE containing a list or dict. In that case 
 *   the value will be just retained, so you can release it later.
 *
 * @param  key the dict key for the value or NULL, followed by vtype and value\n
 *    vtype type of the value may be 0 for a MYX_GRT_VALUE\n
 *    value the value\n
 * Repeat these as many times as needed. End list of values with NULL
 *
 * @return  A GRT value of type dict.
 *****************************************************************************/
MYX_GRT_VALUE *myx_grt_dict_create(const char *struct_name, const char *key, ...)
{
  va_list args;
  MYX_GRT_VALUE *dict;
  MYX_GRT_VALUE *gvalue;
  MYX_GRT_VALUE_TYPE vtype;
  
  g_return_val_if_fail(key != NULL, NULL);

  dict= myx_grt_dict_new(struct_name);

  va_start(args, key);

  while (key != NULL)
  {
    vtype= va_arg(args, MYX_GRT_VALUE_TYPE);
    
    switch (vtype)
    {
    case MYX_INT_VALUE:
      gvalue= myx_grt_value_from_int(va_arg(args, int));
      break;
    case MYX_REAL_VALUE:
      gvalue= myx_grt_value_from_real(va_arg(args, double));
      break;
    case MYX_STRING_VALUE:
      gvalue= myx_grt_value_from_string(va_arg(args, char*));
      break;
    case MYX_LIST_VALUE:
      gvalue= g_new0(MYX_GRT_VALUE, 1);
      gvalue->type= MYX_LIST_VALUE;
      gvalue->value.l= va_arg(args, MYX_GRT_LIST*);
      gvalue->refcount= 1;
      break;
    case MYX_DICT_VALUE:
      gvalue= g_new0(MYX_GRT_VALUE, 1);
      gvalue->type= MYX_DICT_VALUE;
      gvalue->value.d= va_arg(args, MYX_GRT_DICT*);
      gvalue->refcount= 1;
      break;
    default:
      gvalue= va_arg(args, MYX_GRT_VALUE*);
      myx_grt_value_retain(gvalue);
    }

    myx_grt_dict_item_set_value(dict, key, gvalue);
    
    myx_grt_value_release(gvalue);
    
    key= va_arg(args, const char*);
  }
  
  va_end(args);
  
  return dict;
}

MYX_GRT_VALUE_TYPE myx_grt_dict_content_get_type(MYX_GRT_VALUE *dict)
{
  return dict->value.d->content_type;
}

void myx_grt_dict_content_set_type(MYX_GRT_VALUE *dict, MYX_GRT_VALUE_TYPE content_type)
{
  dict->value.d->content_type= content_type;
}

const char * myx_grt_dict_content_get_struct_name(MYX_GRT_VALUE *dict)
{
  g_return_val_if_fail(dict != NULL, NULL);
  g_return_val_if_fail(myx_grt_value_get_type(dict) == MYX_DICT_VALUE, NULL);

  return dict->value.d->content_struct_name;
}

const char * myx_grt_dict_get_object_path(MYX_GRT_VALUE *dict)
{
  g_return_val_if_fail(dict != NULL, NULL);

  return dict->value.d->object_path;
}


/**
 ****************************************************************************
 * @brief Creates a GRT value of MYX_INT_VALUE type
 *
 *   Creates a GRT value of type MYX_INT_VALUE and initialized it with the 
 * passed argument.
 * 
 * @param i the integer value that it will be initialized to.
 *
 * @return A newly created value struct.
 *****************************************************************************/
MYX_GRT_VALUE *myx_grt_value_from_int(int i)
{
  MYX_GRT_VALUE *value= g_new0(MYX_GRT_VALUE, 1);
  value->type= MYX_INT_VALUE;
  value->value.i= i;
  value->refcount= 1;
  return value;
}


/**
 ****************************************************************************
 * @brief creates a GRT value of MYX_REAL_VALUE type
 *
 *   Creates a GRT value of type MYX_REAL_VALUE and initialized it with the 
 * passed argument.
 *
 * @param d the value that it will be initialized to.
 *
 * @return A newly created value struct.
 *****************************************************************************/
MYX_GRT_VALUE *myx_grt_value_from_real(double d)
{
  MYX_GRT_VALUE *value= g_new0(MYX_GRT_VALUE, 1);
  value->type= MYX_REAL_VALUE;
  value->value.r= d;
  value->refcount= 1;
  return value;
}


/**
 ****************************************************************************
 * @brief Creates a GRT value of MYX_STRING_VALUE type
 * 
 *   Creates a GRT value of type MYX_STRING_VALUE and initialized it with the 
 * passed argument. The string will be copied.
 *
 * @param s the value that it will be initialized to.
 * 
 * @return A newly created value struct.
 *****************************************************************************/
MYX_GRT_VALUE *myx_grt_value_from_string(const char *s)
{
  MYX_GRT_VALUE *value= NULL;
  
  if (s)
  {
    value= g_new0(MYX_GRT_VALUE, 1);
    value->type= MYX_STRING_VALUE;
    value->value.s= g_strdup(s);
    value->refcount= 1;
  }

  return value;
}

/**
 ****************************************************************************
 * @brief Creates a copy of the original value
 * 
 *   Creates a GRT value of type MYX_STRING_VALUE and initialized it with the 
 * passed argument. The string will be copied.
 *
 * @param s the value that it will be initialized to.
 * 
 * @return A newly created value struct.
 *****************************************************************************/
MYX_GRT_VALUE * myx_grt_value_dup(MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(value!=NULL, NULL);

  switch (myx_grt_value_get_type(value))
  {
  case MYX_INT_VALUE:
    return myx_grt_value_from_int(myx_grt_value_as_int(value));
  case MYX_REAL_VALUE:
    return myx_grt_value_from_real(myx_grt_value_as_real(value));
  case MYX_STRING_VALUE:
    return myx_grt_value_from_string(myx_grt_value_as_string(value));
  case MYX_LIST_VALUE:
    {
      MYX_GRT_VALUE *list= myx_grt_list_new(myx_grt_list_content_get_type(value),  
        myx_grt_list_content_get_struct_name(value));
      unsigned int i;

      //Copy items
      for (i= 0; i < myx_grt_list_item_count(value); i++)
      {
        MYX_GRT_VALUE *item= myx_grt_value_dup(myx_grt_list_item_get(value, i));
        myx_grt_list_item_add(list, item);

        //release item one time, so it is only refered by the list
        myx_grt_value_release(item);
      }

      return list;
    }
  case MYX_DICT_VALUE:
    {
      MYX_GRT_VALUE *dict;
      unsigned int i;

      //Check if we have a typed dict
      if (myx_grt_dict_content_get_type(value) != MYX_ANY_VALUE)
        dict= myx_grt_dict_new_typed(myx_grt_dict_content_get_type(value),  
          myx_grt_dict_content_get_struct_name(value));
      else
        dict= myx_grt_dict_new(myx_grt_dict_struct_get_name(value));

      //Copy items
      for (i= 0; i<myx_grt_dict_item_count(value); i++)
      {
        MYX_GRT_VALUE *item= myx_grt_value_dup(myx_grt_dict_item_value_by_index(value, i));
        myx_grt_dict_item_set_value(dict, 
          myx_grt_dict_item_key_by_index(value, i), item);

        //release item one time, so it is only refered by the dict
        myx_grt_value_release(item);
      }

      return dict;
    }
  }

  return NULL;
}

/**
 ****************************************************************************
 * @brief Creates a new empty GRT value of MYX_GRT_DICT type
 *
 *   Creates a GRT value of type MYX_GRT_DICT, which is a dictionary or
 * mapping of keys strings to values. You can add, query and remove values
 * to it with the other dict functions.
 *
 * To type the dictionary, specify a content_type. If the content_type is dict
 * you can define the struct that these dicts must implement with the 
 * content_struct_name parameter
 * 
 * @return  A newly created dict value struct.
 *****************************************************************************/
MYX_GRT_VALUE * myx_grt_dict_new_typed(MYX_GRT_VALUE_TYPE content_type, const char *content_struct_name)
{
  MYX_GRT_VALUE *value= g_new0(MYX_GRT_VALUE, 1);
  value->type= MYX_DICT_VALUE;
  value->value.d= g_new0(MYX_GRT_DICT, 1);
  value->refcount= 1;

  value->value.d->content_type= content_type;

  if ((content_struct_name) && (content_struct_name[0]))
    value->value.d->content_struct_name= g_strdup(content_struct_name);

  return value;
}

/**
 ****************************************************************************
 * @brief Creates a new empty GRT value of MYX_GRT_DICT type
 *
 *   Creates a GRT value of type MYX_GRT_DICT, which is a dictionary or
 * mapping of keys strings to values. You can add, query and remove values
 * to it with the other dict functions.
 *
 * If the struct_name is set, this dict is an instance of the given struct
 * 
 * @return  A newly created dict value struct.
 *****************************************************************************/
MYX_GRT_VALUE * myx_grt_dict_new(const char *struct_name)
{
  MYX_GRT_VALUE *value= g_new0(MYX_GRT_VALUE, 1);
  value->type= MYX_DICT_VALUE;
  value->value.d= g_new0(MYX_GRT_DICT, 1);
  value->refcount= 1;

  if ((struct_name) && (struct_name[0]))
    myx_grt_dict_struct_set_name(value, struct_name);

  return value;
}

static int bisect(MYX_GRT_DICT *dict, const char *key, int *index)
{
  int i;
  int j, k;
  
  j= 0;
  k= dict->items_num-1;
  while (j <= k)
  {
    int r;
    i= (j+k)/2;
    r= strcmp(key, dict->items[i].key);
    
    if (r == 0)
    {
      *index= i;
      return 1;
    }
    else if (r < 0)
      k= i-1;
    else if (r > 0)
      j= i+1;
  }
  
  if (j > k)
    *index= j;
  else
    *index= k;

  return 0;
}


/**
 ****************************************************************************
 * @brief Sets a value for a key in the dict
 * 
 *   Inserts a key value pair into the dict. The value will have its
 * reference count increased, therefore can be released afterwards.
 * If the key already exists in the dictionary, the old one will be released
 * before it's replaced.
 * 
 * \b NOTE
 *   The values are inserted in already order so that a binary search can
 * be used to lookup a specific value.
 * 
 * @param dict  a GRT value of type MYX_GRT_DICT
 * @param key   the key name where the value should be inserted
 * @param value the value to be inserted. The value will have its reference
 *     count increased.
 *
 * @return  0 on success, -1 on error
 *****************************************************************************/
int myx_grt_dict_item_set_value(MYX_GRT_VALUE *dict, const char *key, MYX_GRT_VALUE *value)
{
  int i;
  g_return_val_if_fail(dict!=NULL, -1);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, -1);
  g_return_val_if_fail(key!=NULL, -1);
  g_return_val_if_fail(*key, -1);
  g_return_val_if_fail(value!=NULL, -1);
  
  if (dict->value.d->items_num == 0)
  {
    /* dict is empty */
    dict->value.d->items_num++;
    dict->value.d->items= g_new0(MYX_GRT_DICT_ITEM, dict->value.d->items_num);

    dict->value.d->items[0].key= g_strdup(key);
    dict->value.d->items[0].value= myx_grt_value_retain(value);
  }
  else
  {
    if (!bisect(dict->value.d, key, &i))
    {
      // the key was not in the tree already
      dict->value.d->items_num++;
      dict->value.d->items= g_realloc(dict->value.d->items,sizeof(MYX_GRT_DICT_ITEM)*dict->value.d->items_num);
      
      if (i < (int)dict->value.d->items_num-1)
      {
        memmove(dict->value.d->items + i + 1,
                dict->value.d->items + i,
                sizeof(MYX_GRT_DICT_ITEM)*(dict->value.d->items_num-i-1));
      }
      dict->value.d->items[i].key= g_strdup(key);
      dict->value.d->items[i].value= myx_grt_value_retain(value);
    }
    else
    {
      // replace value
      if (dict->value.d->items[i].value != value)
      {
        myx_grt_value_release(dict->value.d->items[i].value);
        dict->value.d->items[i].value= myx_grt_value_retain(value);
      }
    }
  }

  return 0;
}

/**
 ****************************************************************************
 * @brief Sets a string value for a key in the dict
 * 
 *   Creates a new string value and then calls myx_grt_dict_item_set_value
 * 
 * @param dict  a GRT value of type MYX_GRT_DICT
 * @param key   the key name where the value should be inserted
 * @param s     the string to be inserted.
 *
 * @return  0 on success, -1 on error
 *****************************************************************************/
int myx_grt_dict_item_set_value_from_string(MYX_GRT_VALUE *dict, const char *key, const char *s)
{
  int res;
  MYX_GRT_VALUE *value= myx_grt_value_from_string(s);

  if (!value)
    return 0;

  res= myx_grt_dict_item_set_value(dict, key, value);

  //lower reference counter by one
  myx_grt_value_release(value);

  return res;
}

/**
 ****************************************************************************
 * @brief Sets a int value for a key in the dict
 * 
 *   Creates a new int value and then calls myx_grt_dict_item_set_value
 * 
 * @param dict  a GRT value of type MYX_GRT_DICT
 * @param key   the key name where the value should be inserted
 * @param value the int to be inserted.
 *
 * @return  0 on success, -1 on error
 *****************************************************************************/
int myx_grt_dict_item_set_value_from_int(MYX_GRT_VALUE *dict, const char *key, int i)
{
  int res;
  MYX_GRT_VALUE *value= myx_grt_value_from_int(i);

  if (!value)
    return 0;

  res= myx_grt_dict_item_set_value(dict, key, value);

  //lower reference counter by one
  myx_grt_value_release(value);

  return res;
}

/**
 ****************************************************************************
 * @brief Looks up for the key in the dict and returns the value associated to it.
 *
 *   This will return the value for the specified key.
 * 
 * @param dict a GRT value of type MYX_GRT_DICT
 * @param key the key name to search
 *
 * @return  NULL if the value does not exist in the dict or the value if it does.
 *****************************************************************************/
MYX_GRT_VALUE *myx_grt_dict_item_get_value(MYX_GRT_VALUE *dict, const char *key)
{
  int i;
  g_return_val_if_fail(dict!=NULL, NULL);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);
  g_return_val_if_fail(key!=NULL, NULL);
  g_return_val_if_fail(*key, NULL);

  if (!bisect(dict->value.d, key, &i))
    return NULL;
  else
    return dict->value.d->items[i].value;
}

/**
 ****************************************************************************
 * @brief Removes the key and it's value from the dict
 *
 *   Remove the value associated with the key in the dict. The value will be 
 *   released.
 *
 * @param dict a GRT value of type MYX_GRT_DICT
 * @param key the key to remove
 * 
 * @return 0 if the value was removed, -1 if the key was not in the dict.
 *****************************************************************************/
int myx_grt_dict_item_del(MYX_GRT_VALUE *dict, const char *key)
{
  int i;
  g_return_val_if_fail(dict!=NULL, -1);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, -1);
  g_return_val_if_fail(key!=NULL, -1);
  g_return_val_if_fail(*key, -1);

  if (!bisect(dict->value.d, key, &i))
    return -1;
  else
  {
    g_free(dict->value.d->items[i].key);
    myx_grt_value_release(dict->value.d->items[i].value);
    
    memmove(dict->value.d->items + i, dict->value.d->items + i + 1,
            sizeof(MYX_GRT_DICT_ITEM)*(dict->value.d->items_num-i-1));
    dict->value.d->items_num--;
    return 0;
  }
}

/**
 ****************************************************************************
 * @brief Returns the items of elements in the dict value
 *
 *   Returns the number of items in the dict. Use in conjunction with
 *  myx_grt_dict_item_by_index() to traverse full contents of the dictionary.
 *
 * @param dict the dict
 *
 * @return Number of items in the dict.
 *****************************************************************************/
unsigned int myx_grt_dict_item_count(MYX_GRT_VALUE *dict)
{
  g_return_val_if_fail(dict!=NULL, 0);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, 0);
  
  return dict->value.d->items_num;
}


/**
 ****************************************************************************
 * @brief Get the item at index in the dict
 *
 *  Retrieves the item at the specified index in the dict. If you are
 * traversing the dictionary, you should not modify its contents during
 * that.
 * 
 * @param dict the dict
 * @param index index in the dict to fetch
 * @param retkey pointer to where the key string will be returned
 * @param retvalue pointer to where the return GRT value will be returned
 *
 * @return -1 if there's some error (bad parameters or invalid index), 0 otherwise.
 *****************************************************************************/
int myx_grt_dict_item_by_index(MYX_GRT_VALUE *dict, unsigned int index, 
                          const char **retkey, MYX_GRT_VALUE **retvalue)
{
  g_return_val_if_fail(dict!=NULL, -1);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, -1);

  if (index >= dict->value.d->items_num)
    return -1;

  *retkey= dict->value.d->items[index].key;
  *retvalue= dict->value.d->items[index].value;
  
  return 0;
}

/**
 ****************************************************************************
 * @brief Get the item's key name at index in the dict
 *
 *  Retrieves the item's key name at the specified index in the dict. 
 * 
 * @param dict the dict
 * @param index index in the dict to fetch
 *
 * @return NULL if there's some error (bad parameters or invalid index), the string otherwise.
 *****************************************************************************/
const char * myx_grt_dict_item_key_by_index(MYX_GRT_VALUE *dict, unsigned int index)
{
  g_return_val_if_fail(dict!=NULL, NULL);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);

  if (index >= dict->value.d->items_num)
    return NULL;

  return dict->value.d->items[index].key;
}

/**
 ****************************************************************************
 * @brief Get the item's value at index in the dict
 *
 *  Retrieves the item's value at the specified index in the dict. If you are
 * traversing the dictionary, you should not modify its contents during
 * that.
 * 
 * @param dict the dict
 * @param index index in the dict to fetch
 *
 * @return NULL if there's some error (bad parameters or invalid index), the value otherwise.
 *****************************************************************************/
MYX_GRT_VALUE * myx_grt_dict_item_value_by_index(MYX_GRT_VALUE *dict, unsigned int index)
{
  g_return_val_if_fail(dict!=NULL, NULL);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);

  if (index >= dict->value.d->items_num)
    return NULL;
  
  return dict->value.d->items[index].value;
}

/**
 ****************************************************************************
 * @brief Convenience function to create a list value from a stringlist.
 *
 *   Will create a MYX_GRT_VALUE of type list containing the strings in sl.
 *   All the strings will be copied.
 *
 * @param  sl the stringlist
 *
 * @return  The newly created value or NULL on error.
 *
 *****************************************************************************/
MYX_GRT_VALUE *myx_grt_list_create_from_stringlist(MYX_STRINGLIST *sl)
{
  MYX_GRT_VALUE *list;
  unsigned int i;

  g_return_val_if_fail(sl != NULL, NULL);

  list= myx_grt_list_new(MYX_STRING_VALUE, NULL);
  list->value.l->items_num= sl->strings_num;
  list->value.l->items= g_malloc(sizeof(MYX_GRT_VALUE*)*sl->strings_num);
  for (i= 0; i < sl->strings_num; i++)
    list->value.l->items[i]= myx_grt_value_from_string(sl->strings[i]);

  return list;
}

/**
 ****************************************************************************
 * @brief Creates a new empty list GRT value 
 * 
 *   Creates a GRT value of type list. All elements inserted to the list
 * must be of content_type.
 *
 * @param  content_type the type that the contents of the list will have
 *
 * @return  The new list value or NULL if there's an error.
 *****************************************************************************/
MYX_GRT_VALUE *myx_grt_list_new(MYX_GRT_VALUE_TYPE content_type, const char *struct_name)
{
  MYX_GRT_VALUE *value;
  //g_return_val_if_fail(content_type!=0, NULL);

  value= g_new0(MYX_GRT_VALUE, 1);
  value->type= MYX_LIST_VALUE;
  value->value.l= g_new0(MYX_GRT_LIST, 1);
  value->value.l->content_type= content_type;
  value->refcount= 1;

  if (struct_name && struct_name[0])
    myx_grt_list_content_set_struct_name(value, struct_name);

  return value;
}

/**
 ****************************************************************************
 * @brief Returns the content type of the list
 * 
 *   Every list can only have values of one type or any type. This function returns  
 * this content type.
 *
 * @param list a GRT value of type list
 *
 * @return  The content type as MYX_GRT_VALUE_TYPE
 *****************************************************************************/
MYX_GRT_VALUE_TYPE myx_grt_list_content_get_type(MYX_GRT_VALUE *list)
{
  return list->value.l->content_type;
}

void myx_grt_list_content_set_type(MYX_GRT_VALUE *list, MYX_GRT_VALUE_TYPE content_type)
{
  list->value.l->content_type= content_type;
}

/**
 ****************************************************************************
 * @brief Returns the content struct name of the list
 * 
 *   The content struct name is only appropriate when this is a list with the 
 * content type DICT
 *
 * @param list a GRT value of type list
 *
 * @return  The content type as MYX_GRT_VALUE_TYPE
 *****************************************************************************/
const char * myx_grt_list_content_get_struct_name(MYX_GRT_VALUE *list)
{
  g_return_val_if_fail(list != NULL, NULL);
  g_return_val_if_fail(list->type == MYX_LIST_VALUE, NULL);

  return list->value.l->content_struct_name;
}

/**
 ****************************************************************************
 * @brief Returns the content of the list as MYX_STRINGLIST
 * 
 *
 * @param list a GRT value of type list
 *
 * @return  A new allocated MYX_STRINGLIST
 *****************************************************************************/
MYX_STRINGLIST * myx_grt_list_as_stringlist(MYX_GRT_VALUE *list)
{
  MYX_STRINGLIST *strlist;
  unsigned int i;

  g_return_val_if_fail(list->type == MYX_LIST_VALUE, NULL);
  g_return_val_if_fail(list->value.l->content_type == MYX_STRING_VALUE, NULL);

  strlist= g_malloc0(sizeof(MYX_STRINGLIST));
  strlist->strings_num= list->value.l->items_num;
  strlist->strings= g_malloc(sizeof(char *)*strlist->strings_num);

  for (i= 0; i<strlist->strings_num; i++)
  {
    strlist->strings[i]= g_strdup(myx_grt_list_item_get(list, i)->value.s);
  }

  return strlist;
}

/**
 ****************************************************************************
 * @brief Generates a GRT list with contenttype string for a MYX_STRINGLIST
 * 
 * @param str_list a MYX_STRINGLIST stringlist
 *
 * @return  A new allocated GRT value
 *****************************************************************************/
MYX_GRT_VALUE * myx_grt_list_from_stringlist(MYX_STRINGLIST *str_list)
{
  MYX_GRT_VALUE *list= myx_grt_list_new(MYX_STRING_VALUE, "");
  unsigned int i;

  for (i= 0; i<str_list->strings_num; i++)
  {
    myx_grt_list_item_add(list, myx_grt_value_from_string(str_list->strings[i]));
  }

  return list;
}

/**
 ****************************************************************************
 * @brief Inserts an element to the list
 *
 *   Inserts a value to the list. The value to be inserted will not be copied
 * but will be have its reference count increased, therefore can be released.
 *
 * @param list a GRT value of type list
 * @param index the index in the list where the item should be inserted. -1 means append
 * @param value the value to insert.
 * 
 * @return  -1 on error, 0 on success.
 *****************************************************************************/
int myx_grt_list_item_insert(MYX_GRT_VALUE *list, int index, MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(list != NULL, -1);
  g_return_val_if_fail(list->type == MYX_LIST_VALUE, -1);
  g_return_val_if_fail(index <= (int)list->value.l->items_num, -1);
  if (!value)
    return 0;
  g_return_val_if_fail((list->value.l->content_type == value->type) || (list->value.l->content_type == MYX_ANY_VALUE), -1);

  myx_grt_value_retain(value);
  
  list->value.l->items_num++;
  list->value.l->items= g_realloc(list->value.l->items, 
                                  sizeof(MYX_GRT_VALUE)*list->value.l->items_num);
  if (index < 0)
    list->value.l->items[list->value.l->items_num-1]= value;
  else
  {
    memmove(list->value.l->items + index + 1, list->value.l->items + index,
            sizeof(MYX_GRT_VALUE*)*(list->value.d->items_num-index));
    list->value.l->items[index]= value;
  }
  return 0;
}

/**
 ****************************************************************************
 * @brief Adds an element to the list
 *
 *   Adds a value at the end of the list. The value to be inserted will not be copied
 * but will be have its reference count increased, therefore can be released.
 *
 * @param list a GRT value of type list
 * @param value the value to insert.
 * 
 * @return  -1 on error, 0 on success.
 *****************************************************************************/
int myx_grt_list_item_add(MYX_GRT_VALUE *list, MYX_GRT_VALUE *value)
{
  return myx_grt_list_item_insert(list, -1, value);
}

/**
 ****************************************************************************
 * @brief Adds a string to the list
 *
 *   Adds a string at the end of the list.
 *
 * @param list a GRT value of type list
 * @param s the string to insert.
 * 
 * @return  -1 on error, 0 on success.
 *****************************************************************************/
int myx_grt_list_item_add_as_string(MYX_GRT_VALUE *list, const char *s)
{
  MYX_GRT_VALUE *value_string= myx_grt_value_from_string(s);

  myx_grt_list_item_add(list, value_string);

  return myx_grt_value_release(value_string);
}

/**
 ****************************************************************************
 * @brief Removes an element from the list
 *
 *   Removes the element at the given index from the list. The element will
 * be released.
 *
 * @param list GRT value of type list
 * @param index index in the list of the element to remove
 *
 * @return 0 on success, -1 if the element does not exist.
 *****************************************************************************/
int myx_grt_list_item_del(MYX_GRT_VALUE *list, int index)
{
  g_return_val_if_fail(list != NULL, -1);
  g_return_val_if_fail(list->type == MYX_LIST_VALUE, -1);
  g_return_val_if_fail(index >= 0 && index < (int)list->value.l->items_num, -1);

  myx_grt_value_release(list->value.l->items[index]);
  
  memmove(list->value.l->items + index, list->value.l->items + index + 1,
          sizeof(MYX_GRT_VALUE*)*(list->value.l->items_num - index -1 ));

  list->value.l->items_num--;

  return 0;
}

/**
 ****************************************************************************
 * @brief Removes a string from the list
 *
 *   Removes the string from the list. The string element will
 * be released.
 *
 * @param list GRT value of type list
 * @param s string to remove
 *
 * @return 0 on success, -1 if the element does not exist.
 *****************************************************************************/
int myx_grt_list_item_del_as_string(MYX_GRT_VALUE *list, const char *s)
{
  int i;
  int item_exists= -1;

  g_return_val_if_fail(list != NULL, -1);
  g_return_val_if_fail(list->type == MYX_LIST_VALUE, -1);

  for (i= myx_grt_list_item_count(list)-1; i>=0; i--)
  {
    MYX_GRT_VALUE *value= myx_grt_list_item_get(list, i);

    if ((myx_grt_value_get_type(value) == MYX_STRING_VALUE) && 
      (strcmp2(s, myx_grt_value_as_string(value)) == 0))
    {
      myx_grt_list_item_del(list, i);

      item_exists= 0;
    }
  }

  return item_exists;
}

/**
 ****************************************************************************
 * @brief Returns the number of elements in the list
 *
 *   Returns element count in the list.
 *
 * @param list GRT value of type list
 *
 * @return Element count in the list.
 *****************************************************************************/
unsigned int myx_grt_list_item_count(MYX_GRT_VALUE *list)
{
  if (list == NULL)
     return -1;

  g_return_val_if_fail(list->type == MYX_LIST_VALUE, 0);
  
  return list->value.l->items_num;
}


/**
 ****************************************************************************
 * @brief Returns the number at the given index
 *
 *   Returns the element at the given index.
 *
 * @param list GRT value of type list
 * @param index in the list to return
 *
 * @return The item or NULL if the index is invalid.
 *****************************************************************************/
MYX_GRT_VALUE *myx_grt_list_item_get(MYX_GRT_VALUE *list, unsigned int index)
{
  g_return_val_if_fail(list != NULL, NULL);
  g_return_val_if_fail(list->type == MYX_LIST_VALUE, NULL);
  g_return_val_if_fail(index < list->value.l->items_num, NULL);

  return list->value.l->items[index];
}

MYX_GRT_VALUE * myx_grt_list_item_get_reference_value(MYX_GRT *grt, MYX_GRT_VALUE *list, unsigned int index)
{
  const char *ref_id= myx_grt_list_item_get_as_string(list, index);

  return myx_grt_reference_cache_lookup(grt, ref_id);
}

/**
 ****************************************************************************
 * @brief Returns the number at the given index as string
 *
 *   Returns the element at the given index as string.
 *
 * @param list GRT value of type list
 * @param index in the list to return
 *
 * @return The item as string or NULL if the index is invalid.
 *****************************************************************************/
const char * myx_grt_list_item_get_as_string(MYX_GRT_VALUE *list, unsigned int index)
{
  MYX_GRT_VALUE *value= myx_grt_list_item_get(list, index);

  g_return_val_if_fail(value != NULL, NULL);
  //g_return_val_if_fail(value->type == MYX_STRING_VALUE, NULL);
  if (value->type != MYX_STRING_VALUE)
    value->type= value->type;

  return myx_grt_value_as_string(value);
}

/**
 ****************************************************************************
 * @brief Replaces a value in the list
 *
 *  This will replace the value at the specified index with the new one.
 *  The old value will be released and the new one retained.
 *
 * @param list the list
 * @param index index in the list to replace the value. Should be < list_size
 * @param new_value the new value to put in the list
 *
 * @return 0 if ok, -1 on error (such as invalid arguments or bad index)
 *****************************************************************************/
int myx_grt_list_set_item(MYX_GRT_VALUE *list, unsigned int index, MYX_GRT_VALUE *new_value)
{  
  g_return_val_if_fail(list!=NULL, -1);
  g_return_val_if_fail(list->type == MYX_LIST_VALUE, -1);
  g_return_val_if_fail(index < myx_grt_list_item_count(list), -1);
  g_return_val_if_fail(new_value!=NULL, -1);

  if (list->value.l->items[index] != new_value)
  {
    myx_grt_value_release(list->value.l->items[index]);
    list->value.l->items[index]= myx_grt_value_retain(new_value);
  }
  return 0;
}

/**
 ****************************************************************************
 * @brief Clears the given list by removing all items
 *
 *  All items will be released and their reference count will be reduced by one
 *
 * @param list the list
 *
 * @return 0 if ok, -1 on error
 *****************************************************************************/
int myx_grt_list_clear(MYX_GRT_VALUE *list)
{
  g_return_val_if_fail(list!=NULL, -1);

  for (; myx_grt_list_item_count(list) > 0; )
    myx_grt_list_item_del(list, 0);

  return 0;
}



/**
 ****************************************************************************
 * @brief Converts a value of type list to dict
 *
 *  The passed value withh be converted to a dict. The keys in the dict
 * will be strings containing the index of each item. The original list is
 * modified and becomes a dict.
 *
 * @param value a GRT value of type list that will be converted to dict
 *
 * @return 0 on success, -1 on error.
 *****************************************************************************/
int myx_grt_convert_list_to_dict(MYX_GRT_VALUE *value)
{
  unsigned int i;
  MYX_GRT_LIST *list;
  char key[128];

  g_return_val_if_fail(value != NULL, -1);
  g_return_val_if_fail(value->type == MYX_LIST_VALUE, -1);

  list= value->value.l;
  value->value.d= g_new0(MYX_GRT_DICT, 1);
  for (i= 0; i < list->items_num; i++)
  {
    g_snprintf(key, sizeof(key), "%i", i);
    myx_grt_dict_item_set_value(value, key, list->items[i]);
    myx_grt_value_release(list->items[i]);
  }
  g_free(list);
  
  return 0;
}


/**
 ****************************************************************************
 * @brief Traverses a dict and return the requested value
 *
 *   This will treat the given value as a tree, where each node is named by 
 * the key in its parent.
 *   For dictionaries, the path component is used as the key name. For lists,
 * the given component is used to match the name attribute of each list item
 * or if its a number, it's used as the index in the list.
 *
 * @param dict a dict type value to be traversed which nested dicts
 * @param path a / separated list of key names to the value to be returned.
 *
 * @return A pointer to the value if it's found or NULL otherwise.
 *****************************************************************************/
MYX_GRT_VALUE *myx_grt_value_get_by_path(MYX_GRT_VALUE *dict, const char *path)
{
  MYX_GRT_VALUE *value= dict;
  char *p;
  char *part;

  g_return_val_if_fail(dict != NULL, NULL);
  g_return_val_if_fail(path != NULL, NULL);
  g_return_val_if_fail(*path == '/', NULL);

  if (strcmp2(path, "/") == 0)
    return dict;

  p= g_strdup(path);

  part= strtok(p, "/");

  while (part && *part && value)
  {
    if (value->type == MYX_DICT_VALUE)
      value= myx_grt_dict_item_get_value(value, part);
    else if (value->type == MYX_LIST_VALUE)
    {
      unsigned int i;
      MYX_GRT_LIST *list= value->value.l;
      unsigned int lindex;
      int ok= 0;

      if (sscanf(part, "%i", &lindex)==1 && lindex >= 0 && lindex < list->items_num)
      {
        value= list->items[lindex];
        ok= 1;
      }
      if (!ok && list->content_type == MYX_DICT_VALUE)
      {
        for (i= 0; i < list->items_num; i++)
        {
          MYX_GRT_VALUE *name;
          name= myx_grt_dict_item_get_value(list->items[i], "name");
          if (name && strcmp(myx_grt_value_as_string(name), part)==0)
          {
            value= list->items[i];
            break;
          }
        }
      }
   }
    else
    {
      value= NULL;
      break;
    }
    
    part= strtok(NULL, "/");
  }

  g_free(p);

  return value;
}


/**
 ****************************************************************************
 * @brief Traverses a dict and changes the value in the path
 *
 *   This will treat the given value as a tree, where each node is named by 
 * the key in its parent.
 *   For dictionaries, the path component is used as the key name. For lists,
 * the given component is used to match the name attribute of each list item.
 *
 * @param dict a dict type value to be traversed which nested dicts
 * @param path a / separated list of key names to the value to be changed.
 * @param new_value the value to assign in the dict
 *
 * @return 0 on success
 * @return -1 if some component of the path doesn't exist or the
 * referenced object is not a dictionary or list.
 *
 *****************************************************************************/
int myx_grt_value_set_by_path(MYX_GRT_VALUE *dict, const char *path, MYX_GRT_VALUE *new_value)
{
  MYX_GRT_VALUE *value= dict, *parent= NULL;
  char *p;
  char *part;
  char *name;
  unsigned int index;
  int index_ok= 0;
  int rc= -1;

  g_return_val_if_fail(dict != NULL, -1);
  g_return_val_if_fail(path != NULL, -1);
  g_return_val_if_fail(*path == '/', -1);
  g_return_val_if_fail(new_value != NULL, -1);

  p= g_strdup(path);
  name= strrchr(p, '/');
  if (name > p && *(name+1) == 0) /* check if the path ends with a / */
  {
    *name= 0;
    name= strrchr(p, '/')+1;
  }
  else
    name++;
  name= g_strdup(name);

  part= strtok(p, "/");

  while (part && *part && value)
  { 
    index_ok= 0;

    parent= value;

    if (value->type == MYX_DICT_VALUE)
      value= myx_grt_dict_item_get_value(value, part);
    else if (value->type == MYX_LIST_VALUE)
    {
      unsigned int i;
      int ok= 0;
      unsigned int lindex;
      MYX_GRT_LIST *list= value->value.l;

      if (sscanf(part, "%i", &lindex)==1 && lindex >= 0 && lindex < list->items_num)
      {
        value= list->items[lindex];
        ok= 1;
      }
      if (!ok && list->content_type == MYX_DICT_VALUE)
      {
        for (i= 0; i < list->items_num; i++)
        {
          MYX_GRT_VALUE *name;
          name= myx_grt_dict_item_get_value(list->items[i], "name");
          if (name && strcmp(myx_grt_value_as_string(name), part)==0)
          {
            index= i;
            index_ok= 1;
            value= list->items[i];
            break;
          }
        }
      }
   }
    else
    {
      value= NULL;
      break;
    }
    
    part= strtok(NULL, "/");
  }

  g_free(p);

  if (parent)
  {    
    if (parent->type == MYX_DICT_VALUE)
    {
      myx_grt_dict_item_set_value(parent, name, new_value);
      rc= 0;
    }
    else if (parent->type == MYX_LIST_VALUE)
    {
      myx_grt_list_set_item(parent, index, new_value);
      rc= 0;
    }
  }

  g_free(name);
  
  return rc;
} 

/**
 ****************************************************************************
 * @brief Returns the name of a dict
 *
 *   This returns the name of a dict. The name of a dict is defined by
 * one of its item with the key "name"
 *
 * @param dict a dict type value
 *
 * @return the name as a string which must not be freed
 * @return NULL if no item with the key "name" exists
 *
 *****************************************************************************/
const char * myx_grt_dict_name_item_as_string(MYX_GRT_VALUE *dict)
{
  MYX_GRT_VALUE *name_item;
  
  g_return_val_if_fail(dict != NULL, NULL);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);

  name_item= myx_grt_dict_item_get_value(dict, "name");

  if (name_item)
    return myx_grt_value_as_string(name_item);
  else
    return NULL;
}


/**
 ****************************************************************************
 * @brief Returns the _id member of a dict
 *
 *   This returns the id of a dict. The id of a dict is defined by
 * one of its item with the key "_id"
 *
 * @param dict a dict type value
 *
 * @return the name as a string which must not be freed
 * @return NULL if no item with the key "name" exists
 *
 *****************************************************************************/
const char * myx_grt_dict_id_item_as_string(MYX_GRT_VALUE *dict)
{
  MYX_GRT_VALUE *id_item;

  g_return_val_if_fail(dict != NULL, NULL);
  g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);

  id_item= myx_grt_dict_item_get_value(dict, "_id");

  if (id_item)
    return myx_grt_value_as_string(id_item);
  else
    return NULL;
}



MYX_GRT_VALUE_TYPE myx_grt_value_get_type(MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(value != NULL, 0);

  return value->type;
}

int myx_grt_value_is_simple_type(MYX_GRT_VALUE *value)
{
  MYX_GRT_VALUE_TYPE value_type= myx_grt_value_get_type(value);

  if ( (value_type == MYX_INT_VALUE) ||
    (value_type == MYX_REAL_VALUE) ||
    (value_type == MYX_STRING_VALUE) )
    return 1;
  else
    return 0;
}

const char * myx_get_value_type_as_string(MYX_GRT_VALUE_TYPE value_type)
{
  switch (value_type)
  {
    case MYX_ANY_VALUE:
      return "";
    case MYX_INT_VALUE:
      return "int";
    case MYX_REAL_VALUE:
      return "real";
    case MYX_STRING_VALUE:
      return "string";
    case MYX_LIST_VALUE:
      return "list";
    case MYX_DICT_VALUE:
      return "dict";
  }
  return NULL;
}

MYX_GRT_VALUE_TYPE myx_get_value_type_from_string(const char *value_type_name, MYX_GRT_ERROR *error)
{
  *error= MYX_GRT_NO_ERROR;

  //if the value_type_name is NULL or an empty string, return MYX_ANY_VALUE
  if (!value_type_name || !value_type_name[0])
    return MYX_ANY_VALUE;
  else if (strcmp2(value_type_name, "int") == 0)
    return MYX_INT_VALUE;
  else if (strcmp2(value_type_name, "real") == 0)
    return MYX_REAL_VALUE;
  else if (strcmp2(value_type_name, "string") == 0)
    return MYX_STRING_VALUE;
  else if (strcmp2(value_type_name, "list") == 0)
    return MYX_LIST_VALUE;
  else if (strcmp2(value_type_name, "dict") == 0)
    return MYX_DICT_VALUE;

  *error= MYX_GRT_BAD_DATA;
  return MYX_INT_VALUE;
}


/* Get value contents */

int myx_grt_value_as_int(MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(value != NULL, 0);
  g_return_val_if_fail(value->type == MYX_INT_VALUE, 0);

  return value->value.i;
}


double myx_grt_value_as_real(MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(value != NULL, 0.0);
  g_return_val_if_fail(value->type == MYX_REAL_VALUE
                       || value->type == MYX_INT_VALUE, 0.0);

  if (value->type == MYX_REAL_VALUE)
    return value->value.r;
  else
    return value->value.i;
}


const char *myx_grt_value_as_string(MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(value != NULL, NULL);
  g_return_val_if_fail((value->type == MYX_STRING_VALUE), NULL);

  return value->value.s;
}

char * myx_grt_value_formated_as_string(MYX_GRT_VALUE *value)
{
  const char *name;

  switch (value->type)
  {
    case MYX_INT_VALUE:
      return g_strdup_printf("%d", value->value.i);
    case MYX_STRING_VALUE:
      return g_strdup(value->value.s);
    case MYX_REAL_VALUE:
      return g_strdup_printf("%16.8d", value->value.i);
    case MYX_LIST_VALUE:
      return g_strdup("LIST");
    case MYX_DICT_VALUE:
      name= myx_grt_dict_name_item_as_string(value);
      if (name)
        return g_strdup(name);
      else
        return g_strdup("DICT");
  }
  return g_strdup("");
}


MYX_GRT_LIST *myx_grt_value_as_list(MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(value != NULL, NULL);
  g_return_val_if_fail(value->type == MYX_LIST_VALUE, NULL);
  
  return value->value.l;
}


MYX_GRT_DICT *myx_grt_value_as_dict(MYX_GRT_VALUE *value)
{
  g_return_val_if_fail(value != NULL, NULL);
  g_return_val_if_fail(value->type == MYX_DICT_VALUE, NULL);
  
  return value->value.d;
}


/* Utility stuff to get the contents of a dict directly */

const char *myx_grt_dict_item_get_as_string(MYX_GRT_VALUE *dict, const char *key)
{
  MYX_GRT_VALUE *value= myx_grt_dict_item_get_value(dict, key);
  
  if (!value)
    return NULL;

  return myx_grt_value_as_string(value);
}

char * myx_grt_dict_item_get_formated_as_string(MYX_GRT_VALUE *dict, const char *key)
{
  MYX_GRT_VALUE *value= myx_grt_dict_item_get_value(dict, key);

  if (!value)
    return g_strdup("");
  else
    return myx_grt_value_formated_as_string(value);
}

MYX_GRT_VALUE * myx_grt_dict_item_get_reference_value(MYX_GRT *grt, MYX_GRT_VALUE *dict, const char *key)
{
  const char *ref_id= myx_grt_dict_item_get_as_string(dict, key);

  return myx_grt_reference_cache_lookup(grt, ref_id);
}


int myx_grt_dict_item_get_as_int(MYX_GRT_VALUE *dict, const char *key)
{
  MYX_GRT_VALUE *value= myx_grt_dict_item_get_value(dict, key);
  
  if (!value)
    return 0;

  return myx_grt_value_as_int(value);
}


double myx_grt_dict_item_get_as_double(MYX_GRT_VALUE *dict, const char *key)
{
  MYX_GRT_VALUE *value= myx_grt_dict_item_get_value(dict, key);
  
  if (!value)
    return 0.0;

  return myx_grt_value_as_real(value);
}


int _myx_grt_get_refcount(MYX_GRT_VALUE *value)
{
  return value->refcount;
}