1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771
|
/*
* Project: The SPD Image correction and azimuthal regrouping
* http://forge.epn-campus.eu/projects/show/azimuthal
*
* Copyright (C) 2005-2010 European Synchrotron Radiation Facility
* Grenoble, France
*
* Principal authors: P. Boesecke (boesecke@esrf.fr)
* R. Wilcke (wilcke@esrf.fr)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
# define NUMIO_VERSION "numio : V1.34 Peter Boesecke 2011-06-16"
/*+++------------------------------------------------------------------------
NAME
numio.c --- number expressions
SYNOPSIS
# include numio.h
INCLUDE FILES
numio.h
gamma.h
TO LINK WITH
gamma.c
PURPOSE
Reading of double and long integer expressions from strings.
See PUBLIC functions for detail.
CALL
long num_str2long(const char *str, const char **tail, int *perrval);
double num_str2double(const char *str, const char **tail, int *perrval);
AUTHOR
1995 Peter Boesecke (PB)
HISTORY
11-Oct-1996 PB extracted from input.c
13-Oct-1996 PB dpconstant : physical constants and units,
units are preceeded by an underscore '_'
02-Aug-2000 PB dpterm : case '%' added
function doubleexpr added
28-Nov-2000 PB dpfunction : GAMMA_ added
dpconstant : km3, ..., m3 added
04-Dec-2000 PB ->numio.c, .h
04-Feb-2003 PB Inf
03-Aug-2003 PB longexpr, floatexpr, doubleexpr: char * -> const char *
19-Feb-2004 PB dpfunction : min, max
02-Mar-2004 PB If a factor is followed by the underscore operator '_'
it is immediately multiplied with the factor following the
underscore. Parentheses around both factors are not necessary
in this case. This simplifies the use of units, e.g.
"1/1_nm" is identical to "1/(1*nm)".
It was necessar to change the following functions:
dpconstant: unit identifier '_' removed,
no distinction between units and names any more
names and units ordered by length
dpfactor: new multiplicator '_'
Because a unit does not start any more with an underscore
all macros using units must be adapted.
16-Mar-2004 PB SaxsExpression -> numio V1.00
24-Mar-2004 PB STRNCASECMP -> num_strncasecmp
30-Mar-2004 PB error corrected:
dpexpression and lvexpression stop at white space or comma
02-Apr-2004 PB parameter tail added to argument list of num_str2...
num_str2... stops at a white space, a comma or a semicolon.
If the evaluated expression is not complete or faulty, an
error is returned.
03-Apr-2004 PB new units kg, J, W, pixel, photon
13-Jun-2004 PB double constant list (pixel and photon no longer defined)
19-Jun-2004 PB list rearranged, numio_debug created,
name of electron charge changed from e to ec
new units and constants: erg, dyn, cal, Pa, bar,
N, V, A, C, lb, in, ft, lbf, psi, gN, ga
20-Jun-2004 PB debug mode 0|1|2, consistency check of units
07-Jul-2004 PB show quantity in debug mode,
barn symbol is b instead of barn,
new units Ar (a), poundal (pdl)
08-Jul-2004 PB new units Neugrad (gon), knots (kn), Kelvin (K)
05-Feb-2005 PB physical constants from CODATA 2002,
SI prefixes extended: Peta - Yotta, zepto - yocto
previous definition of Exa corrected to Peta
Units from PTB 2004
mi -> mile after PTB 2004, unit Hz, constant mn
inconsistent definition of amu in 2002 and 2004, using 2002,
unit pond without prefixes, because mp hides proton rest
mass mp
16-May-2005 PB Loop in dpfactor removed and dpfactor splitted into
dpfactor1 and dpfactor2
21-May-2005 PB isvariable added
28-Jun-2005 PB test version
NumProgramError, NumNoVariable, NumVar, dpprogram_run
num_str2long calculates dp values with temporary program.
next step: remove return value from all dp routines
29-Jun-2005 PB The file numprog.h has been included into numio.h.
The file numprog.c is included into the code of numio.c.
num_str2double creates a temporary program that is
executed to calculate the value, new functions
num_str2prog4, num_runprog4, num_rmprog.
30-Jun-2005 PB num_str2prog4 and num_runprog4 replaced by
num_str2prog and num_runprog using a variable argument
list (stdarg.h), numprog.c and numprog.h copied into
numio.c and numio.h
06-Aug-2005 PB num_chkvar added
15-Sep-2005 PB dpvariable: Used is incremented and not Value
num_chkvar corrected
11-Dec-2005 PB print routines declared in numio.h
length routines: num_prog_variables, num_prog_accumulators,
num_prog_instructions, num_prog_variable_size,
num_prog_accumulator_size, num_prog_instruction_size
num_prog_size, num_prog_size_all
numprog_up_accumulator: If CurrentAccumulator is NULL,
next is initialized with next = program->AccumulatorList
and not with NULL, to force the reuse of already allocated
accumulators. Otherwise each call to dpprogram_run would
allocate an accumulator with number 1.
15-Mar-2006 PB dpconstant_print: printf argument mismatch corrected
13-Jun-2006 PB units added: liter, minute, hour, day,
POW calculation corrected
19-Apr-2007 PB code corrected to avoid compiler warnings with -Wall
18-Jun-2007 V1.21 PB num_str2double, num_str2num: If str is the null pointer
it is handled like an empty string.
19-Jul-2007 V1.22 PB units degK, degC, degF added, functions degC2K,
degF2K, degK2K, K2degC, K2degF, K2degK added,
isfunction: extended to capital characters 'A'-'Z'
08-Feb-2008 V1.23 PB CEIL: cosh corrected to ceil
21-May-2008 V1.24 PB binary constants added
22-May-2008 V1.25 PB num_double2hex added
23-May-2008 V1.26 PB num_double2hex
20-Mar-2009 V1.27 PB char **tail -> const char **tail
21-Mar-2009 V1.28 PB logical operators added:
NOT, EQU, NEQ, LE, LT, GE, GT, AND, OR, IF
06-Oct-2009 V1.29 PB pi constant NUM_PI defined in numio.h
(not used internally)
30-Jan-2011 V1.30 PB %g -> %lg
double2s shortened
31-Jan-2011 V1.31 PB lcc does not like %ld and %lg on the same line,
splitted to make compiler happy
09-Mar-2011 V1.32 PB double2s: format corrected to avoid leading spaces,
in all public functions: perrval can be NULL
01-Jun-2011 V1.33 PB dpprogram_step: unique error exit
16-Jun-2011 V1.34 PB double constants marked, e.g. 1->1.0
--------------------------------------------------------------------------*/
/****************************************************************************
* Include *
****************************************************************************/
# include "numio.h"
/****************************************************************************
* Static Variables *
****************************************************************************/
static int NUMIO_debug = 0;
/****************************************************************************
* Enum (Basic Instructions) *
* New functions must be added to these tables. Each function needs an *
* entry in dpprogram_step. *
* ATTENTION: InValidNumCommand must have the value 0 *
* Do not change PUSHVAL, PUSHADDR *
****************************************************************************/
enum NumCommand {
InValidNumCommand, PUSHVAL, PUSHADDR, NEG, MUL,
NOT, EQU, NEQ, LE, LT,
GE, GT, AND, OR, IF,
DIV, REST, ADD, SUB, RAD,
DEG, PI, SIN, COS, TAN,
ASIN, ACOS, ATAN, ATAN2, SINH,
COSH, TANH, FLOOR, CEIL, FABS,
EXP, LOG, LOG10, POW, SQRT,
ROUND, GAMMA, FMIN, FMAX, DEGC2K,
K2DEGC, DEGF2K, K2DEGF, DEGK2K, K2DEGK,
DEGF2DEGC, DEGC2DEGF,
EndNumCommand
};
static const char * NumCommandStrings[] = {
"Invalid", "PUSHVAL", "PUSHADDR", "NEG", "MUL",
"NOT", "EQU", "NEQ", "LE", "LT",
"GE", "GT", "AND", "OR", "IF",
"DIV", "REST", "ADD", "SUB", "RAD",
"DEG", "PI", "SIN", "COS", "TAN",
"ASIN", "ACOS", "ATAN", "ATAN2", "SINH",
"COSH", "TANH", "FLOOR", "CEIL", "FABS",
"EXP", "LOG", "LOG10", "POW", "SQRT",
"ROUND", "GAMMA", "FMIN", "FMAX", "DEGC2K",
"K2DEGC", "DEGF2K", "K2DEGF", "DEGK2K", "K2DEGK",
"DEGF2DEGC", "DEGC2DEGF",
(const char *) NULL
};
/****************************************************************************
* Number Program Routines *
****************************************************************************/
/****************************************************************************
* Defines *
****************************************************************************/
# define NumBUFLEN 128
/***************************************************************************
* Number Program Static Variables *
***************************************************************************/
static int NUMPROG_init = 0; /* init flag */
static NumProg * NumProgRoot = (NumProg *) NULL; /* program root */
/***************************************************************************
* Number Program Functions *
***************************************************************************/
int numprog_init ( void );
NumProg * numprog_new ( const char * Name );
int numprog_insert ( const char * Name,
NumProg **pprogram );
int numprog_remove ( const char * Name );
int numprog_search ( const char * Name,
NumProg **pprogram );
int numprog_free ( NumProg *program );
int numprog_append_variable ( NumProg *program, const char * Key,
double InitValue,
NumVar **pvariable );
int numprog_search_variable ( NumProg *program, const char *Key,
NumVar **pvariable, int mode );
int numprog_free_variable_list ( NumProg *program );
long num_prog_variables ( NumProg *program );
size_t num_prog_variable_size ( NumProg *program );
int num_prog_print_variable_list ( FILE * out, NumProg *program,
int level, int verbose );
NumAccu *numprog_up_accumulator ( NumProg *program, double Value );
NumAccu *numprog_down_accumulator ( NumProg *program );
int numprog_free_accumulator_list ( NumProg *program );
long num_prog_accumulators ( NumProg *program );
size_t num_prog_accumulator_size ( NumProg *program );
int num_prog_print_accumulator_list( FILE * out, NumProg *program,
int level, int verbose );
int numprog_append_instruction ( NumProg *program, int mode,
int Command, int Nargs,
double Value, double *Address,
NumInstr **pinstruction );
int numprog_free_instruction_list ( NumProg *program, int mode );
long num_prog_instructions ( NumProg *program, int mode );
size_t num_prog_instruction_size ( NumProg *program, int mode );
int num_prog_print_instruction_list( FILE * out, NumProg *program, int mode,
int level, int verbose );
int num_prog_print_list ( FILE * out, NumProg * program,
int level, int verbose );
/*---------------------------------------------------------------------------
NAME
numprog_newstr --- allocate memory and copy a character string into it
SYNOPSIS
char * numprog_newstr( const char * string );
DESCRIPTION
Allocates strlen(string)+1 bytes of memory and copies string into it.
In case of success the pointer to the allocated memory is returned. The
null pointer is returned in case of an error.
If string is the NULL pointer the NULL pointer is returned.
RETURN VALUE
Returns the pointer to the allocated string or (char *) NULL in case
of an error.
---------------------------------------------------------------------------*/
char * numprog_newstr( const char * string )
{ char * newstring;
if (!string) return( (char *) NULL );
if (!(newstring = (char *) malloc(strlen(string)+1))) return((char *) NULL);
(void) strcpy(newstring,string);
return( newstring );
} /* numprog_newstr */
/*---------------------------------------------------------------------------
NAME
numprog_checkvar --- check whether variable name contains invalid chars
SYNOPSIS
int numprog_checkvar( const char * string );
DESCRIPTION
Checks, whether the input string contains only allowed characters.
first character: ['a'..'z'], ['A'..'Z'] (isalpha)
all other characters: ['0'..'9'], ['a'..'z'], ['A'..'Z'] (isalnum)
RETURN VALUE
0: success, -1: error
---------------------------------------------------------------------------*/
int numprog_checkvar( const char * string )
{
const char * ps;
ps=string;
if (!ps) return(-1);
if (*ps) {
if (isalpha((int) *ps)==0) return(-1);
ps++;
}
while (*ps) {
if (isalnum((int) *ps)==0) return(-1);
ps++;
}
return( 0 );
} /* numprog_checkvar */
/*---------------------------------------------------------------------------
NAME
numprog_cmd2str --- converts NumCommand to a string
SYNOPSIS
NumCommand numprog_cmd;
const char * numprog_cmd2str( int numprog_cmd )
RETURN VALUE
Pointer to a constant result string.
-------------------------------------------------------------------------*/
const char * numprog_cmd2str( int numprog_cmd )
{
if ((numprog_cmd<0)||(numprog_cmd>=EndNumCommand))
numprog_cmd = InValidNumCommand;
return( NumCommandStrings[numprog_cmd] );
} /* numprog_cmd2str */
/*---------------------------------------------------------------------------
NAME
numprog_ins2str --- converts NumInstr to a string
SYNOPSIS
NumCommand numprog_cmd;
const char * numprog_ins2str( char buffer[], size_t buflen, NumInstr * ins )
RETURN VALUE
success: Pointer to a constant result string.
-------------------------------------------------------------------------*/
const char * numprog_ins2str ( char buffer[], size_t buflen, NumInstr * ins )
{
char * pb;
buffer[0] = (char) 0;
pb = buffer;
if (buflen<50) return(pb);
switch (ins->Command) {
case PUSHVAL: /* value parameter */
sprintf(pb,"%10s %10g",
numprog_cmd2str(ins->Command), ins->Value);
break;
case PUSHADDR: /* address parameter */
sprintf(pb,"%10s *%10p",
numprog_cmd2str(ins->Command), ins->Address);
break;
default: /* no parameter */
sprintf(pb,"%10s %10s (%d args)",
numprog_cmd2str(ins->Command), "", ins->Nargs);
break;
}
return( pb );
} /* numprog_ins2str */
/*---------------------------------------------------------------------------
NAME
numprog_init --- Initializes the module
SYNOPSIS
int numprog_init ( void );
DESCRPTION
Initializes the program lists. It should be called by routines
that access directly NumProgRoot, if NUMPROG_init is not set.
Currently, only the initialization flag is set to 1.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int numprog_init ( void )
{
NUMPROG_init = 1;
return(0);
} /* numprog_init */
/*---------------------------------------------------------------------------
NAME
numprog_new --- Create a program
SYNOPSIS
NumProg * numprog_new ( const char * Name );
DESCRPTION
Creates new program with Name and returns a pointer to it.
An existing program is removed and reallocated with a different pointer.
RETURN VALUE
success: POINTER, error: NULL
---------------------------------------------------------------------------*/
PUBLIC NumProg * numprog_new ( const char * Name )
{ NumProg * program;
if (numprog_search ( Name, &program ))
return ( (NumProg *) NULL );
if (numprog_free( program ))
return ( (NumProg *) NULL );
if (numprog_insert ( Name, &program ))
return ( (NumProg *) NULL );
return( program );
} /* numprog_new */
/*---------------------------------------------------------------------------
NAME
numprog_insert --- Insert a program
SYNOPSIS
int numprog_insert ( const char * Name,
NumProg **pprogram );
DESCRPTION
Insert a program with Name and update *pprogram with the pointer to
program. If 'Name' already exists only *pprogram is updated.
In case of success the pointer to program is returned in *pprogram,
otherwise NULL.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int numprog_insert ( const char * Name,
NumProg **pprogram )
{
NumProg * newprogram, * next, * previous;
int notfound = 1;
if (!NUMPROG_init) numprog_init();
if (pprogram) *pprogram = (NumProg *) NULL;
if (!Name) return(-1);
previous = (NumProg *) NULL;
next = NumProgRoot;
/* search insertion point (insertion before next) */
while ( ( next!=(NumProg *) NULL ) && (notfound>0) ) {
notfound = strcmp(next->Name,Name);
if (notfound>0) {previous = next; next = next->Next;}
}
/* create new program Name, if notfound */
if ( notfound ) {
/* create new program Name */
if (!(newprogram = (NumProg*) malloc(sizeof(NumProg)))) return(-1);
newprogram->Name = numprog_newstr( Name );
if (!newprogram->Name) return(-1);
newprogram->VariableList = (NumVar *) NULL;
newprogram->AccumulatorList = (NumAccu *) NULL;
newprogram->CurrentAccumulator = (NumAccu *) NULL;
newprogram->InstructionList = (NumInstr *) NULL;
newprogram->CompiledList = (NumInstr *) NULL;
/* insert newprogram before next */
if (next) next->Previous = newprogram;
newprogram->Next=next;
newprogram->Previous=previous;
if (previous) previous->Next=newprogram;
else NumProgRoot = newprogram;
next = newprogram;
}
if (pprogram) *pprogram = next;
return(0);
} /* numprog_insert */
/*---------------------------------------------------------------------------
NAME
numprog_search --- search program
SYNOPSIS
int numprog_search ( const char * Name, NumProg **pprogram )
DESCRPTION
Search program Name in the program list. If found, the pointer to program
is returned, otherwise NULL. All characters of Name are compared.
Name is searched from the beginning of the list which is inversely
lexicographically ordered.
RETURN VALUE
Attention: The return value indicates errors only. It does not indicate
whether the program was found.
success:0, error:-1
---------------------------------------------------------------------------*/
int numprog_search ( const char * Name, NumProg **pprogram )
{
NumProg * current;
if (!NUMPROG_init) numprog_init();
if (pprogram) *pprogram = (NumProg *) NULL;
if (!Name) return(-1);
/* search Name */
current = NumProgRoot;
if ( current!=(NumProg *) NULL )
while( ( current!=(NumProg *) NULL ) &&
( strcmp(current->Name,Name)!=0) ) {
current = current->Next;
}
if (pprogram) *pprogram = current;
return(0);
} /* numprog_search */
/*---------------------------------------------------------------------------
NAME
numprog_free
SYNOPSIS
int numprog_free( NumProg * program );
DESCRIPTION
Removes program from NumProgRoot and releases its contents
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
PUBLIC int numprog_free( NumProg * program )
{ NumProg * previous, *next;
if (!NUMPROG_init) numprog_init();
/* nothing to do, if NULL pointer is given */
if (program==(NumProg *) NULL) return(0);
/* check, whether program is in NumProgRoot list */
next=NumProgRoot;
while ((next)&&(program!=next)) next=next->Next;
if (next==(NumProg *) NULL) return(-1);
/* change links */
previous = program->Previous;
next = program->Next;
if ( next != (NumProg *) NULL ) next->Previous = previous;
if ( previous != (NumProg *) NULL ) previous->Next = next;
else NumProgRoot = next;
/* free program and its elements */
if (numprog_free_variable_list ( program )) return(-1);
if (numprog_free_accumulator_list ( program )) return(-1);
if (numprog_free_instruction_list ( program, 0 )) return(-1);
if (numprog_free_instruction_list ( program, 1 )) return(-1);
if (program->Name) free(program->Name);
free(program);
return(0);
} /* numprog_free */
/*---------------------------------------------------------------------------
NAME
numprog_remove --- remove program(s)
SYNOPSIS
int numprog_remove ( const char * Name );
DESCRIPTION
Removes program Name and its contents.
If called with Name == (char *) NULL, all programs are removed
RETURN VALUE
success:0, error:-1
--------------------------------------------------------------------------+*/
int numprog_remove ( const char * Name )
{ NumProg * next, * current;
if (!NUMPROG_init) numprog_init();
if ( Name != (char *) NULL ) {
/* search program Name */
if (numprog_search( Name, ¤t )) return(-1);
/* remove current program */
if (numprog_free( current )) return(-1);
} else {
next = NumProgRoot;
while ( next != (NumProg *) NULL ) {
current = next;
next = current->Next;
/* remove current program */
if (numprog_free( current )) return(-1);
}
}
return( 0 );
} /* numprog_remove */
/*---------------------------------------------------------------------------
NAME
num_prog_size --- return program size
SYNOPSIS
size_t num_prog_size( NumProg * program );
DESCRIPTION
Returns size of program or 0 if program is NULL.
RETURN VALUE
program size in bytes
---------------------------------------------------------------------------*/
PUBLIC size_t num_prog_size( NumProg * program )
{
size_t prog_size=0;
if (!NUMPROG_init) numprog_init();
if (program!=(NumProg *) NULL) {
prog_size += sizeof( NumProg );
prog_size += strlen( program->Name )+1;
prog_size += num_prog_variable_size(program);
prog_size += num_prog_accumulator_size(program);
prog_size += num_prog_instruction_size(program,0);
prog_size += num_prog_instruction_size(program,1);
}
return(prog_size);
} /* num_prog_size */
/*---------------------------------------------------------------------------
NAME
num_prog_size_all --- return size of all programs
SYNOPSIS
size_t num_prog_size_all( void );
DESCRIPTION
Returns size of all programs.
RETURN VALUE
program size in bytes
---------------------------------------------------------------------------*/
PUBLIC extern size_t num_prog_size_all ( void )
{
NumProg * program;
size_t prog_size=0;
if (!NUMPROG_init) numprog_init();
program = NumProgRoot;
while(program!=(NumProg *) NULL)
prog_size += num_prog_size ( program );
return(prog_size);
} /* num_prog_size_all */
/*---------------------------------------------------------------------------
NAME
num_prog_print_list --- print program(s)
SYNOPSIS
int num_prog_print_list( FILE * out, NumProg * program,
int level, int verbose );
DESCRIPTION
Prints program list to out.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
PUBLIC int num_prog_print_list( FILE * out, NumProg * program,
int level, int verbose )
{ const char * SeparationLine =
"- - - - - - - - - - - - - - - - - - - - - - - - - - - - -";
NumProg * currentprogram;
if (!NUMPROG_init) numprog_init();
if (level<1) return(0);
if (program) currentprogram = program;
else currentprogram = NumProgRoot;
while(currentprogram!=(NumProg *) NULL) {
if (verbose) {
fprintf(out," %s\n",SeparationLine);
fprintf(out," Name = %s\n",currentprogram->Name);
num_prog_print_variable_list(out,currentprogram,level-1,verbose);
num_prog_print_accumulator_list(out,currentprogram,level-1,verbose);
num_prog_print_instruction_list(out,currentprogram,0,level-1,verbose);
// num_prog_print_instruction_list(out,currentprogram,1,level-1,verbose);
fprintf(out," Previous program = ");
if ((currentprogram->Previous)!=(NumProg *) NULL)
fprintf(out,"%s\n", currentprogram->Previous->Name);
else fprintf(out,"(no previous program)\n");
fprintf(out," Next program = ");
if ((currentprogram->Next)!=(NumProg *) NULL)
fprintf(out,"%s\n", currentprogram->Next->Name);
else fprintf(out,"(no next program)\n");
if (currentprogram->CurrentAccumulator) {
// fprintf splitted to make lcc happy
fprintf(out," CurrentAccumulator = #%ld",
currentprogram->CurrentAccumulator->Number);
fprintf(out," (Value = %lg)\n",
currentprogram->CurrentAccumulator->Value);
} else fprintf(out," CurrentAccumulator = (no current accumulator)\n");
} else {
fprintf(out," Program = '%s'\n",currentprogram->Name);
num_prog_print_variable_list(out,currentprogram,level-1,verbose );
num_prog_print_instruction_list(out,currentprogram,0,level-1,verbose );
// num_prog_print_instruction_list(out,currentprogram,1,level-1,verbose );
num_prog_print_accumulator_list(out,currentprogram,level-1,verbose );
}
if (program) currentprogram = (NumProg *) NULL;
else currentprogram=currentprogram->Next;
}
if (verbose) fprintf(out," %s\n",SeparationLine);
return(0);
} /* num_prog_print_list */
/*---------------------------------------------------------------------------
NAME
numprog_append_variable --- Appends a variable to the end of the list
SYNOPSIS
int numprog_append_variable ( NumProg *program, const char * Key,
double InitValue, NumVar **pvariable );
DESCRPTION
Appends a variable with name Key to the variable list of program and
updates *pvariable with the pointer to the variable. If 'Key' already
exists only *pvariable is updated and an error is returned.
If 'Key' does not exist the new variable is appended to the end of the list.
The pointer to the variable is returned in *pvariable.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
PUBLIC int numprog_append_variable ( NumProg *program, const char * Key,
double InitValue, NumVar **pvariable )
{ NumVar * newvariable, * next, * previous;
int notfound = 1;
if (pvariable) *pvariable = (NumVar *) NULL;
if (!Key) return(-1);
/* Check, whether Key contains only allowed characters */
if ( numprog_checkvar( Key ) ) return(-1);
/* Nothing to do if program pointer is NULL */
if ( !program ) return(0);
previous = (NumVar *) NULL;
next = program->VariableList;
/* search insertion point (insertion before next) */
while ( ( next!=(NumVar *) NULL ) && (notfound!=0) ) {
notfound = strcmp(next->Key,Key);
if (notfound!=0) {previous = next; next = next->Next;}
}
/* create new variable Key, if notfound */
if ( notfound ) {
/* create new variable Key */
if (!(newvariable = (NumVar*) malloc(sizeof(NumVar)))) return(-1);
newvariable->Key = numprog_newstr( Key );
if (!newvariable->Key) return(-1);
newvariable->Value = InitValue;
newvariable->Used = 0;
/* insert newvariable before next */
if (next) next->Previous = newvariable;
newvariable->Next=next;
newvariable->Previous=previous;
if (previous) previous->Next=newvariable;
else program->VariableList = newvariable;
next = newvariable;
}
if (pvariable) *pvariable = next;
if (!notfound) return(-1);
return(0);
} /* numprog_append_variable */
/*---------------------------------------------------------------------------
NAME
numprog_search_variable --- search variable
SYNOPSIS
int numprog_search_variable ( NumProg *program, const char *Key,
NumVar **pvariable, int mode );
DESCRPTION
Searches variable in the variable list of program. In case of success
the pointer to variable is returned in *pvariable, otherwise NULL.
If mode==0, all characters of Key are compared,
if mode==1, the comparison stops if a variable is found where all
characters match the first characters of Key.
Key is searched from the beginning of the list which is inversely
lexicographically ordered.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int numprog_search_variable ( NumProg *program, const char *Key,
NumVar **pvariable, int mode )
{
NumVar * current;
if (pvariable) *pvariable = (NumVar *) NULL;
if (!Key) return(-1);
/* Nothing to search if program pointer is NULL */
if ( !(NumProg * ) program ) return(-1);
/* search Key */
current = program->VariableList;
if ( current!=(NumVar *) NULL ) {
if (mode==0) {
while( ( current!=(NumVar *) NULL ) &&
( strcmp(current->Key,Key)!=0) ) {
current = current->Next;
}
} else {
while( ( current!=(NumVar *) NULL ) &&
( strncmp(current->Key,Key,strlen(current->Key))!=0) ) {
current = current->Next;
}
}
}
if (pvariable) *pvariable = current;
if (current==(NumVar *) NULL) return(-1);
return(0);
} /* numprog_search_variable */
/*---------------------------------------------------------------------------
NAME
numprog_free_variable_list --- remove variable list
SYNOPSIS
int numprog_free_variable_list ( NumProg * program );
DESCRIPTION
Removes variable list from program
RETURN VALUE
success:0, error:-1
--------------------------------------------------------------------------+*/
int numprog_free_variable_list ( NumProg * program )
{ NumVar * variable, * next;
/* Nothing to do if no program pointer is given */
if (!program) return(0);
next = program->VariableList;
program->VariableList = (NumVar *) NULL;
while(next!=(NumVar*) NULL) {
variable = next;
next=next->Next;
free(variable->Key);
free(variable);
}
return(0);
} /* numprog_free_variable_list */
/*---------------------------------------------------------------------------
NAME
num_prog_variables --- return number of variables
SYNOPSIS
long num_prog_variables ( NumProg *program );
DESCRIPTION
Return number of variables.
RETURN VALUE
number of variables
---------------------------------------------------------------------------*/
PUBLIC long num_prog_variables ( NumProg *program )
{ NumVar * variable, * next;
long variable_length=0l;
/* Nothing to do if no program pointer is given */
if (!program) return(variable_length);
next = program->VariableList;
while(next!=(NumVar*) NULL) {
variable = next;
next=next->Next;
variable_length++;
}
return(variable_length);
} /* num_prog_variables */
/*---------------------------------------------------------------------------
NAME
num_prog_variable_size --- return allocated memory size of variable list
SYNOPSIS
size_t num_prog_variable_size ( NumProg *program );
DESCRIPTION
Return allocated memory size of variable list.
RETURN VALUE
allocated memory size
---------------------------------------------------------------------------*/
PUBLIC size_t num_prog_variable_size ( NumProg *program )
{ NumVar * variable, * next;
size_t variable_size=0;
/* Nothing to do if no program pointer is given */
if (!program) return(variable_size);
next = program->VariableList;
while(next!=(NumVar*) NULL) {
variable = next;
next=next->Next;
variable_size += sizeof(NumVar);
}
return(variable_size);
} /* num_prog_variable_size */
/*---------------------------------------------------------------------------
NAME
num_prog_print_variable_list --- print variable list of program
SYNOPSIS
int num_prog_print_variable_list ( FILE * out, NumProg *program,
int level, int verbose );
DESCRIPTION
Prints program variable list to out.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
PUBLIC int num_prog_print_variable_list ( FILE * out, NumProg *program,
int level, int verbose )
{ const char * SeparationLine =
"- - - - - - - - - - - - - - -";
NumVar * variable;
if (level<1) return(0);
/* Nothing to print if program pointer is NULL */
if ( !(NumProg * ) program ) return(0);
variable = program->VariableList;
while(variable!=(NumVar *) NULL) {
if (verbose) {
fprintf(out," %s\n",SeparationLine);
fprintf(out," Variable = %s\n",variable->Key);
fprintf(out," Value = %lg\n",variable->Value);
fprintf(out," Used = %d\n",variable->Used);
fprintf(out," Previous variable = ");
if ((variable->Previous)!=(NumVar *) NULL)
fprintf(out,"%s\n", variable->Previous->Key);
else fprintf(out,"(no previous variable)\n");
fprintf(out," Next variable = ");
if ((variable->Next)!=(NumVar *) NULL)
fprintf(out,"%s\n", variable->Next->Key);
else fprintf(out,"(no next variable)\n");
} else {
fprintf(out," '%s' = %lg = *%p\n",
variable->Key,variable->Value,&(variable->Value));
}
variable=variable->Next;
}
if (verbose) fprintf(out," %s\n",SeparationLine);
return(0);
} /* num_prog_print_variable_list */
/*---------------------------------------------------------------------------
NAME
numprog_up_accumulator --- increments program->CurrentAccumulator
SYNOPSIS
NumAccu *numprog_up_accumulator( NumProg *program, double Value );
DESCRPTION
Increments program->CurrentAccumulator to next Accumulator. If it does not
exist it is appended to the accumulator list of program. The Accumulator is
initialized with Value. The pointer to the Accumulator is returned.
RETURN VALUE
success: POINTER , error: NULL
---------------------------------------------------------------------------*/
NumAccu *numprog_up_accumulator( NumProg *program, double Value )
{
NumAccu * newaccumulator, * next, * current;
long current_number;
/* Nothing to do if program pointer is NULL */
if ( !program ) return( (NumAccu *) NULL );
current = program->CurrentAccumulator;
if (current) next = current->Next;
else next = program->AccumulatorList; // use existing accumulator
if (current) current_number=current->Number;
else current_number=0;
if (!next) {
/* append new accumulator */
if (!(newaccumulator = (NumAccu*) malloc(sizeof(NumAccu))))
return( (NumAccu *) NULL );
newaccumulator->Number = current_number+1;
/* insert newaccumulator before next */
if (next) next->Previous = newaccumulator;
newaccumulator->Next=next;
newaccumulator->Previous=current;
if (current) current->Next=newaccumulator;
else program->AccumulatorList = newaccumulator;
next = newaccumulator;
}
next->Value = Value;
program->CurrentAccumulator = next;
return( next );
} /* numprog_up_accumulator */
/*---------------------------------------------------------------------------
NAME
numprog_down_accumulator --- decrements program->CurrentAccumulator
SYNOPSIS
NumAccu *numprog_down_accumulator( NumProg *program, double Value );
DESCRPTION
Decrements program->CurrentAccumulator to previous Accumulator.
The pointer to the Accumulator is returned.
RETURN VALUE
success: POINTER , error: NULL
---------------------------------------------------------------------------*/
NumAccu *numprog_down_accumulator ( NumProg *program )
{
NumAccu * current, * previous;
/* Nothing to do if program pointer is NULL */
if ( !program ) return( (NumAccu *) NULL );
current = program->CurrentAccumulator;
if (current) previous = current->Previous;
else previous = (NumAccu *) NULL;
program->CurrentAccumulator = previous;
return( previous );
} /* numprog_down_accumulator */
/*---------------------------------------------------------------------------
NAME
numprog_free_accumulator_list --- remove accumulator list
SYNOPSIS
int numprog_free_accumulator_list ( NumProg * program );
DESCRIPTION
Removes accumulator list from program
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int numprog_free_accumulator_list ( NumProg *program )
{ NumAccu * accumulator, * next;
/* Nothing to do if no program pointer is given */
if (!program) return(0);
next = program->AccumulatorList;
program->AccumulatorList = (NumAccu *) NULL;
while(next!=(NumAccu*) NULL) {
accumulator = next;
next=next->Next;
free(accumulator);
}
return(0);
} /* numprog_free_accumulator_list */
/*---------------------------------------------------------------------------
NAME
num_prog_accumulators --- return number of available accumulators
SYNOPSIS
long num_prog_accumulators ( NumProg *program ):
DESCRIPTION
Return number of available accumulators.
RETURN VALUE
number of accumulators
---------------------------------------------------------------------------*/
PUBLIC long num_prog_accumulators ( NumProg *program )
{ NumAccu * accumulator, * next;
long accu_length=0l;
/* Nothing to do if no program pointer is given */
if (!program) return(accu_length);
next = program->AccumulatorList;
while(next!=(NumAccu*) NULL) {
accumulator = next;
next=next->Next;
accu_length++;
}
return(accu_length);
} /* num_prog_accumulators */
/*---------------------------------------------------------------------------
NAME
num_prog_accumulator_size --- return alloc. memory size of accumulator table
SYNOPSIS
size_t num_prog_accumulator_size ( NumProg *program );
DESCRIPTION
Return allocated memory size of accumulator table
RETURN VALUE
allocated memory size
---------------------------------------------------------------------------*/
PUBLIC size_t num_prog_accumulator_size ( NumProg *program )
{ NumAccu * accumulator, * next;
size_t accu_size=0;
/* Nothing to do if no program pointer is given */
if (!program) return(accu_size);
next = program->AccumulatorList;
while(next!=(NumAccu*) NULL) {
accumulator = next;
next=next->Next;
accu_size += sizeof(NumAccu);
}
return(accu_size);
} /* num_prog_accumulator_size */
/*---------------------------------------------------------------------------
NAME
num_prog_print_accumulator_list --- print accumulator list of program
SYNOPSIS
int num_prog_print_accumulator_list ( FILE * out, NumProg *program,
int level, int verbose );
DESCRIPTION
Prints program accumulator list to out.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
PUBLIC int num_prog_print_accumulator_list ( FILE * out, NumProg *program,
int level, int verbose )
{ const char * SeparationLine =
"- - - - - - - - - - - - - - -";
NumAccu * accumulator;
if (level<1) return(0);
/* Nothing to print if program pointer is NULL */
if ( !(NumProg * ) program ) return(0);
accumulator = program->AccumulatorList;
while(accumulator!=(NumAccu *) NULL) {
if (verbose) {
fprintf(out," %s\n",SeparationLine);
fprintf(out," Accumulator = #%ld\n",accumulator->Number);
fprintf(out," Value = %lg\n",accumulator->Value);
fprintf(out," Previous accumulator = ");
if ((accumulator->Previous)!=(NumAccu *) NULL)
fprintf(out,"#%ld\n", accumulator->Previous->Number);
else fprintf(out,"(no previous accumulator)\n");
fprintf(out," Next accumulator = ");
if ((accumulator->Next)!=(NumAccu *) NULL)
fprintf(out,"#%ld\n", accumulator->Next->Number);
else fprintf(out,"(no next accumulator)\n");
} else {
// fprintf splitted to make lcc happy
fprintf(out," Accumulator #%ld =", accumulator->Number);
fprintf(out," %lg\n", accumulator->Value);
}
accumulator=accumulator->Next;
}
if (verbose) fprintf(out," %s\n",SeparationLine);
return(0);
} /* num_prog_print_accumulator_list */
/*---------------------------------------------------------------------------
NAME
numprog_append_instruction --- Appends a program instruction
SYNOPSIS
int numprog_append_instruction ( NumProg *program, int mode,
int Command, int Nargs,
double Value, double *Address,
NumInstr **pinstruction );
DESCRIPTION
mode 0:
Appends a new instruction to the end of the instruction list that starts at
program->InstructionList.
mode 1:
Appends a new instruction to the end of the instruction list that starts at
program->CompiledList.
Nargs is the number of arguments required by the command, e.g.
Nargs = 0 : no argument, accumulator number is increased by 1
Nargs = 1 : 1 argument, accumulator number is not changed
Nargs = N : N arguments, accumulator number is decreased by N-1
This information is only used by numprog_optimize
It updates *pinstruction with the pointer to the new instruction.
In case of success the pointer to the instruction is returned in
*pinstruction, otherwise NULL.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
PUBLIC int numprog_append_instruction ( NumProg *program, int mode,
int Command, int Nargs,
double Value, double *Address,
NumInstr **pinstruction )
{
NumInstr * newinstruction, * next, * previous, **proot;
if (pinstruction) *pinstruction = (NumInstr *) NULL;
/* Nothing to append if program pointer is NULL */
if ( !program ) return(0);
previous = (NumInstr *) NULL;
switch (mode) {
case 0:
proot = &(program->InstructionList);
break;
case 1:
proot = &(program->CompiledList);
break;
default:
/* Error */
return(-1);
}
next = *proot;
/* search insertion point (insertion before next) */
while ( ( next!=(NumInstr *) NULL ) ) {
previous = next; next = next->Next;
}
/* create new instruction */
if (!next) {
/* create new instruction */
if (!(newinstruction = (NumInstr*) malloc(sizeof(NumInstr))))
return(-1);
newinstruction->Command = Command;
newinstruction->Nargs = Nargs;
newinstruction->Value = Value;
newinstruction->Address = Address;
/* insert newinstruction before next */
if (next) next->Previous = newinstruction;
newinstruction->Next=next;
newinstruction->Previous=previous;
if (previous) previous->Next=newinstruction;
else *proot = newinstruction;
next = newinstruction;
}
if (pinstruction) *pinstruction = next;
return(0);
} /* numprog_append_instruction */
/*---------------------------------------------------------------------------
NAME
numprog_free_instruction_list --- remove instruction list
SYNOPSIS
int numprog_free_instruction_list ( NumProg * program, int mode );
DESCRIPTION
mode 0:
Removes the list starting at program->InstructionList.
mode 1:
Removes the list starting at program->CompiledList.
RETURN VALUE
success:0, error:-1
--------------------------------------------------------------------------+*/
int numprog_free_instruction_list ( NumProg *program, int mode )
{ NumInstr * instruction, * next, **proot;
/* Nothing to do if program pointer is NULL */
if (!program) return(0);
switch (mode) {
case 0:
proot = &(program->InstructionList);
break;
case 1:
proot = &(program->CompiledList);
break;
default:
/* Error */
return(-1);
}
next = *proot;
*proot = (NumInstr *) NULL;
while(next!=(NumInstr*) NULL) {
instruction = next;
next=next->Next;
free(instruction);
}
return(0);
} /* numprog_free_instruction_list */
/*---------------------------------------------------------------------------
NAME
num_prog_instructions --- return number of instructions
SYNOPSIS
long num_prog_instructions ( NumProg *program, int mode );
DESCRIPTION
Return number of instructions.
mode 0:
InstructionList.
mode 1:
CompiledList.
mode 2:
If CompiledList exist returns its length, otherwise
return the length of InstructionList
RETURN VALUE
number of instructions
---------------------------------------------------------------------------*/
PUBLIC long num_prog_instructions ( NumProg *program, int mode )
{ NumInstr * current;
long instruction_length=0l;
/* Nothing to do if program pointer is NULL */
if (!program) return(instruction_length);
switch (mode) {
case 0:
current = program->InstructionList;
break;
case 1:
current = program->CompiledList;
break;
case 2:
if (program->CompiledList)
current = program->CompiledList;
else current = program->InstructionList;
break;
default:
/* Error */
return(instruction_length);
}
while(current!=(NumInstr*) NULL) {
instruction_length++;
current=current->Next;
}
return(instruction_length);
} /* num_prog_instructions */
/*---------------------------------------------------------------------------
NAME
num_prog_instruction_size --- return alloc. memory size of instruction list
SYNOPSIS
size_t num_prog_instruction_size ( NumProg *program, int mode );
DESCRIPTION
Return allocated memory size of instruction list
mode 0:
InstructionList.
mode 1:
CompiledList.
RETURN VALUE
allocated memory size
---------------------------------------------------------------------------*/
PUBLIC size_t num_prog_instruction_size ( NumProg *program, int mode )
{ NumInstr * instruction, * next, **proot;
size_t instruction_size=0;
/* Nothing to do if program pointer is NULL */
if (!program) return(instruction_size);
switch (mode) {
case 0:
proot = &(program->InstructionList);
break;
case 1:
proot = &(program->CompiledList);
break;
default:
/* Error */
return(0);
}
next = *proot;
while(next!=(NumInstr*) NULL) {
instruction = next;
next=next->Next;
instruction_size += sizeof(NumInstr);
}
return(instruction_size);
} /* num_prog_instruction_size */
/*---------------------------------------------------------------------------
NAME
num_prog_print_instruction_list --- print instruction list of program
SYNOPSIS
int num_prog_print_instruction_list ( FILE * out, NumProg *program,
int mode, int level, int verbose );
DESCRIPTION
mode 0:
Prints the list starting at program->InstructionList to out.
mode 1:
Prints the list starting at program->CompiledList to out.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
PUBLIC int num_prog_print_instruction_list ( FILE * out, NumProg *program,
int mode, int level, int verbose )
{ const char * SeparationLine =
"- - - - - - - - - - - - - - -";
char buffer[NumBUFLEN];
size_t buflen=NumBUFLEN;
NumInstr * instruction, **proot;
if (level<1) return(0);
/* Nothing to print if program pointer is NULL */
if ( !program ) return(0);
switch (mode) {
case 0:
fprintf(out," %s\n","InstructionList");
proot = &(program->InstructionList);
break;
case 1:
fprintf(out," %s\n","CompiledList");
proot = &(program->CompiledList);
break;
default:
/* Error */
return(-1);
}
instruction = *proot;
while(instruction!=(NumInstr *) NULL) {
if (verbose) {
fprintf(out," %s\n",SeparationLine);
fprintf(out," Command = %s\n",
numprog_ins2str ( buffer, buflen, instruction ) );
fprintf(out," Previous instruction = ");
if ((instruction->Previous)!=(NumInstr *) NULL)
fprintf(out,"%s\n", numprog_ins2str ( buffer,buflen,instruction->Previous ));
else fprintf(out,"(no previous instruction)\n");
fprintf(out," Next instruction = ");
if ((instruction->Next)!=(NumInstr *) NULL)
fprintf(out,"%d\n", instruction->Next->Command);
else fprintf(out,"(no next instruction)\n");
} else {
fprintf(out," %s\n", numprog_ins2str ( buffer, buflen, instruction ) );
}
instruction=instruction->Next;
}
if (verbose) fprintf(out," %s\n",SeparationLine);
return(0);
} /* num_prog_print_instruction_list */
/***************************************************************************
* Double Constant List Structure Definition *
***************************************************************************/
typedef struct Double_Constant {
char * Key; /* pointer to key string */
double Value; /* Value of the constant */
char * Quantity; /* pointer to description string */
char * Unit; /* pointer to unit string */
struct Double_Constant *Previous,*Next; /* previous and next constants */
} DPConstant;
/****************************************************************************
* Double Constant Static Variables *
****************************************************************************/
static int DPConstantInit = 0; /* init flag */
static DPConstant * DPConstantRoot = (DPConstant *) NULL; /* list root */
static const double Yotta = 1e+24, Yotta2 = 1e+48, Yotta3 = 1e+72;
static const double Zetta = 1e+21, Zetta2 = 1e+42, Zetta3 = 1e+63;
static const double Exa = 1e+18, Exa2 = 1e+36, Exa3 = 1e+54;
static const double Peta = 1e+15, Peta2 = 1e+30, Peta3 = 1e+45;
static const double Tera = 1e+12, Tera2 = 1e+24, Tera3 = 1e+36;
static const double Giga = 1e+09, Giga2 = 1e+18, Giga3 = 1e+27;
static const double Mega = 1e+06, Mega2 = 1e+12, Mega3 = 1e+18;
static const double Kilo = 1e+03, Kilo2 = 1e+06, Kilo3 = 1e+09;
static const double Hekto = 1e+02, Hekto2 = 1e+04, Hekto3 = 1e+06;
// static const double Deka = 1e+01, Deka2 = 1e+02, Deka3 = 1e+03; //unused
static const double deci = 1e-01, deci2 = 1e-02, deci3 = 1e-03;
static const double centi = 1e-02, centi2 = 1e-04, centi3 = 1e-06;
static const double milli = 1e-03, milli2 = 1e-06, milli3 = 1e-09;
static const double micro = 1e-06, micro2 = 1e-12, micro3 = 1e-18;
static const double nano = 1e-09, nano2 = 1e-18, nano3 = 1e-27;
static const double pico = 1e-12, pico2 = 1e-24, pico3 = 1e-36;
static const double femto = 1e-15, femto2 = 1e-30, femto3 = 1e-45;
static const double atto = 1e-18, atto2 = 1e-36, atto3 = 1e-54;
// static const double zepto = 1e-21, zepto2 = 1e-42, zepto3 = 1e-63; // unused
static const double zepto = 1e-21, zepto3 = 1e-63;
static const double yocto = 1e-24, yocto2 = 1e-48, yocto3 = 1e-72;
/****************************************************************************
* Internal Functions *
****************************************************************************/
/*---------------------------------------------------------------------------
NAME
num_newstr --- allocate memory and copy a character string into it
SYNOPSIS
char * num_newstr( const char * string );
DESCRIPTION
Allocates strlen(string)+1 bytes of memory and copies string into it.
In case of success the pointer to the allocated memory is returned. The
null pointer is returned in case of an error.
If string is the NULL pointer the NULL pointer is returned.
RETURN VALUE
Returns the pointer to the allocated string or (char *) NULL in case
of an error.
---------------------------------------------------------------------------*/
char * num_newstr( const char * string )
{ char * newstring;
if (!string) return( (char *) NULL );
if (!(newstring = (char *) malloc(strlen(string)+1))) return((char *) NULL);
(void) strcpy(newstring,string);
return( newstring );
} /* num_newstr */
/****************************************************************************
* Program Functions *
****************************************************************************/
/*---------------------------------------------------------------------------
NAME
dpprogram_step --- Step through a single instruction
SYNOPSIS
int dpprogram_step ( NumProg * program, NumInstr * instruction,
int * perrval );
DESCRPTION
Executes a single instruction. It uses program->CurrentAccumulator as current
accumulator. In case of success this pointer is updated.
At entry accumulator->Value must contain argumentN,
accumulator->Previous->Value must contain argumentN-1
and so on until argument1.
The instruction is executed with these input arguments and the result is
written to the accumulator that contained originally argument1.
Instructions without input arguments (PUSHVAL, PUSHADDR, PI) write the
output value to accumulator->Next->Value. If necessary, memory for this
accumulator is allocated.
The values of the variables that are defined in program->VariableList
are used for calculation.
ERROR VALUES
*perrval
NumSuccess : success
program errors: (in this case the returned address is NULL)
NumNoAccumulator : not enough accumulator cells available
NumNoInstruction : unknown instruction
calculation errors:
NumDivByZero : division by zero
NumDomainError : some of the input arguments are outside
RETURN VALUE
success: 0 in case of success or a calculation error (DivByZero, DomainError)
error :-1 in case of a program error (NumNoAccumulator, NumNoInstruction)
In case of return value 0 the calculations can be repeated with changed
variable values, in case of return value -1 the program is corrupted and
cannot be used.
---------------------------------------------------------------------------*/
int dpprogram_step ( NumProg * program, NumInstr * instruction,
int * perrval )
{
int errval;
const double pi = 3.1415926535897932384626;
const double degtorad = pi/180.0;
const double radtodeg = 180.0/pi;
double argument1, argument2, argument3;
NumAccu * accumulator;
errval = NumSuccess;
if (!program) {
errval = NumProgramError;
goto dpprogram_step_error;
}
if (!instruction) {
errval = NumNoInstruction;
goto dpprogram_step_error;
}
accumulator = program->CurrentAccumulator;
switch (instruction->Command) {
case PUSHVAL:
accumulator = numprog_up_accumulator( program, instruction->Value );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
break;
case PUSHADDR:
accumulator = numprog_up_accumulator( program,*(instruction->Address) );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
break;
case NEG:
argument1 = accumulator->Value;
accumulator->Value = -argument1;
break;
case MUL:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = argument1 * argument2;
break;
case NOT:
argument1 = accumulator->Value;
accumulator->Value = argument1?0.0:1.0;
break;
case EQU:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = (argument1 == argument2)?1.0:0.0;
break;
case NEQ:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = (argument1 != argument2)?1.0:0.0;
break;
case LE:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = (argument1 <= argument2)?1.0:0.0;
break;
case LT:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = (argument1 < argument2)?1.0:0.0;
break;
case GE:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = (argument1 >= argument2)?1.0:0.0;
break;
case GT:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = (argument1 > argument2)?1.0:0.0;
break;
case AND:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = (argument1 && argument2)?1.0:0.0;
break;
case OR:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = (argument1 || argument2)?1.0:0.0;
break;
case IF:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
argument3 = accumulator->Next->Next->Value;
accumulator->Value = argument1 ? argument2 : argument3;
break;
case DIV:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
if ( argument2 != 0.0 ) {
accumulator->Value = argument1 / argument2;
} else errval = NumDivByZero;
break;
case REST:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
if ( argument2 != 0.0 ) {
accumulator->Value =
(double) ((long)floor(argument1+0.5)%(long)floor(argument2+0.5));
} else errval = NumDivByZero;
break;
case ADD:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = argument1 + argument2;
break;
case SUB:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = argument1 - argument2;
break;
case RAD:
argument1 = accumulator->Value;
accumulator->Value = degtorad * argument1;
break;
case DEG:
argument1 = accumulator->Value;
accumulator->Value = radtodeg * argument1;
break;
case PI:
accumulator = numprog_up_accumulator( program, pi );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
break;
case SIN:
argument1 = accumulator->Value;
accumulator->Value = sin( argument1 );
break;
case COS:
argument1 = accumulator->Value;
accumulator->Value = cos( argument1 );
break;
case TAN:
argument1 = accumulator->Value;
accumulator->Value = tan( argument1 );
break;
case ASIN:
argument1 = accumulator->Value;
if (fabs(argument1)<=1.0) {
accumulator->Value = asin( argument1 );
} else errval=NumDomainError;
break;
case ACOS:
argument1 = accumulator->Value;
if (fabs(argument1)<=1.0) {
accumulator->Value = acos( argument1 );
} else errval=NumDomainError;
break;
case ATAN:
argument1 = accumulator->Value;
accumulator->Value = atan( argument1 );
break;
case ATAN2:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value = atan2( argument1 , argument2 );
break;
case SINH:
argument1 = accumulator->Value;
accumulator->Value = sinh( argument1 );
break;
case COSH:
argument1 = accumulator->Value;
accumulator->Value = cosh( argument1 );
break;
case TANH:
argument1 = accumulator->Value;
accumulator->Value = tanh( argument1 );
break;
case FLOOR:
argument1 = accumulator->Value;
accumulator->Value = floor( argument1 );
break;
case CEIL:
argument1 = accumulator->Value;
accumulator->Value = ceil( argument1 );
break;
case FABS:
argument1 = accumulator->Value;
accumulator->Value = fabs( argument1 );
break;
case EXP:
argument1 = accumulator->Value;
accumulator->Value = exp( argument1 );
break;
case LOG:
argument1 = accumulator->Value;
if (argument1>0.0) {
accumulator->Value = log( argument1 );
} else errval=NumDomainError;
break;
case LOG10:
argument1 = accumulator->Value;
if (argument1>0.0) {
accumulator->Value = log10( argument1 );
} else errval=NumDomainError;
break;
case POW:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
if ( argument2 == 0.0 ) {
// argument1 is 0 -> result is defined as 1.0
accumulator->Value = 1.0;
} else {
if ( argument1 > 0.0 ) {
// positive argument1 -> solutions for all argument2
if ( argument2 > 0.0 ) {
accumulator->Value = pow( argument1 , argument2 );
} else {
accumulator->Value = 1.0/pow( argument1 , -argument2 );
}
} else {
// argument1 is negative or zero
if ( argument1 == 0.0 ) {
// argument1 is zero
if ( argument2 > 0.0 ) {
accumulator->Value = 0.0;
} else errval=NumDomainError;
} else {
// argument1 is negative
if ( (floor(argument2+0.5)-argument2)==0.0 ) {
// only solutions for integer values of argument2
if ( argument2 > 0.0 ) {
accumulator->Value = pow( argument1 , argument2 );
} else {
accumulator->Value = 1.0/pow( argument1 , -argument2 );
}
} else errval=NumDomainError;
}
}
}
break;
case SQRT:
argument1 = accumulator->Value;
if (argument1>=0.0) {
accumulator->Value = sqrt( argument1 );
} else errval=NumDomainError;
break;
case ROUND:
argument1 = accumulator->Value;
accumulator->Value = floor( argument1 + 0.5 );
break;
case GAMMA:
argument1 = accumulator->Value;
accumulator->Value = gamma( argument1 );
break;
case FMIN:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value =
(argument1<argument2)?argument1:argument2;
break;
case FMAX:
accumulator = numprog_down_accumulator( program );
if (!accumulator) {
errval = NumNoAccumulator;
goto dpprogram_step_error;
}
argument1 = accumulator->Value;
argument2 = accumulator->Next->Value;
accumulator->Value =
(argument1>argument2)?argument1:argument2;
break;
case DEGC2K:
argument1 = accumulator->Value;
accumulator->Value = argument1 + 273.15;
break;
case K2DEGC:
argument1 = accumulator->Value;
accumulator->Value = argument1 - 273.15;
break;
case DEGF2K:
argument1 = accumulator->Value;
accumulator->Value = (5.0/9.0) * (argument1 - 32.0) + 273.15;
break;
case K2DEGF:
argument1 = accumulator->Value;
accumulator->Value = ((argument1 - 273.15) * (9.0/5.0)) + 32.0;
break;
case DEGK2K:
argument1 = accumulator->Value;
accumulator->Value = argument1;
break;
case K2DEGK:
argument1 = accumulator->Value;
accumulator->Value = argument1;
break;
case DEGF2DEGC:
argument1 = accumulator->Value;
accumulator->Value = (5.0/9.0) * (argument1 - 32.0);
break;
case DEGC2DEGF:
argument1 = accumulator->Value;
accumulator->Value = (argument1 * (9.0/5.0)) + 32.0;
break;
default:
errval = NumNoInstruction;
goto dpprogram_step_error;
} /* cmd */
if (perrval) *perrval=errval;
return( 0 );
dpprogram_step_error:
if (perrval) *perrval=errval;
return( -1 );
} /* dpprogram_step */
/*---------------------------------------------------------------------------
NAME
dpprogram_run --- Executes all instructions of the program
SYNOPSIS
int dpprogram_run ( NumProg * program, int * perrval );
DESCRPTION
Executes all instructions starting with the first instruction of
InstructionList and with the first accumulator of AccumulatorList.
At entry, the pointer program->CurrentAccumulator is reset to NULL.
The program is executed by subsequent calls to dpprogram_step. In
case of success, program->CurrentAccumulator->Value points to the result.
ERROR VALUES
*perrval
NumSuccess : success
program errors : (in this case the returned value is -1)
NumNoAccumulator : not enough accumulator cells available
NumNoInstruction : unknown instruction
calculation errors:
NumDivByZero : division by zero
NumDomainError : some of the input arguments are outside
RETURN VALUE
success: 0 in case of success or a calculation error (DivByZero, DomainError)
error :-1 in case of a program error (NumNoAccumulator, NumNoInstruction)
In case of return value 0 the calculations can be repeated with changed
variable values, in case of return value -1 the program is corrupted and
cannot be used.
---------------------------------------------------------------------------*/
PUBLIC int dpprogram_run ( NumProg * program, int * perrval )
{
int status=-1;
NumInstr * instruction;
if (!program) {
*perrval = NumProgramError; return( status );
}
instruction = program->InstructionList;
if (!instruction) {
*perrval = NumNoInstruction; return( status );
}
program->CurrentAccumulator = ( NumAccu * ) NULL;
while ( instruction ) {
status = dpprogram_step( program, instruction, perrval );
if ( *perrval ) return( status );
instruction = instruction->Next;
}
return( status );
} /* dpprogram_run */
/*---------------------------------------------------------------------------
NAME
dpprogram_optimize --- contract instructions where possible
SYNOPSIS
NumInstr * dpprogram_optimize ( NumProg * program,
NumInstr * firstinstruction,
int * perrval );
DESCRIPTION
The routine executes the input instruction list and uses
program->CurrentAccumulator as first accumulator. The instruction list
is executed either until the command PUSHADDR is given or until
program->CurrentAccumulator->Number reaches the same number that it
had at entry.
If PUSHADDR is found, the values of all accumulators between first
accumulator->Next and program->CurrentAccumulator are copied with PUSHVAL
to program->CompiledList.
In the second case, the values of all accumulators between first
accumulator->Next and program->CurrentAccumulator are copied with PUSHVAL
to program->CompiledList.
If existing, the last instruction that would
use or modify the value of the first accumulator is appended to
program->CompiledList, because it cannot be executed immediately.
The last copied instruction is returned.
ERROR VALUES
*perrval
NumSuccess : success
program errors : (in this case the returned value is NULL)
NumNoAccumulator : not enough accumulator cells available
NumNoInstruction : unknown instruction
calculation errors (only, if no variables are involved):
NumDivByZero : division by zero
NumDomainError : some of the input arguments are outside
RETURN VALUE
pointer to the last used instruction or NULL if end of list is reached
NULL, together with an error value, is also returned in case of a
program error.
---------------------------------------------------------------------------*/
NumInstr * dpprogram_optimize ( NumProg * program,
NumInstr * firstinstruction,
int * perrval )
{
NumInstr *instruction, *lastinstruction;
NumAccu *accumulator, *firstaccumulator, *lastaccumulator;
int accunum, firstaccunum, lastaccunum;
if (!program) {
*perrval = NumProgramError; return( (NumInstr *) NULL );
}
if (!firstinstruction) {
*perrval = NumNoInstruction; return( (NumInstr *) NULL );
}
firstaccumulator = program->CurrentAccumulator;
if ( firstaccumulator ) firstaccunum = firstaccumulator->Number;
else firstaccunum = 0;
instruction = firstinstruction;
accumulator = firstaccumulator;
accunum = firstaccunum;
while ( ( instruction ) &&
( instruction->Command != PUSHADDR ) &&
( accunum+1-instruction->Nargs > firstaccunum ) ) {
dpprogram_step( program, instruction, perrval );
if ( *perrval ) return( instruction );
accumulator = program->CurrentAccumulator;
accunum = accumulator->Number;
instruction = instruction->Next;
}
lastinstruction = instruction;
lastaccumulator = accumulator;
lastaccunum = accunum;
/* copy (lastaccunum-firstaccunum) accumulator values starting at
firstaccumulator->Next to lastaccumulator (inclusive) */
if ( firstaccumulator ) accumulator=firstaccumulator->Next;
else accumulator = program->AccumulatorList;
for (accunum=firstaccunum+1;accunum<=lastaccunum;accunum++) {
/* write accumulator values with PUSHVAL into CompiledList */
if (numprog_append_instruction ( program, 1, PUSHVAL, 0,
accumulator->Value, NULL,
NULL ) ) return( (NumInstr *) NULL );
accumulator=accumulator->Next;
}
if ( lastinstruction ) {
/* copy last instruction */
instruction=lastinstruction;
if (numprog_append_instruction ( program, 1,
instruction->Command, instruction->Nargs,
instruction->Value, instruction->Address,
NULL ) ) return( (NumInstr *) NULL );
}
if ( lastinstruction ) {
/* the last instruction was not executed */
if (lastinstruction->Nargs==0) {
numprog_up_accumulator ( program, *(lastinstruction->Address) );
} else {
for (accunum=lastaccunum;accunum>lastaccunum+1-lastinstruction->Nargs;
accunum--)
numprog_down_accumulator ( program );
}
}
return( lastinstruction );
} /* dpprogram_optimize */
/*---------------------------------------------------------------------------
NAME
dpprogram_compile --- Optimizes the program
SYNOPSIS
int dpprogram_compile ( NumProg * program, int * perrval );
DESCRIPTION
The program is optimized by calculating parts of the program that do not
contain variables.
ERROR VALUES
*perrval
NumSuccess : success
program errors : (in this case the returned value is -1)
NumNoAccumulator : not enough accumulator cells available
NumNoInstruction : unknown instruction
calculation errors:
NumDivByZero : division by zero
NumDomainError : some of the input arguments are outside
RETURN VALUE
success: 0 in case of success or a calculation error (DivByZero, DomainError)
error :-1 in case of a program error (NumNoAccumulator, NumNoInstruction)
In case of return value 0 the calculation has caused an error, e.g.
DivByZero, DomainError. If the program is run it will also return a
calculation error. In case of return value -1 the program is corrupted and
cannot be used.
---------------------------------------------------------------------------*/
PUBLIC int dpprogram_compile ( NumProg * program, int * perrval )
{
NumInstr * instruction;
if (!program) {
*perrval = NumProgramError; return( -1 );
}
instruction = program->InstructionList;
if (!instruction) {
*perrval = NumNoInstruction; return( -1 );
}
program->CurrentAccumulator = ( NumAccu * ) NULL;
while ( instruction ) {
instruction = dpprogram_optimize( program, instruction, perrval );
if ( *perrval ) return( instruction?0:-1 );
if (instruction ) instruction = instruction->Next;
}
/* Replace InstructionList by CompiledList */
if ( numprog_free_instruction_list ( program, 0 ) ) {
*perrval = NumProgramError; return( -1 );
}
program->InstructionList = program->CompiledList;
program->CompiledList = ( NumInstr * ) NULL;
return( 0 );
} /* dpprogram_compile */
/****************************************************************************
* Number Functions *
****************************************************************************/
# define EXPRESSION dpcondition
void dpcondition( NumProg * program, const char **ps,
int level, int * perrval);
void dplogicsum( NumProg * program, const char **ps,
int level, int * perrval);
void dplogicproduct( NumProg * program, const char **ps,
int level, int * perrval);
void dpequality( NumProg * program, const char **ps,
int level, int * perrval);
void dpcomparison( NumProg * program, const char **ps,
int level, int * perrval);
void dpexpression( NumProg * program, const char **ps,
int level, int *perrval);
void dpterm( NumProg * program, const char **ps,
int level, int *perrval);
void dpfactor0( NumProg * program, const char **ps,
int level, int *perrval);
void dpfactor1( NumProg * program, const char **ps,
int level, int *perrval);
void dpfactor2( NumProg * program, const char **ps,
int level, int *perrval);
void dpconstant( NumProg * program, const char **ps,
int *perrval);
void dpvariable( NumProg * program, const char **ps,
int *perrval);
void dpfunction( NumProg * program, const char **ps,
int level, int *perrval);
long int lvexpression( const char **ps, int level, int *perrval);
long int lvfactor( const char **ps, int level, int *perrval);
long int lvterm( const char **ps, int level, int *perrval);
int isfunction( const char * s )
/* A function name starts with a character and contains characters and
numbers. It ends with a parenthesis '('. This function returns 1
if the string s starts with a function name */
{
if (('0'<=*s) && (*s<'9')) return ( 0 ); /* no function */
while ( (('0'<=*s) && (*s<'9')) || (('a'<=*s) && (*s<'z')) ||
(('A'<=*s) && (*s<'Z')) ) s++;
if (*s=='(') return ( 1 ); else return ( 0 );
} /* isfunction */
int isvariable( const char * s )
/* In an expression, a variable name is preceded by an underscore. This
function returns 1 if the string s starts with a variable name */
{
if ('_'!=*s) return ( 0 ); /* no variable */
return ( 1 );
} /* isvariable */
void print_spaces( FILE * out, int n )
{ for (n=n;n>0;n--) fprintf(out," ");
} /* print_spaces */
/***************************************************************************
* Double Constant Functions *
***************************************************************************/
int dpconstant_insert ( const char * Quantity, const char * Unit,
const char * Key, double value,
DPConstant ** pdpconstant );
int dpconstant_search ( const char * Key, DPConstant ** pdpconstant,
int mode );
int dpconstant_free ( void );
int dpconstant_print ( FILE * out, int level, int verbose );
int dpconstant_init ( void );
/*---------------------------------------------------------------------------
NAME
dpconstant_insert --- Define Value of Key
SYNOPSIS
int dpconstant_insert ( const char * Quantity, const char * Unit,
const char * Key, double Value ,
DPConstant ** pdpconstant )
DESCRPTION
Insert or update the constant Key with Value, return pointer to dpconstant.
If 'Key' already exists, its 'Value', 'Quantity' and 'Unit' are updated,
otherwise 'Key' is created.
In case of success the pointer to the new dpconstant is returned, otherwise
NULL.
'Quantity' and 'Unit' are optional strings. They are only used if
NUMIO_debug is > 0. If 'Quantity' or 'Unit' is NULL the corresponding value
is ignored.
The key list is inversely lexicographically ordered, i.e. longer keys are
preceeding shorter keys (nnn->nn->n->mmm->mm>m).
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int dpconstant_insert ( const char * Quantity, const char * Unit,
const char * Key, double Value ,
DPConstant ** pdpconstant )
{
DPConstant * newdpconstant, * next, * previous;
int notfound = 1;
if (pdpconstant) *pdpconstant = (DPConstant *) NULL;
previous = (DPConstant *) NULL;
next = DPConstantRoot;
/* search insertion point (insertion before next) */
while ( ( next!=(DPConstant *) NULL ) && (notfound>0) ) {
notfound = strcmp(next->Key,Key);
if (notfound>0) {previous = next; next = next->Next;}
}
/* create new constant Key, if notfound */
if ( notfound ) {
/* create new constant Key */
if (!(newdpconstant = (DPConstant*) malloc(sizeof(DPConstant)))) return(-1);
newdpconstant->Key = num_newstr( Key );
if (!newdpconstant->Key) return(-1);
newdpconstant->Quantity = (char *) NULL;
newdpconstant->Unit = (char *) NULL;
/* insert newdpconstant before next */
if (next) next->Previous = newdpconstant;
newdpconstant->Next=next;
newdpconstant->Previous=previous;
if (previous) previous->Next=newdpconstant;
else DPConstantRoot = newdpconstant;
next = newdpconstant;
}
/* update Value */
next->Value = Value;
/* update Quantity and Unit for debugging */
if (NUMIO_debug > 0) {
if (next->Quantity) free( next->Quantity );
next->Quantity = num_newstr( Quantity );
/* Ignore error if newstr returns NULL, because Quantity can be NULL */
if (next->Unit) free( next->Unit );
next->Unit = num_newstr( Unit );
/* Ignore error if newstr returns NULL, because Unit can be NULL */
}
if (pdpconstant) *pdpconstant = next;
return(0);
} /* dpconstant_insert */
/*---------------------------------------------------------------------------
NAME
dpconstant_search --- search Key
SYNOPSIS
int dpconstant_search ( const char * Key, DPConstant ** pdpconstant )
DESCRPTION
Search the Key. In case of success the pointer to dpconstant is returned,
otherwise NULL.
If mode==0, all characters of Key are compared,
if mode==1, the comparison stops if a dpconstant is found where all
characters match the first characters of Key.
Key is searched from the beginning of the list which is inversely
lexicographically ordered.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int dpconstant_search ( const char * Key, DPConstant ** pdpconstant, int mode )
{
DPConstant * current;
/* return NULL in case that Key was not found */
if (pdpconstant) *pdpconstant = (DPConstant *) NULL;
/* search Key */
current = DPConstantRoot;
if ( current!=(DPConstant *) NULL ) {
if (mode==0) {
while( ( current!=(DPConstant *) NULL ) &&
( strcmp(current->Key,Key)!=0) ) {
current = current->Next;
}
} else {
while( ( current!=(DPConstant *) NULL ) &&
( strncmp(current->Key,Key,strlen(current->Key))!=0) ) {
current = current->Next;
}
}
}
if (pdpconstant) *pdpconstant = current;
if (current==(DPConstant *) NULL) return(-1);
if (NUMIO_debug > 0) {
if (current->Unit) {
if (current->Quantity) {
printf(" %s = %.15g %s (%s)\n",
current->Key,current->Value,current->Unit,current->Quantity);
} else {
printf(" %s = %.15g %s\n",current->Key,current->Value,current->Unit);
}
} else {
if (current->Quantity) {
printf(" %s = %.15g (%s)\n",
current->Key,current->Value,current->Quantity);
} else {
printf(" %s = %.15g\n",current->Key,current->Value);
}
}
}
return(0);
} /* dpconstant_search */
/*+++------------------------------------------------------------------------
NAME
dpconstant_free --- free list of constants
SYNOPSIS
int dpconstant_free( void )
DESCRIPTION
Releases all Keys
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int dpconstant_free( void )
{
DPConstant * current, * next;
next = DPConstantRoot;
while(next!=(DPConstant*) NULL) {
current = next;
next=next->Next;
if (current->Quantity) free(current->Quantity);
if (current->Unit) free(current->Unit);
free(current->Key);
free(current);
}
DPConstantRoot = (DPConstant *) NULL;
return(0);
} /* dpconstant_free */
/*+++------------------------------------------------------------------------
NAME
dpconstant_print --- print all constants
SYNOPSIS
int dpconstant_print( FILE * out, int level, int verbose )
DESCRIPTION
Prints all constants to the file out
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int dpconstant_print( FILE * out, int level, int verbose )
{ const char * SeparationLine =
"- - - - - - - -";
DPConstant * current;
if (level<1) return(0);
current = DPConstantRoot;
while (current!=(DPConstant*) NULL) {
if (verbose) {
fprintf(out," %s\n",SeparationLine);
fprintf(out," Key = %s\n",current->Key);
fprintf(out," Value = %lg\n",current->Value);
if (current->Quantity)
fprintf(out," Quantity = %s\n",current->Quantity);
if (current->Unit)
fprintf(out," Unit = %s\n",current->Unit);
fprintf(out," Previous Key = ");
if ((current->Previous)!=(DPConstant*) NULL)
fprintf(out,"%s\n", current->Previous->Key);
else fprintf(out,"(no previous dpconstant)\n");
fprintf(out," Next Key = ");
if ((current->Next)!=(DPConstant*) NULL)
fprintf(out,"%s\n", current->Next->Key);
else fprintf(out,"(no next dpconstant)\n");
} else {
if (current->Quantity)
if (current->Unit)
fprintf(out," '%s' = %lg %s (%s)\n",
current->Key,current->Value,current->Unit, current->Quantity);
else
fprintf(out," '%s' = %lg (%s)\n",
current->Key,current->Value,current->Quantity);
else
if (current->Unit)
fprintf(out," '%s' = %lg %s\n",
current->Key,current->Value,current->Unit);
else
fprintf(out," '%s' = %lg\n",current->Key,current->Value);
}
current=current->Next;
}
if (verbose) fprintf(out," %s\n",SeparationLine);
return(0);
} /* dpconstant_print */
/*---------------------------------------------------------------------------
NAME
dpconstant_insert_unit --- Insert full range of unit from atto to Exa
SYNOPSIS
int dpconstant_insert_unit ( const char * Quantity, const char * Unit,
const char * Baseunit, double Basevalue );
DESCRPTION
Baseunit preceeded by prefixes from atto to Exa is inserted into the list.
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int dpconstant_insert_unit ( const char * Quantity, const char * Unit,
const char * Baseunit, double Basevalue )
{
char unit[128];
DPConstant * element;
if (strlen(Baseunit)>64) return(-1);
/* Yotta */
if ( sprintf( unit, "Y%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Yotta*Basevalue, &element )) return(-1);
/* Zetta */
if ( sprintf( unit, "Z%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Zetta*Basevalue, &element )) return(-1);
/* Exa */
if ( sprintf( unit, "E%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Exa*Basevalue, &element )) return(-1);
/* Peta */
if ( sprintf( unit, "P%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Peta*Basevalue, &element )) return(-1);
/* Tera */
if ( sprintf( unit, "T%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Tera*Basevalue, &element )) return(-1);
/* Giga */
if ( sprintf( unit, "G%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Giga*Basevalue, &element )) return(-1);
/* Mega */
if ( sprintf( unit, "M%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Mega*Basevalue, &element )) return(-1);
/* Kilo */
if ( sprintf( unit, "k%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Kilo*Basevalue, &element )) return(-1);
/* Hekto */
if ( sprintf( unit, "h%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Hekto*Basevalue, &element )) return(-1);
/* Base unit */
if (dpconstant_insert ( Quantity, Unit,
Baseunit, Basevalue, &element )) return(-1);
/* deci */
if ( sprintf( unit, "d%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, deci*Basevalue, &element )) return(-1);
/* centi */
if ( sprintf( unit, "c%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, centi*Basevalue, &element )) return(-1);
/* milli */
if ( sprintf( unit, "m%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, milli*Basevalue, &element )) return(-1);
/* micro */
if ( sprintf( unit, "u%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, micro*Basevalue, &element )) return(-1);
/* nano */
if ( sprintf( unit, "n%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, nano*Basevalue, &element )) return(-1);
/* pico */
if ( sprintf( unit, "p%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, pico*Basevalue, &element )) return(-1);
/* femto */
if ( sprintf( unit, "f%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, femto*Basevalue, &element )) return(-1);
/* atto */
if ( sprintf( unit, "a%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, atto*Basevalue, &element )) return(-1);
/* zepto */
if ( sprintf( unit, "z%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, zepto*Basevalue, &element )) return(-1);
/* yocto */
if ( sprintf( unit, "y%s", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, yocto*Basevalue, &element )) return(-1);
return(0);
} /* dpconstant_insert_unit */
/*---------------------------------------------------------------------------
NAME
dpconstant_insert_unit2 --- Insert full range of unit for square
SYNOPSIS
int dpconstant_insert_unit2 ( const char * Quantity, const char * Unit,
const char * Baseunit, double Basevalue );
DESCRPTION
The square of the Baseunit preceeded by prefixes from atto to Exa and
followed by 2 is inserted into the list, e.g. km2 = Kilo*Kilo*m*m
Baseunit and Basevalue are not squared!
Unit is the unit symbol of Quantity, e.g. Unit="m^2", Quantity="area"
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int dpconstant_insert_unit2 ( const char * Quantity, const char * Unit,
const char * Baseunit, double Basevalue )
{
double Basevalue2;
char unit[128];
DPConstant * element;
if (strlen(Baseunit)>63) return(-1);
Basevalue2 = Basevalue*Basevalue;
/* Yotta2 */
if ( sprintf( unit, "Y%s2",Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Yotta2*Basevalue2, &element )) return(-1);
/* Zetta2 */
if ( sprintf( unit, "Z%s2",Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Zetta2*Basevalue2, &element )) return(-1);
/* Exa2 */
if ( sprintf( unit, "E%s2",Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Exa2*Basevalue2, &element )) return(-1);
/* Peta2 */
if ( sprintf( unit, "P%s2",Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Peta2*Basevalue2, &element )) return(-1);
/* Tera2 */
if ( sprintf( unit, "T%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Tera2*Basevalue2, &element )) return(-1);
/* Giga2 */
if ( sprintf( unit, "G%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Giga2*Basevalue2, &element )) return(-1);
/* Mega2 */
if ( sprintf( unit, "M%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Mega2*Basevalue2, &element )) return(-1);
/* Kilo2 */
if ( sprintf( unit, "k%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Kilo2*Basevalue2, &element )) return(-1);
/* Hekto2 */
if ( sprintf( unit, "h%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Hekto2*Basevalue2, &element )) return(-1);
/* Base unit ^2 */
if ( sprintf( unit, "%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Basevalue2, &element )) return(-1);
/* deci2 */
if ( sprintf( unit, "d%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, deci2*Basevalue2, &element )) return(-1);
/* centi2 */
if ( sprintf( unit, "c%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, centi2*Basevalue2, &element )) return(-1);
/* milli2 */
if ( sprintf( unit, "m%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, milli2*Basevalue2, &element )) return(-1);
/* micro2 */
if ( sprintf( unit, "u%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, micro2*Basevalue2, &element )) return(-1);
/* nano2 */
if ( sprintf( unit, "n%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, nano2*Basevalue2, &element )) return(-1);
/* pico2 */
if ( sprintf( unit, "p%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, pico2*Basevalue2, &element )) return(-1);
/* femto2 */
if ( sprintf( unit, "f%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, femto2*Basevalue2, &element )) return(-1);
/* atto2 */
if ( sprintf( unit, "a%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, atto2*Basevalue2, &element )) return(-1);
/* yocto2 */
if ( sprintf( unit, "y%s2", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, yocto2*Basevalue2, &element )) return(-1);
return(0);
} /* dpconstant_insert_unit2 */
/*---------------------------------------------------------------------------
NAME
dpconstant_insert_unit3 --- Insert full range of unit for unit^3
SYNOPSIS
int dpconstant_insert_unit3 ( const char * Quantity, const char * Unit,
const char * Baseunit, double Basevalue );
DESCRPTION
The cube of the Baseunit preceeded by prefixes from atto to Exa and
followed by 3 is inserted into the list, e.g. km2 = Kilo*Kilo*m*m
Baseunit and Basevalue are the linear values, they are not cubed!
Unit is the unit symbol of Quantity, e.g. Unit="m^3", Quantity="volume"
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int dpconstant_insert_unit3 ( const char * Quantity, const char * Unit,
const char * Baseunit, double Basevalue )
{
double Basevalue3;
char unit[128];
DPConstant * element;
if (strlen(Baseunit)>63) return(-1);
Basevalue3 = Basevalue*Basevalue;
/* Yotta3 */
if ( sprintf( unit, "Y%s3",Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Yotta3*Basevalue3, &element )) return(-1);
/* Zetta3 */
if ( sprintf( unit, "Z%s3",Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Zetta3*Basevalue3, &element )) return(-1);
/* Exa3 */
if ( sprintf( unit, "E%s3",Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Exa3*Basevalue3, &element )) return(-1);
/* Peta3 */
if ( sprintf( unit, "P%s3",Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Peta3*Basevalue3, &element )) return(-1);
/* Tera3 */
if ( sprintf( unit, "T%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Tera3*Basevalue3, &element )) return(-1);
/* Giga3 */
if ( sprintf( unit, "G%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Giga3*Basevalue3, &element )) return(-1);
/* Mega3 */
if ( sprintf( unit, "M%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Mega3*Basevalue3, &element )) return(-1);
/* Kilo3 */
if ( sprintf( unit, "k%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Kilo3*Basevalue3, &element )) return(-1);
/* Hekto3 */
if ( sprintf( unit, "h%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Hekto3*Basevalue3, &element )) return(-1);
/* Base unit ^3 */
if ( sprintf( unit, "%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, Basevalue3, &element )) return(-1);
/* deci3 */
if ( sprintf( unit, "d%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, deci3*Basevalue3, &element )) return(-1);
/* centi3 */
if ( sprintf( unit, "c%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, centi3*Basevalue3, &element )) return(-1);
/* milli3 */
if ( sprintf( unit, "m%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, milli3*Basevalue3, &element )) return(-1);
/* micro3 */
if ( sprintf( unit, "u%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, micro3*Basevalue3, &element )) return(-1);
/* nano3 */
if ( sprintf( unit, "n%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, nano3*Basevalue3, &element )) return(-1);
/* pico3 */
if ( sprintf( unit, "p%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, pico3*Basevalue3, &element )) return(-1);
/* femto3 */
if ( sprintf( unit, "f%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, femto3*Basevalue3, &element )) return(-1);
/* atto3 */
if ( sprintf( unit, "a%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, atto3*Basevalue3, &element )) return(-1);
/* zepto3 */
if ( sprintf( unit, "z%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, zepto3*Basevalue3, &element )) return(-1);
/* yocto3 */
if ( sprintf( unit, "y%s3", Baseunit) < 1 ) return(-1);
if (dpconstant_insert ( Quantity, Unit,
unit, yocto3*Basevalue3, &element )) return(-1);
return(0);
} /* dpconstant_insert_unit3 */
/*+++------------------------------------------------------------------------
NAME
dpconstant_init --- Inits constants
SYNOPSIS
int dpconstant_init( void )
DESCRIPTION
Inits the constants list
RETURN VALUE
success:0, error:-1
---------------------------------------------------------------------------*/
int dpconstant_init ( void )
/* Physical and mathematical constants are taken from:
[86] Lawrence Berkeley Laboratory
University of California
Berkeley, California 94720
X-Ray data booklet, second printing, with corrections, April 1986
Constants that are not contained in this edition are taken from
[91] Kuchling, Taschenbuch der Physik, Verlag Harri Deutsch 1991
There are inconsistencies between in some definitions, e.g. amu, ga and p.
86+ and 91+ means that the values were calculated from values in [86] or [91].
If possible, all physical constants replaced by values from
[2002] Peter J. Mohr and Barry N. Taylor,
CODATA Recommended Values of the Fundamental Physical Constants: 2002
taken from http://physics.nist.gov/constants
All units are preferably taken from
[2004] PTB Die gesetzlichen Einheiten in Deutschland
(download from http://www.ptb.de/ 20/0104)
Inconsistency for amu_ in 2002 and 2004
*/
{
const double pi = 3.1415926535897932384626; /* pi number 86 */
const double gamma = 0.577215664901532861; /* gamma number 86 */
const double e = 2.718281828459045235; /* Euler number 86 */
const double rad_ = 1.0; /* angle (rad) */
const double deg_ = pi/180.0; /* angle (rad) */
const double gon_ = pi/200.0; /* angle (rad) */
const double arcmin_ = pi/180.0/60.0; /* angle (rad) */
const double arcsec_ = pi/180.0/3600.0; /* angle (rad) */
const double inf = DBL_MAX; /* "infinity" (workaround) */
// const double k = 1.380662e-23; /* Boltzmann constant (J/K) 86 */
const double k = 1.3806505e-23; /* Boltzmann constant (J/K) 2002 */
// const double me = 9.109534e-31; /* electron rest mass (kg) 86 */
const double me = 9.1093826e-31; /* electron rest mass (kg) 2002 */
// const double mp = 1.6726485e-27; /* proton rest mass (kg) 86 */
const double mp = 1.67262171e-27; /* proton rest mass (kg) 2002 */
// const double md = 3.3436369e-27; /* deuteron rest mass (kg) 86+ */
const double md = 3.34358335e-27; /* deuteron rest mass (kg) 2002 */
const double mn = 1.67492728e-27; /* neutron rest mass (kg) 2002 */
//const double NA = 6.022045e23; /* Avogadro number (1/mol) 86 */
// const double NA = 6.0221367e23; /* Avogadro number (1/mol) 91 */
const double NA = 6.0221415e23; /* Avogadro number (1/mol) 2002 */
// const double re = 2.8179380e-15; /* classical electron radius (m) 86 */
const double re = 2.817940325e-15; /* classical electron radius (m) 2002 */
const double c = 2.99792458e8; /* velocity of light (m/s) 2002 */
// const double ec = 1.6021892e-19; /* electron charge magnitude (C) 86 */
const double ec = 1.60217653e-19; /* elementary charge (C) 2002 */
// const double h = 6.626176e-34; /* Planck's number (J*s) 86 */
const double h = 6.6260693e-34; /* Planck constant (J*s) 2002 */
// const double gN = 6.6720e-11; /* gravitational constant (m3/kg/s2) 86 */
const double gN = 6.6742e-11; /* Newtonian constant of gravitation
(m3/kg/s2) 2002 */
//const double ga = 9.8062; /* gravitational acceleration at sea
// level at 45 deg latitude (m/s2) 86 */
const double ga = 9.80665; /* gravitational acceleration (m/s2) 2004 */
const double u0 = 4e-7*pi; /* permeability of vacuum (H/m) 86 */
const double e0 = 1.0/(u0*c*c); /* permittivity of vacuum (F/m) 86 */
const double m_ = 1.0; /* length (m) meter */
const double in_ = 2.54e-2; /* length (m) inch 86 2004 */
const double ft_ = 0.3048; /* length (m) foot 91 2004 */
const double yd_ = 0.9144; /* length (m) yard 91 2004 */
const double mile_ = 1609.344; /* length (m) mile 91 2004 */
const double sm_ = 1852; /* length (m) nautical mile 91 2004 */
const double barn_ = 1e-28; /* area (m2) barn 86 2004 */
const double a_ = 1e2; /* area (m2) Ar 91 2004 */
const double sec_ = 1.0; /* time (s) second */
const double min_ = 60.0*sec_; /* time (s) minute */
const double hr_ = 60.0*min_; /* time (s) hour */
const double d_ = 24.0*hr_; /* time (s) day */
const double Hz_ = 1.0; /* frequency (1/s) Herz */
const double kn_ = sm_/3600.0/sec_; /* speed (m/s) knots 91 2004 */
const double l_ = 1e-3; /* volume (m3) liter */
const double g_ = 1e-3; /* mass (kg) gram */
// const double amu_ = 1.6605402e-27; /* mass (kg) atomic mass unit 91 */
// const double amu_ = 1.6605655e-27; /* mass (kg) atomic mass unit 86 2004 */
const double amu_ = 1.66053886e-27; /* 0.00000028e-27
mass (kg) atomic mass constant 2002*/
const double lb_ = 0.45359237; /* mass (kg) pound 91 2004 */
// const double oz_ = 0.02834952; /* mass (kg) ounze 91 */
const double oz_ = 0.0283495; /* mass (kg) ounze (avoirdupois) 2004 */
const double J_ = 1.0; /* energy (J) Joule */
// const double cal_ = 4.184; /* energy (J) calorie 86 */
const double cal_ = 4.1868; /* energy (J) calorie 2004 */
const double erg_ = 1e-7; /* energy (J) erg 91 2004 */
const double W_ = 1.0; /* power (W) Watt */
const double K_ = 1.0; /* temperature (K) Kelvin */
const double degK_ = K_;
const double degC_ = K_;
const double degF_ = (5.0/9.0)*K_;
const double N_ = 1.0; /* force (N) Newton */
const double p_ = 9.80665e-3; /* force (N) pond 91 2004 */
const double dyn_ = 1e-5; /* force (N) dyn 91 2004 */
//const double lbf_ = 4.44822; /* force (N) pound force 91 */
const double lbf_ = lb_*ga; /* force (N) pound force */
const double pdl_ = 0.138255; /* force (N) poundal 91 */
const double Pa_ = 1.0; /* pressure (Pa) Pascal */
const double bar_ = 1e5; /* pressure (Pa) bar 91 2004 */
const double Torr_ = 133.3224; /* pressure (Pa) Torr 91 2004 */
const double atm_ = 1.01325e5; /* pressure (Pa)
physical atmosphere 2004 */
const double at_ = 0.980665e5; /* pressure (Pa)
technical atmosphere 2004 */
// const double psi_ = 6894.76; /* pressure (Pa) lbf per square in 91 */
const double psi_ = lbf_/in_/in_; /* pressure (Pa) lbf per square in */
const double V_ = 1.0; /* voltage (V) Volt */
const double A_ = 1.0; /* electric current (A) Ampere */
const double C_ = 1.0; /* electric charge (C) Coulomb */
const double F_ = 1.0; /* electric capacity (F=A*s/V) Farad */
const double Ohm_ = 1.0; /* electric resistance (Ohm=V/A) */
const double S_ = 1.0; /* electric conductivity (S=A/V) Siemens */
const double T_ = 1.0; /* magnetic induction (T=V*s/m2) Tesla */
const double Wb_ = 1.0; /* magnetic flux (Wb=V*s) Weber */
const double H_ = 1.0; /* magnetic inductivity (H=V*s/A) Henry */
const double mol_ = 1.0; /* molecular amount */
const double Byte_ = 1.0; /* binary, B means Bel and
cannot be used here */
DPConstant * element;
/* --- mathematical constants and units */
/* Infinity */
if (dpconstant_insert ( "infinity", NULL,
"inf", inf, &element )) return(-1);
/* Gamma */
if (dpconstant_insert ( "gamma number", NULL,
"gamma", gamma, &element )) return(-1);
/* pi */
if (dpconstant_insert ( "pi number", NULL,
"pi", pi, &element )) return(-1);
/* e */
if (dpconstant_insert ( "Euler number", NULL,
"e", e, &element )) return(-1);
/* --- angle */
/* rad (rad)*/
if (dpconstant_insert ( "angle", "rad",
"rad", rad_, &element )) return(-1);
/* mrad (rad) */
if (dpconstant_insert ( "angle", "rad",
"mrad", milli*rad_, &element )) return(-1);
/* urad (rad) */
if (dpconstant_insert ( "angle", "rad",
"urad", micro*rad_, &element )) return(-1);
/* deg (rad)*/
if (dpconstant_insert ( "angle", "rad",
"deg", deg_, &element )) return(-1);
/* mdeg (rad) */
if (dpconstant_insert ( "angle", "rad",
"mdeg", milli*deg_, &element )) return(-1);
/* udeg (rad) */
if (dpconstant_insert ( "angle", "rad",
"udeg", micro*deg_, &element )) return(-1);
/* gon (rad)*/
if (dpconstant_insert ( "angle", "rad",
"gon", gon_, &element )) return(-1);
/* mgon (rad) */
if (dpconstant_insert ( "angle", "rad",
"mgon", milli*gon_, &element )) return(-1);
/* ugon (rad) */
if (dpconstant_insert ( "angle", "rad",
"ugon", micro*gon_, &element )) return(-1);
/* arcmin (rad)*/
if (dpconstant_insert ( "angle", "rad",
"arcmin", arcmin_, &element )) return(-1);
/* arcsec (rad)*/
if (dpconstant_insert ( "angle", "rad",
"arcsec", arcsec_, &element )) return(-1);
/* --- spherical angle (sr) */
if (dpconstant_insert ( "spherical angle", "sr",
"sr", rad_*rad_, &element )) return(-1);
/* --- physical constants and units */
/* k Boltzmann constant (J/K) */
if (dpconstant_insert ( "Boltzmann constant", "J/K",
"k", k, &element )) return(-1);
/* me electron rest mass (kg) */
if (dpconstant_insert ( "electron rest mass", "kg",
"me", me, &element )) return(-1);
/* mp proton rest mass (kg) */
if (dpconstant_insert ( "proton rest mass", "kg",
"mp", mp, &element )) return(-1);
/* mp deuteron rest mass (kg) */
if (dpconstant_insert ( "deuteron rest mass", "kg",
"md", md, &element )) return(-1);
/* mn neutron rest mass (kg) */
if (dpconstant_insert ( "neutron rest mass", "kg",
"mn", mn, &element )) return(-1);
/* NA Avogadro number (1/mol) */
if (dpconstant_insert ( "Avogadro number", "1/mol",
"NA", NA, &element )) return(-1);
/* re classical electron radius (m) */
if (dpconstant_insert ( "classical electron radius", "m",
"re", re, &element )) return(-1);
/* c velocity of light */
if (dpconstant_insert ( "velocity of light", "m/s",
"c", c, &element )) return(-1);
/* ec electron charge */
if (dpconstant_insert ( "electron charge", "C",
"ec", ec, &element )) return(-1);
/* h Planck's number (J*s) */
if (dpconstant_insert ( "Planck constant", "J*s",
"h", h, &element )) return(-1);
/* gN gravitational constant (m3/kg/s2) */
if (dpconstant_insert ( "gravitational constant", "m3/kg/s2",
"gN", gN, &element )) return(-1);
/* ga gravitational acceleration (m/s2) */
if (dpconstant_insert ( "gravitational acceleration", "m/s2",
"ga", ga, &element )) return(-1);
/* u0 permeability of vacuum = 4e-7*pi H/m */
if ( dpconstant_insert ( "permeability of vacuum", "H/m",
"u0", u0, &element ) ) return(-1);
/* e0 permittivity of vacuum = 1/u0/c2 (F/m) */
if ( dpconstant_insert ( "permittivity of vacuum", "F/m",
"e0", e0, &element ) ) return(-1);
/* Charge Coulomb (C) */
if ( dpconstant_insert_unit ( "electric charge", "C", "C", C_ ) ) return(-1);
/* Voltage Volt (V) */
if ( dpconstant_insert_unit ( "voltage", "V", "V", V_ ) ) return(-1);
/* Electric capacity Farad (F=As/V) */
if ( dpconstant_insert_unit ( "electric capacity", "F",
"F", F_ ) ) return(-1);
/* Resistance (Ohm=V/A) */
if ( dpconstant_insert_unit ( "electric resistance", "Ohm",
"Ohm", Ohm_ ) ) return(-1);
/* Conductivity Siemens (S=A/V) */
if ( dpconstant_insert_unit ( "electric conductivity", "S",
"S", S_ ) ) return(-1);
/* Magnetic induction Tesla (T) */
if ( dpconstant_insert_unit ( "magnetic induction", "T",
"T", T_ ) ) return(-1);
if ( dpconstant_insert_unit ( "magnetic induction", "T",
"G", 1e-4*T_ ) ) return(-1);
/* Magnetic flux Weber (Wb=Vs) */
if ( dpconstant_insert_unit ( "magnetic flux", "Wb",
"Wb", Wb_ ) ) return(-1);
/* Magnetic inductivity Henry (H=Vs/A) */
if ( dpconstant_insert_unit ( "magnetic inductivity", "H",
"H", H_ ) ) return(-1);
/* Electric current Ampere (A) */
if ( dpconstant_insert_unit ( "electric current", "A",
"A", A_ ) ) return(-1);
/* Time (s) */
if ( dpconstant_insert_unit ( "time", "s", "s", sec_ ) ) return(-1);
if ( dpconstant_insert ( "time", "s",
"min", min_, &element ) ) return(-1);
if ( dpconstant_insert ( "time", "s",
"hr", hr_, &element ) ) return(-1);
if ( dpconstant_insert ( "time", "s",
"d", d_, &element ) ) return(-1);
/* Time^2 (s^2) */
if ( dpconstant_insert_unit2 ( "time^2", "s2", "s", sec_ ) ) return(-1);
/* Frequency (1/s) */
if ( dpconstant_insert_unit ( "frequency", "1/s", "Hz", Hz_ ) ) return(-1);
/* Length (m) */
if ( dpconstant_insert_unit ( "length", "m", "m", m_ ) ) return(-1);
if ( dpconstant_insert ( "length", "m",
"in", in_, &element )) return(-1);
if ( dpconstant_insert ( "length", "m",
"ft", ft_, &element )) return(-1);
if ( dpconstant_insert ( "length", "m",
"yd", yd_, &element )) return(-1);
if ( dpconstant_insert ( "length", "m",
"mile", mile_, &element )) return(-1);
if ( dpconstant_insert ( "length", "m",
"sm", sm_, &element )) return(-1);
/* Speed (m/s) */
if ( dpconstant_insert ( "speed", "m/s",
"kn", kn_, &element )) return(-1);
/* Area (m^2) */
if ( dpconstant_insert_unit2 ( "area", "m2", "m", m_ ) ) return(-1);
if ( dpconstant_insert ( "area", "m2",
"b", barn_, &element )) return(-1);
if ( dpconstant_insert ( "area", "m2",
"a", a_, &element )) return(-1);
if ( dpconstant_insert ( "area", "m2",
"ha", Hekto*a_, &element )) return(-1);
/* Volume (m^3) */
if ( dpconstant_insert_unit3 ( "volume", "m3", "m", m_ ) ) return(-1);
if ( dpconstant_insert_unit ( "volume", "m3", "l", l_ ) ) return(-1);
/* Mass (kg) */
if ( dpconstant_insert_unit ( "mass", "kg", "g", g_ ) ) return(-1);
if ( dpconstant_insert ( "mass", "kg",
"lb", lb_, &element ) ) return(-1);
if ( dpconstant_insert ( "mass", "kg",
"oz", oz_, &element ) ) return(-1);
/* amu atomic mass unit (kg) */
if (dpconstant_insert ( "mass", "kg",
"amu", amu_, &element )) return(-1);
/* Molecular amount (mol) */
if ( dpconstant_insert_unit ( "molecular amount", "mol",
"mol", mol_ ) ) return(-1);
/* Energy Joule (J) */
if ( dpconstant_insert_unit ( "energy", "J", "J", J_ ) ) return(-1);
if ( dpconstant_insert_unit ( "energy", "J", "eV", ec*V_ ) ) return(-1);
if ( dpconstant_insert_unit ( "energy", "J", "cal", cal_ ) ) return(-1);
if ( dpconstant_insert ( "energy", "J",
"erg", erg_, &element ) ) return(-1);
/* Temperature Kelvin (K) */
if ( dpconstant_insert ( "temperature", "K",
"K", K_, &element ) ) return(-1);
if ( dpconstant_insert ( "temperature", "K",
"mK", milli*K_, &element ) ) return(-1);
if ( dpconstant_insert ( "temperature", "K",
"uK", micro*K_, &element ) ) return(-1);
/* Temperature degrees Kelvin, Celsius, Fahrenheit (K) */
if ( dpconstant_insert ( "temperature", "K",
"degK", degK_, &element ) ) return(-1);
if ( dpconstant_insert ( "temperature", "K",
"degC", degC_, &element ) ) return(-1);
if ( dpconstant_insert ( "temperature", "K",
"degF", degF_, &element ) ) return(-1);
/* Power Watt (W) */
if ( dpconstant_insert_unit ( "power", "W", "W", W_ ) ) return(-1);
/* Force Newton (N) */
if ( dpconstant_insert_unit ( "force", "N", "N", N_ ) ) return(-1);
/* if ( dpconstant_insert_unit ( "force", "N", "p", p_ ) ) return(-1); */
if ( dpconstant_insert ( "force", "N", "p", p_, &element ) ) return(-1);
if ( dpconstant_insert ( "force", "N",
"lbf", lbf_, &element ) ) return(-1);
if ( dpconstant_insert ( "force", "N",
"pdl", pdl_, &element ) ) return(-1);
if ( dpconstant_insert ( "force", "N",
"dyn", dyn_, &element ) ) return(-1);
/* Pressure Pascal (Pa) */
if ( dpconstant_insert_unit ( "pressure", "Pa", "Pa", Pa_ ) ) return(-1);
if ( dpconstant_insert_unit ( "pressure", "Pa", "bar", bar_ ) ) return(-1);
if ( dpconstant_insert ( "physical atmospheric pressure", "Pa",
"atm", atm_, &element ) ) return(-1);
if ( dpconstant_insert ( "technical atmospheric pressure", "Pa",
"at", at_, &element ) ) return(-1);
if ( dpconstant_insert ( "pressure", "Pa",
"psi", psi_, &element ) ) return(-1);
if ( dpconstant_insert ( "pressure", "Pa", "Torr", Torr_,
&element ) ) return(-1);
/* Binary constants (Byte) */
if ( dpconstant_insert ( "Byte", "Byte", "Byte",
Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "KiloByte", "Byte", "kByte",
Kilo*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "MegaByte", "Byte", "MByte",
Mega*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "GigaByte", "Byte", "GByte",
Giga*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "TeraByte", "Byte", "TByte",
Tera*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "PetaByte", "Byte", "PByte",
Peta*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "ExaByte", "Byte", "EByte",
Exa*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "ZettaByte", "Byte", "ZByte",
Zetta*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "YottaByte", "Byte", "YByte",
Yotta*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "KibiByte", "Byte", "KiByte",
pow(1024,1)*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "MebiByte", "Byte", "MiByte",
pow(1024,2)*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "GibiByte", "Byte", "GiByte",
pow(1024,3)*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "TebiByte", "Byte", "TiByte",
pow(1024,4)*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "PebiByte", "Byte", "PiByte",
pow(1024,5)*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "ExbiByte", "Byte", "EiByte",
pow(1024,6)*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "ZebiByte", "Byte", "ZiByte",
pow(1024,7)*Byte_, &element ) ) return(-1);
if ( dpconstant_insert ( "YobiByte", "Byte", "YiByte",
pow(1024,8)*Byte_, &element ) ) return(-1);
DPConstantInit = 1;
if ( NUMIO_debug > 1 ) dpconstant_print ( stdout, 1, 0 );
return( 0 );
} /* dpconstant_init */
/***************************************************************************
* Number Functions *
***************************************************************************/
void dpconstant( NumProg * program, const char **ps, int *perrval)
{
double value;
DPConstant * constant;
*perrval = NumSuccess;
value = 1.0;
if (!DPConstantInit) dpconstant_init();
if ( !dpconstant_search( *ps, &constant, 1 ) ) {
value=constant->Value; *ps=*ps+strlen(constant->Key);
if (numprog_append_instruction ( program, 0, PUSHVAL, 0, value,
NULL, NULL)) { *perrval = NumProgramError; return; }
} else {
/* --- no float constant */
*perrval = NumNoFloatNumber;
}
return;
} /* dpconstant */
void dpvariable( NumProg * program, const char **ps, int *perrval)
{
double *addr;
NumVar *variable;
*perrval = NumSuccess;
if ( !numprog_search_variable ( program, *ps, &variable, 1 ) ) {
addr=&(variable->Value); *ps=*ps+strlen(variable->Key);
variable->Used++;
if (numprog_append_instruction ( program, 0, PUSHADDR, 0, *addr,
addr, NULL )) { *perrval = NumProgramError; return; }
} else {
/* --- undefined variable */
*perrval = NumNoVariable;
}
return;
} /* dpvariable */
/*---------------------------------------------------------------------------
NAME
dpfunction --- Append a function call to program.
SYNOPSI
void dpfunction( NumProg * program, const char **ps,
int level, int *perrval);
DESCRIPTION
Append a function call to program.
To add a new function FX the following steps needs to be done
file numprog.h: Add FX to NumCommand and NumCommandStrings
dpprogram_step: Add an entry of FX to dpprogram_step. All functions
needs to be defined there.
dpfunction: Add the string FX_ to dpfunction. It must be terminated
with '(', e.g. const char *FX_="fx(".
Append the instruction with numprog_append_instruction,
as it is done for the other functions. NArgs must be
exactly the number of arguments that are needed to
calculate the function value.
----------------------------------------------------------------------------*/
void dpfunction( NumProg * program, const char **ps,
int level, int *perrval)
{
const double pi = 3.1415926535897932384626;
// const double degtorad = pi/180.0; // unused
// const double radtodeg = 180.0/pi; // unused
char * RAD_="rad("; char * DEG_="deg("; char * PI_ ="pi(";
char * SIN_="sin("; char * COS_="cos("; char * TAN_="tan(";
char * ASIN_="asin("; char * ACOS_="acos("; char * ATAN_="atan(";
char * ATAN2_="atan2("; char * SINH_="sinh("; char * COSH_="cosh(";
char * TANH_="tanh("; char * FLOOR_="floor("; char * CEIL_="ceil(";
char * FABS_="abs("; char * EXP_="exp("; char * LOG_="log(";
char * LOG10_="log10("; char * POW_="pow("; char * SQRT_="sqrt(";
char * ROUND_="round("; char * GAMMA_="gamma(";
char * FMIN_="min("; char * FMAX_="max(";
char * DEGC2K_="degC2K("; char * K2DEGC_="K2degC(";
char * DEGF2K_="degF2K("; char * K2DEGF_="K2degF(";
char * DEGK2K_="degK2K("; char * K2DEGK_="K2degK(";
char * DEGF2DEGC_="degF2degC("; char * DEGC2DEGF_="degC2degF(";
*perrval = NumSuccess;
/* rad-function */
if (!strncmp(*ps,RAD_,strlen(RAD_))) { *ps+=strlen(RAD_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, RAD, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* rad_ */
/* deg-function */
else if (!strncmp(*ps,DEG_,strlen(DEG_))) { *ps+=strlen(DEG_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, DEG, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* DEG_ */
/* pi-function */
else if (!strncmp(*ps,PI_,strlen(PI_))) { *ps+=strlen(PI_);
if (numprog_append_instruction ( program, 0, PUSHVAL, 0, pi,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* PI_ */
/* sin-function */
else if (!strncmp(*ps,SIN_,strlen(SIN_))) { *ps+=strlen(SIN_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, SIN, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* SIN_ */
/* cos-function */
else if (!strncmp(*ps,COS_,strlen(COS_))) { *ps+=strlen(COS_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, COS, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* COS_ */
/* tan-function */
else if (!strncmp(*ps,TAN_,strlen(TAN_))) { *ps+=strlen(TAN_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, TAN, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* TAN_ */
/* asin-function */
else if (!strncmp(*ps,ASIN_,strlen(ASIN_))) { *ps+=strlen(ASIN_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, ASIN, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* ASIN_ */
/* acos-function */
else if (!strncmp(*ps,ACOS_,strlen(ACOS_))) { *ps+=strlen(ACOS_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, ACOS, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* ACOS_ */
/* atan-function */
else if (!strncmp(*ps,ATAN_,strlen(ATAN_))) { *ps+=strlen(ATAN_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, ATAN, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* ATAN_ */
/* atan2-function */
else if (!strncmp(*ps,ATAN2_,strlen(ATAN2_))) { *ps+=strlen(ATAN2_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if ((**ps)!=',') *perrval = NumCommaExpected; else (*ps)++;
if (*perrval != NumSuccess) return;
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, ATAN2, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* ATAN2_ */
/* sinh-function */
else if (!strncmp(*ps,SINH_,strlen(SINH_))) { *ps+=strlen(SINH_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, SINH, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* SINH_ */
/* cosh-function */
else if (!strncmp(*ps,COSH_,strlen(COSH_))) { *ps+=strlen(COSH_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, COSH, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* COSH_ */
/* tanh-function */
else if (!strncmp(*ps,TANH_,strlen(TANH_))) { *ps+=strlen(TANH_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, TANH, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* TANH_ */
/* floor-function */
else if (!strncmp(*ps,FLOOR_,strlen(FLOOR_))) { *ps+=strlen(FLOOR_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, FLOOR, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* FLOOR_ */
/* ceil-function */
else if (!strncmp(*ps,CEIL_,strlen(CEIL_))) { *ps+=strlen(CEIL_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, CEIL, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* FLOOR_ */
/* fabs-function */
else if (!strncmp(*ps,FABS_,strlen(FABS_))) { *ps+=strlen(FABS_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, FABS, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* FABS_ */
/* exp-function */
else if (!strncmp(*ps,EXP_,strlen(EXP_))) { *ps+=strlen(EXP_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, EXP, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* EXP_ */
/* log-function */
else if (!strncmp(*ps,LOG_,strlen(LOG_))) { *ps+=strlen(LOG_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, LOG, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* LOG_ */
/* log10-function */
else if (!strncmp(*ps,LOG10_,strlen(LOG10_))) { *ps+=strlen(LOG10_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, LOG10, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* LOG10_ */
/* pow-function */
else if (!strncmp(*ps,POW_,strlen(POW_))) { *ps+=strlen(POW_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if ((**ps)!=',') *perrval = NumCommaExpected; else (*ps)++;
if (*perrval != NumSuccess) return;
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, POW, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* POW_ */
/* sqrt-function */
else if (!strncmp(*ps,SQRT_,strlen(SQRT_))) { *ps+=strlen(SQRT_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, SQRT, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* SQRT_ */
/* round-function */
else if (!strncmp(*ps,ROUND_,strlen(ROUND_))) { *ps+=strlen(ROUND_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, ROUND, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* ROUND_ */
/* gamma-function */
else if (!strncmp(*ps,GAMMA_,strlen(GAMMA_))) { *ps+=strlen(GAMMA_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, GAMMA, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* GAMMA_ */
/* min-function */
else if (!strncmp(*ps,FMIN_,strlen(FMIN_))) { *ps+=strlen(FMIN_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if ((**ps)!=',') *perrval = NumCommaExpected; else (*ps)++;
if (*perrval != NumSuccess) return;
EXPRESSION(program,ps,level+1,perrval);
if (numprog_append_instruction ( program, 0, FMIN, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* FMIN_ */
/* max-function */
else if (!strncmp(*ps,FMAX_,strlen(FMAX_))) { *ps+=strlen(FMAX_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if ((**ps)!=',') *perrval = NumCommaExpected; else (*ps)++;
if (*perrval != NumSuccess) return;
EXPRESSION(program,ps,level+1,perrval);
if (numprog_append_instruction ( program, 0, FMAX, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* FMAX_ */
/* degC2K-function */
else if (!strncmp(*ps,DEGC2K_,strlen(DEGC2K_))) { *ps+=strlen(DEGC2K_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, DEGC2K, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* DEGC2K_ */
/* K2degC-function */
else if (!strncmp(*ps,K2DEGC_,strlen(K2DEGC_))) { *ps+=strlen(K2DEGC_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, K2DEGC, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* K2DEGC_ */
/* degF2K-function */
else if (!strncmp(*ps,DEGF2K_,strlen(DEGF2K_))) { *ps+=strlen(DEGF2K_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, DEGF2K, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* DEGF2K_ */
/* K2degF-function */
else if (!strncmp(*ps,K2DEGF_,strlen(K2DEGF_))) { *ps+=strlen(K2DEGF_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, K2DEGF, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* K2DEGF_ */
/* degK2K-function */
else if (!strncmp(*ps,DEGK2K_,strlen(DEGK2K_))) { *ps+=strlen(DEGK2K_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, DEGK2K, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* DEGK2K_ */
/* K2degK-function */
else if (!strncmp(*ps,K2DEGK_,strlen(K2DEGK_))) { *ps+=strlen(K2DEGK_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, K2DEGK, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* K2DEGK_ */
/* degF2degC-function */
else if (!strncmp(*ps,DEGF2DEGC_,strlen(DEGF2DEGC_))) {
*ps+=strlen(DEGF2DEGC_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, DEGF2DEGC, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* DEGF2DEGC_ */
/* degC2degF-function */
else if (!strncmp(*ps,DEGC2DEGF_,strlen(DEGC2DEGF_))) {
*ps+=strlen(DEGC2DEGF_);
EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) return;
if (numprog_append_instruction ( program, 0, DEGC2DEGF, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* DEGC2DEGF_ */
else { /* unknown function */
*perrval = NumNoFloatFunction; return;
}
if ((**ps)!=')') *perrval = NumBadParenthesis; else (*ps)++;
return;
} /* dpfunction */
void dpterm( NumProg * program, const char **ps, int level, int * perrval)
{
*perrval = NumSuccess;
dpfactor0(program,ps,level,perrval);
if (*perrval!=NumSuccess) return;
while (**ps) {
switch (**ps) {
case '*' :
(*ps)++; dpfactor0(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, MUL, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
case '/' :
(*ps)++; dpfactor0(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, DIV, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
case '%' :
(*ps)++; dpfactor0(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, REST, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default :
return;
} /* switch */
if (*perrval!=NumSuccess) return;
} /* while */
return;
} /* dpterm */
// Unary operator
void dpfactor0( NumProg * program, const char **ps,
int level, int * perrval)
{
*perrval = NumSuccess;
switch (**ps) {
case '!' :
(*ps)++; dpfactor1(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, NOT, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default :
dpfactor1(program,ps,level,perrval);
} /* switch */
if (*perrval!=NumSuccess) return;
return;
} /* dpfactor0 */
// Unit multiplicator
void dpfactor1( NumProg * program, const char **ps,
int level, int * perrval)
{
*perrval = NumSuccess;
dpfactor2(program,ps,level,perrval);
if (*perrval!=NumSuccess) return;
while (**ps) {
switch (**ps) {
case '_' :
(*ps)++; dpfactor2(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, MUL, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default :
return;
} /* switch */
if (*perrval!=NumSuccess) return;
} /* while */
return;
} /* dpfactor1 */
// Values
void dpfactor2( NumProg * program, const char **ps,
int level, int * perrval)
{
char * DOUBLE_ = "(double)";
double value;
char *pe;
*perrval = NumSuccess;
switch (**ps) {
case '(' :
/* --- (double) */
if (!strncmp(*ps,DOUBLE_,strlen(DOUBLE_))) {
*ps=*ps+strlen(DOUBLE_);
value = (double) lvfactor(ps,level,perrval);
if (*perrval != NumSuccess) break;
if (numprog_append_instruction ( program, 0, PUSHVAL, 0, value,
NULL, NULL )) { *perrval = NumProgramError; return; }
break;}
/* --- expression */
(*ps)++; EXPRESSION(program,ps,level+1,perrval);
if (*perrval != NumSuccess) break;
if ((**ps)!=')') *perrval = NumBadParenthesis; else (*ps)++;
break;
default :
/* --- number */
if ( (('0'<=**ps)&&(**ps<='9'))||('.'==**ps) ) {
value = strtod(*ps,&pe);
*ps = (const char *) pe;
if (numprog_append_instruction ( program, 0, PUSHVAL, 0, value,
NULL, NULL )) { *perrval = NumProgramError; return; }
} /* number */
/* --- function */
else if (isfunction(*ps)) {
dpfunction(program,ps,level,perrval);
if (*perrval != NumSuccess) break;
} /* function */
/* --- variable */
else if (isvariable(*ps)) {
(*ps)++; /* skip underscore */
dpvariable(program,ps,perrval);
if (*perrval != NumSuccess) break;
} /* variable */
/* --- constant */
else { dpconstant(program,ps,perrval);
if (*perrval != NumSuccess) break;
} /* constant */
break;
} /* switch */
if (*perrval!=NumSuccess) return;
return;
} /* dpfactor2 */
void dpexpression( NumProg * program, const char **ps,
int level, int * perrval)
{
*perrval = NumSuccess;
switch (**ps) {
case '+':
(*ps)++; dpterm(program,ps,level,perrval);
break;
case '-':
(*ps)++; dpterm(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, NEG, 1, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default :
dpterm(program,ps,level,perrval);
break;
} /* switch */
if (*perrval!=NumSuccess) return;
while (**ps) {
switch (**ps) {
case '+':
(*ps)++; dpterm(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, ADD, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
case '-':
(*ps)++; dpterm(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, SUB, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
case ')':
if (level<=0) *perrval = NumBadParenthesis;
return;
// case ';':
// case ',':
// return;
default :
return; // Return without error
// if (isspace(**ps)) { //++++++++++++++++++
// return;
// }
// *perrval = NumScanError; break;
} /* switch */
if (*perrval!=NumSuccess) return;
} /* while */
return;
} /* dpexpression */
// condition = logicsum ["?" logicsum ":" logicsum ]
void dpcondition( NumProg * program, const char **ps,
int level, int * perrval)
{ *perrval = NumSuccess;
dplogicsum(program,ps,level,perrval);
if (*perrval != NumSuccess) return;
switch (**ps) {
case '?':
(*ps)++; dplogicsum(program,ps,level,perrval);
if (*perrval != NumSuccess) return;
switch (**ps) {
case ':':
(*ps)++; dplogicsum(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, IF, 3, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default:
*perrval = NumScanError;
} // switch
default: ; // continue
} // switch
if (*perrval != NumSuccess) return;
return;
} // dpcondition
// logicsum = logicproduct { || logicproduct }
void dplogicsum( NumProg * program, const char **ps,
int level, int * perrval)
{ const char *pps;
*perrval = NumSuccess;
dplogicproduct(program,ps,level,perrval);
if (*perrval != NumSuccess) return;
while (**ps) {
pps = *ps;
switch (*pps) {
case '|': (pps)++;
switch (*pps) {
case '|': (pps)++; // "||" OR
*ps = pps; dplogicproduct(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, OR, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default: return;
}
break;
default: return;
} // switch
if (*perrval != NumSuccess) return;
} // while
return;
} // dplogicsum
// logicproduct = equality { && equality }
void dplogicproduct( NumProg * program, const char **ps,
int level, int * perrval)
{ const char *pps;
*perrval = NumSuccess;
dpequality(program,ps,level,perrval);
if (*perrval != NumSuccess) return;
while (**ps) {
pps = *ps;
switch (*pps) {
case '&': (pps)++;
switch (*pps) {
case '&': (pps)++; // "&&" AND
*ps = pps; dpequality(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, AND, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default: return;
}
break;
default: return;
} // switch
if (*perrval != NumSuccess) return;
} // while
return;
} // dplogicproduct
// equality = comparison { "==" | "!=" comparison }
void dpequality( NumProg * program, const char **ps,
int level, int * perrval)
{ const char *pps;
*perrval = NumSuccess;
dpcomparison(program,ps,level,perrval);
if (*perrval != NumSuccess) return;
pps = *ps;
switch (*pps) {
case '=': (pps)++;
switch (*pps) {
case '=': (pps)++; // "==" EQ
*ps = pps; dpcomparison(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, EQU, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default: ; // continue
}
break;
case '!': (pps)++;
switch (*pps) {
case '=': (pps)++; // "==" NE
*ps = pps; dpcomparison(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, NEQ, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default: ; // continue
}
default: ; // continue
} // switch
return;
} // dpequality
// comparison = expression { "<" | "<=" | ">" | ">=" expression }
void dpcomparison( NumProg * program, const char **ps,
int level, int * perrval)
{ const char *pps;
*perrval = NumSuccess;
dpexpression(program,ps,level,perrval);
if (*perrval != NumSuccess) return;
pps = *ps;
switch (*pps) {
case '<': (pps)++;
switch (*pps) {
case '=': (pps)++; // "<=" LE
*ps = pps; dpexpression(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, LE, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default: // "<" LT
*ps = pps; dpexpression(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, LT, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
} // switch
break;
case '>': (pps)++;
switch (*pps) {
case '=': (pps)++; // ">=" GE
*ps = pps; dpexpression(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, GE, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
default: // ">" LT
*ps = pps; dpexpression(program,ps,level,perrval);
if (*perrval==NumSuccess) {
if (numprog_append_instruction ( program, 0, GT, 2, 0,
NULL, NULL )) { *perrval = NumProgramError; return; }
}
break;
} // switch
break;
} // switch
return;
} // dpcomparison
/*--------------------------------------------------------------------------
num_str2double : reads an expression of the type
EXPRESSION = condition
condition = logicsum ["?" logicsum ":" logicsum ]
logicsum = logicproduct { "||" logicproduct }
logicproduct = equality { "&&" equality }
equality = comparison ["=="|"!=" comparison]
comparison = expression ["<"|"<="|">"|">=" expression]
expression = ["+"|"-"] term {"+"|"-" term}
term = factor1 {"*"|"/"|"%" factor1}
factor0 = ["!"] factor1
factor1 = factor2 {"_" factor2}
factor2 = number | function | constant | "(" EXPRESSION ")"
| "(double)" lvfactor | variable
number = double precision type floating point number
function = name "(" expression {"," expression} ")"
name = "a"|..|"z" {"a"|..|"z"|"0"|..|"9"}
constant = unit | name
unit = name
variable = "_" name
The conversion stops at at the end of the string, a white space,
a comma or a semicolon. If tail is not NULL the pointer to the
remaining string, including the terminating character, is returned
in *tail.
If the evaluated expression is not complete or faulty, an
error value is returned.
A temporary program with the name 'str2double' is created. It is
exectuted after successful creation. The resulting value is
returned. The program is deleted after the run.
return float value (o) : value of expression
const char *str (i) : input string
char **tail (o) : pointer to rest string if tail!=NULL
int * perrval (o) : output status
NumSuccess : successful conversion
NumBadParenthesis : wrong number of parentheses
NumNoFloatNumber : mysterious character found
NumDomainError :
etc.
--------------------------------------------------------------------------*/
PUBLIC double num_str2double(const char *str, const char **tail, int *perrval)
{
int errval;
double value=0.0;
const char * ps="";
NumProg * program;
if (str) ps = str;
errval = NumSuccess;
while (isspace(*ps)) ps++; // skip leading white space
if ( !(program = numprog_new( "str2double" )) ) errval = NumProgramError;
if (!errval) EXPRESSION( program, &ps, 0 , &errval);
if (tail) *tail = ps;
if (!errval) dpprogram_run ( program, &errval );
if (!errval) value = program->CurrentAccumulator->Value;
if (errval>=NumProgramError) num_prog_print_list ( stderr, program, 2, 0 );
else if (NUMIO_debug>2) num_prog_print_list ( stdout, program, 2, 0 );
if (numprog_free( program )) errval = NumProgramError;
if (perrval) *perrval=errval;
return( value );
} /* num_str2double */
/*--------------------------------------------------------------------------
num_str2prog : Converts an expression with nvar variables to a program
that can be exectuted with num_runprog. The syntax
is like it is described for num_str2double.
Constant expressions are evaluated.
Exactly nvar variable names must be given.
If not, random errors will occur.
return NumProg * (o) : pointer to the program
const char *name (i) : program name
const char *str (i) : input string
char **tail (o) : pointer to rest string if tail!=NULL
int * perrval (o) : output status
int nvar (i) : number of variables
const char *nam1,
... (i) : names of the variables
--------------------------------------------------------------------------*/
PUBLIC NumProg * num_str2prog( const char *name,
const char *str, const char **tail, int *perrval,
int nvar, ... )
{
int errval;
va_list ap;
const char * vname;
int n;
const char * ps="";
NumProg * program;
if (str) ps = str;
errval = NumSuccess;
while (isspace(*ps)) ps++; // skip leading white space
if ( !(program = numprog_new( name )) ) errval = NumProgramError;
if (!errval) {
va_start(ap, nvar);
for (n=0;n<nvar;n++) {
vname = va_arg(ap, const char *);
if (!errval)
if (numprog_append_variable ( program, vname, 1.0, NULL ))
errval = NumVariableError;
}
va_end(ap);
}
if (!errval) EXPRESSION( program, &ps, 0 , &errval);
if (tail) *tail = ps;
if (!errval) dpprogram_compile ( program, &errval );
if (errval>=NumProgramError) num_prog_print_list ( stderr, program, 2, 0 );
else if (NUMIO_debug>2) num_prog_print_list ( stdout, program, 2, 0 );
if (errval) {
if (numprog_free( program )) errval = NumProgramError;
program = (NumProg *) NULL;
}
if (perrval) *perrval = errval;
return( program );
} /* num_str2prog */
/*--------------------------------------------------------------------------
num_chkvar : Returns, how often the n-th variable is used in program.
It returns the Used-flag of the n-th variable.
return int (o) : 0: does not depend on variable n
1: depends on variable n
NumProg * (i) : program pointer created with num_str2prog4
int n (i) : variable number (according to num_str2prog)
int * perrval (o) : output status
--------------------------------------------------------------------------*/
PUBLIC int num_chkvar ( NumProg * program, int n, int *perrval )
{
int errval;
NumVar * variable = (NumVar *) NULL;
int used = 0;
int i;
errval = NumSuccess;
if (!program) errval=NumProgramError;
if (!errval) {
variable=program->VariableList;
/* go to n-th variable */
for (i=1;i<n;i++) {
if (variable) variable=variable->Next;
else break;
}
if (variable) used = variable->Used;
else errval=NumNoVariable;
}
if (perrval) *perrval=errval;
return( used );
} /* num_chkvar */
/*--------------------------------------------------------------------------
num_runprog : Runs a program with nvar variables that was created with
num_str2prog and returns the result.
return double (o) : evaluated value
NumProg * (i) : program pointer created with num_str2prog4
int * perrval (o) : output status
double var1,
... (i) : variable values. They must be given in the
same order as they have been defined with
num_str2prog. Exactly the in num_str2prog
defined number of variables values must
be given. If not, random errors will occur.
ATTENTION: The types of var1, ... are not
known to the compiler, values are not casted
automatically to double.
--------------------------------------------------------------------------*/
PUBLIC double num_runprog( NumProg * program, int *perrval, ... )
{
int errval;
va_list ap;
double var;
NumVar * variable = (NumVar *) NULL;
double value=0.0;
errval = NumSuccess;
if (!program) errval=NumProgramError;
if (!errval) {
variable = program->VariableList;
va_start(ap, perrval); // perrval is the last argument before ...
while (variable) {
var = va_arg(ap, double);
variable->Value = var; variable=variable->Next;
}
va_end(ap);
}
if (!errval) dpprogram_run ( program, &errval );
if (!errval) value = program->CurrentAccumulator->Value;
if (errval>=NumProgramError) num_prog_print_list ( stderr, program, 2, 0 );
else if (NUMIO_debug>2) num_prog_print_list ( stdout, program, 2, 0 );
if (perrval) *perrval=errval;
return( value );
} /* num_runprog */
/*--------------------------------------------------------------------------
num_searchprog: Returns the pointer of a program
return NumProg * (o) : pointer to the program or
null-pointer if it was not found
const char * name (i) : program name
int * perrval (o) : output status
Attention: *perrval indicates errors only. It does not indicate
whether the program was found.
--------------------------------------------------------------------------*/
PUBLIC NumProg *num_searchprog ( const char *name, int *perrval )
{
int errval;
NumProg *program;
errval = NumSuccess;
if ( numprog_search ( name, &program ) ) errval=NumProgramError;
if (perrval) *perrval=errval;
return( program );
} /* num_searchprog */
/*--------------------------------------------------------------------------
num_rmprog : Removes the specified program or all defined programs, if
the null-pointer is given
return int (o) : 0: Success, -1: error
NumProg * (i) : pointer to the program that should be remoed,
or null-pointer to remove all programs
int * perrval (o) : output status
--------------------------------------------------------------------------*/
PUBLIC int num_rmprog( NumProg * program, int *perrval )
{
int errval;
int status;
errval = NumSuccess;
if ( (status = numprog_free( program )) ) errval = NumProgramError;
if (perrval) *perrval=errval;
return( status );
} /* num_rmprog */
long int lvterm( const char **ps, int level, int * perrval)
{
long int value;
long int divisor;
*perrval = NumSuccess;
value = lvfactor(ps,level,perrval);
if (*perrval!=NumSuccess) return ( value );
while (**ps) {
switch (**ps) {
case '*' :
(*ps)++; value *= lvfactor(ps,level,perrval); break;
case '/' :
(*ps)++; divisor = lvfactor(ps,level,perrval);
if ( divisor != 0l) value /= divisor;
else *perrval=NumDivByZero;
break;
case '%' :
(*ps)++; divisor = lvfactor(ps,level,perrval);
if ( divisor != 0l) value %= divisor;
else *perrval=NumDivByZero;
break;
default :
return ( value );
} /* switch */
if (*perrval!=NumSuccess) return ( value );
} /* while */
return ( value );
} /* lvterm */
long int lvfactor( const char **ps, int level, int * perrval)
{
const double long_max = (double) LONG_MAX;
const double long_min = (double) LONG_MIN;
NumProg * program;
double dpargument;
char *pe;
long int value;
char * TRUE_ = "true"; char * FALSE_ = "false";
char * YES_ = "yes"; char * NO_ = "no";
char * LONG_ = "(long int)"; char * ROUND_ = "(round)";
*perrval = NumSuccess;
value = 1l;
switch (**ps) {
case '(' :
/* --- (long int) */
if (!strncmp(*ps,LONG_,strlen(LONG_))) {
*ps=*ps+strlen(LONG_);
/* Temporary program required to get dpfactor1 result */
if (!( program = numprog_new( "lvfactor" ) ) ) {
*perrval = NumProgramError; break;
}
dpfactor1(program,ps,level,perrval);
if (*perrval!=NumSuccess) {
numprog_free( program ); break;
}
dpprogram_run ( program, perrval );
if (*perrval!=NumSuccess) {
numprog_free( program ); break;
}
dpargument = program->CurrentAccumulator->Value;
if (numprog_free( program )) {
*perrval = NumProgramError; break;
}
if ((long_min <= dpargument) && (dpargument <= long_max))
value=(long int) dpargument;
else *perrval = NumIntegerOverflow;
break; }
if (!strncmp(*ps,ROUND_,strlen(ROUND_))) {
*ps=*ps+strlen(ROUND_);
/* Temporary program required to get dpfactor1 result */
if (!( program = numprog_new( "lvfactor" ) ) ) {
*perrval = NumProgramError; break;
}
dpfactor1(program,ps,level,perrval);
if (*perrval!=NumSuccess) {
numprog_free( program ); break;
}
dpprogram_run ( program, perrval );
if (*perrval!=NumSuccess) {
numprog_free( program ); break;
}
dpargument = program->CurrentAccumulator->Value;
if (numprog_free( program )) {
*perrval = NumProgramError; break;
}
dpargument = floor(dpargument+0.5);
if ((long_min <= dpargument) && (dpargument <= long_max))
value=(long int) dpargument;
else *perrval = NumIntegerOverflow;
break; }
/* --- expression */
(*ps)++; value *= lvexpression(ps,level+1,perrval);
if (*perrval != NumSuccess) break;
if ((**ps)!=')') *perrval = NumBadParenthesis; else (*ps)++;
break;
default :
/* --- number */
if (('0'<=**ps) && (**ps<='9')) {
value=strtol(*ps,&pe,10);
*ps = (const char *) pe;
}
/* --- function */
else if (isfunction(*ps)) {
/* Temporary program required to get dpfunction result */
if (!( program = numprog_new( "lvfactor" ) ) ) {
*perrval = NumProgramError; break;
}
dpfunction(program,ps,level,perrval);
if (*perrval!=NumSuccess) {
numprog_free( program ); break;
}
dpprogram_run ( program, perrval );
if (*perrval!=NumSuccess) {
numprog_free( program ); break;
}
dpargument = program->CurrentAccumulator->Value;
if (numprog_free( program )) {
*perrval = NumProgramError; break;
}
dpargument = floor(dpargument+0.5);
if ((long_min <= dpargument) && (dpargument <= long_max))
value=(long int) dpargument; else *perrval = NumIntegerOverflow;
if (*perrval != NumSuccess) break;
} /* function */
/* --- constant */
else if (!num_strncasecmp(*ps,TRUE_,strlen(TRUE_))) {
value=1l; *ps=*ps+strlen(TRUE_);}
else if (!num_strncasecmp(*ps,FALSE_,strlen(FALSE_))) {
value=0l; *ps=*ps+strlen(FALSE_);}
else if (!num_strncasecmp(*ps,YES_,strlen(YES_))) {
value=1l; *ps=*ps+strlen(YES_);}
else if (!num_strncasecmp(*ps,NO_,strlen(NO_))) {
value=0l; *ps=*ps+strlen(NO_);}
else {
/* Temporary program required to get dpconstant result */
if (!( program = numprog_new( "lvfactor" ) ) ) {
*perrval = NumProgramError; break;
}
dpconstant(program,ps,perrval);
if (*perrval!=NumSuccess) {
numprog_free( program ); break;
}
dpprogram_run ( program, perrval );
if (*perrval!=NumSuccess) {
numprog_free( program ); break;
}
dpargument = program->CurrentAccumulator->Value;
if (numprog_free( program )) {
*perrval = NumProgramError; break;
}
dpargument = floor(dpargument+0.5);
if ((long_min <= dpargument) && (dpargument <= long_max))
value=(long int) dpargument; else *perrval = NumIntegerOverflow;
if (*perrval != NumSuccess) break;
}
/* --- exit */
break;
} /* switch */
if (*perrval!=NumSuccess) return ( value );
return( value );
} /* lvfactor */
long int lvexpression( const char **ps, int level, int * perrval)
{ long int value;
*perrval = NumSuccess;
value = 0l;
switch (**ps) {
case '+':
(*ps)++; value += lvterm(ps,level,perrval); break;
case '-':
(*ps)++; value -= lvterm(ps,level,perrval); break;
default :
value += lvterm(ps,level,perrval); break;
} /* switch */
if (*perrval!=NumSuccess) return ( value );
while (**ps) {
switch (**ps) {
case '+':
(*ps)++; value += lvterm(ps,level,perrval); break;
case '-':
(*ps)++; value -= lvterm(ps,level,perrval); break;
case ')':
if (level<=0) *perrval = NumBadParenthesis;
return( value );
case ';':
case ',':
return( value );
default :
if (isspace(**ps)) return( value );
*perrval = NumScanError; break;
} /* switch */
if (*perrval!=NumSuccess) return ( value );
} /* while */
return(value);
} /* lvexpression */
/*--------------------------------------------------------------------------
num_str2long : reads an expression of the type
lvexpression = ["+"|"-"] lvterm {"+"|"-" lvterm}
lvterm = lvfactor {"*"|"/" lvfactor}
lvfactor = lvnumber | lvconstant |"(" lvexpression ")"
| dpfunction | "(long int)" dpfactor
| "(round)" dpfactor1 | dpconstant
lvnumber = long integer type number
lvconstant = "true" | "false" | "yes" | "no"
dpfunction = see above
dpfactor1 = see above
The result of dpfunction is rounded to the closest long integer value.
The conversion stops at at the end of the string, a white space,
a comma or a semicolon. If tail is not NULL the pointer to the
remaining string, including the terminating character, is returned
in *tail.
If the evaluated expression is not complete or faulty, an
error value is returned.
return long value (o) : value of expression
const char *str (i) : input string
char **tail (o) : pointer to rest string if tail!=NULL
int * perrval (o) : output status
NumSuccess : successful conversion
NumBadParenthesis : wrong number of parentheses
NumNoIntegerNumber : mysterious character found
--------------------------------------------------------------------------*/
PUBLIC long num_str2long(const char *str, const char **tail, int *perrval)
{
int errval;
long int value;
const char * ps="";
if (str) ps = str;
while (isspace(*ps)) ps++; // skip leading white space
value = lvexpression( &ps, 0 , &errval);
if (tail) *tail = ps;
if (perrval) *perrval=errval;
return( value );
} /* num_str2long */
/*--------------------------------------------------------------------------
num_long2str : writes a long value to buffer
return char * (o) : pointer to buffer,
char * NULL in case of an error
unsigned long buflen (i) : buffer length (includes terminating NULL)
long value (i) : value to write
int * perrval (o) : 0 Success, otherwise error
--------------------------------------------------------------------------*/
PUBLIC char *num_long2str( char buffer[], unsigned long buflen,
long value, int * perrval )
{
int errval;
char tmp[128];
errval = NumWriteError;
if ( sprintf(tmp,"%ld", value ) < 1 ) goto num_long2str_error;
strncpy( buffer, tmp, buflen-1 );
buffer[buflen-1] = '\0';
errval = NumSuccess;
if (perrval) *perrval=errval;
return( buffer );
num_long2str_error:
if (perrval) *perrval=errval;
return( (char *) NULL );
} /* num_long2str */
/*--------------------------------------------------------------------------
num_long2hex : writes a long value hexadecimal to buffer
return char * (o) : pointer to buffer,
char * NULL in case of an error
unsigned long buflen (i) : buffer length (includes terminating NULL)
long value (i) : value to write
int * perrval (o) : 0 Success, otherwise error
--------------------------------------------------------------------------*/
PUBLIC char *num_long2hex( char buffer[], unsigned long buflen,
long value, int * perrval )
{
int errval;
char tmp[128];
errval = NumWriteError;
if ( sprintf(tmp,"0x%lx", value ) < 1 ) goto num_long2hex_error;
strncpy( buffer, tmp, buflen-1 );
buffer[buflen-1] = '\0';
errval = NumSuccess;
if (perrval) *perrval=errval;
return( buffer );
num_long2hex_error:
if (perrval) *perrval=errval;
return( (char *) NULL );
} /* num_long2hex */
/*---------------------------------------------------------------------------
double2s( buffer, value, ndigits )
Conversion of double to string and output to buffer. The pointer to
buffer is returned. In case of an error the null pointer is returned.
The length of buffer must be 32 or larger.
Only the absolute value of ndigits is used.
---------------------------------------------------------------------------*/
char * double2s( char buffer[], double value, int ndigits )
# define FORMAT_LEN 20
{ char format[FORMAT_LEN];
if (ndigits<0) ndigits=-ndigits;
if (ndigits>80) ndigits=80;
if (ndigits==0) sprintf(format,"%%lg");
else sprintf(format,"%%.%dlg",ndigits);
if ( sprintf(buffer,format,value)<1) return((char *) NULL);
return(buffer);
} /* double2s */
/*--------------------------------------------------------------------------
num_double2str : writes a float value with unit into buffer
The input value must have a normalized form, e.g. it
must be given in meters, rad, seconds or Joule.
The value is expressed relative to uniti,
i.e. if value is 1 and unit is "mm" the
output string will be "1000_mm" = (1/mm)_mm.
return char * (o) : pointer to buffer,
char * NULL in case of an error
unsigned long buflen (i) : buffer length (includes terminating NULL)
double value (i) : value to write
const char * unit (i) : unit to use or empty string or NULL
int ndigits (i) : number of digits to write
int * perrval (o) : 0 Success, otherwise error
--------------------------------------------------------------------------*/
PUBLIC char *num_double2str( char buffer[], unsigned long buflen,
double value, const char * unit, int ndigits,
int * perrval )
{
int errval;
char tmp[128], *tmp_unit;
double val, unit_val, tmp_unit_val;
if ((unit) && (strlen(unit)>0)) {
// get unit
unit_val = num_str2double( unit, NULL, &errval );
if (errval) goto num_double2str_error;
errval = NumDivByZero;
if ( unit_val == 0.0 ) goto num_double2str_error;
val = value/unit_val;
// test unit (0 multiplied with unit)
errval = NumMemoryAllocationError;
tmp_unit = (char *) malloc( sizeof(char)*(strlen(unit)+3) );
if (!tmp_unit) goto num_double2str_error;
sprintf(tmp_unit,"0_%s",unit);
tmp_unit_val = num_str2double( tmp_unit, NULL, &errval );
free(tmp_unit);
if (errval) goto num_double2str_error;
// write val to tmp
errval = NumWriteError;
if ( !double2s( tmp, val, ndigits ) ) goto num_double2str_error;
// copy tmp and unit to buffer
errval = NumWriteError;
if (tmp_unit_val == 0.0 ) {
if ( buflen < ( strlen(tmp)+strlen(unit)+2 ) ) goto num_double2str_error;
if ( sprintf(buffer,"%s_%s", tmp, unit ) < 2 ) goto num_double2str_error;
} else {
if ( buflen < ( strlen(tmp)+strlen(unit)+4 ) ) goto num_double2str_error;
if ( sprintf(buffer,"%s_(%s)", tmp,unit ) < 2 ) goto num_double2str_error;
}
} else {
// write value to tmp with ndigits
errval = NumWriteError;
if ( !double2s( tmp, value, ndigits ) ) goto num_double2str_error;
// copy tmp to buffer
strncpy( buffer, tmp, buflen-1 );
buffer[buflen-1] = '\0';
}
errval = NumSuccess;
if (perrval) *perrval=errval;
return( buffer );
num_double2str_error:
if (perrval) *perrval=errval;
return( (char *) NULL );
} /* num_double2str */
/*--------------------------------------------------------------------------
num_double2hex : rounds a double value and writes it hexadecimal to buffer
(without unit, without decimals)
If ndigits is negative it is only used for negative
values.
return char * (o) : pointer to buffer,
char * NULL in case of an error
unsigned long buflen (i) : buffer length (includes terminating NULL)
double value (i) : value to write
int ndigits (i) : number of digits to write
int * perrval (o) : 0 Success, otherwise error
--------------------------------------------------------------------------*/
PUBLIC char *num_double2hex( char buffer[], unsigned long buflen,
double value, int ndigits, int * perrval )
{
int errval;
char *ps;
double nhex, hex, rest, m;
double base=16.0;
int sign;
errval = NumWriteError;
if (buflen<4) goto num_double2hex_error; // too short for "0x0\n"
ps = &(buffer[0]);
*ps='0';ps++;
*ps='x';ps++;
if (value<0.0) {
// use complement
rest = -(value+1.0); sign=-1;
} else {
rest = value; sign=+1.0;
}
if (rest>0.0)
hex = pow(base,floor(log(rest)/log(base)));
else hex = 1.0;
if ((value<0.0)||(ndigits>0)) {
if (fabs(ndigits)>1) nhex = pow(base,fabs(ndigits)-1.0);
else nhex = 1.0;
if (hex<nhex) hex=nhex;
}
while ((hex>=1.0)&&(ps<buffer+buflen-1)) {
m = floor(rest/hex);
rest -= m*hex ;
if (sign<0) m = base-m-1.0; // use complement
if ((0.0<=m)&&(m<=base)) {
switch ( (int) m ) {
case 0: *ps = '0'; break;
case 1: *ps = '1'; break;
case 2: *ps = '2'; break;
case 3: *ps = '3'; break;
case 4: *ps = '4'; break;
case 5: *ps = '5'; break;
case 6: *ps = '6'; break;
case 7: *ps = '7'; break;
case 8: *ps = '8'; break;
case 9: *ps = '9'; break;
case 10: *ps = 'a'; break;
case 11: *ps = 'b'; break;
case 12: *ps = 'c'; break;
case 13: *ps = 'd'; break;
case 14: *ps = 'e'; break;
case 15: *ps = 'f'; break;
}
ps++;
} else break;
hex /= base;
}
*ps = '\0';
errval = NumSuccess;
if (perrval) *perrval = errval;
return( buffer );
num_double2hex_error:
if (perrval) *perrval = errval;
return( (char *) NULL );
} /* num_double2hex */
/*--------------------------------------------------------------------------
num_errval2str : writes error message into a buffer
char buffer[] (i) : output buffer for message
unsigned long buflen (i) : length of buffer (including terminating NULL)
int errval (i) : error message to write
return char * (o) : pointer to buffer
NumSuccess, NumNoFloatNumber, NumDomainError, NumCommaExpected
NumBadParenthesis, NumDivByZero, NumMemoryAllocationError
NumIntegerOverflow, NumNoIntegerNumber, NumNoFloatFunction,
NumScanError
--------------------------------------------------------------------------*/
PUBLIC char *num_errval2str( char buffer[], unsigned long buflen, int errval )
{
char * value;
value=buffer;
if ( (buffer)&&(buflen>0) ) {
switch (errval) {
case NumSuccess :
strncpy(buffer,"success",buflen-1);
buffer[buflen-1]='\0';
break;
case NumMemoryAllocationError :
strncpy(buffer,"memory allocation failed",buflen-1);
buffer[buflen-1]='\0';
break;
case NumScanError :
strncpy(buffer,"error scanning expression",buflen-1);
buffer[buflen-1]='\0';
break;
case NumCommaExpected :
strncpy(buffer,"missing comma in expression",buflen-1);
buffer[buflen-1]='\0';
break;
case NumBadParenthesis :
strncpy(buffer,"bad parenthesis in expression",buflen-1);
buffer[buflen-1]='\0';
break;
case NumNoFloatNumber :
strncpy(buffer,"expression is not a float number",buflen-1);
buffer[buflen-1]='\0';
break;
case NumNoFloatFunction :
strncpy(buffer,"unknown float function in expression",buflen-1);
buffer[buflen-1]='\0';
break;
case NumDomainError :
strncpy(buffer,"domain error",buflen-1);
buffer[buflen-1]='\0';
break;
case NumNoIntegerNumber :
strncpy(buffer,"expression is not an integer number",buflen-1);
buffer[buflen-1]='\0';
break;
case NumIntegerOverflow :
strncpy(buffer,"integer overflow",buflen-1);
buffer[buflen-1]='\0';
break;
case NumDivByZero :
strncpy(buffer,"division by zero",buflen-1);
buffer[buflen-1]='\0';
break;
case NumWriteError :
strncpy(buffer,"error writing value",buflen-1);
buffer[buflen-1]='\0';
break;
case NumProgramError :
strncpy(buffer,"error creating program",buflen-1);
buffer[buflen-1]='\0';
break;
case NumNoVariable :
strncpy(buffer,"undefined variable",buflen-1);
buffer[buflen-1]='\0';
break;
case NumNoInstruction :
strncpy(buffer,"unknown program instruction",buflen-1);
buffer[buflen-1]='\0';
break;
case NumNoAccumulator :
strncpy(buffer,"not enough program registers",buflen-1);
buffer[buflen-1]='\0';
break;
default:
strncpy(buffer,"unknown error value",buflen-1);
buffer[buflen-1]='\0';
} // switch
} else value=NULL;
return( value );
} /* num_errval2str */
/*---------------------------------------------------------------------------
NAME
num_strncasecmp --- strncasecmp
SYNOPSIS
int num_strncasecmp(const char *s1, const char *s2, size_t n);
DESCRIPTION
The function compares the first n characters ot the two
strings s1 and s2, ignoring the case of the characters.
It returns an integer less than, equal to, or greater than
zero if s1 is less than, to matches, or is greater than s2.
It can be used instead of the function strncasecmp if this
function is not available.
RETURN VALUE
The function returns an integer less than, equal to, or
greater than zero.
---------------------------------------------------------------------------*/
int num_strncasecmp(const char *s1, const char *s2, size_t n)
{ int value;
size_t s1len, s2len;
char *_s1, *_s2;
register unsigned int i;
s1len = strlen(s1);
s1len = (s1len<n)?s1len:n;
_s1 = (char *) malloc( s1len+1 );
for (i=0;i<s1len;i++) _s1[i]=(char)tolower(s1[i]);
_s1[s1len]='\0';
s2len = strlen(s2);
s2len = (s2len<n)?s2len:n;
_s2 = (char *) malloc( s2len+1 );
for (i=0;i<s2len;i++) _s2[i]=(char)tolower(s2[i]);
_s2[s2len]='\0';
value = strcmp(_s1,_s2);
free(_s2);
free(_s1);
return(value);
} /* num_strncasecmp */
/*--------------------------------------------------------------------------*/
/* char *numio_version : returns pointer to the version string */
/*--------------------------------------------------------------------------*/
PUBLIC char *numio_version ( void )
{ return ( NUMIO_VERSION );
} /* numio_version */
/*--------------------------------------------------------------------------*/
/* void numio_debug : selects debug mode 0|1|2 */
/* 0: off, 1: show constant replacement, 2: + show initialization */
/*--------------------------------------------------------------------------*/
PUBLIC extern void numio_debug ( int debug )
{ NUMIO_debug = debug;
} /* numio_debug */
/****************************************************************************/
|