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 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
|
/* Copyright (c) 2004, 2005 MySQL AB
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; 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>
*/
typedef const char * (*dict_identification_func)(MYX_GRT_VALUE *dict);
static int myx_grt_generic_diff_make(MYX_GRT *grt, MYX_GRT_VALUE *list,
MYX_GRT_VALUE * path, MYX_GRT_VALUE *source,
MYX_GRT_VALUE *target, dict_identification_func dict_id_func);
/*
****************************************************************************
* @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++)
{
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 Calls a bridge function
*
* Calls a bridge function given by module, name and value. The function returns
* a status. 1 means that the object still exists and 0 means that the object has
* been deleted.
*
* The bridge function that gets called returns a MYX_GRT_VALUE int value that
* holds the bridge function result.
* NULL or 0 ... success
* 1 ........... error
* 2 ........... the object has already been deleted
*
* The following functions have to be implemented by the bridge
*
* _initDict(dict) used to add bride_data to a dict based on it's struct_name.
* This may trigger the creation of a new real world object (e.g.
* forms.Form
*
*
* _updateToGrt(dict || list) used to check if the real world object assoziated with
* the dict still exists and to update the GRT list/dict to be
* in sync with the real world list/dict
*
* _updateFromGrt(dict || list) used to check if the real world object assoziated with
* the dict still exists and to update the real world list/dict to
* be in sync with the GRT list/dict
*
*
* _valueToGrt(any) used to update simple values according the the current value of
* the real world object member value
*
* _valueFromGrt(any) used to update the real world object member value from the
* GRT value
*
*
* _delValue(any) used to delete the real world value
*
* @param module the bridge module
* @param func_name the name of the bridge function
* @param the value managed by the bridge
*
* @return the value status, 1 for a valid value, 0 for a deleted value
*****************************************************************************/
int myx_grt_bridge_func(MYX_GRT_MODULE *module, const char *func_name, MYX_GRT_VALUE *value)
{
MYX_GRT *grt= module->loader->grt;
MYX_GRT_FUNCTION *func= NULL;
// find function by name
if (value->extended)
func= myx_grt_module_function_get(myx_grt_value_bridge_module_get(value), func_name);
if (func)
{
MYX_GRT_ERROR error;
MYX_GRT_VALUE *res= myx_grt_function_call(grt, func, value, &error);
if (error != MYX_GRT_NO_ERROR)
{
char *error_msg= g_strdup_printf("The function %s of the %s bridge failed with an error %d.",
func_name, module->name, error);
myx_grt_messages_stack_add(grt, 1, error_msg, NULL, 0, 0);
g_free(error_msg);
if (res)
myx_grt_value_release(res);
return 0;
}
if (res)
{
int res_val= myx_grt_value_as_int(res);
myx_grt_value_release(res);
// error
if (res_val == 1)
{
char *message= g_strdup_printf("The function %s of the %s bridge returned an error.", func_name, module->name);
myx_grt_messages_stack_add(grt, 1, message, NULL, 0, 0);
g_free(message);
return 0;
}
// object has been deleted
else if (res_val == 2)
{
return 0;
}
}
return 1;
}
else
{
char *error_msg= g_strdup_printf("The bridge does not implement the function %s.", func_name);
if (value->extended)
value->extended->bridge_module= NULL;
myx_grt_messages_stack_add(grt, 1, error_msg, NULL, 0, 0);
g_free(error_msg);
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 *grt, MYX_GRT_VALUE *dict, const char *struct_name)
{
return myx_grt_bridge_dict_struct_set_name(grt, dict, struct_name, 1, NULL);
}
/**
****************************************************************************
* @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_no_cache_register(MYX_GRT *grt, MYX_GRT_VALUE *dict, const char *struct_name)
{
return myx_grt_bridge_dict_struct_set_name(grt, dict, struct_name, 0, NULL);
}
/**
****************************************************************************
* @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);
if (!(dict->type == MYX_DICT_VALUE))
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)
{
const char *name;
g_return_val_if_fail(dict != NULL, NULL);
name= myx_grt_dict_struct_get_name(dict);
return name ? myx_grt_struct_get(grt, name) : NULL;
}
/**
****************************************************************************
* @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_inherits_from(grt, myx_grt_dict_struct_get_name(dict), parent_struct_name);
}
MYX_PUBLIC_FUNC int myx_grt_dict_struct_is_or_inherits_from(MYX_GRT *grt, MYX_GRT_VALUE *dict, const char *parent_struct_name)
{
const char *my_struct= myx_grt_dict_struct_get_name(dict);
return strcmp(my_struct, parent_struct_name)==0 || myx_grt_struct_inherits_from(grt, my_struct, parent_struct_name);
}
static void print_value(MYX_GRT *grt, MYX_GRT_VALUE *value, int depth)
{
unsigned int i;
unsigned int count;
char spaces[1024];
char buffer[200];
g_return_if_fail(depth < (int)sizeof(spaces));
if (!value)
{
MYX_PRINT(grt, "NULL"NL);
return;
}
switch (value->type)
{
case MYX_INT_VALUE:
g_snprintf(buffer, sizeof(buffer), "%i"NL, myx_grt_value_as_int(value));
MYX_PRINT(grt, buffer);
break;
case MYX_REAL_VALUE:
g_snprintf(buffer, sizeof(buffer), "%f"NL, myx_grt_value_as_real(value));
MYX_PRINT(grt, buffer);
break;
case MYX_STRING_VALUE:
/*MYX_PRINT(grt, value->value.s);
MYX_PRINT(grt, NL);*/
g_snprintf(buffer, sizeof(buffer), "\"%s\""NL, myx_grt_value_as_string(value));
MYX_PRINT(grt, buffer);
break;
case MYX_LIST_VALUE:
MYX_PRINT(grt, "["NL);
count= myx_grt_list_item_count(value);
for (i= 0; i < count; i++)
{
MYX_GRT_VALUE* Entry = myx_grt_list_item_get(value, i);
g_snprintf(spaces, sizeof(spaces), "%*s", depth, "");
MYX_PRINT(grt, spaces);
print_value(grt, Entry, 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);
count= myx_grt_dict_item_count(value);
for (i= 0; i < count; i++)
{
g_snprintf(spaces, sizeof(spaces), "%*s", depth+1, "");
MYX_PRINT(grt, spaces);
myx_grt_printf(grt, "%s = ", myx_grt_dict_item_key_by_index(value, i));
print_value(grt, myx_grt_dict_item_value_by_index(value, i), 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;
}
static char * value_as_lua_code(MYX_GRT_VALUE *value, char *code, int depth)
{
unsigned int i;
unsigned int count;
char spaces[1024];
char buffer[200];
if (!(depth < (int)sizeof(spaces)))
return code;
if (!value)
{
return code;
}
switch (value->type)
{
case MYX_INT_VALUE:
g_snprintf(buffer, sizeof(buffer), "%i", myx_grt_value_as_int(value));
code= str_g_append(code, buffer);
break;
case MYX_REAL_VALUE:
g_snprintf(buffer, sizeof(buffer), "%f", myx_grt_value_as_real(value));
code= str_g_append(code, buffer);
break;
case MYX_STRING_VALUE:
g_snprintf(buffer, sizeof(buffer), "\"%s\"", myx_grt_value_as_string(value));
code= str_g_append(code, buffer);
break;
case MYX_LIST_VALUE:
code= str_g_append(code, "{"NL);
count= myx_grt_list_item_count(value);
for (i= 0; i < count; i++)
{
g_snprintf(spaces, sizeof(spaces), "%*s", depth * 2, "");
code= str_g_append(code, spaces);
code= value_as_lua_code(myx_grt_list_item_get(value, i), code, depth+1);
if (i < count - 1)
code= str_g_append(code, ", " NL);
else
code= str_g_append(code, NL);
}
g_snprintf(spaces, sizeof(spaces), "%*s", depth * 2, "");
code= str_g_append(code, spaces);
code= str_g_append(code, "}");
break;
case MYX_DICT_VALUE:
count= myx_grt_dict_item_count(value);
code= str_g_append(code, "{"NL);
for (i= 0; i < count; i++)
{
g_snprintf(spaces, sizeof(spaces), "%*s", depth * 2, "");
code= str_g_append(code, spaces);
g_snprintf(buffer, sizeof(buffer), "%s = ", myx_grt_dict_item_key_by_index(value, i));
code= str_g_append(code, buffer);
code= value_as_lua_code(myx_grt_dict_item_value_by_index(value, i), code, depth+1);
if (i < count - 1)
code= str_g_append(code, ", " NL);
else
code= str_g_append(code, NL);
}
if (depth > 1)
{
g_snprintf(spaces, sizeof(spaces), "%*s", abs(depth - 1) * 2, "");
code= str_g_append(code, spaces);
}
code= str_g_append(code, "}");
break;
default:
break;
}
return code;
}
/**
****************************************************************************
* @brief Returns the value formated as Lua code
*
* @param grt the GRT environment
* @param value the value to be formated
*
* @return a string containing Lua code that has to be freed
*****************************************************************************/
char * myx_grt_value_as_lua_code(MYX_GRT_VALUE *value, int depth)
{
char *code= NULL;
return value_as_lua_code(value, code, depth);
}
/**
****************************************************************************
* @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);
if (!value)
return 0;
g_return_val_if_fail(value->refcount > 0, -1);
value->refcount--;
if (value->refcount == 0)
{
// First let listeners know.
myx_grt_value_listener_call(value, MYX_GVCR_DELETE);
// Remove listeners
if (value->extended)
{
if (value->extended->listeners)
{
unsigned int i;
for (i= 0; i < value->extended->listeners_num; i++)
g_free(value->extended->listeners[i]);
g_free(value->extended->listeners);
}
// Finally let the bridge do whatever it needs if the value is managed by a bridge.
if (myx_grt_value_is_bridged(value))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(value), "_delValue", value);
g_free(value->extended);
}
switch (value->type)
{
case MYX_ANY_VALUE:
break;
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.
* Proxy objects cannot have their reference count increased
*
* @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);
if (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(MYX_GRT *grt, 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(grt, 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;
}
void myx_grt_dict_content_set_struct_name(MYX_GRT_VALUE *dict, const char *struct_name)
{
dict->value.d->content_struct_name= g_strdup(struct_name);
}
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;
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 Changes a GRT value of MYX_INT_VALUE type
*
* @param value the value to change
* @param i the new integer.
*
* @return the original GRT value with the new integer
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_bridge_value_change_int(MYX_GRT_VALUE *value, int i)
{
g_return_val_if_fail(value!=NULL, NULL);
g_return_val_if_fail(myx_grt_value_get_type(value) == MYX_INT_VALUE, NULL);
value->value.i= i;
return value;
}
/**
****************************************************************************
* @brief Changes a GRT value of MYX_REAL_VALUE type
*
* @param value the value to change
* @param d the new number.
*
* @return the original GRT value with the new number
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_bridge_value_change_real(MYX_GRT_VALUE *value, double d)
{
g_return_val_if_fail(value!=NULL, NULL);
g_return_val_if_fail(myx_grt_value_get_type(value) == MYX_REAL_VALUE, NULL);
value->value.r= d;
return value;
}
/**
****************************************************************************
* @brief Changes a GRT value of MYX_STRING_VALUE type
*
* @param value the value to change
* @param s the new string. The string will be copied.
*
* @return the original GRT value with the new string
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_bridge_value_change_string(MYX_GRT_VALUE *value, const char *s)
{
g_return_val_if_fail(value!=NULL, NULL);
g_return_val_if_fail(myx_grt_value_get_type(value) == MYX_STRING_VALUE, NULL);
if (value->value.s)
g_free(value->value.s);
value->value.s= g_strdup(s);
return value;
}
static MYX_GRT_VALUE * myx_grt_value_dup_bridge_settings(MYX_GRT_VALUE *src_value, MYX_GRT_VALUE *dest_value)
{
if (myx_grt_value_is_bridged(src_value))
{
myx_grt_value_extend(dest_value);
dest_value->extended->bridge_module= src_value->extended->bridge_module;
dest_value->extended->bridge_data_object= src_value->extended->bridge_data_object;
dest_value->extended->bridge_data_owner= src_value->extended->bridge_data_owner;
if (dest_value->extended->bridge_dict_key)
g_free(dest_value->extended->bridge_dict_key);
if (src_value->extended->bridge_dict_key)
dest_value->extended->bridge_dict_key= g_strdup(src_value->extended->bridge_dict_key);
else
dest_value->extended->bridge_dict_key= NULL;
dest_value->extended->bridge_list_index= src_value->extended->bridge_list_index;
}
return dest_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)
{
if (value != NULL)
{
switch (myx_grt_value_get_type(value))
{
case MYX_ANY_VALUE:
break;
case MYX_INT_VALUE:
return myx_grt_value_dup_bridge_settings(value, myx_grt_value_from_int(myx_grt_value_as_int(value)));
case MYX_REAL_VALUE:
return myx_grt_value_dup_bridge_settings(value, myx_grt_value_from_real(myx_grt_value_as_real(value)));
case MYX_STRING_VALUE:
return myx_grt_value_dup_bridge_settings(value, 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;
myx_grt_value_dup_bridge_settings(value, list);
//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(NULL, myx_grt_dict_struct_get_name(value));
myx_grt_value_dup_bridge_settings(value, dict);
}
//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
*
* @param struct_name the struct name the object is an instance of
*
* @return A newly created dict value struct.
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_dict_new(MYX_GRT *grt, const char *struct_name)
{
return myx_grt_bridge_dict_new(grt, struct_name, NULL);
}
/**
****************************************************************************
* @brief Adds a new listener to the GRT value
*
* Creates a new MYX_GRT_VALUE_LISTENER and adds it to the list of listeners
* for this GRT value. If the listener list has not been allocated yet
* it will be allocated.
*
* @param grt pointer to the grt environment
* @param value the value the listener should be added
* @param user_data pointer that can be set by the caller to store userdata
* @param listener_callback pointer to the callback function
*****************************************************************************/
void myx_grt_value_listener_add(MYX_GRT *grt, MYX_GRT_VALUE *value, void *user_data,
MYX_GRT_VALUE_LISTENER_CALLBACK listener_callback)
{
unsigned int i;
gboolean foundListener = FALSE;
if (myx_grt_value_has_listeners(value))
{
for (i= 0; i < value->extended->listeners_num; i++)
{
MYX_GRT_VALUE_LISTENER *listener= value->extended->listeners[i];
if (listener->function == listener_callback)
{
foundListener = TRUE;
break;
};
};
};
// Add listener only if it has not already been done.
if (!foundListener)
{
MYX_GRT_VALUE_LISTENER *listener= g_new0(MYX_GRT_VALUE_LISTENER, 1);
listener->listeners_grt = grt;
listener->function= listener_callback;
listener->user_data= user_data;
myx_grt_value_extend(value);
value->extended->listeners_num++;
value->extended->listeners= g_realloc(value->extended->listeners,
sizeof(MYX_GRT_VALUE_LISTENER *) * value->extended->listeners_num);
value->extended->listeners[value->extended->listeners_num - 1]= listener;
};
}
/**
****************************************************************************
* @brief Removes the given listener from the GRT value
*
* Removes MYX_GRT_VALUE_LISTENER from the given value.
*
* @param value the value the listener should be removed from
* @param listener_callback pointer to the callback function
*****************************************************************************/
void myx_grt_value_listener_remove(MYX_GRT_VALUE *value, void *user_data, MYX_GRT_VALUE_LISTENER_CALLBACK listener_callback_remove)
{
if (myx_grt_value_has_listeners(value))
{
unsigned int i;
for (i= 0; i < value->extended->listeners_num; i++)
{
MYX_GRT_VALUE_LISTENER *listener= value->extended->listeners[i];
if ((listener->function == listener_callback_remove) && (listener->user_data == user_data))
break;
};
if (i < value->extended->listeners_num)
{
// Found the listener.
g_free(value->extended->listeners[i]);
--value->extended->listeners_num;
if (i < value->extended->listeners_num)
memmove(value->extended->listeners + i, value->extended->listeners+ i + 1,
sizeof(MYX_GRT_VALUE_LISTENER*)*(value->extended->listeners_num-i));
};
}
}
MYX_PUBLIC_FUNC int myx_grt_value_listener_get(MYX_GRT *grt, MYX_GRT_VALUE *value, void *user_data,
int (*listener_callback_get)(MYX_GRT *grt, MYX_GRT_VALUE *value, MYX_GRT_VALUE_CALLBACK_REASON reason, void *user_data))
{
if (myx_grt_value_has_listeners(value))
{
unsigned int i;
for (i= 0; i < value->extended->listeners_num; i++)
{
MYX_GRT_VALUE_LISTENER *listener= value->extended->listeners[i];
if ((listener->function == listener_callback_get) && (listener->user_data == user_data))
break;
};
if (i < value->extended->listeners_num)
{
// Found the listener.
return 1;
};
}
return 0;
}
/**
****************************************************************************
* @brief Calls back to all registered listeners
*
* Calls back to all registered listeners of the value with the given reason
*
* @param value the value which listener should be called
* @param reason the reason to call the listeners
*****************************************************************************/
int myx_grt_value_listener_call(MYX_GRT_VALUE *value, MYX_GRT_VALUE_CALLBACK_REASON reason)
{
int res= 0;
if (myx_grt_value_has_listeners(value))
{
unsigned int i;
for (i= 0; i < value->extended->listeners_num; i++)
{
int func_res;
MYX_GRT_VALUE_LISTENER *listener= value->extended->listeners[i];
func_res= listener->function(listener->listeners_grt, value, reason, listener->user_data);
if ((res == 0) && (func_res != 0))
res= func_res;
}
}
return res;
}
/**
****************************************************************************
* @brief Initialize an empty GRT dict value with its child values defined
* by the struct
*
* All direct child values defined by the given struct are also created.
*
* @param grt pointer to the GRT environment
* @param dict a pointer to the empty dict
* @param name the name of the object
* @param _id the _id of the object
* @param owner the owner _id of the object
*
* @return A newly created dict value struct.
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_dict_init_obj(MYX_GRT *grt, MYX_GRT_VALUE *dict, const char *name, const char *_id,
const char *owner)
{
MYX_GRT_STRUCT *gstruct= myx_grt_struct_get(grt, myx_grt_dict_struct_get_name(dict));
unsigned int i;
unsigned int count= myx_grt_struct_get_member_count_total(grt, gstruct);
// create child values
for (i= 0; i < count; i++)
{
MYX_GRT_STRUCT_MEMBER *member= myx_grt_struct_get_member_by_index_total(grt, gstruct, i);
MYX_GRT_VALUE_TYPE member_type= myx_grt_struct_member_get_type(member);
const char *member_name= myx_grt_struct_get_member_name(member);
const char *member_default= myx_grt_struct_get_member_default(member);
if (member_type == MYX_STRING_VALUE)
{
MYX_GRT_VALUE *child_value;
if (strcmp2(member_name, "name") == 0)
child_value= myx_grt_value_from_string(name);
else if (strcmp2(member_name, "_id") == 0)
{
if (_id && _id[0])
child_value= myx_grt_value_from_string(_id);
else
{
// if the _id is not set yet
if (myx_grt_dict_item_get_value(dict, "_id") == NULL)
{
char *guid= myx_grt_get_guid();
child_value= myx_grt_value_from_string(guid);
g_free(guid);
}
// if the _id was already set (e.g. by a bridge, keep existing value)
else
continue;
}
}
else if (strcmp2(member_name, "owner") == 0)
{
if (owner && owner[0])
child_value= myx_grt_value_from_string(owner);
else
child_value= NULL;
}
else if (member_default)
child_value= myx_grt_value_from_string(member_default);
else
child_value= myx_grt_value_from_string("");
/*if (myx_grt_struct_member_get_is_ref(member))
{
const char *content_struct= myx_grt_struct_member_get_content_struct_name(member);
myx_grt_dict_struct_set_name(child_value, content_struct);
}*/
myx_grt_dict_item_set_value(dict, member_name, child_value);
}
else if (member_type == MYX_INT_VALUE)
{
if (member_default && member_default[0])
myx_grt_dict_item_set_value_from_int(dict, member_name, atoi(member_default));
else
myx_grt_dict_item_set_value_from_int(dict, member_name, 0);
}
else if (member_type == MYX_REAL_VALUE)
{
if (member_default && member_default[0])
myx_grt_dict_item_set_value_from_real(dict, member_name, atof(member_default));
else
myx_grt_dict_item_set_value_from_real(dict, member_name, 0.0);
}
else if (member_type == MYX_LIST_VALUE)
{
MYX_GRT_VALUE_TYPE content_type= myx_grt_struct_member_get_content_type(member);
const char *content_struct= myx_grt_struct_member_get_content_struct_name(member);
MYX_GRT_VALUE *child_value= myx_grt_list_new(content_type, content_struct);
myx_grt_dict_item_set_value(dict, member_name, child_value);
}
else if (member_type == MYX_DICT_VALUE)
{
MYX_GRT_VALUE_TYPE content_type= myx_grt_struct_member_get_content_type(member);
const char *content_struct= myx_grt_struct_member_get_content_struct_name(member);
MYX_GRT_VALUE *child_value= NULL;
if (content_type != MYX_ANY_VALUE)
child_value= myx_grt_dict_new_typed(content_type, content_struct);
myx_grt_dict_item_set_value(dict, member_name, child_value);
/*const char *struct_name= myx_grt_struct_member_get_struct_name(member);
MYX_GRT_VALUE *child_value;
if (struct_name)
child_value= myx_grt_dict_new(struct_name);
else
{
MYX_GRT_VALUE_TYPE content_type= myx_grt_struct_member_get_content_type(member);
const char *content_struct= myx_grt_struct_member_get_content_struct_name(member);
child_value= myx_grt_dict_new_typed(content_type, content_struct);
}*/
}
}
return dict;
}
/**
****************************************************************************
* @brief Creates a new empty GRT value of MYX_GRT_DICT type and initialize
* its child values defined by the struct
*
* Creates a GRT value of type MYX_GRT_DICT, which is a dictionary or
* mapping of keys strings to values. All direct child values defined by the
* given struct are also created.
*
* @param grt pointer to the GRT environment
* @param struct_name the name of the struct
* @param name the name of the object
* @param _id the _id of the object
* @param owner the owner _id of the object
*
* @return A newly created dict value struct.
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_dict_new_obj(MYX_GRT *grt, const char *struct_name,
const char *name, const char *_id, const char *owner)
{
MYX_GRT_VALUE *dict= myx_grt_dict_new(grt, struct_name);
return myx_grt_dict_init_obj(grt, dict, name, _id, owner);
}
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)
{
return myx_grt_bridge_dict_item_set_value(dict, key, value, TRUE);
}
/**
****************************************************************************
* @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)
{
return myx_grt_bridge_dict_item_set_value_from_string(dict, key, s, TRUE);
}
/**
****************************************************************************
* @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 i 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)
{
return myx_grt_bridge_dict_item_set_value_from_int(dict, key, i, TRUE);
}
/**
****************************************************************************
* @brief Sets a real value for a key in the dict
*
* Creates a new real 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 d the double to be inserted.
*
* @return 0 on success, -1 on error
*****************************************************************************/
int myx_grt_dict_item_set_value_from_real(MYX_GRT_VALUE *dict, const char *key, double d)
{
return myx_grt_bridge_dict_item_set_value_from_real(dict, key, d, TRUE);
}
/**
****************************************************************************
* @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)
{
return myx_grt_bridge_dict_item_get_value(dict, key, 1);
}
/**
****************************************************************************
* @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)
{
return myx_grt_bridge_dict_item_del(dict, key, 1);
}
/**
****************************************************************************
* @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)
{
return myx_grt_bridge_dict_item_count(dict, 1);
}
/**
****************************************************************************
* @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 >= myx_grt_dict_item_count(dict))
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)
{
return myx_grt_bridge_dict_item_key_by_index(dict, index, 1);
}
/**
****************************************************************************
* @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)
{
return myx_grt_bridge_dict_item_value_by_index(dict, index, 1);
}
/**
****************************************************************************
* @brief Returns the complex (dicts, lists) items of elements in the dict value
*
* Returns the number of complex (dicts, lists) items in the dict. Use in
* conjunction with myx_grt_dict_item_key_by_index_complex() and
* myx_grt_dict_item_value_by_index_complex() 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_complex(MYX_GRT_VALUE *dict)
{
unsigned int i;
unsigned int count= 0;
g_return_val_if_fail(dict!=NULL, 0);
g_return_val_if_fail(dict->type == MYX_DICT_VALUE, 0);
// if the dict is managed by a bridge, update
if (myx_grt_value_is_bridged(dict))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(dict), "_updateToGrt", dict);
for (i= 0; i < dict->value.d->items_num; i++)
{
MYX_GRT_DICT_ITEM *item= dict->value.d->items + i;
MYX_GRT_VALUE_TYPE item_type= myx_grt_value_get_type(item->value);
if ((item_type == MYX_DICT_VALUE) || (item_type == MYX_LIST_VALUE))
count++;
}
return count;
}
/**
****************************************************************************
* @brief Get the item's key name at index in the dict. Only complex (dicts, lists)
* values are counted.
*
* Retrieves the item's key name at the specified index in the dict. Only complex
* (dicts, lists) values are counted.
*
* @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_complex(MYX_GRT_VALUE *dict, unsigned int index)
{
unsigned int i;
unsigned int count= 0;
const char *key= NULL;
g_return_val_if_fail(dict!=NULL, NULL);
g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);
// if the dict is managed by a bridge, update
/*if (dict->bridge_module)
myx_grt_bridge_func(dict->bridge_module, "_updateToGrt", dict);*/
// loop over all items, only look at dicts and lists
for (i= 0; i < dict->value.d->items_num; i++)
{
MYX_GRT_DICT_ITEM *item= dict->value.d->items + i;
MYX_GRT_VALUE_TYPE item_type= myx_grt_value_get_type(item->value);
if ((item_type == MYX_DICT_VALUE) || (item_type == MYX_LIST_VALUE))
{
count++;
// if the number of complex items equals the index, return the key
if (count == index + 1)
{
key= dict->value.d->items[i].key;
break;
}
}
}
return key;
}
/**
****************************************************************************
* @brief Get the item's value at index in the dict. Only complex (dicts, lists)
* values are counted.
*
* Retrieves the item's value at the specified index in the dict. Only complex
* (dicts, lists) values are counted.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_complex(MYX_GRT_VALUE *dict, unsigned int index)
{
unsigned int i;
unsigned int count= 0;
MYX_GRT_VALUE *value= NULL;
g_return_val_if_fail(dict!=NULL, NULL);
g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);
// if the dict is managed by a bridge, update
/*if (dict->bridge_module)
myx_grt_bridge_func(dict->bridge_module, "_updateToGrt", dict);*/
// loop over all items, only look at dicts and lists
for (i= 0; i < dict->value.d->items_num; i++)
{
MYX_GRT_DICT_ITEM *item= dict->value.d->items + i;
MYX_GRT_VALUE_TYPE item_type= myx_grt_value_get_type(item->value);
if ((item_type == MYX_DICT_VALUE) || (item_type == MYX_LIST_VALUE))
{
count++;
// if the number of complex items equals the index, return the key
if (count == index + 1)
{
value= dict->value.d->items[i].value;
break;
}
}
}
// If the value is managed by a bridge, get its value from there.
if (myx_grt_value_is_bridged(value))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(value), "_valueToGrt", value);
return 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
* @param struct_name if content_type is dict and this is set only dicts of the
* given struct can be inserted
*
* @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)
{
return myx_grt_bridge_list_item_insert(list, index, value, 1);
}
/**
****************************************************************************
* @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)
{
return myx_grt_bridge_list_item_del(list, index, 1);
}
/**
****************************************************************************
* @brief Removes a string from the list
*
* Removes the string from the list. The string value 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 Removes a value from the list
*
* Removes the value from the list. The value will
* be released.
*
* @param list GRT value of type list
* @param value value to remove
*
* @return 0 on success, -1 if the value does not exist.
*****************************************************************************/
int myx_grt_list_item_del_value(MYX_GRT_VALUE *list, MYX_GRT_VALUE *value)
{
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 *item= myx_grt_list_item_get(list, i);
if (value == item)
{
myx_grt_list_item_del(list, i);
item_exists= 0;
}
}
return item_exists;
}
/**
****************************************************************************
* @brief Removes a GRT object with the given name from the list
*
* Removes the GRT object from the list. The value will
* be released.
*
* @param list GRT value of type list
* @param name name of the object to remove
*
* @return 0 on success, -1 if the value does not exist.
*****************************************************************************/
int myx_grt_list_del_by_object_name(MYX_GRT_VALUE *list, const char *name)
{
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 (strcmp2(name, myx_grt_dict_name_item_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 0;
g_return_val_if_fail(list->type == MYX_LIST_VALUE, 0);
// if the list is managed by a bridge
if (myx_grt_value_is_bridged(list))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(list), "_updateToGrt", list);
return list->value.l->items_num;
}
/**
****************************************************************************
* @brief Returns the value at the given index
*
* Returns the element at the given index. The index is 0 based.
*
* @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)
{
return myx_grt_bridge_list_item_get(list, index, 1);
}
/**
****************************************************************************
* @brief Returns the referenced value at the given index
*
* Returns the value an element at the given index referes to. The index is 0 based.
*
* @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_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 object with the name "name" from a list.
*
* Returns the first value that is a dict and has a entry "name".
* The index is 0 based.
*
* @param list GRT value of type list
* @param name the name of the object
*
* @return The item or NULL if the name is not found.
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_list_item_get_by_object_name(MYX_GRT_VALUE *list, const char *name)
{
MYX_GRT_VALUE *value= NULL;
unsigned int i;
if (!list)
return NULL;
for (i= 0; i < myx_grt_list_item_count(list); i++)
{
MYX_GRT_VALUE *item= myx_grt_list_item_get(list, i);
if (myx_grt_value_get_type(item) == MYX_DICT_VALUE)
{
const char *item_name= myx_grt_dict_item_get_as_string(item, "name");
if (item_name && (strcmp2(item_name, name) == 0))
{
value= item;
break;
}
}
}
return value;
}
/**
****************************************************************************
* @brief Returns the object with the name "name" from a ref list.
*
* Returns the first ref value that is a dict and has a entry "name".
* The index is 0 based.
*
* @param grt pointer to the GRT
* @param list GRT value of type list
* @param name the name of the object
*
* @return The refered value or NULL if the name is not found.
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_list_item_get_reference_value_by_object_name(MYX_GRT *grt, MYX_GRT_VALUE *list, const char *name)
{
MYX_GRT_VALUE *value= NULL;
unsigned int i;
for (i= 0; i < myx_grt_list_item_count(list); i++)
{
MYX_GRT_VALUE *item= myx_grt_list_item_get_reference_value(grt, list, i);
if (item)
{
const char *item_name= myx_grt_dict_item_get_as_string(item, "name");
if (item_name && (strcmp2(item_name, name) == 0))
{
value= item;
break;
}
}
}
return value;
}
/**
****************************************************************************
* @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)
return "";*/
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_item_set(MYX_GRT_VALUE *list, unsigned int index, MYX_GRT_VALUE *value)
{
MYX_GRT_VALUE* currentValue;
MYX_GRT_VALUE* newValue = 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(value!=NULL, -1);
// Replace the value.
currentValue = list->value.l->items[index];
if (currentValue != value)
{
// If there is a struct assign, make sure we just change the value and do not replace it
if (myx_grt_value_is_simple_type(currentValue) && myx_grt_value_is_simple_type(value))
{
// Change the actual value in the existing value.
switch (myx_grt_value_get_type(currentValue))
{
case MYX_INT_VALUE:
myx_grt_bridge_value_change_int(currentValue, myx_grt_value_as_int(value));
break;
case MYX_REAL_VALUE:
myx_grt_bridge_value_change_real(currentValue, myx_grt_value_as_real(value));
break;
case MYX_STRING_VALUE:
if (myx_grt_value_get_type(value) == MYX_STRING_VALUE)
myx_grt_bridge_value_change_string(currentValue, myx_grt_value_as_string(value));
else
{
char* newString = myx_grt_value_formated_as_string(value);
myx_grt_bridge_value_change_string(currentValue, newString);
g_free(newString);
};
break;
default:
break;
};
newValue = currentValue;
}
else
{
// TODO: simply replacing the value might cause trouble with additional fields in the struct. Avoid it.
if (value != NULL)
{
list->value.l->items[index]= myx_grt_value_retain(value);
if (myx_grt_value_is_bridged(list))
{
// Duplicate additional fields. Use either the existing values (if set) or that of the list.
if (myx_grt_value_is_bridged(currentValue))
{
myx_grt_value_extend(value);
value->extended->bridge_module= currentValue->extended->bridge_module;
value->extended->bridge_data_owner= currentValue->extended->bridge_data_owner;
if (value->extended->bridge_dict_key)
g_free(value->extended->bridge_dict_key);
value->extended->bridge_dict_key= g_strdup(list->extended->bridge_dict_key);
value->extended->bridge_list_index= index;
}
else
{
myx_grt_value_extend(value);
value->extended->bridge_module= list->extended->bridge_module;
// Note: special handling for data owner here. Read comment in myx_grt_bridge_list_item_insert for
// more details.
value->extended->bridge_data_owner= list->extended->bridge_data_owner;
if (value->extended->bridge_dict_key)
g_free(value->extended->bridge_dict_key);
value->extended->bridge_dict_key= g_strdup(list->extended->bridge_dict_key);
value->extended->bridge_list_index= index;
};
};
}
else
list->value.l->items[index]= NULL;
myx_grt_value_release(currentValue);
}
if (newValue && myx_grt_value_is_bridged(newValue) && myx_grt_value_is_bridged(list))
{
myx_grt_bridge_func(myx_grt_value_bridge_module_get(newValue), "_valueFromGrt", newValue);
//??? not sure if this is really needed
//myx_grt_bridge_func(list->bridge_module, "_updateFromGrt", list);
}
};
myx_grt_value_listener_call(list, MYX_GVCR_LIST_ITEM_CHANGE);
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 Removes the last path part from the given path end returns the result.
*
* @param path The path which must be traversed one level up.
*
* @return Either NULL (if path is NULL or just "/") or the path without the last part.
* The caller is responsible to free the returned string if not NULL.
*****************************************************************************/
MYX_PUBLIC_FUNC char * myx_get_parent_path(const char *path)
{
char* delimiter;
if (path == NULL || strcmp2(path, "/") == 0)
return NULL;
delimiter= strrchr(path, '/');
if (delimiter == NULL)
return NULL;
return g_strndup(path, (int) (delimiter - path));
}
/**
****************************************************************************
* @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_dict_item_get_by_path(MYX_GRT *grt, 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;
unsigned int Count = myx_grt_list_item_count(value);
if (sscanf(part, "%i", &lindex) == 1 && lindex >= 0 && lindex < Count)
{
value = myx_grt_list_item_get(value, lindex);
ok = 1;
};
if (!ok && list->content_type == MYX_DICT_VALUE)
{
int FoundEntry = 0;
for (i= 0; i < Count; i++)
{
MYX_GRT_VALUE *name;
MYX_GRT_VALUE* ListValue = myx_grt_list_item_get(value, i);
name= myx_grt_dict_item_get_value(ListValue, "name");
if (name && strcmp(myx_grt_value_as_string(name), part) == 0)
{
value = ListValue;
FoundEntry = TRUE;
break;
}
}
if (!FoundEntry)
value = NULL;
}
}
else if (value->type == MYX_STRING_VALUE)
{
value= myx_grt_reference_cache_lookup(grt, myx_grt_value_as_string(value));
if (value)
value= myx_grt_dict_item_get_value(value, part);
}
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_dict_item_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)
{
if(lindex < list->items_num)
{
index= lindex;
index_ok= 1;
value= list->items[lindex];
ok= 1;
}
else if(lindex == list->items_num) // insert after the last element
{
index= lindex;
value= NULL;
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 && index_ok)
{
myx_grt_list_item_set(parent, index, new_value);
rc= 0;
}
else if(parent->type == MYX_LIST_VALUE && index == myx_grt_list_item_count(parent))
{
myx_grt_list_item_add(parent, new_value);
}
}
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;
}
/**
****************************************************************************
* @brief Sets the _id item of the given dict value to a unique identifier
*
* Sets the item _id of the given dict value to a unique identifier
*
* @param dict a dict type value
*
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_dict_generate_id(MYX_GRT_VALUE *dict)
{
char *guid;
g_return_val_if_fail(dict != NULL, NULL);
g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);
guid= myx_grt_get_guid();
myx_grt_dict_item_set_value_from_string(dict, "_id", guid);
g_free(guid);
return dict;
}
MYX_GRT_VALUE_TYPE myx_grt_value_get_type(MYX_GRT_VALUE *value)
{
if (value)
return value->type;
else
return MYX_ANY_VALUE;
}
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;
}
/**
****************************************************************************
* @brief Allocated memory for the extended information. If the memory has
* already been allocated, the function does nothing
*
* @param value
*
* @return the extended value
****************************************************************************/
MYX_GRT_VALUE * myx_grt_value_extend(MYX_GRT_VALUE *value)
{
if (!value->extended)
value->extended= g_new0(MYX_GRT_VALUE_EXTENDED, 1);
return value;
}
/**
****************************************************************************
* @brief Checks if the given value is a bridged object
*
* @param value
*
* @return 1 if the value is a dict and a proxy object, 0 otherwise
****************************************************************************/
int myx_grt_value_is_bridged(MYX_GRT_VALUE *value)
{
if ((value) && (value->extended) && (value->extended->bridge_module))
return 1;
else
return 0;
}
/**
****************************************************************************
* @brief Checks if the given value has listeners allocated
*
* @param value
*
* @return 1 if the value is a dict and a proxy object, 0 otherwise
****************************************************************************/
int myx_grt_value_has_listeners(MYX_GRT_VALUE *value)
{
if ((value) && (value->extended) && (value->extended->listeners))
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);
// if this value is managed by a bridge, get current value of the item
if (myx_grt_value_is_bridged(value))
if (!myx_grt_bridge_func(myx_grt_value_bridge_module_get(value), "_valueToGrt", value))
return 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 this value is managed by a bridge, get current value of the item
if (myx_grt_value_is_bridged(value))
if (!myx_grt_bridge_func(myx_grt_value_bridge_module_get(value), "_valueToGrt", value))
return 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);
// if this value is managed by a bridge, get current value of the item
if (myx_grt_value_is_bridged(value))
if (!myx_grt_bridge_func(myx_grt_value_bridge_module_get(value), "_valueToGrt", value))
return "";
return value->value.s;
}
char * myx_grt_value_formated_as_string(MYX_GRT_VALUE *value)
{
const char *name;
if (!value)
return g_strdup("NULL");
// if this value is managed by a bridge, get current value of the item
if (myx_grt_value_is_bridged(value))
if (!myx_grt_bridge_func(myx_grt_value_bridge_module_get(value), "_valueToGrt", value))
return "";
switch (value->type)
{
case MYX_ANY_VALUE:
break;
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("%.6g", value->value.r);
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);
// if this value is managed by a bridge, get current value of the item
if (myx_grt_value_is_bridged(value))
if (!myx_grt_bridge_func(myx_grt_value_bridge_module_get(value), "_updateToGrt", value))
return 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);
// if this value is managed by a bridge, get current value of the item
if (myx_grt_value_is_bridged(value))
if (!myx_grt_bridge_func(myx_grt_value_bridge_module_get(value), "_updateToGrt", value))
return 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_real(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;
}
/**
****************************************************************************
* @brief Creates an error GRT value that can be returned from a function call
*
* Creates a GRT value of type MYX_GRT_DICT, that contains the value as
* it's successful return value. The value will be released.
*
* @param message the error message
* @param detail detail information about the error
*
* @return A newly created dict value struct containing the error information.
*****************************************************************************/
MYX_GRT_VALUE *make_return_value(MYX_GRT_VALUE *value)
{
MYX_GRT_VALUE *tmp= myx_grt_dict_create(NULL, NULL,
"value", MYX_ANY_VALUE, value,
NULL);
// do not release the value because myx_grt_dict_create will not retain the value
//myx_grt_value_release(value);
return tmp;
}
/**
****************************************************************************
* @brief Creates an error GRT value that can be returned from a function call
*
* Creates a GRT value of type MYX_GRT_DICT, that contains a error and a detail
* error message.
*
* @param message the error message
* @param detail detail information about the error
*
* @return A newly created dict value struct containing the error information.
*****************************************************************************/
MYX_GRT_VALUE *make_return_value_error(const char *message, const char *detail)
{
return myx_grt_dict_create(NULL, NULL,
"error", MYX_STRING_VALUE, message,
"detail", MYX_STRING_VALUE, detail,
NULL);
}
void myx_grt_value_bridge_module_set(MYX_GRT_VALUE *value, MYX_GRT_MODULE *module)
{
if (value)
{
myx_grt_value_extend(value);
value->extended->bridge_module= module;
}
}
MYX_GRT_MODULE * myx_grt_value_bridge_module_get(MYX_GRT_VALUE *value)
{
g_return_val_if_fail(value != NULL, NULL);
if (value->extended)
return value->extended->bridge_module;
else
return NULL;
}
void myx_grt_value_bridge_data_object_set(MYX_GRT_VALUE *value, void *data)
{
unsigned int i;
if (value)
{
myx_grt_value_extend(value);
value->extended->bridge_data_object= data;
// Recursively go down the subtree and set all data to the same value if
// the value is not a dict (which has usually own data).
// TODO: Do recursive bridge_data_set for non-NULL values.
if (data == NULL)
{
// No update needed for value at this point. The bridge is going to reset all values
// so we assume the member count is correct at this moment.
// Since only dicts and lists can have child values we only consider those here.
switch (myx_grt_value_get_type(value))
{
case MYX_DICT_VALUE:
{
for (i= 0; i < value->value.d->items_num; ++i)
{
MYX_GRT_VALUE* childValue= value->value.d->items[i].value;
if ((childValue != NULL) && (childValue->type != MYX_DICT_VALUE))
myx_grt_value_bridge_data_owner_set(childValue, data);
};
break;
};
case MYX_LIST_VALUE:
{
for (i= 0; i < value->value.l->items_num; ++i)
{
MYX_GRT_VALUE* childValue= value->value.l->items[i];
if ((childValue != NULL) && (childValue->type != MYX_DICT_VALUE))
myx_grt_value_bridge_data_owner_set(childValue, data);
};
break;
};
default:
break;
};
};
};
}
void * myx_grt_value_bridge_data_object_get(MYX_GRT_VALUE *value)
{
g_return_val_if_fail(value != NULL, NULL);
if (value->extended)
return value->extended->bridge_data_object;
else
return NULL;
}
void myx_grt_value_bridge_data_owner_set(MYX_GRT_VALUE *value, void *data)
{
if (value)
{
myx_grt_value_extend(value);
value->extended->bridge_data_owner= data;
}
}
void * myx_grt_value_bridge_data_owner_get(MYX_GRT_VALUE *value)
{
g_return_val_if_fail(value != NULL, NULL);
if (value->extended)
return value->extended->bridge_data_owner;
else
return NULL;
}
void myx_grt_value_bridge_dict_key_set(MYX_GRT_VALUE *value, const char *key)
{
if (value)
{
myx_grt_value_extend(value);
if (value->extended->bridge_dict_key)
g_free(value->extended->bridge_dict_key);
value->extended->bridge_dict_key= g_strdup(key);
}
}
const char * myx_grt_value_bridge_dict_key_get(MYX_GRT_VALUE *value)
{
g_return_val_if_fail(value != NULL, NULL);
if (value->extended)
return value->extended->bridge_dict_key;
else
return NULL;
}
void myx_grt_value_bridge_list_index_set(MYX_GRT_VALUE *value, int index)
{
if (value)
{
myx_grt_value_extend(value);
value->extended->bridge_list_index= index;
}
}
int myx_grt_value_bridge_list_index_get(MYX_GRT_VALUE *value)
{
g_return_val_if_fail(value != NULL, -1);
if (value->extended)
return value->extended->bridge_list_index;
else
return 0;
}
MYX_PUBLIC_FUNC int myx_grt_bridge_dict_item_set_value(MYX_GRT_VALUE *dict, const char *key, MYX_GRT_VALUE *value, int do_bridge_callback)
{
int i;
gboolean assignDictBridgeData = FALSE;
MYX_GRT_VALUE* newValue = value;
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 (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);
if (value)
{
dict->value.d->items[0].value= myx_grt_value_retain(value);
assignDictBridgeData = TRUE;
}
else
dict->value.d->items[0].value= NULL;
}
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);
if (value)
{
dict->value.d->items[i].value= myx_grt_value_retain(value);
assignDictBridgeData = TRUE;
}
else
dict->value.d->items[i].value= NULL;
}
else
{
// Replace the value.
MYX_GRT_VALUE* currentValue = dict->value.d->items[i].value;
if (currentValue != value)
{
// If there is a struct assign, make sure we just change the value and do not replace it
if (myx_grt_value_is_simple_type(currentValue) && myx_grt_value_is_simple_type(value) &&
myx_grt_dict_struct_get_name(dict) != NULL)
{
// Change the actual value in the existing value.
switch (myx_grt_value_get_type(currentValue))
{
case MYX_INT_VALUE:
myx_grt_bridge_value_change_int(currentValue, myx_grt_value_as_int(value));
break;
case MYX_REAL_VALUE:
myx_grt_bridge_value_change_real(currentValue, myx_grt_value_as_real(value));
break;
case MYX_STRING_VALUE:
if (myx_grt_value_get_type(value) == MYX_STRING_VALUE)
myx_grt_bridge_value_change_string(currentValue, myx_grt_value_as_string(value));
else
{
char* newString = myx_grt_value_formated_as_string(value);
myx_grt_bridge_value_change_string(currentValue, newString);
g_free(newString);
};
break;
default:
break;
};
newValue = currentValue;
}
else
{
// TODO: simply replacing the value might cause trouble with additional fields in the struct. Avoid it.
if (value)
{
dict->value.d->items[i].value= myx_grt_value_retain(value);
if (myx_grt_value_is_bridged(dict))
{
// Duplicate additional fields. Use either the existing values (if set) or that of the dict.
if (myx_grt_value_is_bridged(value))
{
if (currentValue)
{
value->extended->bridge_module= currentValue->extended->bridge_module;
value->extended->bridge_data_owner= currentValue->extended->bridge_data_owner;
}
/*
else if (value->bridge_module == NULL)
{
value->bridge_module= dict->bridge_module;
value->bridge_data_owner= dict->bridge_data_owner;
}*/
if (value->extended->bridge_dict_key)
g_free(value->extended->bridge_dict_key);
newValue->extended->bridge_dict_key= g_strdup(key);
}
else
assignDictBridgeData = TRUE;
};
}
else
dict->value.d->items[i].value= NULL;
myx_grt_value_release(currentValue);
}
}
}
}
// set bridge data if the dict is managed by a bridge
if (newValue && myx_grt_value_is_bridged(dict))
{
if (assignDictBridgeData)
{
myx_grt_value_extend(newValue);
newValue->extended->bridge_module= dict->extended->bridge_module;
newValue->extended->bridge_data_owner= dict->extended->bridge_data_object;
if (newValue->extended->bridge_dict_key)
g_free(newValue->extended->bridge_dict_key);
newValue->extended->bridge_dict_key= g_strdup(key);
};
if (do_bridge_callback && newValue->extended)
{
myx_grt_bridge_func(newValue->extended->bridge_module, "_valueFromGrt", newValue);
//??? not sure if this is really needed
//myx_grt_bridge_func(dict->bridge_module, "_updateFromGrt", dict);
}
}
myx_grt_value_listener_call(dict, MYX_GVCR_DICT_ITEM_CHANGE);
return 0;
}
MYX_PUBLIC_FUNC int myx_grt_bridge_dict_item_set_value_from_string(MYX_GRT_VALUE *dict, const char *key, const char *s, int do_bridge_callback)
{
int res;
MYX_GRT_VALUE *value= myx_grt_value_from_string(s);
if (!value)
return 0;
res= myx_grt_bridge_dict_item_set_value(dict, key, value, do_bridge_callback);
//lower reference counter by one
myx_grt_value_release(value);
return res;
}
MYX_PUBLIC_FUNC int myx_grt_bridge_dict_item_set_value_from_int(MYX_GRT_VALUE *dict, const char *key, int i, int do_bridge_callback)
{
int res;
MYX_GRT_VALUE *value= myx_grt_value_from_int(i);
if (!value)
return 0;
res= myx_grt_bridge_dict_item_set_value(dict, key, value, do_bridge_callback);
//lower reference counter by one
myx_grt_value_release(value);
return res;
}
MYX_PUBLIC_FUNC int myx_grt_bridge_dict_item_set_value_from_real(MYX_GRT_VALUE *dict, const char *key, double d, int do_bridge_callback)
{
int res;
MYX_GRT_VALUE *value= myx_grt_value_from_real(d);
if (!value)
return 0;
res= myx_grt_bridge_dict_item_set_value(dict, key, value, do_bridge_callback);
//lower reference counter by one
myx_grt_value_release(value);
return res;
}
MYX_GRT_VALUE * myx_grt_bridge_dict_item_get_value(MYX_GRT_VALUE *dict, const char *key, int do_bridge_callback)
{
int i;
MYX_GRT_VALUE * result= NULL;
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
{
result= dict->value.d->items[i].value;
// If this list is managed by a bridge, update it before accessing the values.
if (myx_grt_value_is_bridged(dict) && (do_bridge_callback == 1))
{
// if the dict is still available
/*if (!myx_grt_bridge_func(dict->bridge_module, "_updateToGrt", dict))
return NULL;*/
// If the value is managed by a bridge, get its value from there.
if (result && myx_grt_value_is_bridged(result))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(result), "_valueToGrt", result);
}
return result;
}
}
int myx_grt_bridge_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_bridge_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_bridge_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;
}
unsigned int myx_grt_bridge_dict_item_count(MYX_GRT_VALUE *dict, int do_bridge_callback)
{
g_return_val_if_fail(dict!=NULL, 0);
if (!(dict->type == MYX_DICT_VALUE))
g_return_val_if_fail(dict->type == MYX_DICT_VALUE, 0);
if (myx_grt_value_is_bridged(dict) && (do_bridge_callback == 1))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(dict), "_updateToGrt", dict);
return dict->value.d->items_num;
}
const char * myx_grt_bridge_dict_item_key_by_index(MYX_GRT_VALUE *dict, unsigned int index, int do_bridge_callback)
{
g_return_val_if_fail(dict!=NULL, NULL);
g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);
// to reduce the number of callbacks, the bridge will not be called
if (index >= myx_grt_bridge_dict_item_count(dict, 0))
return NULL;
return dict->value.d->items[index].key;
}
MYX_GRT_VALUE * myx_grt_bridge_dict_item_value_by_index(MYX_GRT_VALUE *dict, unsigned int index, int do_bridge_callback)
{
MYX_GRT_VALUE* result;
g_return_val_if_fail(dict != NULL, NULL);
g_return_val_if_fail(dict->type == MYX_DICT_VALUE, NULL);
// to reduce the number of callbacks, the bridge will not be called
if (index >= myx_grt_bridge_dict_item_count(dict, 0))
return NULL;
result= dict->value.d->items[index].value;
// If the value is managed by a bridge, get its value from there.
if ((result != NULL) && myx_grt_value_is_bridged(result) && (do_bridge_callback == 1))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(result), "_valueToGrt", result);
return result;
}
int myx_grt_bridge_dict_item_del(MYX_GRT_VALUE *dict, const char *key, int do_bridge_callback)
{
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;
}
if (myx_grt_value_is_bridged(dict) && (do_bridge_callback == 1))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(dict), "_updateFromGrt", dict);
myx_grt_value_listener_call(dict, MYX_GVCR_DICT_ITEM_CHANGE);
}
unsigned int myx_grt_bridge_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;
}
MYX_GRT_VALUE *myx_grt_bridge_list_item_get(MYX_GRT_VALUE *list, unsigned int index, int do_bridge_callback)
{
g_return_val_if_fail(list != NULL, NULL);
g_return_val_if_fail(list->type == MYX_LIST_VALUE, NULL);
// if this list is managed by a bridge, update it before accessing the values
/*if (do_bridge_callback && list->bridge_module)
if (!myx_grt_bridge_func(list->bridge_module, "_updateToGrt", list))
return NULL;*/
if (do_bridge_callback)
g_return_val_if_fail(index < myx_grt_list_item_count(list), NULL);
else
g_return_val_if_fail(index < myx_grt_bridge_list_item_count(list), NULL);
// if this list is managed by a bridge, get current value of the item
if (do_bridge_callback && myx_grt_value_is_bridged(list->value.l->items[index]) &&
(myx_grt_value_is_simple_type(list->value.l->items[index])))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(list->value.l->items[index]), "_valueToGrt", list->value.l->items[index]);
return list->value.l->items[index];
}
int myx_grt_bridge_list_item_insert(MYX_GRT_VALUE *list, int index, MYX_GRT_VALUE *value, int do_bridge_callback)
{
MYX_GRT_VALUE *item= 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;
// Autoconvert
if ((list->value.l->content_type == MYX_REAL_VALUE) && (item->type == MYX_INT_VALUE))
item= myx_grt_value_from_real(myx_grt_value_as_real(value));
else
{
g_return_val_if_fail((list->value.l->content_type == item->type) || (list->value.l->content_type == MYX_ANY_VALUE), -1);
myx_grt_value_retain(item);
}
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)
{
index= list->value.l->items_num-1;
list->value.l->items[index]= item;
}
else
{
if ((index + 1) < (int) list->value.l->items_num)
memmove(list->value.l->items + index + 1, list->value.l->items + index,
sizeof(MYX_GRT_VALUE*)*(list->value.l->items_num-index));
list->value.l->items[index]= item;
}
// set bridge data if the list is managed by a bridge
if (myx_grt_value_is_bridged(list))
{
item->extended->bridge_module= myx_grt_value_bridge_module_get(list);
// Note! For a new list item, the list's bridge_data_owner is set as the items bridge_data_owner
// because each list item needs to know which "real world object" is its owner
// Example: Application.Forms[0]
// each form in the forms list should know to which application it belongs, therefore
// Application->bridge_data_object= pointer_to_real_world_application_object,
// Forms[]->bridge_data_owner == Application->bridge_data_object &&
// Forms[1]->bridge_data_owner == Application->bridge_data_object,
// Forms[1]->bridge_data_object= pointer_to_real_world_form_object
item->extended->bridge_data_owner= list->extended->bridge_data_owner;
if (item->extended->bridge_dict_key)
g_free(item->extended->bridge_dict_key);
item->extended->bridge_dict_key= g_strdup(list->extended->bridge_dict_key);
item->extended->bridge_list_index= index;
if (do_bridge_callback)
{
myx_grt_bridge_func(myx_grt_value_bridge_module_get(item), "_valueFromGrt", item);
//??? not sure if this is really needed
//myx_grt_bridge_func(list->bridge_module, "_updateFromGrt", list);
}
}
myx_grt_value_listener_call(list, MYX_GVCR_LIST_CHANGE);
return 0;
}
/**
****************************************************************************
* @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_bridge_list_item_del(MYX_GRT_VALUE *list, int index, int do_bridge_callback)
{
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--;
// call bridge function
if (do_bridge_callback && myx_grt_value_is_bridged(list))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(list), "_updateFromGrt", list);
myx_grt_value_listener_call(list, MYX_GVCR_LIST_CHANGE);
return 0;
}
MYX_GRT_VALUE * myx_grt_bridge_dict_new(MYX_GRT *grt, const char *struct_name, void *bridge_data)
{
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 a struct_name was given, assign it to the dict
if ((struct_name) && (struct_name[0]))
myx_grt_bridge_dict_struct_set_name(grt, value, struct_name, 1, bridge_data);
return value;
}
/**
****************************************************************************
* @brief Assigns a MYX_GRT_STRUCT to a dict value and also set the bridge_data
*
* This will assign a MYX_GRT_STRUCT to a dict value and also set the
* bridge_data if given
*
* @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_bridge_dict_struct_set_name(MYX_GRT *grt, MYX_GRT_VALUE *dict, const char *struct_name,
int register_in_cache, void *bridge_data)
{
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);
if (grt)
{
myx_grt_value_extend(dict);
// check if the struct is managed by a bridge
dict->extended->bridge_module= myx_grt_struct_get_bridge(grt, myx_grt_struct_get(grt, struct_name));
// set bridge_data
dict->extended->bridge_data_object= bridge_data;
// call bridge function
if (myx_grt_value_bridge_module_get(dict))
myx_grt_bridge_func(myx_grt_value_bridge_module_get(dict), "_initDict", dict);
// cache object
/*if (register_in_cache)
{
const char *_id= myx_grt_dict_item_get_as_string(dict, "_id");
if (!_id)
{
char *new_id= myx_grt_get_guid();
myx_grt_bridge_dict_item_set_value_from_string(dict, "_id", new_id);
myx_grt_cache_object(dict, new_id);
g_free(new_id);
}
else
myx_grt_cache_object(dict, _id);
}*/
}
return 0;
}
static MYX_GRT_VALUE * myx_append_path(MYX_GRT_VALUE *path, int index,
const char *postfix, const char *action)
{
char *path_new= NULL;
const char *path_cchar= myx_grt_value_as_string(path);
MYX_GRT_VALUE *retval= NULL;
if(index >= 0)
{
char *idx= g_strdup_printf("%d", index);
path_new= g_strconcat(action, path_cchar, "/", idx, NULL);
}
else
{
const char *slash= (postfix && *postfix) ? "/" : "";
path_new= g_strconcat(action, path_cchar, slash, postfix, NULL);
}
retval= myx_grt_value_from_string(path_new);
g_free(path_new);
return retval;
}
/**
****************************************************************************
* @brief Compares types of two dict objects
*
* @param grt pointer to the GRT environment
* @param source the source value
* @param target the target value
*
* @return true if the dicts hold the same type, false if not
*****************************************************************************/
static int myx_grt_dict_compare_types(MYX_GRT *grt, MYX_GRT_VALUE *source, MYX_GRT_VALUE *target)
{
const char *c1, *c2;
g_return_val_if_fail(myx_grt_value_get_type(source) == MYX_DICT_VALUE, 0);
g_return_val_if_fail(myx_grt_value_get_type(target) == MYX_DICT_VALUE, 0);
g_return_val_if_fail(myx_grt_dict_content_get_type(source) == myx_grt_dict_content_get_type(target), 0);
c1= myx_grt_dict_struct_get_name(source);
c2= myx_grt_dict_struct_get_name(target);
if(c1 && c2)
{
g_return_val_if_fail(strcmp(c1, c2) == 0, 0);
}
else if(c1 || c2)
{
return 0;
}
c1= myx_grt_dict_content_get_struct_name(source);
c2= myx_grt_dict_content_get_struct_name(target);
if(c1 && c2)
{
g_return_val_if_fail(strcmp(c1, c2) == 0, 0);
}
else if(c1 || c2)
{
return 0;
}
return 1;
}
/**
****************************************************************************
* @brief Compares types of two list objects
*
* @param grt pointer to the GRT environment
* @param source the source value
* @param target the target value
*
* @return true if the lists hold the same type, false if not
*****************************************************************************/
static int myx_grt_list_compare_types(MYX_GRT *grt, MYX_GRT_VALUE *source, MYX_GRT_VALUE *target)
{
const char *c1, *c2;
g_return_val_if_fail(myx_grt_value_get_type(source) == MYX_LIST_VALUE, 0);
g_return_val_if_fail(myx_grt_value_get_type(target) == MYX_LIST_VALUE, 0);
g_return_val_if_fail(myx_grt_list_content_get_type(source) == myx_grt_list_content_get_type(target), 0);
c1= myx_grt_list_content_get_struct_name(source);
c2= myx_grt_list_content_get_struct_name(target);
if(c1 && c2)
{
g_return_val_if_fail(strcmp(c1 ,c2) == 0, 0);
}
else if(c1 || c2)
{
return 0;
}
return 1;
}
/**
****************************************************************************
* @brief Compares types of two objects
*
* @param grt pointer to the GRT environment
* @param source the source value
* @param target the target value
*
* @return true if the values are of the same type, false if not
*****************************************************************************/
static int myx_grt_value_compare_types(MYX_GRT *grt, MYX_GRT_VALUE *source, MYX_GRT_VALUE *target)
{
MYX_GRT_VALUE_TYPE st= myx_grt_value_get_type(source);
MYX_GRT_VALUE_TYPE tt= myx_grt_value_get_type(target);
g_return_val_if_fail(st == tt, 0);
if(!myx_grt_value_is_simple_type(source))
{
if(st == MYX_DICT_VALUE)
{
return myx_grt_dict_compare_types(grt, source, target);
}
else if(st == MYX_LIST_VALUE)
{
return myx_grt_list_compare_types(grt, source, target);
}
else
{
return 0;
}
}
return 1;
}
static int myx_grt_simple_value_compare(MYX_GRT *grt, MYX_GRT_VALUE *source, MYX_GRT_VALUE *target)
{
int int_s, int_t;
double double_s, double_t;
const char *cchar_s, *cchar_t;
MYX_GRT_VALUE_TYPE st= myx_grt_value_get_type(source);
MYX_GRT_VALUE_TYPE tt= myx_grt_value_get_type(target);
g_return_val_if_fail(st == tt, -2);
switch(st)
{
case MYX_INT_VALUE:
int_s= myx_grt_value_as_int(source);
int_t= myx_grt_value_as_int(target);
return int_s < int_t ? -1 : int_s == int_t ? 0 : 1;
case MYX_REAL_VALUE:
double_s= myx_grt_value_as_real(source);
double_t= myx_grt_value_as_real(target);
return double_s < double_t ? -1 : double_s == double_t ? 0 : 1;
case MYX_STRING_VALUE:
cchar_s= myx_grt_value_as_string(source);
cchar_t= myx_grt_value_as_string(target);
return strcmp(cchar_s, cchar_t);
default:
break;
}
return -2;
}
// returns 0 if there were no changes
static int myx_grt_simple_value_diff_make(MYX_GRT *grt, MYX_GRT_VALUE *list,
MYX_GRT_VALUE * path, MYX_GRT_VALUE *source,
MYX_GRT_VALUE *target)
{
const char *cchar_s, *cchar_t;
double double_s, double_t;
int int_s, int_t;
int retval= 0;
MYX_GRT_VALUE_TYPE st= myx_grt_value_get_type(source);
MYX_GRT_VALUE_TYPE tt= myx_grt_value_get_type(target);
g_return_val_if_fail(st == tt, 0);
switch(st)
{
case MYX_INT_VALUE:
int_s= myx_grt_value_as_int(source);
int_t= myx_grt_value_as_int(target);
if(int_s != int_t)
{
// change int value
myx_grt_list_item_add(list, myx_append_path(path, -1, "", "/"));
myx_grt_list_item_add(list, myx_grt_value_from_int(int_s));
myx_grt_bridge_value_change_int(target, int_s);
retval= 1;
}
break;
case MYX_REAL_VALUE:
double_s= myx_grt_value_as_real(source);
double_t= myx_grt_value_as_real(target);
if(double_s != double_t)
{
// change double value
myx_grt_list_item_add(list, myx_append_path(path, -1, "", "/"));
myx_grt_list_item_add(list, myx_grt_value_from_real(double_s));
myx_grt_bridge_value_change_real(target, double_s);
retval= 1;
}
break;
case MYX_STRING_VALUE:
cchar_s= myx_grt_value_as_string(source);
cchar_t= myx_grt_value_as_string(target);
if(strcmp(cchar_s, cchar_t) != 0)
{
// change const char* value
myx_grt_list_item_add(list, myx_append_path(path, -1, "", "/"));
myx_grt_list_item_add(list, myx_grt_value_from_string(cchar_s));
myx_grt_bridge_value_change_string(target, cchar_s);
retval= 1;
}
break;
default:
break;
}
return retval;
}
/*
static const char *get_dict_id(MYX_GRT_VALUE *dict)
{
return myx_grt_dict_id_item_as_string(dict);
}
*/
static const char *get_dict_name(MYX_GRT_VALUE *dict)
{
const char *old_name= myx_grt_dict_item_get_as_string(dict, "oldName");
if(old_name)
return old_name;
return myx_grt_dict_item_get_as_string(dict, "name");
}
// returns 0 if there were no changes
static int myx_grt_list_diff_make(MYX_GRT *grt, MYX_GRT_VALUE *list,
MYX_GRT_VALUE * path, MYX_GRT_VALUE *source,
MYX_GRT_VALUE *target,
dict_identification_func dict_id_func)
{
int next, is_simple;
const char *s_id, *t_id;
unsigned int s_count= myx_grt_list_item_count(source);
unsigned int t_count;
MYX_GRT_VALUE *s_value, *t_value, *sub_path;
unsigned int i, j;
int retval= 0;
// find possible / * +
restart1:
for(i= 0; i < s_count; i++)
{
next= 0;
s_value= myx_grt_list_item_get(source, i);
t_count= myx_grt_list_item_count(target);
if(myx_grt_value_is_simple_type(s_value))
{
for(j= 0; j < t_count; j++)
{
t_value= myx_grt_list_item_get(target, j);
if(myx_grt_simple_value_compare(grt, s_value, t_value) == 0)
{
if(i != j)
{
// this value could be already matched earlier
// this is detected by checking if source[j] is equal to target[j]
if((j < i) && (myx_grt_simple_value_compare(grt, myx_grt_list_item_get(source, j), t_value) == 0))
continue;
// move s_value in diff
sub_path= myx_append_path(path, i, "", "*");
myx_grt_list_item_add(list, sub_path);
myx_grt_value_release(sub_path);
myx_grt_list_item_add(list, myx_grt_value_from_int(j));
myx_grt_list_item_del(target, j);
if(i > myx_grt_list_item_count(target))
myx_grt_list_item_insert(target, myx_grt_list_item_count(target), s_value);
else
myx_grt_list_item_insert(target, i, s_value);
retval= 1;
}
next= 1;
break;
}
}
if(next == 0) // s_value is not found in target
{
// add s_value to diff
sub_path= myx_append_path(path, i, "", "+");
myx_grt_list_item_add(list, sub_path);
myx_grt_value_release(sub_path);
s_value= myx_grt_value_dup(s_value);
myx_grt_list_item_add(list, s_value);
if(i > myx_grt_list_item_count(target))
myx_grt_list_item_insert(target, myx_grt_list_item_count(target), s_value);
else
myx_grt_list_item_insert(target, i, s_value);
retval= 1;
goto restart1;
}
}
else // s_value is a dict or a list
{
//s_id= myx_grt_dict_id_item_as_string(s_value);
//s_id= myx_grt_dict_item_get_as_string(s_value, "name");
s_id= dict_id_func(s_value);
for(j= 0; j < t_count; j++)
{
t_value= myx_grt_list_item_get(target, j);
//t_id= myx_grt_dict_id_item_as_string(t_value);
//t_id= myx_grt_dict_item_get_as_string(t_value, "name");
t_id= dict_id_func(t_value);
if(strcmp(s_id, t_id) == 0)
{
sub_path= myx_append_path(path, i, "", "");
retval= myx_grt_generic_diff_make(grt, list, sub_path, s_value, t_value, dict_id_func) || retval;
if(i != j)
{
// move s_value in diff
sub_path= myx_append_path(path, i, "", "*");
myx_grt_list_item_add(list, sub_path);
myx_grt_value_release(sub_path);
myx_grt_list_item_add(list, myx_grt_value_from_int(j));
myx_grt_list_item_del(target, j);
if(i > myx_grt_list_item_count(target))
myx_grt_list_item_insert(target, myx_grt_list_item_count(target), s_value);
else
myx_grt_list_item_insert(target, i, s_value);
retval= 1;
}
next= 1;
break;
}
}
if(next == 0) // s_value is not found in target
{
// add s_value to diff
sub_path= myx_append_path(path, i, "", "+");
myx_grt_list_item_add(list, sub_path);
myx_grt_value_release(sub_path);
s_value= myx_grt_value_dup(s_value);
myx_grt_list_item_add(list, s_value);
if(i > myx_grt_list_item_count(target))
myx_grt_list_item_insert(target, myx_grt_list_item_count(target), s_value);
else
myx_grt_list_item_insert(target, i, s_value);
retval= 1;
goto restart1;
}
}
} // for(i)
s_count= myx_grt_list_item_count(source);
restart2:
t_count= myx_grt_list_item_count(target);
for(j= 0; j < t_count; j++)
{
next= 0;
t_value= myx_grt_list_item_get(target, j);
is_simple= myx_grt_value_is_simple_type(t_value);
if(!is_simple)
{
//t_id= myx_grt_dict_id_item_as_string(t_value);
t_id= dict_id_func(t_value);
}
for(i= 0; i < s_count; i++)
{
s_value= myx_grt_list_item_get(source, i);
if(is_simple)
{
next= (myx_grt_simple_value_compare(grt, s_value, t_value) == 0);
}
else // t_value is dict/list
{
//s_id= myx_grt_dict_id_item_as_string(s_value);
s_id= dict_id_func(s_value);
next= (strcmp(s_id, t_id) == 0);
}
if(next)
{
break;
}
}
if(next == 0)
{
// del t_value from the diff
sub_path= myx_append_path(path, j, "", "-");
myx_grt_list_item_add(list, sub_path);
myx_grt_value_release(sub_path);
myx_grt_list_item_add(list, myx_grt_value_from_string(""));
myx_grt_list_item_del(target, j);
retval= 1;
goto restart2;
}
}
return retval;
}
// returns 0 if nothing was changed
static int myx_grt_dict_diff_make(MYX_GRT *grt, MYX_GRT_VALUE *list,
MYX_GRT_VALUE * path, MYX_GRT_VALUE *source,
MYX_GRT_VALUE *target, dict_identification_func dict_id_func)
{
const char *s_key, *t_key;
MYX_GRT_VALUE *s_value, *t_value, *sub_path;
unsigned int i, j, next;
unsigned int s_count;
unsigned int t_count;
MYX_GRT_VALUE_TYPE st;
MYX_GRT_VALUE_TYPE tt;
int retval= 0; // the change flag
// find possible / + -
restart1:
s_count= myx_grt_dict_item_count(source);
for(i= 0; i < s_count; i++)
{
next= 0;
myx_grt_dict_item_by_index(source, i, &s_key, &s_value);
t_count= myx_grt_dict_item_count(target);
for(j= 0; j < t_count; j++)
{
myx_grt_dict_item_by_index(target, j, &t_key, &t_value);
st= myx_grt_value_get_type(s_value);
tt= myx_grt_value_get_type(t_value);
if(strcmp(s_key, t_key) == 0)
{
if(st == tt)
{
// diff s_value/t_value
sub_path= myx_append_path(path, -1, s_key, "");
retval= myx_grt_generic_diff_make(grt, list, sub_path, s_value, t_value, dict_id_func) || retval;
myx_grt_value_release(sub_path);
}
else if(s_value != NULL) // values have differenent types just exchange
{
sub_path= myx_append_path(path, -1, s_key, "/");
myx_grt_list_item_add(list, sub_path);
myx_grt_list_item_add(list, s_value);
myx_grt_value_release(sub_path);
retval= 1;
}
else
{
myx_grt_list_item_add(list, myx_append_path(path, -1, t_key, "-"));
myx_grt_list_item_add(list, myx_grt_value_from_string(""));
myx_grt_dict_item_del(target, t_key);
retval= 1;
}
next= 1;
break;
}
}
if(next == 0)
{
myx_grt_list_item_add(list, myx_append_path(path, -1, s_key, "+"));
s_value= myx_grt_value_dup(s_value);
myx_grt_list_item_add(list, s_value);
myx_grt_dict_item_set_value(target, g_strdup(s_key), s_value);
retval= 1;
goto restart1;
}
}
// find possible -
s_count= myx_grt_dict_item_count(source);
restart2:
t_count= myx_grt_dict_item_count(target);
for(j= 0; j < t_count; j++)
{
next= 0;
myx_grt_dict_item_by_index(target, j, &t_key, &t_value);
for(i= 0; i < s_count; i++)
{
myx_grt_dict_item_by_index(source, i, &s_key, &s_value);
if(strcmp(s_key, t_key) == 0)
{
next= 1;
break;
}
}
if(next == 0)
{
// delete s_value from diff
myx_grt_list_item_add(list, myx_append_path(path, -1, t_key, "-"));
myx_grt_list_item_add(list, myx_grt_value_from_string(""));
myx_grt_dict_item_del(target, t_key);
retval= 1;
goto restart2;
}
}
return retval;
}
// returns 1 if diff found any changes
static int myx_grt_generic_diff_make(MYX_GRT *grt, MYX_GRT_VALUE *list,
MYX_GRT_VALUE * path, MYX_GRT_VALUE *source,
MYX_GRT_VALUE *target, dict_identification_func dict_id_func)
{
MYX_GRT_VALUE_TYPE st= myx_grt_value_get_type(source);
MYX_GRT_VALUE_TYPE tt= myx_grt_value_get_type(target);
g_return_val_if_fail(st == tt, 0);
switch(st)
{
case MYX_ANY_VALUE:
break;
case MYX_INT_VALUE:
case MYX_REAL_VALUE:
case MYX_STRING_VALUE:
return myx_grt_simple_value_diff_make(grt, list, path, source, target);
case MYX_LIST_VALUE:
return myx_grt_list_diff_make(grt, list, path, source, target, dict_id_func);
case MYX_DICT_VALUE:
return myx_grt_dict_diff_make(grt, list, path, source, target, dict_id_func);
}
return 0;
}
MYX_GRT_VALUE * myx_grt_value_diff_make_with_params(MYX_GRT *grt, MYX_GRT_VALUE *source,
MYX_GRT_VALUE *target, dict_identification_func dict_id_func)
{
MYX_GRT_VALUE * retval= myx_grt_list_new(MYX_ANY_VALUE, NULL);
MYX_GRT_VALUE * source_dup, *target_dup;
MYX_GRT_VALUE * path;
g_return_val_if_fail(retval != NULL, NULL);
g_return_val_if_fail(myx_grt_value_compare_types(grt, source, target) == 1, NULL);
path= myx_grt_value_from_string("");
source_dup= myx_grt_value_dup(source);
target_dup= myx_grt_value_dup(target);
myx_grt_generic_diff_make(grt, retval, path, target_dup, source_dup, dict_id_func);
return retval;
}
/**
****************************************************************************
* @brief Creates a diff between two GRT values
*
* Recursivly walks through the given value and its members and builds the
* diff as a GRT list, that is of the form
*
* {path, change, path, change, path, change}.
*
* Example:
* {"/name", MYX_STRING_VALUE, "./engine", MYX_STRING_VALUE, "-/columns/1", NULL,
* "+/columns/2", MYX_DICT_VALUE}
*
* the encoding of the path is the following. If it starts with
* / a value change
* - a value remove
* + a value added
* * a value changed position in list
*
* for lists, the index that was removed or added is given
*
* @param grt pointer to the GRT environment
* @param source the source value
* @param target the target value
*
* @return the diff as a GRT value
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_value_diff_make(MYX_GRT *grt, MYX_GRT_VALUE *source, MYX_GRT_VALUE *target)
{
return myx_grt_value_diff_make_with_params(grt, source, target, get_dict_name);
}
/**
****************************************************************************
* @brief Applies a diff to a value
*
* Applies a diff to the given value and also returns the updated value
*
* @param grt pointer to the GRT environment
* @param value the value the diff will be applied to
* @param diff the diff
*
* @return value updated (not duplicated)
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_value_diff_apply(MYX_GRT *grt, MYX_GRT_VALUE *value, MYX_GRT_VALUE *diff)
{
unsigned int i, diff_count;
diff_count= myx_grt_list_item_count(diff);
for(i= 0; i < diff_count; i++)
{
unsigned move_from_index, move_to_index;
char *parent_path;
MYX_GRT_VALUE *parent_obj;
const char *diff_path= myx_grt_value_as_string(myx_grt_list_item_get(diff, i++));
MYX_GRT_VALUE *action_obj= myx_grt_list_item_get(diff, i);
char diff_op= *diff_path++;
MYX_GRT_VALUE_TYPE type;
parent_path= myx_get_parent_path(diff_path);
parent_path= (parent_path && parent_path[0]) ? parent_path : g_strdup("/");
parent_obj= myx_grt_dict_item_get_by_path(grt, value, parent_path);
//diff_path= diff_path+strlen(parent_path)-1;
diff_path= diff_path+strlen(parent_path);
if(diff_path[0] != '/')
{
diff_path= g_strconcat("/", diff_path, NULL);
}
switch(diff_op)
{
case '+':
type= myx_grt_value_get_type(parent_obj);
if(type == MYX_LIST_VALUE)
{
if(diff_path[0] == '/')
{
++diff_path;
}
myx_grt_list_item_insert(parent_obj, atoi(diff_path), action_obj);
}
else if(type == MYX_DICT_VALUE)
{
//myx_grt_dict_item_set_by_path(value, diff_path, action_obj);
myx_grt_dict_item_set_by_path(parent_obj, diff_path, action_obj);
}
break;
case '/':
//myx_grt_dict_item_set_by_path(, diff_path, action_obj);
myx_grt_dict_item_set_by_path(parent_obj, diff_path, action_obj);
break;
case '-':
type= myx_grt_value_get_type(parent_obj);
if(diff_path[0] == '/')
{
++diff_path;
}
if(type == MYX_LIST_VALUE)
{
myx_grt_list_item_del(parent_obj, atoi(diff_path));
}
else if(type == MYX_DICT_VALUE)
{
myx_grt_dict_item_del(parent_obj, diff_path);
}
break;
case '*':
if(diff_path[0] == '/')
{
++diff_path;
}
// assuming parent_obj is list (* applicable only to lists)
move_to_index= atoi(diff_path);
move_from_index= myx_grt_value_as_int(action_obj);
action_obj= myx_grt_list_item_get(parent_obj, move_from_index);
action_obj= myx_grt_value_retain(action_obj);
myx_grt_list_item_del(parent_obj, move_from_index);
if(myx_grt_list_item_count(parent_obj) < move_to_index)
{
myx_grt_list_item_insert(parent_obj, myx_grt_list_item_count(parent_obj), action_obj);
}
else
{
myx_grt_list_item_insert(parent_obj, move_to_index, action_obj);
}
myx_grt_value_release(action_obj);
break;
}
g_free(parent_path);
}
return value;
}
|