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
|
/*
* Copyright (C) 2007-2022 S[&]T, The Netherlands.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "coda-internal.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "coda-type.h"
#include "coda-expr.h"
#include "coda-ascbin.h"
#include "coda-ascii.h"
#include "coda-bin.h"
#include "coda-xml.h"
#include "coda-netcdf.h"
#include "coda-grib.h"
#ifdef HAVE_HDF4
#include "coda-hdf4.h"
#endif
#ifdef HAVE_HDF5
#include "coda-hdf5.h"
#endif
/** \defgroup coda_types CODA Types
* Each data element or group of data elements (such as an array or record) in a product file has a unique description,
* in CODA. This description is independent of the file format of the product (e.g. ascii, binary, XML, netCDF, etc.)
* Each of those descriptions is referred to as a CODA type (which is of type #coda_type).
* For self describing formats such as netCDF, HDF4, and HDF5 files the type definition is taken from the products
* themselves. For other formats, such as ascii and binary products the type definition is fixed and is provided by
* .codadef files.
* For some file formats CODA can use a predefined format stored in a .codadef file to further restricit the format
* of a self describing file. For XML files, for instance, CODA will treat all 'leaf elements' as ascii text if no
* definition for the product is available in a .codadef. However, with a definition, CODA will know how to interpret
* the 'leaf elements' (i.e. whether the content of an XML element should be a string, an integer, a time value, etc.).
*
* As an example, there is a type that describes the MPH of an ENVISAT product (which is a record).
* This record contains a name, a textual description, the number of fields, and for each of the fields the field name
* and (again) a CODA type describing that field.
*
* CODA types are grouped into several classes (#coda_type_class). The available classes are:
* - record (#coda_record_class)
* - array (#coda_array_class)
* - integer (#coda_integer_class)
* - real (#coda_real_class)
* - text (#coda_text_class)
* - raw (#coda_raw_class)
* - special (#coda_special_class)
*
* The record and array types are the compound types that structurally define the product. It is possible to have
* records which fields are again records or arrays and arrays may have again arrays or records as elements.
* At the deepest level of a product tree (i.e. the 'leaf elements') you will allways find a basic type. These basic
* types are represented by the classes integer, real, text, and raw for respectively integer numbers, floating point
* numbers, text strings, and series of uninterpreted bytes.
*
* For each of the basic type classes you can use the coda_type_get_read_type() function to determine the best native
* type (#coda_native_type) in which to store the data as it is read from file into memory. The native types contain
* signed and unsigned integers ranging from 8 to 64 bits, the float and double types, char and string for textual
* information, and a special bytes type for raw data.
*
* Finally, CODA also supports several special data types (#coda_special_class, #coda_special_type). These are types
* that provide a mapping from the data in a product to a more convenient type for you as user. For example, there
* is a special time type that converts the many time formats that are used in products to a double value representing
* the amount of seconds since 2000-01-01T00:00:00.000000, which is the default time format in CODA.
* When you encounter a special type you can always use the coda_type_get_special_base_type() function to bypass the
* special interpretation of the data and look at the data in its actual form (e.g. for an ASCII time string you will
* get a #coda_text_class type).
*
* CODA is able to deal with many dynamic properties that can be encountered in product files.
* Some of these dynamic properties are: the size of arrays, the availabillity of optional record fields, the bit/byte
* offset of record fields, and the size of string data or raw data.
* For data types where these properties are dynamic, you will only be able to retrieve the actual
* size/availabillity/etc. by moving a cursor to the data element and use the CODA Cursor functions to retrieve the
* requested property (e.g. if the size of an array is not fixed, coda_type_get_array_dim() will return a
* dimension value of -1 and coda_cursor_get_array_dim() will return the real dimension value).
*
* More information about the CODA types and descriptions of the mappings of self describing formats to CODA types
* can be found in other parts of the CODA documentation that is included with the CODA package.
*/
/** \typedef coda_type
* CODA Type handle
* \ingroup coda_types
*/
/** \typedef coda_native_type
* Machine specific data types
* \ingroup coda_types
*/
/** \typedef coda_type_class
* CODA Type classes
* \ingroup coda_types
*/
/** \typedef coda_special_type
* The special data types
* \ingroup coda_types
*/
static THREAD_LOCAL coda_type_record *empty_record_singleton[] =
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#define num_empty_record_singletons ((int)(sizeof(empty_record_singleton)/sizeof(empty_record_singleton[0])))
static THREAD_LOCAL coda_type_raw *raw_file_singleton = NULL;
static THREAD_LOCAL coda_type_special *no_data_singleton[] =
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#define num_no_data_singletons ((int)(sizeof(no_data_singleton)/sizeof(no_data_singleton[0])))
coda_conversion *coda_conversion_new(double numerator, double denominator, double add_offset, double invalid_value)
{
coda_conversion *conversion;
if (denominator == 0.0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "denominator may not be 0 for conversion");
return NULL;
}
conversion = (coda_conversion *)malloc(sizeof(coda_conversion));
if (conversion == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_conversion), __FILE__, __LINE__);
return NULL;
}
conversion->numerator = numerator;
conversion->denominator = denominator;
conversion->add_offset = add_offset;
conversion->invalid_value = invalid_value;
conversion->unit = NULL;
return conversion;
}
int coda_conversion_set_unit(coda_conversion *conversion, const char *unit)
{
char *new_unit = NULL;
if (conversion->unit != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "conversion already has a unit");
return -1;
}
if (unit != NULL)
{
new_unit = strdup(unit);
if (new_unit == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
return -1;
}
}
conversion->unit = new_unit;
return 0;
}
void coda_conversion_delete(coda_conversion *conversion)
{
if (conversion == NULL)
{
return;
}
if (conversion->unit != NULL)
{
free(conversion->unit);
}
free(conversion);
}
static void mapping_delete(coda_ascii_mapping *mapping)
{
if (mapping == NULL)
{
return;
}
if (mapping->str != NULL)
{
free(mapping->str);
}
free(mapping);
}
static void mappings_delete(coda_ascii_mappings *mappings)
{
if (mappings == NULL)
{
return;
}
if (mappings->mapping != NULL)
{
int i;
for (i = 0; i < mappings->num_mappings; i++)
{
if (mappings->mapping[i] != NULL)
{
mapping_delete(mappings->mapping[i]);
}
}
free(mappings->mapping);
}
free(mappings);
}
coda_ascii_integer_mapping *coda_ascii_integer_mapping_new(const char *str, int64_t value)
{
coda_ascii_integer_mapping *mapping;
if (str == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "empty string value (%s:%u)", __FILE__, __LINE__);
return NULL;
}
if (strlen(str) > MAX_ASCII_NUMBER_LENGTH)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "string too large (%ld) for ascii integer mapping",
(long)strlen(str));
return NULL;
}
mapping = (coda_ascii_integer_mapping *)malloc(sizeof(coda_ascii_integer_mapping));
if (mapping == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_ascii_integer_mapping), __FILE__, __LINE__);
return NULL;
}
mapping->length = 0;
mapping->str = NULL;
mapping->value = value;
mapping->str = strdup(str);
if (mapping->str == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
free(mapping);
return NULL;
}
mapping->length = (int)strlen(str);
return mapping;
}
void coda_ascii_integer_mapping_delete(coda_ascii_integer_mapping *mapping)
{
mapping_delete((coda_ascii_mapping *)mapping);
}
coda_ascii_float_mapping *coda_ascii_float_mapping_new(const char *str, double value)
{
coda_ascii_float_mapping *mapping;
if (str == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "empty string value (%s:%u)", __FILE__, __LINE__);
return NULL;
}
if (strlen(str) > MAX_ASCII_NUMBER_LENGTH)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "string too large (%ld) for ascii float mapping", (long)strlen(str));
return NULL;
}
mapping = (coda_ascii_float_mapping *)malloc(sizeof(coda_ascii_float_mapping));
if (mapping == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_ascii_float_mapping), __FILE__, __LINE__);
return NULL;
}
mapping->length = 0;
mapping->str = NULL;
mapping->value = value;
mapping->str = strdup(str);
if (mapping->str == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
free(mapping);
return NULL;
}
mapping->length = (int)strlen(str);
return mapping;
}
void coda_ascii_float_mapping_delete(coda_ascii_float_mapping *mapping)
{
mapping_delete((coda_ascii_mapping *)mapping);
}
static int mapping_type_add_mapping(coda_type *type, coda_ascii_mappings **mappings, coda_ascii_mapping *mapping)
{
if (mapping == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "empty mapping (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (mappings == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "empty mappings (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (*mappings == NULL)
{
*mappings = malloc(sizeof(coda_ascii_mappings));
if (*mappings == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
sizeof(coda_ascii_mappings), __FILE__, __LINE__);
return -1;
}
(*mappings)->default_bit_size = (type->bit_size >= 0 ? type->bit_size : -1);
(*mappings)->num_mappings = 0;
(*mappings)->mapping = NULL;
}
if ((*mappings)->num_mappings % BLOCK_SIZE == 0)
{
coda_ascii_mapping **new_mapping;
new_mapping = realloc((*mappings)->mapping,
((*mappings)->num_mappings + BLOCK_SIZE) * sizeof(coda_ascii_mapping *));
if (new_mapping == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
((*mappings)->num_mappings + BLOCK_SIZE) * sizeof(coda_ascii_mapping *), __FILE__, __LINE__);
return -1;
}
(*mappings)->mapping = new_mapping;
}
(*mappings)->mapping[(*mappings)->num_mappings] = mapping;
(*mappings)->num_mappings++;
if (type->bit_size >= 0 && (*mappings)->default_bit_size >= 0 &&
mapping->length != ((*mappings)->default_bit_size >> 3))
{
type->bit_size = -1;
}
return 0;
}
static int mapping_type_set_bit_size(coda_type *type, coda_ascii_mappings *mappings, int64_t bit_size)
{
int i;
assert(mappings != NULL);
assert(bit_size >= 0);
if (mappings->default_bit_size >= 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "ascii type already has a size");
return -1;
}
mappings->default_bit_size = bit_size;
type->bit_size = bit_size;
for (i = 0; i < mappings->num_mappings; i++)
{
if (mappings->mapping[i]->length != (bit_size >> 3))
{
type->bit_size = -1;
break;
}
}
return 0;
}
void coda_type_record_field_delete(coda_type_record_field *field)
{
if (field == NULL)
{
return;
}
if (field->name != NULL)
{
free(field->name);
}
if (field->real_name != NULL)
{
free(field->real_name);
}
if (field->type != NULL)
{
coda_type_release(field->type);
}
if (field->available_expr != NULL)
{
coda_expression_delete(field->available_expr);
}
if (field->bit_offset_expr != NULL)
{
coda_expression_delete(field->bit_offset_expr);
}
free(field);
}
static void record_delete(coda_type_record *type)
{
if (type == NULL)
{
return;
}
if (type->name != NULL)
{
free(type->name);
}
if (type->description != NULL)
{
free(type->description);
}
if (type->size_expr != NULL)
{
coda_expression_delete(type->size_expr);
}
if (type->attributes != NULL)
{
coda_type_release((coda_type *)type->attributes);
}
if (type->hash_data != NULL)
{
hashtable_delete(type->hash_data);
}
if (type->real_name_hash_data != NULL)
{
hashtable_delete(type->real_name_hash_data);
}
if (type->num_fields > 0)
{
int i;
for (i = 0; i < type->num_fields; i++)
{
coda_type_record_field_delete(type->field[i]);
}
free(type->field);
}
if (type->union_field_expr != NULL)
{
coda_expression_delete(type->union_field_expr);
}
free(type);
}
static void array_delete(coda_type_array *type)
{
int i;
if (type == NULL)
{
return;
}
if (type->name != NULL)
{
free(type->name);
}
if (type->description != NULL)
{
free(type->description);
}
if (type->size_expr != NULL)
{
coda_expression_delete(type->size_expr);
}
if (type->attributes != NULL)
{
coda_type_release((coda_type *)type->attributes);
}
if (type->base_type != NULL)
{
coda_type_release(type->base_type);
}
for (i = 0; i < type->num_dims; i++)
{
if (type->dim_expr[i] != NULL)
{
coda_expression_delete(type->dim_expr[i]);
}
}
free(type);
}
static void number_delete(coda_type_number *type)
{
if (type == NULL)
{
return;
}
if (type->name != NULL)
{
free(type->name);
}
if (type->description != NULL)
{
free(type->description);
}
if (type->size_expr != NULL)
{
coda_expression_delete(type->size_expr);
}
if (type->attributes != NULL)
{
coda_type_release((coda_type *)type->attributes);
}
if (type->unit != NULL)
{
free(type->unit);
}
if (type->conversion != NULL)
{
coda_conversion_delete(type->conversion);
}
if (type->mappings != NULL)
{
mappings_delete(type->mappings);
}
free(type);
}
static void text_delete(coda_type_text *type)
{
if (type == NULL)
{
return;
}
if (type->name != NULL)
{
free(type->name);
}
if (type->description != NULL)
{
free(type->description);
}
if (type->size_expr != NULL)
{
coda_expression_delete(type->size_expr);
}
if (type->attributes != NULL)
{
coda_type_release((coda_type *)type->attributes);
}
if (type->fixed_value != NULL)
{
free(type->fixed_value);
}
free(type);
}
static void raw_delete(coda_type_raw *type)
{
if (type == NULL)
{
return;
}
if (type->name != NULL)
{
free(type->name);
}
if (type->description != NULL)
{
free(type->description);
}
if (type->size_expr != NULL)
{
coda_expression_delete(type->size_expr);
}
if (type->attributes != NULL)
{
coda_type_release((coda_type *)type->attributes);
}
if (type->fixed_value != NULL)
{
free(type->fixed_value);
}
free(type);
}
static void special_delete(coda_type_special *type)
{
if (type == NULL)
{
return;
}
if (type->name != NULL)
{
free(type->name);
}
if (type->description != NULL)
{
free(type->description);
}
if (type->size_expr != NULL)
{
coda_expression_delete(type->size_expr);
}
if (type->attributes != NULL)
{
coda_type_release((coda_type *)type->attributes);
}
if (type->base_type != NULL)
{
coda_type_release(type->base_type);
}
if (type->unit != NULL)
{
free(type->unit);
}
if (type->value_expr != NULL)
{
coda_expression_delete(type->value_expr);
}
free(type);
}
void coda_type_release(coda_type *type)
{
if (type == NULL)
{
return;
}
if (type->retain_count > 0)
{
type->retain_count--;
return;
}
switch (type->type_class)
{
case coda_record_class:
record_delete((coda_type_record *)type);
break;
case coda_array_class:
array_delete((coda_type_array *)type);
break;
case coda_integer_class:
case coda_real_class:
number_delete((coda_type_number *)type);
break;
case coda_text_class:
text_delete((coda_type_text *)type);
break;
case coda_raw_class:
raw_delete((coda_type_raw *)type);
break;
case coda_special_class:
special_delete((coda_type_special *)type);
break;
}
}
int coda_type_set_read_type(coda_type *type, coda_native_type read_type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
switch (type->type_class)
{
case coda_record_class:
case coda_array_class:
case coda_raw_class:
case coda_special_class:
coda_set_error(CODA_ERROR_DATA_DEFINITION, "read type cannot be set explicitly for %s type",
coda_type_get_class_name(type->type_class));
return -1;
case coda_integer_class:
if (read_type == coda_native_type_int8 || read_type == coda_native_type_uint8 ||
read_type == coda_native_type_int16 || read_type == coda_native_type_uint16 ||
read_type == coda_native_type_int32 || read_type == coda_native_type_uint32 ||
read_type == coda_native_type_int64 || read_type == coda_native_type_uint64)
{
type->read_type = read_type;
return 0;
}
break;
case coda_real_class:
if (read_type == coda_native_type_float || read_type == coda_native_type_double)
{
type->read_type = read_type;
return 0;
}
break;
case coda_text_class:
if (read_type == coda_native_type_char || read_type == coda_native_type_string)
{
type->read_type = read_type;
return 0;
}
break;
}
coda_set_error(CODA_ERROR_DATA_DEFINITION, "invalid read type (%s) for %s type",
coda_type_get_native_type_name(read_type), coda_type_get_class_name(type->type_class));
return -1;
}
int coda_type_set_name(coda_type *type, const char *name)
{
char *new_name = NULL;
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "name argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->name != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a name");
return -1;
}
if (!coda_is_identifier(name))
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "name '%s' is not a valid identifier", name);
return -1;
}
new_name = strdup(name);
if (new_name == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
return -1;
}
type->name = new_name;
return 0;
}
int coda_type_set_description(coda_type *type, const char *description)
{
char *new_description = NULL;
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (description == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "description argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->description != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a description");
return -1;
}
if (description != NULL)
{
new_description = strdup(description);
if (new_description == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
return -1;
}
}
type->description = new_description;
return 0;
}
int coda_type_set_bit_size(coda_type *type, int64_t bit_size)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->bit_size >= 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a bit size");
return -1;
}
if (type->size_expr != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a bit size expression");
return -1;
}
if (bit_size < 0)
{
char s[21];
coda_str64(bit_size, s);
coda_set_error(CODA_ERROR_DATA_DEFINITION, "bit size (%s) must be >= 0", s);
return -1;
}
if (type->format == coda_format_ascii)
{
if ((bit_size & 0x7) != 0)
{
char s[21];
coda_str64(bit_size, s);
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"bit size (%s) should be a rounded number of bytes for ascii type", s);
return -1;
}
}
switch (type->type_class)
{
case coda_integer_class:
case coda_real_class:
if (((coda_type_number *)type)->mappings != NULL)
{
return mapping_type_set_bit_size(type, ((coda_type_number *)type)->mappings, bit_size);
}
break;
case coda_special_class:
case coda_record_class:
case coda_array_class:
case coda_text_class:
case coda_raw_class:
break;
}
type->bit_size = bit_size;
return 0;
}
int coda_type_set_byte_size(coda_type *type, int64_t byte_size)
{
return coda_type_set_bit_size(type, 8 * byte_size);
}
int coda_type_set_bit_size_expression(coda_type *type, coda_expression *bit_size_expr)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (bit_size_expr == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "bit_size_expr argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->size_expr != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a bit size expression");
return -1;
}
if (type->type_class == coda_record_class || type->type_class == coda_array_class)
{
/* for compound types we also allow setting a bit size expression if the bit_size is currently 0 */
if (type->bit_size > 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a bit size");
return -1;
}
}
else
{
if (type->bit_size >= 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a bit size");
return -1;
}
}
type->size_expr = bit_size_expr;
type->bit_size = -1;
return 0;
}
int coda_type_set_byte_size_expression(coda_type *type, coda_expression *byte_size_expr)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (byte_size_expr == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "byte_size_expr argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->size_expr != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a byte size expression");
return -1;
}
if (type->type_class == coda_record_class || type->type_class == coda_array_class)
{
/* for compound types we also allow setting a byte size expression if the bit_size is currently 0 */
if (type->bit_size > 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a byte size");
return -1;
}
}
else
{
if (type->bit_size >= 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a byte size");
return -1;
}
}
type->size_expr = byte_size_expr;
type->bit_size = -8;
return 0;
}
int coda_type_add_attribute(coda_type *type, coda_type_record_field *attribute)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (attribute == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "attribute argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->attributes == NULL)
{
type->attributes = coda_type_record_new(type->format);
if (type->attributes == NULL)
{
return -1;
}
}
return coda_type_record_add_field(type->attributes, attribute);
}
int coda_type_set_attributes(coda_type *type, coda_type_record *attributes)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (attributes == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "attributes argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->attributes != NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "attributes are already set (%s:%u)", __FILE__, __LINE__);
return -1;
}
type->attributes = attributes;
attributes->retain_count++;
return 0;
}
coda_type_record_field *coda_type_record_field_new(const char *name)
{
coda_type_record_field *field;
if (name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "name argument is NULL (%s:%u)", __FILE__, __LINE__);
return NULL;
}
if (!coda_is_identifier(name))
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "field name '%s' is not a valid identifier for field definition",
name);
return NULL;
}
field = (coda_type_record_field *)malloc(sizeof(coda_type_record_field));
if (field == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_record_field), __FILE__, __LINE__);
return NULL;
}
field->name = NULL;
field->real_name = NULL;
field->type = NULL;
field->hidden = 0;
field->optional = 0;
field->available_expr = NULL;
field->bit_offset = -1;
field->bit_offset_expr = NULL;
field->name = strdup(name);
if (field->name == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
coda_type_record_field_delete(field);
return NULL;
}
return field;
}
int coda_type_record_field_set_real_name(coda_type_record_field *field, const char *real_name)
{
if (field == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (real_name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "real_name argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (field->real_name != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "field already has a real name");
return -1;
}
field->real_name = strdup(real_name);
if (field->real_name == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
return -1;
}
return 0;
}
int coda_type_record_field_set_type(coda_type_record_field *field, coda_type *type)
{
if (field == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (field->type != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "field already has a type");
return -1;
}
field->type = type;
type->retain_count++;
return 0;
}
int coda_type_record_field_set_hidden(coda_type_record_field *field)
{
if (field == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
field->hidden = 1;
return 0;
}
int coda_type_record_field_set_optional(coda_type_record_field *field)
{
if (field == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
field->optional = 1;
return 0;
}
int coda_type_record_field_set_available_expression(coda_type_record_field *field, coda_expression *available_expr)
{
if (field == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (available_expr == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "available_expr argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (field->available_expr != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "field already has an available expression");
return -1;
}
field->available_expr = available_expr;
field->optional = 1;
return 0;
}
int coda_type_record_field_set_bit_offset_expression(coda_type_record_field *field, coda_expression *bit_offset_expr)
{
if (field == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (bit_offset_expr == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "bit_offset_expr argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (field->bit_offset_expr != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "field already has a bit offset expression");
return -1;
}
if (field->type->format != coda_format_ascii && field->type->format != coda_format_binary)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "bit offset expression not allowed for record field with %s format",
coda_type_get_format_name(field->type->format));
return -1;
}
field->bit_offset_expr = bit_offset_expr;
return 0;
}
int coda_type_record_field_validate(const coda_type_record_field *field)
{
if (field == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (field->type == NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "missing type for field definition");
return -1;
}
return 0;
}
int coda_type_record_field_get_type(const coda_type_record_field *field, coda_type **type)
{
*type = field->type;
return 0;
}
coda_type_record *coda_type_record_new(coda_format format)
{
coda_type_record *type;
type = (coda_type_record *)malloc(sizeof(coda_type_record));
if (type == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_record), __FILE__, __LINE__);
return NULL;
}
type->format = format;
type->retain_count = 0;
type->type_class = coda_record_class;
type->read_type = coda_native_type_not_available;
type->name = NULL;
type->description = NULL;
type->bit_size = -1;
type->size_expr = NULL;
type->attributes = NULL;
type->hash_data = NULL;
type->real_name_hash_data = NULL;
type->num_fields = 0;
type->field = NULL;
type->has_hidden_fields = 0;
type->has_optional_fields = 0;
type->is_union = 0;
type->union_field_expr = NULL;
if (format == coda_format_ascii || format == coda_format_binary)
{
type->read_type = coda_native_type_bytes;
type->bit_size = 0;
}
type->hash_data = hashtable_new(0);
if (type->hash_data == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not create hashtable) (%s:%u)", __FILE__,
__LINE__);
record_delete(type);
return NULL;
}
type->real_name_hash_data = hashtable_new(1);
if (type->real_name_hash_data == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not create hashtable) (%s:%u)", __FILE__,
__LINE__);
record_delete(type);
return NULL;
}
return type;
}
coda_type_record *coda_type_union_new(coda_format format)
{
coda_type_record *type;
type = coda_type_record_new(format);
if (type != NULL)
{
type->is_union = 1;
}
return type;
}
coda_type_record *coda_type_empty_record(coda_format format)
{
assert(format < num_empty_record_singletons);
if (empty_record_singleton[format] == NULL)
{
empty_record_singleton[format] = coda_type_record_new(format);
assert(empty_record_singleton[format] != NULL);
}
return empty_record_singleton[format];
}
int coda_type_record_insert_field(coda_type_record *type, long index, coda_type_record_field *field)
{
const char *real_name;
long i;
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (field == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (field->type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type of field argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->is_union && !field->optional)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "fields added to a union need to be optional");
return -1;
}
if (type->format != field->type->format)
{
/* we only allow switching from binary or xml to ascii */
if (!(field->type->format == coda_format_ascii &&
(type->format == coda_format_binary || type->format == coda_format_xml)))
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "cannot add field with %s format to record with %s format",
coda_type_get_format_name(field->type->format), coda_type_get_format_name(type->format));
return -1;
}
}
if (type->num_fields % BLOCK_SIZE == 0)
{
coda_type_record_field **new_field;
new_field = realloc(type->field, (type->num_fields + BLOCK_SIZE) * sizeof(coda_type_record_field *));
if (new_field == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(type->num_fields + BLOCK_SIZE) * sizeof(coda_type_record_field *), __FILE__, __LINE__);
return -1;
}
type->field = new_field;
}
if (hashtable_insert_name(type->hash_data, index, field->name) != 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "duplicate field with name %s for record definition", field->name);
return -1;
}
real_name = (field->real_name != NULL ? field->real_name : field->name);
if (hashtable_get_index_from_name(type->real_name_hash_data, real_name) < 0)
{
/* only add the 'real_name' to the hash table if it was not there yet */
hashtable_insert_name(type->real_name_hash_data, index, real_name);
}
if (index < type->num_fields)
{
for (i = type->num_fields; i > index; i--)
{
type->field[i] = type->field[i - 1];
}
}
type->num_fields++;
type->field[index] = field;
if (type->format == coda_format_ascii || type->format == coda_format_binary)
{
if (type->is_union)
{
/* set bit_offset */
if (field->bit_offset_expr != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "bit offset expression not allowed for union field");
return -1;
}
field->bit_offset = 0;
/* update bit_size */
if (type->num_fields == 1)
{
type->bit_size = field->type->bit_size;
}
else if (type->bit_size != field->type->bit_size)
{
type->bit_size = -1;
}
}
else
{
/* set bit_offset */
if (field->bit_offset_expr == NULL)
{
if (index == 0)
{
field->bit_offset = 0;
}
else if (type->field[index - 1]->bit_offset >= 0 && type->field[index - 1]->type->bit_size >= 0 &&
!type->field[index - 1]->optional)
{
field->bit_offset = type->field[index - 1]->bit_offset + type->field[index - 1]->type->bit_size;
}
}
for (i = index + 1; i < type->num_fields; i++)
{
if (type->field[i]->bit_offset_expr == NULL)
{
if (type->field[i - 1]->bit_offset >= 0 && type->field[i - 1]->type->bit_size >= 0 &&
!type->field[i - 1]->optional)
{
type->field[i]->bit_offset =
type->field[i - 1]->bit_offset + type->field[i - 1]->type->bit_size;
}
}
}
/* update bit_size */
if (type->bit_size >= 0)
{
if (field->type->bit_size >= 0 && !type->field[type->num_fields - 1]->optional)
{
type->bit_size += field->type->bit_size;
}
else
{
type->bit_size = -1;
}
}
}
}
return 0;
}
int coda_type_record_add_field(coda_type_record *type, coda_type_record_field *field)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
return coda_type_record_insert_field(type, type->num_fields, field);
}
int coda_type_record_create_field(coda_type_record *type, const char *real_name, coda_type *field_type)
{
coda_type_record_field *field;
char *field_name;
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (real_name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "real_name argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (field_type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field_type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
field_name = coda_type_record_get_unique_field_name(type, real_name);
if (field_name == NULL)
{
return -1;
}
field = coda_type_record_field_new(field_name);
if (field == NULL)
{
free(field_name);
return -1;
}
if (strcmp(field_name, real_name) != 0)
{
if (coda_type_record_field_set_real_name(field, real_name) != 0)
{
coda_type_record_field_delete(field);
free(field_name);
return -1;
}
}
free(field_name);
if (coda_type_record_field_set_type(field, field_type) != 0)
{
coda_type_record_field_delete(field);
return -1;
}
if (coda_type_record_add_field(type, field) != 0)
{
coda_type_record_field_delete(field);
return -1;
}
return 0;
}
int coda_type_union_set_field_expression(coda_type_record *type, coda_expression *field_expr)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (field_expr == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field_expr argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (!type->is_union)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "record type is not a union");
return -1;
}
if (type->union_field_expr != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "record type already has a union field expression");
return -1;
}
type->union_field_expr = field_expr;
if (type->num_fields > 0)
{
long i;
for (i = 0; i < type->num_fields; i++)
{
/* set bit_offset */
if (type->field[i]->bit_offset_expr != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "bit offset expression not allowed for union field '%s'",
type->field[i]->name);
return -1;
}
type->field[i]->bit_offset = 0;
/* update bit_size */
if (i == 0)
{
type->bit_size = type->field[i]->type->bit_size;
}
else if (type->bit_size != type->field[i]->type->bit_size)
{
type->bit_size = -1;
}
}
}
return 0;
}
int coda_type_record_validate(const coda_type_record *type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->is_union)
{
if (type->num_fields == 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "number of fields should be >= 1 for union type");
return -1;
}
if (type->format == coda_format_ascii || type->format == coda_format_binary)
{
if (type->union_field_expr == NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "missing union field expression");
return -1;
}
}
}
return 0;
}
char *coda_type_record_get_unique_field_name(const coda_type_record *type, const char *name)
{
if (type->format == coda_format_xml)
{
name = coda_element_name_from_xml_name(name);
}
return coda_identifier_from_name(name, type->hash_data);
}
coda_type_array *coda_type_array_new(coda_format format)
{
coda_type_array *type;
type = (coda_type_array *)malloc(sizeof(coda_type_array));
if (type == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_array), __FILE__, __LINE__);
return NULL;
}
type->format = format;
type->retain_count = 0;
type->type_class = coda_array_class;
if (format == coda_format_ascii || format == coda_format_binary)
{
type->read_type = coda_native_type_bytes;
}
else
{
type->read_type = coda_native_type_not_available;
}
type->name = NULL;
type->description = NULL;
type->bit_size = -1;
type->size_expr = NULL;
type->attributes = NULL;
type->base_type = NULL;
type->num_elements = 1;
type->num_dims = 0;
return type;
}
int coda_type_array_set_base_type(coda_type_array *type, coda_type *base_type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->base_type != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "array already has a base type");
return -1;
}
if (type->format != base_type->format)
{
/* we only allow switching from binary or xml to ascii */
if (!(base_type->format == coda_format_ascii &&
(type->format == coda_format_binary || type->format == coda_format_xml)))
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "cannot add element with %s format to array with %s format",
coda_type_get_format_name(base_type->format), coda_type_get_format_name(type->format));
return -1;
}
}
if (type->format == coda_format_xml)
{
/* we don't allow arrays of arrays */
if (base_type->format == coda_format_xml && base_type->type_class == coda_array_class)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "Arrays of arrays are not allowed for xml format");
return -1;
}
}
type->base_type = base_type;
base_type->retain_count++;
if (type->format == coda_format_ascii || type->format == coda_format_binary)
{
/* update bit_size */
if (type->num_elements >= 0 && base_type->bit_size >= 0)
{
type->bit_size = type->num_elements * base_type->bit_size;
}
}
return 0;
}
int coda_type_array_add_fixed_dimension(coda_type_array *type, long dim)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (dim < 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "invalid dimension size (%ld) for array type", dim);
return -1;
}
if (type->num_dims == CODA_MAX_NUM_DIMS)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "maximum number of dimensions (%d) exceeded for array type",
CODA_MAX_NUM_DIMS);
return -1;
}
type->dim[type->num_dims] = dim;
type->dim_expr[type->num_dims] = NULL;
type->num_dims++;
/* update num_elements */
if (type->num_elements != -1)
{
if (type->num_dims == 1)
{
type->num_elements = dim;
}
else
{
type->num_elements *= dim;
}
if (type->format == coda_format_ascii || type->format == coda_format_binary)
{
/* update bit_size */
if (type->base_type != NULL && type->base_type->bit_size >= 0)
{
type->bit_size = type->num_elements * type->base_type->bit_size;
}
}
}
return 0;
}
int coda_type_array_add_variable_dimension(coda_type_array *type, coda_expression *dim_expr)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->num_dims == CODA_MAX_NUM_DIMS)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "maximum number of dimensions (%d) exceeded for array definition",
CODA_MAX_NUM_DIMS);
return -1;
}
if (type->format == coda_format_ascii || type->format == coda_format_binary)
{
if (dim_expr == NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "dimension without size specification not allowed for %s array",
coda_type_get_format_name(type->format));
return -1;
}
}
type->dim[type->num_dims] = -1;
type->dim_expr[type->num_dims] = dim_expr;
type->num_dims++;
/* update num_elements */
type->num_elements = -1;
if ((type->format == coda_format_ascii || type->format == coda_format_binary) && type->bit_size >= 0)
{
/* update bit_size */
type->bit_size = -1;
}
return 0;
}
int coda_type_array_validate(const coda_type_array *type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->num_dims == 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "number of dimensions is 0 for array definition");
return -1;
}
return 0;
}
coda_type_number *coda_type_number_new(coda_format format, coda_type_class type_class)
{
coda_type_number *type;
if (type_class != coda_integer_class && type_class != coda_real_class)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "invalid type class (%s) for number type",
coda_type_get_class_name(type_class));
return NULL;
}
type = (coda_type_number *)malloc(sizeof(coda_type_number));
if (type == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_number), __FILE__, __LINE__);
return NULL;
}
type->format = format;
type->retain_count = 0;
type->type_class = type_class;
type->read_type = (type_class == coda_integer_class ? coda_native_type_int64 : coda_native_type_double);
type->name = NULL;
type->description = NULL;
type->bit_size = -1;
type->size_expr = NULL;
type->attributes = NULL;
type->unit = NULL;
type->endianness = coda_big_endian;
type->conversion = NULL;
type->mappings = NULL;
return type;
}
int coda_type_number_set_unit(coda_type_number *type, const char *unit)
{
if (unit == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "unit argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->unit != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a unit");
return -1;
}
type->unit = strdup(unit);
if (type->unit == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
return -1;
}
return 0;
}
int coda_type_number_set_endianness(coda_type_number *type, coda_endianness endianness)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
type->endianness = endianness;
return 0;
}
int coda_type_number_set_conversion(coda_type_number *type, coda_conversion *conversion)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->conversion != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a conversion");
return -1;
}
type->conversion = conversion;
return 0;
}
int coda_type_number_add_ascii_float_mapping(coda_type_number *type, coda_ascii_float_mapping *mapping)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (mapping == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "mapping argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_real_class)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "cannot add floating point ascii mapping to integer type");
return -1;
}
return mapping_type_add_mapping((coda_type *)type, &type->mappings, (coda_ascii_mapping *)mapping);
}
int coda_type_number_add_ascii_integer_mapping(coda_type_number *type, coda_ascii_integer_mapping *mapping)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (mapping == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "mapping argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_integer_class)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "cannot add integer ascii mapping to floating point type");
return -1;
}
return mapping_type_add_mapping((coda_type *)type, &type->mappings, (coda_ascii_mapping *)mapping);
}
int coda_type_number_validate(const coda_type_number *type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->format == coda_format_binary)
{
if (type->bit_size >= 0)
{
switch (type->read_type)
{
case coda_native_type_int8:
case coda_native_type_uint8:
if (type->bit_size > 8)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "incorrect bit size (%ld) for integer type - "
"it should be <= 8 when the read type is %s", (long)type->bit_size,
coda_type_get_native_type_name(type->read_type));
return -1;
}
break;
case coda_native_type_int16:
case coda_native_type_uint16:
if (type->bit_size > 16)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "incorrect bit size (%ld) for integer type - "
"it should be <= 16 when the read type is %s", (long)type->bit_size,
coda_type_get_native_type_name(type->read_type));
return -1;
}
break;
case coda_native_type_int32:
case coda_native_type_uint32:
if (type->bit_size > 32)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "incorrect bit size (%ld) for integer type - "
"it should be <= 32 when the read type is %s", (long)type->bit_size,
coda_type_get_native_type_name(type->read_type));
return -1;
}
break;
case coda_native_type_int64:
case coda_native_type_uint64:
if (type->bit_size > 64)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "incorrect bit size (%ld) for integer type - "
"it should be <= 64 when the read type is %s", (long)type->bit_size,
coda_type_get_native_type_name(type->read_type));
return -1;
}
break;
case coda_native_type_float:
if (type->bit_size != 32)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "incorrect bit size (%ld) for floating point type - "
"it should be 32 when the read type is %s", (long)type->bit_size,
coda_type_get_native_type_name(type->read_type));
return -1;
}
break;
case coda_native_type_double:
if (type->bit_size != 64)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "incorrect bit size (%ld) for floating point type - "
"it should be 64 when the read type is %s", (long)type->bit_size,
coda_type_get_native_type_name(type->read_type));
return -1;
}
break;
default:
assert(0);
break;
}
}
else if (type->size_expr == NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"missing bit size or bit size expression for binary integer type");
return -1;
}
if (type->endianness == coda_little_endian && type->bit_size >= 0 && type->bit_size % 8 != 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"bit size (%ld) must be a multiple of 8 for little endian binary integer type",
(long)type->bit_size);
return -1;
}
}
return 0;
}
coda_type_text *coda_type_text_new(coda_format format)
{
coda_type_text *type;
type = (coda_type_text *)malloc(sizeof(coda_type_text));
if (type == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_text), __FILE__, __LINE__);
return NULL;
}
type->format = format;
type->retain_count = 0;
type->type_class = coda_text_class;
type->read_type = coda_native_type_string;
type->name = NULL;
type->description = NULL;
type->bit_size = -1;
type->size_expr = NULL;
type->attributes = NULL;
type->fixed_value = NULL;
type->special_text_type = ascii_text_default;
return type;
}
int coda_type_text_set_fixed_value(coda_type_text *type, const char *fixed_value)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (fixed_value == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "fixed_value argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->fixed_value != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "text type already has a fixed value");
return -1;
}
type->fixed_value = strdup(fixed_value);
if (type->fixed_value == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
return -1;
}
return 0;
}
int coda_type_text_set_special_text_type(coda_type_text *type, coda_ascii_special_text_type special_text_type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->format != coda_format_ascii)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "special ascii text type not allowed for %s format",
coda_type_get_format_name(type->format));
return -1;
}
if (type->special_text_type != ascii_text_default)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "text type already has a special text type set");
return -1;
}
type->special_text_type = special_text_type;
return 0;
}
int coda_type_text_validate(const coda_type_text *type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->format == coda_format_ascii || type->format == coda_format_binary)
{
if (type->size_expr == NULL && type->bit_size < 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "missing bit size or bit size expression for text type");
return -1;
}
if (type->bit_size >= 0 && type->bit_size % 8 != 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "bit size (%ld) must be a multiple of 8 for text type",
(long)type->bit_size);
return -1;
}
}
if (type->read_type == coda_native_type_char && type->bit_size != 8)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "bit size (%ld) must be 8 for text type when read type is 'char'",
(long)type->bit_size);
return -1;
}
if (type->fixed_value != NULL)
{
if (type->bit_size < 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"bit size for text type should be fixed if a fixed value is provided");
return -1;
}
/* if there is a fixed_value its length should equal the byte size of the data element */
if ((type->bit_size >> 3) != (int64_t)strlen(type->fixed_value))
{
char s[21];
coda_str64(type->bit_size >> 3, s);
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"byte size of fixed value (%ld) should equal byte size (%s) for text type",
strlen(type->fixed_value), s);
return -1;
}
}
return 0;
}
coda_type_raw *coda_type_raw_new(coda_format format)
{
coda_type_raw *type;
type = (coda_type_raw *)malloc(sizeof(coda_type_raw));
if (type == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_raw), __FILE__, __LINE__);
return NULL;
}
type->format = format;
type->retain_count = 0;
type->type_class = coda_raw_class;
type->read_type = coda_native_type_bytes;
type->name = NULL;
type->description = NULL;
type->bit_size = -1;
type->size_expr = NULL;
type->attributes = NULL;
type->fixed_value_length = -1;
type->fixed_value = NULL;
return type;
}
int coda_type_raw_set_fixed_value(coda_type_raw *type, long length, const char *fixed_value)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (length > 0 && fixed_value == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "fixed_value argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->fixed_value != NULL || type->fixed_value_length >= 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "text type already has a fixed value");
return -1;
}
if (length > 0)
{
type->fixed_value = malloc(length);
if (type->fixed_value == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
return -1;
}
memcpy(type->fixed_value, fixed_value, length);
type->fixed_value_length = length;
}
else
{
type->fixed_value_length = 0;
}
return 0;
}
int coda_type_raw_validate(const coda_type_raw *type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->format == coda_format_ascii || type->format == coda_format_binary)
{
if (type->size_expr == NULL && type->bit_size < 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "missing bit size or bit size expression for raw type");
return -1;
}
}
if (type->fixed_value != NULL)
{
int64_t byte_size;
if (type->bit_size < 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"bit size for raw type should be fixed if a fixed value is provided");
return -1;
}
/* if there is a fixed_value its length should equal the byte size of the data element */
byte_size = (type->bit_size >> 3) + (type->bit_size & 0x7 ? 1 : 0);
if (byte_size != type->fixed_value_length)
{
char s[21];
coda_str64(byte_size, s);
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"length of fixed value (%ld) should equal rounded byte size (%s) for raw type",
type->fixed_value_length, s);
return -1;
}
}
return 0;
}
coda_type_raw *coda_type_raw_file_singleton(void)
{
if (raw_file_singleton == NULL)
{
coda_type_raw *type;
coda_expression *byte_size_expr;
type = coda_type_raw_new(coda_format_binary);
if (type == NULL)
{
return NULL;
}
if (coda_expression_from_string("filesize()", &byte_size_expr) != 0)
{
raw_delete(type);
}
if (coda_type_set_byte_size_expression((coda_type *)type, byte_size_expr) != 0)
{
coda_expression_delete(byte_size_expr);
raw_delete(type);
return NULL;
}
raw_file_singleton = type;
}
return raw_file_singleton;
}
coda_type_special *coda_type_no_data_singleton(coda_format format)
{
assert(format < num_no_data_singletons);
if (no_data_singleton[format] == NULL)
{
coda_type_special *type;
type = (coda_type_special *)malloc(sizeof(coda_type_special));
if (type == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_special), __FILE__, __LINE__);
return NULL;
}
type->format = format;
type->retain_count = 0;
type->type_class = coda_special_class;
type->read_type = coda_native_type_not_available;
type->name = NULL;
type->description = NULL;
type->bit_size = 0;
type->size_expr = NULL;
type->attributes = NULL;
type->special_type = coda_special_no_data;
type->base_type = NULL;
type->unit = NULL;
type->value_expr = NULL;
type->base_type = (coda_type *)coda_type_raw_new(format);
if (type->base_type == NULL)
{
special_delete(type);
return NULL;
}
if (coda_type_set_bit_size(type->base_type, 0) != 0)
{
special_delete(type);
return NULL;
}
no_data_singleton[format] = type;
}
return no_data_singleton[format];
}
coda_type_special *coda_type_vsf_integer_new(coda_format format)
{
coda_type_special *type;
type = (coda_type_special *)malloc(sizeof(coda_type_special));
if (type == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_special), __FILE__, __LINE__);
return NULL;
}
type->format = format;
type->retain_count = 0;
type->type_class = coda_special_class;
type->read_type = coda_native_type_double;
type->name = NULL;
type->description = NULL;
type->bit_size = -1;
type->size_expr = NULL;
type->attributes = NULL;
type->special_type = coda_special_vsf_integer;
type->base_type = NULL;
type->unit = NULL;
type->value_expr = NULL;
type->base_type = (coda_type *)coda_type_record_new(format);
coda_type_set_description(type->base_type, "Variable Scale Factor Integer");
return type;
}
int coda_type_vsf_integer_set_type(coda_type_special *type, coda_type *base_type)
{
coda_type_record_field *field;
if (type->format != base_type->format)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"cannot use element type with %s format for vsf integer with %s format",
coda_type_get_format_name(base_type->format), coda_type_get_format_name(type->format));
return -1;
}
if (((coda_type_record *)type->base_type)->num_fields != 1)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "value should be second field of a vsf integer record");
return -1;
}
field = coda_type_record_field_new("value");
if (field == NULL)
{
return -1;
}
if (coda_type_record_field_set_type(field, base_type) != 0)
{
coda_type_record_field_delete(field);
return -1;
}
if (coda_type_record_add_field((coda_type_record *)type->base_type, field) != 0)
{
coda_type_record_field_delete(field);
return -1;
}
/* update bit_size */
type->bit_size = type->base_type->bit_size;
return 0;
}
int coda_type_vsf_integer_set_scale_factor(coda_type_special *type, coda_type *scale_factor)
{
coda_type_record_field *field;
coda_native_type scalefactor_type;
if (type->format != scale_factor->format)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"cannot use scale factor type with %s format for vsf integer with %s format",
coda_type_get_format_name(scale_factor->format), coda_type_get_format_name(type->format));
return -1;
}
if (((coda_type_record *)type->base_type)->num_fields != 0)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "scale factor should be first field of a vsf integer record");
return -1;
}
if (coda_type_get_read_type(scale_factor, &scalefactor_type) != 0)
{
return -1;
}
switch (scalefactor_type)
{
case coda_native_type_int8:
case coda_native_type_uint8:
case coda_native_type_int16:
case coda_native_type_uint16:
case coda_native_type_int32:
break;
default:
/* we do not support uint32_t/int64_t/uint64_t scale factors.
* This allows us to use a more accurate pow10 function when applying the scale factor.
*/
coda_set_error(CODA_ERROR_DATA_DEFINITION, "invalid scalefactor type (%s) for vsf integer type",
coda_type_get_native_type_name(scalefactor_type));
return -1;
}
field = coda_type_record_field_new("scale_factor");
if (field == NULL)
{
return -1;
}
if (coda_type_record_field_set_type(field, scale_factor) != 0)
{
coda_type_record_field_delete(field);
return -1;
}
if (coda_type_record_add_field((coda_type_record *)type->base_type, field) != 0)
{
coda_type_record_field_delete(field);
return -1;
}
/* update bit_size */
type->bit_size = type->base_type->bit_size;
return 0;
}
int coda_type_vsf_integer_set_unit(coda_type_special *type, const char *unit)
{
if (unit == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "unit argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->unit != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "type already has a unit");
return -1;
}
type->unit = strdup(unit);
if (type->unit == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
return -1;
}
return 0;
}
int coda_type_vsf_integer_validate(coda_type_special *type)
{
if (((coda_type_record *)type->base_type)->num_fields != 2)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "vsf integer type requires both a base type and scale factor");
return -1;
}
return 0;
}
coda_type_special *coda_type_time_new(coda_format format, coda_expression *value_expr)
{
coda_type_special *type;
if (value_expr == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "value_expr argument is NULL (%s:%u)", __FILE__, __LINE__);
return NULL;
}
type = (coda_type_special *)malloc(sizeof(coda_type_special));
if (type == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_special), __FILE__, __LINE__);
return NULL;
}
type->format = format;
type->retain_count = 0;
type->type_class = coda_special_class;
type->read_type = coda_native_type_double;
type->name = NULL;
type->description = NULL;
type->bit_size = -1;
type->size_expr = NULL;
type->attributes = NULL;
type->special_type = coda_special_time;
type->base_type = NULL;
type->unit = NULL;
type->value_expr = value_expr;
type->unit = strdup("s since 2000-01-01");
if (type->unit == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate string) (%s:%u)", __FILE__,
__LINE__);
special_delete(type);
return NULL;
}
return type;
}
int coda_type_time_add_ascii_float_mapping(coda_type_special *type, coda_ascii_float_mapping *mapping)
{
char strexpr[64];
coda_expression *cond_expr;
coda_expression *value_expr;
coda_expression *node_expr;
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (mapping == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "mapping argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->special_type != coda_special_time)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "cannot add floating point ascii mapping to '%s' special type",
coda_type_get_special_type_name(type->special_type));
return -1;
}
if (type->base_type == NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "special type does not have a base type");
return -1;
}
if (type->base_type->type_class != coda_text_class)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "cannot add floating point ascii mapping to time type with '%s'"
" base class", coda_type_get_class_name(type->base_type->type_class));
return -1;
}
sprintf(strexpr, "%d", mapping->length);
value_expr = coda_expression_new(expr_constant_integer, strdup(strexpr), NULL, NULL, NULL, NULL);
if (mapping->length == 0)
{
/* wrap existing value_expr in if-construct: if(length(.)==0,<value>,<value_expr>) */
node_expr = coda_expression_new(expr_goto_here, NULL, NULL, NULL, NULL, NULL);
cond_expr = coda_expression_new(expr_length, NULL, node_expr, NULL, NULL, NULL);
}
else
{
/* wrap existing value_expr in if-construct: if(str(.,<length>)=="<str>",<value>,<value_expr>) */
node_expr = coda_expression_new(expr_goto_here, NULL, NULL, NULL, NULL, NULL);
cond_expr = coda_expression_new(expr_string, NULL, node_expr, value_expr, NULL, NULL);
value_expr = coda_expression_new(expr_constant_string, strdup(mapping->str), NULL, NULL, NULL, NULL);
}
cond_expr = coda_expression_new(expr_equal, NULL, cond_expr, value_expr, NULL, NULL);
coda_strfl(mapping->value, strexpr);
value_expr = coda_expression_new(expr_constant_float, strdup(strexpr), NULL, NULL, NULL, NULL);
type->value_expr = coda_expression_new(expr_if, NULL, cond_expr, value_expr, type->value_expr, NULL);
coda_ascii_float_mapping_delete(mapping);
return 0;
}
int coda_type_time_set_base_type(coda_type_special *type, coda_type *base_type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (base_type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "base_type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->special_type != coda_special_time)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "cannot set base type for '%s' special type",
coda_type_get_special_type_name(type->special_type));
return -1;
}
if (type->base_type != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "special type already has a base type");
return -1;
}
type->base_type = base_type;
base_type->retain_count++;
/* update bit_size */
type->bit_size = type->base_type->bit_size;
return 0;
}
int coda_type_time_validate(coda_type_special *type)
{
if (type->base_type == NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "missing base type for time type");
return -1;
}
return 0;
}
coda_type_special *coda_type_complex_new(coda_format format)
{
coda_type_special *type;
type = (coda_type_special *)malloc(sizeof(coda_type_special));
if (type == NULL)
{
coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
(long)sizeof(coda_type_special), __FILE__, __LINE__);
return NULL;
}
type->format = format;
type->retain_count = 0;
type->type_class = coda_special_class;
type->read_type = coda_native_type_not_available;
type->name = NULL;
type->description = NULL;
type->bit_size = -1;
type->size_expr = NULL;
type->attributes = NULL;
type->special_type = coda_special_complex;
type->base_type = NULL;
type->unit = NULL;
type->value_expr = NULL;
return type;
}
int coda_type_complex_set_type(coda_type_special *type, coda_type *element_type)
{
coda_type_record_field *field;
if (type->base_type != NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "complex type already has an element type");
return -1;
}
if (element_type->type_class != coda_integer_class && element_type->type_class != coda_real_class)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "invalid type class (%s) for element type of complex type",
coda_type_get_class_name(type->type_class));
return -1;
}
if (type->format != element_type->format)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION,
"cannot use element type with %s format for complex type with %s format",
coda_type_get_format_name(element_type->format), coda_type_get_format_name(type->format));
return -1;
}
type->base_type = (coda_type *)coda_type_record_new(type->format);
field = coda_type_record_field_new("real");
coda_type_record_field_set_type(field, element_type);
coda_type_record_add_field((coda_type_record *)type->base_type, field);
field = coda_type_record_field_new("imaginary");
coda_type_record_field_set_type(field, element_type);
coda_type_record_add_field((coda_type_record *)type->base_type, field);
/* set bit_size */
type->bit_size = type->base_type->bit_size;
return 0;
}
int coda_type_complex_validate(coda_type_special *type)
{
if (type->base_type == NULL)
{
coda_set_error(CODA_ERROR_DATA_DEFINITION, "missing element type for complex type");
return -1;
}
return 0;
}
void coda_type_done(void)
{
int i;
for (i = 0; i < num_empty_record_singletons; i++)
{
if (empty_record_singleton[i] != NULL)
{
coda_type_release((coda_type *)empty_record_singleton[i]);
}
empty_record_singleton[i] = NULL;
}
for (i = 0; i < num_no_data_singletons; i++)
{
if (no_data_singleton[i] != NULL)
{
coda_type_release((coda_type *)no_data_singleton[i]);
}
no_data_singleton[i] = NULL;
}
}
/** \addtogroup coda_types
* @{
*/
/** Returns the name of a storage format.
* \param format CODA storage format
* \return if the format is known a string containing the name of the format, otherwise the string "unknown".
*/
LIBCODA_API const char *coda_type_get_format_name(coda_format format)
{
switch (format)
{
case coda_format_ascii:
return "ascii";
case coda_format_binary:
return "binary";
case coda_format_xml:
return "xml";
case coda_format_netcdf:
return "netcdf";
case coda_format_grib:
return "grib";
case coda_format_cdf:
return "cdf";
case coda_format_hdf4:
return "hdf4";
case coda_format_hdf5:
return "hdf5";
case coda_format_rinex:
return "rinex";
case coda_format_sp3:
return "sp3";
}
return "unknown";
}
/** Returns the name of a type class.
* In case the type class is not recognised the string "unknown" is returned.
* \param type_class CODA type class
* \return if the type class is known a string containing the name of the class, otherwise the string "unknown".
*/
LIBCODA_API const char *coda_type_get_class_name(coda_type_class type_class)
{
switch (type_class)
{
case coda_record_class:
return "record";
case coda_array_class:
return "array";
case coda_integer_class:
return "integer";
case coda_real_class:
return "real";
case coda_text_class:
return "text";
case coda_raw_class:
return "raw";
case coda_special_class:
return "special";
}
return "unknown";
}
/** Returns the name of a native type.
* In case the native type is not recognised the string "unknown" is returned.
* \note Mind that there is also a special native type #coda_native_type_not_available which will result in the string
* 'N/A'.
* \param native_type CODA native type
* \return if the native type is known a string containing the name of the native type, otherwise the string "unknown".
*/
LIBCODA_API const char *coda_type_get_native_type_name(coda_native_type native_type)
{
switch (native_type)
{
case coda_native_type_not_available:
return "N/A";
case coda_native_type_int8:
return "int8";
case coda_native_type_uint8:
return "uint8";
case coda_native_type_int16:
return "int16";
case coda_native_type_uint16:
return "uint16";
case coda_native_type_int32:
return "int32";
case coda_native_type_uint32:
return "uint32";
case coda_native_type_int64:
return "int64";
case coda_native_type_uint64:
return "uint64";
case coda_native_type_float:
return "float";
case coda_native_type_double:
return "double";
case coda_native_type_char:
return "char";
case coda_native_type_string:
return "string";
case coda_native_type_bytes:
return "bytes";
}
return "unknown";
}
/** Returns the name of a special type.
* In case the special type is not recognised the string "unknown" is returned.
* \param special_type CODA special type
* \return if the special type is known a string containing the name of the special type, otherwise the string "unknown".
*/
LIBCODA_API const char *coda_type_get_special_type_name(coda_special_type special_type)
{
switch (special_type)
{
case coda_special_no_data:
return "no_data";
case coda_special_vsf_integer:
return "vsf_integer";
case coda_special_time:
return "time";
case coda_special_complex:
return "complex";
}
return "unknown";
}
/** Determine whether the type has any attributes.
* If the record returned by coda_type_get_attributes() has one or more fields then \a has_attributes will be set to 1,
* otherwise it will be set to 0.
* \param type CODA type.
* \param has_attributes Pointer to the variable where attribute availability status will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_has_attributes(const coda_type *type, int *has_attributes)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (has_attributes == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "has_attributes argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*has_attributes = (type->attributes != NULL);
return 0;
}
/** Get the storage format of a type.
* \param type CODA type.
* \param format Pointer to a variable where the format will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_format(const coda_type *type, coda_format *format)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (format == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "format argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*format = type->format;
return 0;
}
/** Get the class of a type.
* \param type CODA type.
* \param type_class Pointer to a variable where the type class will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_class(const coda_type *type, coda_type_class *type_class)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type_class == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type_class argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*type_class = type->type_class;
return 0;
}
/** Get the best native type for reading data of a CODA type.
* The native type that is returned indicates which storage type can best be used when reading data of this
* CODA type to memory. Compound types (arrays and records) that can be read directly (using a raw byte
* array) will return a read type #coda_native_type_bytes. If a type can not be read directly (e.g. compound types in
* XML, netCDF, HDF4, and HDF5 products) the special native type value #coda_native_type_not_available will be returned.
* \note Be aware that types of class #coda_integer_class can return a native type #coda_native_type_double if the
* integer type has a conversion associated with it and conversions are enabled.
* \see coda_set_option_perform_conversions()
* \param type CODA type.
* \param read_type Pointer to a variable where the native type for reading will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_read_type(const coda_type *type, coda_native_type *read_type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (read_type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "read_type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if ((type->type_class == coda_integer_class || type->type_class == coda_real_class) &&
coda_option_perform_conversions && ((coda_type_number *)type)->conversion != NULL)
{
*read_type = coda_native_type_double;
return 0;
}
*read_type = type->read_type;
return 0;
}
/** Get the length in bytes of a string data type.
* If the type does not refer to text data the function will return an error.
* If the size is not fixed and can only be determined from information inside a product then \a length will be set
* to -1.
* \param type CODA type.
* \param length Pointer to a variable where the string length (not including terminating 0) will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_string_length(const coda_type *type, long *length)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_text_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to text (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (length == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "length argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*length = (type->bit_size < 0 ? -1 : (long)(type->bit_size >> 3));
return 0;
}
/** Get the bit size for the data type.
* Depending on the type of data and its format this function will return the following:
* For data in ascii or binary format all data types will return the amount of bits the data occupies in the product
* file. This means that e.g. ascii floats and ascii integers will return 8 times the byte size of the ascii
* representation, records and arrays return the sum of the bit sizes of their fields/array-elements.
* For XML data you will be able to retrieve bit sizes for all data except arrays and attribute records.
* You will not be able to retrieve bit/byte sizes for data in netCDF, HDF4, or HDF5 format.
* If the size is not fixed and can only be determined from information inside a product then \a bit_size will be set
* to -1.
* \param type CODA type.
* \param bit_size Pointer to a variable where the bit size will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_bit_size(const coda_type *type, int64_t *bit_size)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (bit_size == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "bit_size argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*bit_size = (type->bit_size >= 0 ? type->bit_size : -1);
return 0;
}
/** Get the name of a type.
* A type can have an optional name that uniquely defines it within a product class. This is something that is used
* internally within CODA to allow reuse of type definitions. If a type has a name, only a single instance of
* the definition will be used for all places where the type is used (i.e. a single coda_type object will be used for
* all cases where this type is used). For this reason type names are unique within the scope of a product class.
* You should never rely in your code on types having a specific name, or having a name at all. The internal type reuse
* approach within a product class may change unannounced.
* If the type is unnamed a NULL pointer will be returned.
* The \a name parameter will either be a NULL pointer or a 0 terminated string.
* \param type CODA type.
* \param name Pointer to the variable where the name of the type will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_name(const coda_type *type, const char **name)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "name argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*name = type->name;
return 0;
}
/** Get the description of a type.
* If the type does not have a description a NULL pointer will be returned.
* The \a description parameter will either be a NULL pointer or a 0 terminated string.
* \param type CODA type.
* \param description Pointer to the variable where the description of the type will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_description(const coda_type *type, const char **description)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (description == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "description argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*description = type->description;
return 0;
}
/** Get the unit of a type.
* You will only receive unit information for ascii, binary, and xml data (for other formats a NULL pointer will be
* returned).
* The unit information is a string with the same text as can be found in the unit column of the CODA Product Format
* Definition documentation for this type.
* If you try to retrieve the unit for an array type then the unit of its base type will be returned.
* The \a unit parameter will either be a NULL pointer or a 0 terminated string.
* \param type CODA type.
* \param unit Pointer to the variable where the unit information of the type will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_unit(const coda_type *type, const char **unit)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (unit == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "unit argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
switch (type->type_class)
{
case coda_array_class:
{
coda_type *base_type;
if (coda_type_get_array_base_type(type, &base_type) != 0)
{
return -1;
}
return coda_type_get_unit(base_type, unit);
}
case coda_integer_class:
case coda_real_class:
if (coda_option_perform_conversions && ((coda_type_number *)type)->conversion != NULL)
{
*unit = ((coda_type_number *)type)->conversion->unit;
}
else
{
*unit = ((coda_type_number *)type)->unit;
}
break;
case coda_special_class:
*unit = ((coda_type_special *)type)->unit;
break;
default:
*unit = NULL;
break;
}
return 0;
}
/** Get the associated fixed value string of a type if it has one.
* Fixed values will only occur for #coda_text_class and #coda_raw_class types and only for ascii, binary, or xml
* formatted data (in all other cases a NULL pointer will be returned).
* It is possible to pass a NULL pointer for the length parameter to omit the retrieval of the length.
* If the type does not have a fixed value a NULL pointer will be returned and the \a length parameter (if it is not a
* NULL pointer) will be set to 0.
* For ascii and xml data the \a fixed_value will be a 0 terminated string. For binary data there will not be a 0
* termination character. Since fixed values for raw data can contain \\0 values you should use the returned \a length
* parameter to determine the size of the fixed value.
* The \a length parameter will contain the length of the fixed value without taking a terminating '\\0' into account.
* \param type CODA type.
* \param fixed_value Pointer to the variable where the pointer to the fixed value for the type will be stored.
* \param length Pointer to the variable where the string length of the fixed value will be stored (can be NULL).
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_fixed_value(const coda_type *type, const char **fixed_value, long *length)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (fixed_value == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "fixed_value argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
switch (type->type_class)
{
case coda_text_class:
*fixed_value = ((coda_type_text *)type)->fixed_value;
if (length != NULL)
{
*length = ((*fixed_value == NULL) ? 0 : (long)strlen(*fixed_value));
}
break;
case coda_raw_class:
*fixed_value = ((coda_type_raw *)type)->fixed_value;
if (length != NULL)
{
*length = ((*fixed_value == NULL) ? 0 : ((coda_type_raw *)type)->fixed_value_length);
}
break;
default:
*fixed_value = NULL;
break;
}
return 0;
}
/** Get the type for the associated attribute record.
* Note that this record may not have any fields if there are no attributes for this type.
* \param type CODA type.
* \param attributes Pointer to the variable where the pointer to the type defining the attribute record will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_attributes(const coda_type *type, coda_type **attributes)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (attributes == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "attributes argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->attributes == NULL)
{
*attributes = (coda_type *)coda_type_empty_record(type->format);
}
else
{
*attributes = (coda_type *)type->attributes;
}
return 0;
}
/** Get the number of fields of a record type.
* If the type is not a record class the function will return an error.
* \param type CODA type.
* \param num_fields Pointer to a variable where the number of fields will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_num_record_fields(const coda_type *type, long *num_fields)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (num_fields == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "num_fields argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*num_fields = ((coda_type_record *)type)->num_fields;
return 0;
}
/** Get the field index from a field name for a record type.
* If the type is not a record class the function will return an error.
* \param type CODA type.
* \param name Name of the record field.
* \param index Pointer to a variable where the field index will be stored (0 <= \a index < number of fields).
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_record_field_index_from_name(const coda_type *type, const char *name, long *index)
{
long field_index;
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "name argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (index == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "index argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
field_index = hashtable_get_index_from_name(((coda_type_record *)type)->hash_data, name);
if (field_index < 0)
{
coda_set_error(CODA_ERROR_INVALID_NAME, "record does not contain a field named '%s'", name);
return -1;
}
*index = field_index;
return 0;
}
/* Get the field index from a field name for a record type where the field name may not be zero terminated.
* If the type is not a record class the function will return an error.
* \param type CODA type.
* \param name Name of the record field.
* \param name_length Maximum length of the name parameter.
* \param index Pointer to a variable where the field index will be stored (0 <= \a index < number of fields).
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_record_field_index_from_name_n(const coda_type *type, const char *name, int name_length,
long *index)
{
long field_index;
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "name argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (index == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "index argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
field_index = hashtable_get_index_from_name_n(((coda_type_record *)type)->hash_data, name, name_length);
if (field_index < 0)
{
coda_set_error(CODA_ERROR_INVALID_NAME, "record does not contain a field named '%.*s'", name_length, name);
return -1;
}
*index = field_index;
return 0;
}
/** Get the field index based on the 'real name' of the field for a record type.
* If the type is not a record class the function will return an error.
* If a field has no explicit 'real name' set, a match against the regular field name will be performed.
* \param type CODA type.
* \param real_name Real name of the record field.
* \param index Pointer to a variable where the field index will be stored (0 <= \a index < number of fields).
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_record_field_index_from_real_name(const coda_type *type, const char *real_name,
long *index)
{
long field_index;
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (real_name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "real_name argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (index == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "index argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
field_index = hashtable_get_index_from_name(((coda_type_record *)type)->real_name_hash_data, real_name);
if (field_index < 0)
{
coda_set_error(CODA_ERROR_INVALID_NAME, "record does not contain a field with real name '%s'", real_name);
return -1;
}
*index = field_index;
return 0;
}
/** Get the CODA type for a record field.
* If the type is not a record class the function will return an error.
* \param type CODA type.
* \param index Field index (0 <= \a index < number of fields).
* \param field_type Pointer to the variable where the type of the record field will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_record_field_type(const coda_type *type, long index, coda_type **field_type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (field_type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "field_type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (index < 0 || index >= ((coda_type_record *)type)->num_fields)
{
coda_set_error(CODA_ERROR_INVALID_INDEX, "field index (%ld) is not in the range [0,%ld) (%s:%u)", index,
((coda_type_record *)type)->num_fields, __FILE__, __LINE__);
return -1;
}
*field_type = ((coda_type_record *)type)->field[index]->type;
return 0;
}
/** Get the name of a record field.
* If the type is not a record class the function will return an error.
* The \a name parameter will be 0 terminated.
* \param type CODA type.
* \param index Field index (0 <= \a index < number of fields).
* \param name Pointer to the variable where the name of the record field will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_record_field_name(const coda_type *type, long index, const char **name)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "name argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (index < 0 || index >= ((coda_type_record *)type)->num_fields)
{
coda_set_error(CODA_ERROR_INVALID_INDEX, "field index (%ld) is not in the range [0,%ld) (%s:%u)", index,
((coda_type_record *)type)->num_fields, __FILE__, __LINE__);
return -1;
}
*name = ((coda_type_record *)type)->field[index]->name;
return 0;
}
/** Get the unaltered name of a record field.
* The real name of a field is the name of the field without the identifier restriction.
* For (partially) self-describing formats such as XML, HDF, and netCDF, the name of a field as used by CODA will
* actually be a conversion of the name of the stored element to something that conforms to the rules of an identifier
* (i.e. only allowing a-z, A-Z, 0-9 and underscores characters and names have to start with an alpha character).
* The real name property of a field represents the original name of the element (e.g. XML element name, HDF5 DataSet
* name, netCDF variable name, etc.).
* If the concept of a real name does not apply, this function will return the same result as
* coda_type_get_record_field_name().
*
* If the type is not a record class the function will return an error.
* The \a real_name parameter will be 0 terminated.
* \param type CODA type.
* \param index Field index (0 <= \a index < number of fields).
* \param real_name Pointer to the variable where the real name of the record field will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_record_field_real_name(const coda_type *type, long index, const char **real_name)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (real_name == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "name argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (index < 0 || index >= ((coda_type_record *)type)->num_fields)
{
coda_set_error(CODA_ERROR_INVALID_INDEX, "field index (%ld) is not in the range [0,%ld) (%s:%u)", index,
((coda_type_record *)type)->num_fields, __FILE__, __LINE__);
return -1;
}
if (((coda_type_record *)type)->field[index]->real_name != NULL)
{
if (type->format == coda_format_xml)
{
*real_name = coda_element_name_from_xml_name(((coda_type_record *)type)->field[index]->real_name);
}
else
{
*real_name = ((coda_type_record *)type)->field[index]->real_name;
}
}
else
{
*real_name = ((coda_type_record *)type)->field[index]->name;
}
return 0;
}
/** Get the hidden status of a record field.
* If the type is not a record class the function will return an error.
* The hidden property is only applicable for ascii, binary, and xml data (fields can not be hidden for other formats).
* If the record field has the hidden property \a hidden will be set to 1, otherwise it will be set to 0.
* \note The C API of CODA does not hide record fields itself. This property is used by interfaces on top of the CODA C
* interface (such as the MATLAB and IDL interfaces) to eliminate hidden fields when retrieving complete records.
* \param type CODA type.
* \param index Field index (0 <= \a index < number of fields).
* \param hidden Pointer to the variable where the hidden status of the record field will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_record_field_hidden_status(const coda_type *type, long index, int *hidden)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (hidden == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "hidden argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (index < 0 || index >= ((coda_type_record *)type)->num_fields)
{
coda_set_error(CODA_ERROR_INVALID_INDEX, "field index (%ld) is not in the range [0,%ld) (%s:%u)", index,
((coda_type_record *)type)->num_fields, __FILE__, __LINE__);
return -1;
}
*hidden = ((coda_type_record *)type)->field[index]->hidden;
return 0;
}
/** Get the available status of a record field.
* If the type is not a record class the function will return an error.
* The available status is only applicable for data in ascii, binary, or XML format (fields are always available for
* netCDF, HDF4, and HDF5 data).
* The available status is a dynamic property and can thus only really be determined using the function
* coda_cursor_get_record_field_available_status(). The coda_type_get_record_field_hidden_status() function, however,
* indicates whether the availability of a field is dynamic or not. If it is not dynamic (i.e. it is always available)
* \a available will be 1, if not (i.e. it has to be determined dynamically) \a available will be -1.
* \param type CODA type.
* \param index Field index (0 <= \a index < number of fields).
* \param available Pointer to the variable where the available status of the record field will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_record_field_available_status(const coda_type *type, long index, int *available)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (available == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "available argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (index < 0 || index >= ((coda_type_record *)type)->num_fields)
{
coda_set_error(CODA_ERROR_INVALID_INDEX, "field index (%ld) is not in the range [0,%ld) (%s:%u)", index,
((coda_type_record *)type)->num_fields, __FILE__, __LINE__);
return -1;
}
*available = (((coda_type_record *)type)->field[index]->optional ? -1 : 1);
return 0;
}
/** Get the union status of a record.
* If the record is a union (i.e. all fields are dynamically available and only one field can be available at any time)
* \a is_union will be set to 1, otherwise it will be set to 0.
* If the type is not a record class the function will return an error.
* \param type CODA type.
* \param is_union Pointer to a variable where the union status will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_record_union_status(const coda_type *type, int *is_union)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_record_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a record (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (is_union == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "is_union argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*is_union = ((coda_type_record *)type)->is_union;
return 0;
}
/** Get the number of dimensions for an array.
* If the type is not an array class the function will return an error.
* \param type CODA type.
* \param num_dims Pointer to the variable where the number of dimensions will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_array_num_dims(const coda_type *type, int *num_dims)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_array_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to an array (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (num_dims == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "num_dims argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*num_dims = ((coda_type_array *)type)->num_dims;
return 0;
}
/** Retrieve the dimensions with a constant value for an array.
* The function returns both the number of dimensions \a num_dims and the size for each of the dimensions \a dim that
* have a constant/fixed size.
* \note If the size of a dimension is variable (it differs per product or differs per occurrence inside one product)
* then this function will set the value for that dimension to \c -1. Otherwise it will set the dimension
* entry in \a dim to the constant value for that dimension as defined by the CODA product format definition.
* Variable dimension sizes can only occur when a CODA product format definition is used.
* If the type is not an array class the function will return an error.
* \param type CODA type.
* \param num_dims Pointer to the variable where the number of dimensions will be stored.
* \param dim Pointer to the variable where the dimensions will be stored. Dimensions that will vary per product or
* within a product will have value \c -1. The caller needs to make sure that the variable has enough room to store the
* dimensions array. It is guaranteed that the number of dimensions will never exceed #CODA_MAX_NUM_DIMS.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_array_dim(const coda_type *type, int *num_dims, long dim[])
{
int i;
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_array_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to an array (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (num_dims == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "num_dims argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (dim == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "dim argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*num_dims = ((coda_type_array *)type)->num_dims;
for (i = 0; i < ((coda_type_array *)type)->num_dims; i++)
{
dim[i] = ((coda_type_array *)type)->dim[i];
}
return 0;
}
/** Get the CODA type for the elements of an array.
* If the type is not an array class the function will return an error.
* \param type CODA type.
* \param base_type Pointer to the variable where the base type will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_array_base_type(const coda_type *type, coda_type **base_type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_array_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to an array (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (base_type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "base_type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*base_type = ((coda_type_array *)type)->base_type;
return 0;
}
/** Get the special type for a type.
* This function will return the specific special type for types of class #coda_special_class.
* If the type is not a special type the function will return an error.
* \param type CODA type.
* \param special_type Pointer to a variable where the special type will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_special_type(const coda_type *type, coda_special_type *special_type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_special_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a special type (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (special_type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "special_type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*special_type = ((coda_type_special *)type)->special_type;
return 0;
}
/** Get the base type for a special type.
* If the type is not a special type the function will return an error.
* \param type CODA type.
* \param base_type Pointer to the variable where the base type will be stored.
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #coda_errno).
*/
LIBCODA_API int coda_type_get_special_base_type(const coda_type *type, coda_type **base_type)
{
if (type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (type->type_class != coda_special_class)
{
coda_set_error(CODA_ERROR_INVALID_TYPE, "type does not refer to a special type (current type is %s)",
coda_type_get_class_name(type->type_class));
return -1;
}
if (base_type == NULL)
{
coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "base_type argument is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
*base_type = ((coda_type_special *)type)->base_type;
return 0;
}
/** @} */
|