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
|
/*
* tclClockFmt.c --
*
* Contains the date format (and scan) routines. This code is back-ported
* from the time and date facilities of tclSE engine, by Serg G. Brester.
*
* Copyright (c) 2015 by Sergey G. Brester aka sebres. All rights reserved.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#include "tclStrIdxTree.h"
#include "tclDate.h"
/*
* Miscellaneous forward declarations and functions used within this file
*/
static void ClockFmtObj_DupInternalRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr);
static void ClockFmtObj_FreeInternalRep(Tcl_Obj *objPtr);
static int ClockFmtObj_SetFromAny(Tcl_Interp *, Tcl_Obj *objPtr);
static void ClockFmtObj_UpdateString(Tcl_Obj *objPtr);
static Tcl_HashEntry * ClockFmtScnStorageAllocProc(Tcl_HashTable *, void *keyPtr);
static void ClockFmtScnStorageFreeProc(Tcl_HashEntry *hPtr);
static void ClockFmtScnStorageDelete(ClockFmtScnStorage *fss);
TCL_DECLARE_MUTEX(ClockFmtMutex); /* Serializes access to common format list. */
#ifndef TCL_CLOCK_FULL_COMPAT
#define TCL_CLOCK_FULL_COMPAT 1
#endif
/*
* Derivation of tclStringHashKeyType with extra memory management trickery.
*/
static const Tcl_HashKeyType ClockFmtScnStorageHashKeyType = {
TCL_HASH_KEY_TYPE_VERSION, /* version */
0, /* flags */
TclHashStringKey, /* hashKeyProc */
TclCompareStringKeys, /* compareKeysProc */
ClockFmtScnStorageAllocProc, /* allocEntryProc */
ClockFmtScnStorageFreeProc /* freeEntryProc */
};
#define IntFieldAt(info, offset) \
((int *) (((char *) (info)) + (offset)))
#define WideFieldAt(info, offset) \
((Tcl_WideInt *) (((char *) (info)) + (offset)))
/*
* Clock scan and format facilities.
*/
/*
*----------------------------------------------------------------------
*
* Clock_str2int, Clock_str2wideInt --
*
* Fast inline-convertion of string to signed int or wide int by given
* start/end.
*
* The given string should contain numbers chars only (because already
* pre-validated within parsing routines)
*
* Results:
* Returns a standard Tcl result.
* TCL_OK - by successful conversion, TCL_ERROR by (wide) int overflow
*
*----------------------------------------------------------------------
*/
static inline void
Clock_str2int_no(
int *out,
const char *p,
const char *e,
int sign)
{
/* assert(e <= p + 10); */
int val = 0;
/* overflow impossible for 10 digits ("9..9"), so no needs to check at all */
while (p < e) { /* never overflows */
val = val * 10 + (*p++ - '0');
}
if (sign < 0) {
val = -val;
}
*out = val;
}
static inline void
Clock_str2wideInt_no(
Tcl_WideInt *out,
const char *p,
const char *e,
int sign)
{
/* assert(e <= p + 18); */
Tcl_WideInt val = 0;
/* overflow impossible for 18 digits ("9..9"), so no needs to check at all */
while (p < e) { /* never overflows */
val = val * 10 + (*p++ - '0');
}
if (sign < 0) {
val = -val;
}
*out = val;
}
/* int & Tcl_WideInt overflows may happens here (expected case) */
#if (defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__)
# pragma GCC optimize("no-trapv")
#endif
static inline int
Clock_str2int(
int *out,
const char *p,
const char *e,
int sign)
{
int val = 0;
/* overflow impossible for 10 digits ("9..9"), so no needs to check before */
const char *eNO = p + 10;
if (eNO > e) {
eNO = e;
}
while (p < eNO) { /* never overflows */
val = val * 10 + (*p++ - '0');
}
if (sign >= 0) {
while (p < e) { /* check for overflow */
int prev = val;
val = val * 10 + (*p++ - '0');
if (val / 10 < prev) {
return TCL_ERROR;
}
}
} else {
val = -val;
while (p < e) { /* check for overflow */
int prev = val;
val = val * 10 - (*p++ - '0');
if (val / 10 > prev) {
return TCL_ERROR;
}
}
}
*out = val;
return TCL_OK;
}
static inline int
Clock_str2wideInt(
Tcl_WideInt *out,
const char *p,
const char *e,
int sign)
{
Tcl_WideInt val = 0;
/* overflow impossible for 18 digits ("9..9"), so no needs to check before */
const char *eNO = p + 18;
if (eNO > e) {
eNO = e;
}
while (p < eNO) { /* never overflows */
val = val * 10 + (*p++ - '0');
}
if (sign >= 0) {
while (p < e) { /* check for overflow */
Tcl_WideInt prev = val;
val = val * 10 + (*p++ - '0');
if (val / 10 < prev) {
return TCL_ERROR;
}
}
} else {
val = -val;
while (p < e) { /* check for overflow */
Tcl_WideInt prev = val;
val = val * 10 - (*p++ - '0');
if (val / 10 > prev) {
return TCL_ERROR;
}
}
}
*out = val;
return TCL_OK;
}
int
TclAtoWIe(
Tcl_WideInt *out,
const char *p,
const char *e,
int sign)
{
return Clock_str2wideInt(out, p, e, sign);
}
#if (defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__)
# pragma GCC reset_options
#endif
/*
*----------------------------------------------------------------------
*
* Clock_itoaw, Clock_witoaw --
*
* Fast inline-convertion of signed int or wide int to string, using
* given padding with specified padchar and width (or without padding).
*
* This is a very fast replacement for sprintf("%02d").
*
* Results:
* Returns position in buffer after end of conversion result.
*
*----------------------------------------------------------------------
*/
static inline char *
Clock_itoaw(
char *buf,
int val,
char padchar,
unsigned short width)
{
char *p;
static const int wrange[] = {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000
};
/* positive integer */
if (val >= 0) {
/* check resp. recalculate width */
while (width <= 9 && val >= wrange[width]) {
width++;
}
/* number to string backwards */
p = buf + width;
*p-- = '\0';
do {
char c = val % 10;
val /= 10;
*p-- = '0' + c;
} while (val > 0);
/* filling with pad-char */
while (p >= buf) {
*p-- = padchar;
}
return buf + width;
}
/* negative integer */
if (!width) {
width++;
}
/* check resp. recalculate width (regarding sign) */
width--;
while (width <= 9 && val <= -wrange[width]) {
width++;
}
width++;
/* number to string backwards */
p = buf + width;
*p-- = '\0';
/* differentiate platforms with -1 % 10 == 1 and -1 % 10 == -1 */
if (-1 % 10 == -1) {
do {
char c = val % 10;
val /= 10;
*p-- = '0' - c;
} while (val < 0);
} else {
do {
char c = val % 10;
val /= 10;
*p-- = '0' + c;
} while (val < 0);
}
/* sign by 0 padding */
if (padchar != '0') {
*p-- = '-';
}
/* filling with pad-char */
while (p >= buf + 1) {
*p-- = padchar;
}
/* sign by non 0 padding */
if (padchar == '0') {
*p = '-';
}
return buf + width;
}
char *
TclItoAw(
char *buf,
int val,
char padchar,
unsigned short width)
{
return Clock_itoaw(buf, val, padchar, width);
}
static inline char *
Clock_witoaw(
char *buf,
Tcl_WideInt val,
char padchar,
unsigned short width)
{
char *p;
static const int wrange[] = {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000
};
/* positive integer */
if (val >= 0) {
/* check resp. recalculate width */
if (val >= 10000000000LL) {
Tcl_WideInt val2 = val / 10000000000LL;
while (width <= 9 && val2 >= wrange[width]) {
width++;
}
width += 10;
} else {
while (width <= 9 && val >= wrange[width]) {
width++;
}
}
/* number to string backwards */
p = buf + width;
*p-- = '\0';
do {
char c = (val % 10);
val /= 10;
*p-- = '0' + c;
} while (val > 0);
/* filling with pad-char */
while (p >= buf) {
*p-- = padchar;
}
return buf + width;
}
/* negative integer */
if (!width) {
width++;
}
/* check resp. recalculate width (regarding sign) */
width--;
if (val <= -10000000000LL) {
Tcl_WideInt val2 = val / 10000000000LL;
while (width <= 9 && val2 <= -wrange[width]) {
width++;
}
width += 10;
} else {
while (width <= 9 && val <= -wrange[width]) {
width++;
}
}
width++;
/* number to string backwards */
p = buf + width;
*p-- = '\0';
/* differentiate platforms with -1 % 10 == 1 and -1 % 10 == -1 */
if (-1 % 10 == -1) {
do {
char c = val % 10;
val /= 10;
*p-- = '0' - c;
} while (val < 0);
} else {
do {
char c = val % 10;
val /= 10;
*p-- = '0' + c;
} while (val < 0);
}
/* sign by 0 padding */
if (padchar != '0') {
*p-- = '-';
}
/* filling with pad-char */
while (p >= buf + 1) {
*p-- = padchar;
}
/* sign by non 0 padding */
if (padchar == '0') {
*p = '-';
}
return buf + width;
}
/*
* Global GC as LIFO for released scan/format object storages.
*
* Used to holds last released CLOCK_FMT_SCN_STORAGE_GC_SIZE formats
* (after last reference from Tcl-object will be removed). This is helpful
* to avoid continuous (re)creation and compiling by some dynamically resp.
* variable format objects, that could be often reused.
*
* As long as format storage is used resp. belongs to GC, it takes place in
* FmtScnHashTable also.
*/
#if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0
static struct ClockFmtScnStorage_GC {
ClockFmtScnStorage *stackPtr;
ClockFmtScnStorage *stackBound;
unsigned count;
} ClockFmtScnStorage_GC = {NULL, NULL, 0};
/*
*----------------------------------------------------------------------
*
* ClockFmtScnStorageGC_In --
*
* Adds an format storage object to GC.
*
* If current GC is full (size larger as CLOCK_FMT_SCN_STORAGE_GC_SIZE)
* this removes last unused storage at begin of GC stack (LIFO).
*
* Assumes caller holds the ClockFmtMutex.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
static inline void
ClockFmtScnStorageGC_In(
ClockFmtScnStorage *entry)
{
/* add new entry */
TclSpliceIn(entry, ClockFmtScnStorage_GC.stackPtr);
if (ClockFmtScnStorage_GC.stackBound == NULL) {
ClockFmtScnStorage_GC.stackBound = entry;
}
ClockFmtScnStorage_GC.count++;
/* if GC ist full */
if (ClockFmtScnStorage_GC.count > CLOCK_FMT_SCN_STORAGE_GC_SIZE) {
/* GC stack is LIFO: delete first inserted entry */
ClockFmtScnStorage *delEnt = ClockFmtScnStorage_GC.stackBound;
ClockFmtScnStorage_GC.stackBound = delEnt->prevPtr;
TclSpliceOut(delEnt, ClockFmtScnStorage_GC.stackPtr);
ClockFmtScnStorage_GC.count--;
delEnt->prevPtr = delEnt->nextPtr = NULL;
/* remove it now */
ClockFmtScnStorageDelete(delEnt);
}
}
/*
*----------------------------------------------------------------------
*
* ClockFmtScnStorage_GC_Out --
*
* Restores (for reusing) given format storage object from GC.
*
* Assumes caller holds the ClockFmtMutex.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
static inline void
ClockFmtScnStorage_GC_Out(
ClockFmtScnStorage *entry)
{
TclSpliceOut(entry, ClockFmtScnStorage_GC.stackPtr);
ClockFmtScnStorage_GC.count--;
if (ClockFmtScnStorage_GC.stackBound == entry) {
ClockFmtScnStorage_GC.stackBound = entry->prevPtr;
}
entry->prevPtr = entry->nextPtr = NULL;
}
#endif
/*
* Global format storage hash table of type ClockFmtScnStorageHashKeyType
* (contains list of scan/format object storages, shared across all threads).
*
* Used for fast searching by format string.
*/
static Tcl_HashTable FmtScnHashTable;
static int initialized = 0;
/*
* Wrappers between pointers to hash entry and format storage object
*/
static inline Tcl_HashEntry *
HashEntry4FmtScn(
ClockFmtScnStorage *fss)
{
return (Tcl_HashEntry*)(fss + 1);
}
static inline ClockFmtScnStorage *
FmtScn4HashEntry(
Tcl_HashEntry *hKeyPtr)
{
return (ClockFmtScnStorage*)(((char*)hKeyPtr) - sizeof(ClockFmtScnStorage));
}
/*
*----------------------------------------------------------------------
*
* ClockFmtScnStorageAllocProc --
*
* Allocate space for a hash entry containing format storage together
* with the string key.
*
* Results:
* The return value is a pointer to the created entry.
*
*----------------------------------------------------------------------
*/
static Tcl_HashEntry *
ClockFmtScnStorageAllocProc(
TCL_UNUSED(Tcl_HashTable *),/* Hash table. */
void *keyPtr) /* Key to store in the hash table entry. */
{
ClockFmtScnStorage *fss;
const char *string = (const char *) keyPtr;
Tcl_HashEntry *hPtr;
unsigned size = strlen(string) + 1;
unsigned allocsize = sizeof(ClockFmtScnStorage) + sizeof(Tcl_HashEntry);
allocsize += size;
if (size > sizeof(hPtr->key)) {
allocsize -= sizeof(hPtr->key);
}
fss = (ClockFmtScnStorage *)Tcl_Alloc(allocsize);
/* initialize */
memset(fss, 0, sizeof(*fss));
hPtr = HashEntry4FmtScn(fss);
memcpy(&hPtr->key.string, string, size);
hPtr->clientData = 0; /* currently unused */
return hPtr;
}
/*
*----------------------------------------------------------------------
*
* ClockFmtScnStorageFreeProc --
*
* Free format storage object and space of given hash entry.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
static void
ClockFmtScnStorageFreeProc(
Tcl_HashEntry *hPtr)
{
ClockFmtScnStorage *fss = FmtScn4HashEntry(hPtr);
if (fss->scnTok != NULL) {
Tcl_Free(fss->scnTok);
fss->scnTok = NULL;
fss->scnTokC = 0;
}
if (fss->fmtTok != NULL) {
Tcl_Free(fss->fmtTok);
fss->fmtTok = NULL;
fss->fmtTokC = 0;
}
Tcl_Free(fss);
}
/*
*----------------------------------------------------------------------
*
* ClockFmtScnStorageDelete --
*
* Delete format storage object.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
static void
ClockFmtScnStorageDelete(
ClockFmtScnStorage *fss)
{
Tcl_HashEntry *hPtr = HashEntry4FmtScn(fss);
/*
* This will delete a hash entry and call "Tcl_Free" for storage self, if
* some additionally handling required, freeEntryProc can be used instead
*/
Tcl_DeleteHashEntry(hPtr);
}
/*
* Type definition of clock-format tcl object type.
*/
static const Tcl_ObjType ClockFmtObjType = {
"clock-format", /* name */
ClockFmtObj_FreeInternalRep, /* freeIntRepProc */
ClockFmtObj_DupInternalRep, /* dupIntRepProc */
ClockFmtObj_UpdateString, /* updateStringProc */
ClockFmtObj_SetFromAny, /* setFromAnyProc */
TCL_OBJTYPE_V0
};
#define ObjClockFmtScn(objPtr) \
(*((ClockFmtScnStorage **)&(objPtr)->internalRep.twoPtrValue.ptr1))
#define ObjLocFmtKey(objPtr) \
(*((Tcl_Obj **)&(objPtr)->internalRep.twoPtrValue.ptr2))
static void
ClockFmtObj_DupInternalRep(
Tcl_Obj *srcPtr,
Tcl_Obj *copyPtr)
{
ClockFmtScnStorage *fss = ObjClockFmtScn(srcPtr);
if (fss != NULL) {
Tcl_MutexLock(&ClockFmtMutex);
fss->objRefCount++;
Tcl_MutexUnlock(&ClockFmtMutex);
}
ObjClockFmtScn(copyPtr) = fss;
/* regards special case - format not localizable */
if (ObjLocFmtKey(srcPtr) != srcPtr) {
TclInitObjRef(ObjLocFmtKey(copyPtr), ObjLocFmtKey(srcPtr));
} else {
ObjLocFmtKey(copyPtr) = copyPtr;
}
copyPtr->typePtr = &ClockFmtObjType;
/* if no format representation, dup string representation */
if (fss == NULL) {
copyPtr->bytes = (char *)Tcl_Alloc(srcPtr->length + 1);
memcpy(copyPtr->bytes, srcPtr->bytes, srcPtr->length + 1);
copyPtr->length = srcPtr->length;
}
}
static void
ClockFmtObj_FreeInternalRep(
Tcl_Obj *objPtr)
{
ClockFmtScnStorage *fss = ObjClockFmtScn(objPtr);
if (fss != NULL && initialized) {
Tcl_MutexLock(&ClockFmtMutex);
/* decrement object reference count of format/scan storage */
if (--fss->objRefCount <= 0) {
#if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0
/* don't remove it right now (may be reusable), just add to GC */
ClockFmtScnStorageGC_In(fss);
#else
/* remove storage (format representation) */
ClockFmtScnStorageDelete(fss);
#endif
}
Tcl_MutexUnlock(&ClockFmtMutex);
}
ObjClockFmtScn(objPtr) = NULL;
if (ObjLocFmtKey(objPtr) != objPtr) {
TclUnsetObjRef(ObjLocFmtKey(objPtr));
} else {
ObjLocFmtKey(objPtr) = NULL;
}
objPtr->typePtr = NULL;
}
static int
ClockFmtObj_SetFromAny(
TCL_UNUSED(Tcl_Interp *),
Tcl_Obj *objPtr)
{
/* validate string representation before free old internal representation */
(void)TclGetString(objPtr);
/* free old internal representation */
TclFreeInternalRep(objPtr);
/* initial state of format object */
ObjClockFmtScn(objPtr) = NULL;
ObjLocFmtKey(objPtr) = NULL;
objPtr->typePtr = &ClockFmtObjType;
return TCL_OK;
}
static void
ClockFmtObj_UpdateString(
Tcl_Obj *objPtr)
{
const char *name = "UNKNOWN";
size_t len;
ClockFmtScnStorage *fss = ObjClockFmtScn(objPtr);
if (fss != NULL) {
Tcl_HashEntry *hPtr = HashEntry4FmtScn(fss);
name = hPtr->key.string;
}
len = strlen(name);
objPtr->length = len++,
objPtr->bytes = (char *)Tcl_AttemptAlloc(len);
if (objPtr->bytes) {
memcpy(objPtr->bytes, name, len);
}
}
/*
*----------------------------------------------------------------------
*
* ClockFrmObjGetLocFmtKey --
*
* Retrieves format key object used to search localized format.
*
* This is normally stored in second pointer of internal representation.
* If format object is not localizable, it is equal the given format
* pointer (special case to fast fallback by not-localizable formats).
*
* Results:
* Returns tcl object with key or format object if not localizable.
*
* Side effects:
* Converts given format object to ClockFmtObjType on demand for caching
* the key inside its internal representation.
*
*----------------------------------------------------------------------
*/
Tcl_Obj*
ClockFrmObjGetLocFmtKey(
Tcl_Interp *interp,
Tcl_Obj *objPtr)
{
Tcl_Obj *keyObj;
if (objPtr->typePtr != &ClockFmtObjType) {
if (ClockFmtObj_SetFromAny(interp, objPtr) != TCL_OK) {
return NULL;
}
}
keyObj = ObjLocFmtKey(objPtr);
if (keyObj) {
return keyObj;
}
keyObj = Tcl_ObjPrintf("FMT_%s", TclGetString(objPtr));
TclInitObjRef(ObjLocFmtKey(objPtr), keyObj);
return keyObj;
}
/*
*----------------------------------------------------------------------
*
* FindOrCreateFmtScnStorage --
*
* Retrieves format storage for given string format.
*
* This will find the given format in the global storage hash table
* or create a format storage object on demaind and save the
* reference in the first pointer of internal representation of given
* object.
*
* Results:
* Returns scan/format storage pointer to ClockFmtScnStorage.
*
* Side effects:
* Converts given format object to ClockFmtObjType on demand for caching
* the format storage reference inside its internal representation.
* Increments objRefCount of the ClockFmtScnStorage reference.
*
*----------------------------------------------------------------------
*/
static ClockFmtScnStorage *
FindOrCreateFmtScnStorage(
Tcl_Interp *interp,
Tcl_Obj *objPtr)
{
const char *strFmt = TclGetString(objPtr);
ClockFmtScnStorage *fss = NULL;
int isNew;
Tcl_HashEntry *hPtr;
Tcl_MutexLock(&ClockFmtMutex);
/* if not yet initialized */
if (!initialized) {
/* initialize hash table */
Tcl_InitCustomHashTable(&FmtScnHashTable, TCL_CUSTOM_TYPE_KEYS,
&ClockFmtScnStorageHashKeyType);
initialized = 1;
}
/* get or create entry (and alocate storage) */
hPtr = Tcl_CreateHashEntry(&FmtScnHashTable, strFmt, &isNew);
if (hPtr != NULL) {
fss = FmtScn4HashEntry(hPtr);
#if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0
/* unlink if it is currently in GC */
if (isNew == 0 && fss->objRefCount == 0) {
ClockFmtScnStorage_GC_Out(fss);
}
#endif
/* new reference, so increment in lock right now */
fss->objRefCount++;
ObjClockFmtScn(objPtr) = fss;
}
Tcl_MutexUnlock(&ClockFmtMutex);
if (fss == NULL && interp != NULL) {
Tcl_AppendResult(interp, "retrieve clock format failed \"",
strFmt ? strFmt : "", "\"", (char *)NULL);
Tcl_SetErrorCode(interp, "TCL", "EINVAL", (char *)NULL);
}
return fss;
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetClockFrmScnFromObj --
*
* Returns a clock format/scan representation of (*objPtr), if possible.
* If something goes wrong, NULL is returned, and if interp is non-NULL,
* an error message is written there.
*
* Results:
* Valid representation of type ClockFmtScnStorage.
*
* Side effects:
* Caches the ClockFmtScnStorage reference as the internal rep of (*objPtr)
* and in global hash table, shared across all threads.
*
*----------------------------------------------------------------------
*/
ClockFmtScnStorage *
Tcl_GetClockFrmScnFromObj(
Tcl_Interp *interp,
Tcl_Obj *objPtr)
{
ClockFmtScnStorage *fss;
if (objPtr->typePtr != &ClockFmtObjType) {
if (ClockFmtObj_SetFromAny(interp, objPtr) != TCL_OK) {
return NULL;
}
}
fss = ObjClockFmtScn(objPtr);
if (fss == NULL) {
fss = FindOrCreateFmtScnStorage(interp, objPtr);
}
return fss;
}
/*
*----------------------------------------------------------------------
*
* ClockLocalizeFormat --
*
* Wrap the format object in options to the localized format,
* corresponding given locale.
*
* This searches localized format in locale catalog, and if not yet
* exists, it executes ::tcl::clock::LocalizeFormat in given interpreter
* and caches its result in the locale catalog.
*
* Results:
* Localized format object.
*
* Side effects:
* Caches the localized format inside locale catalog.
*
*----------------------------------------------------------------------
*/
Tcl_Obj *
ClockLocalizeFormat(
ClockFmtScnCmdArgs *opts)
{
ClockClientData *dataPtr = opts->dataPtr;
Tcl_Obj *valObj = NULL, *keyObj;
keyObj = ClockFrmObjGetLocFmtKey(opts->interp, opts->formatObj);
/* special case - format object is not localizable */
if (keyObj == opts->formatObj) {
return opts->formatObj;
}
/* prevents loss of key object if the format object (where key stored)
* becomes changed (loses its internal representation during evals) */
Tcl_IncrRefCount(keyObj);
if (opts->mcDictObj == NULL) {
ClockMCDict(opts);
if (opts->mcDictObj == NULL) {
goto done;
}
}
/* try to find in cache within locale mc-catalog */
if (Tcl_DictObjGet(NULL, opts->mcDictObj, keyObj, &valObj) != TCL_OK) {
goto done;
}
/* call LocalizeFormat locale format fmtkey */
if (valObj == NULL) {
Tcl_Obj *callargs[4];
callargs[0] = dataPtr->literals[LIT_LOCALIZE_FORMAT];
callargs[1] = opts->localeObj;
callargs[2] = opts->formatObj;
callargs[3] = opts->mcDictObj;
if (Tcl_EvalObjv(opts->interp, 4, callargs, 0) == TCL_OK) {
valObj = Tcl_GetObjResult(opts->interp);
}
/* ensure mcDictObj remains unshared */
if (opts->mcDictObj->refCount > 1) {
/* smart reference (shared dict as object with no ref-counter) */
opts->mcDictObj = TclDictObjSmartRef(opts->interp,
opts->mcDictObj);
}
if (!valObj) {
goto done;
}
/* cache it inside mc-dictionary (this incr. ref count of keyObj/valObj) */
if (Tcl_DictObjPut(opts->interp, opts->mcDictObj, keyObj, valObj) != TCL_OK) {
valObj = NULL;
goto done;
}
Tcl_ResetResult(opts->interp);
/* check special case - format object is not localizable */
if (valObj == opts->formatObj) {
/* mark it as unlocalizable, by setting self as key (without refcount incr) */
if (valObj->typePtr == &ClockFmtObjType) {
TclUnsetObjRef(ObjLocFmtKey(valObj));
ObjLocFmtKey(valObj) = valObj;
}
}
}
done:
TclUnsetObjRef(keyObj);
return (opts->formatObj = valObj);
}
/*
*----------------------------------------------------------------------
*
* FindTokenBegin --
*
* Find begin of given scan token in string, corresponding token type.
*
* Results:
* Position of token inside string if found. Otherwise - end of string.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static const char *
FindTokenBegin(
const char *p,
const char *end,
ClockScanToken *tok,
int flags)
{
if (p < end) {
char c;
/* next token a known token type */
switch (tok->map->type) {
case CTOKT_INT:
case CTOKT_WIDE:
if (!(flags & CLF_STRICT)) {
/* should match at least one digit or space */
while (!isdigit(UCHAR(*p)) && !isspace(UCHAR(*p)) &&
(p = Tcl_UtfNext(p)) < end) {}
} else {
/* should match at least one digit */
while (!isdigit(UCHAR(*p)) && (p = Tcl_UtfNext(p)) < end) {}
}
return p;
case CTOKT_WORD:
c = *(tok->tokWord.start);
goto findChar;
case CTOKT_SPACE:
while (!isspace(UCHAR(*p)) && (p = Tcl_UtfNext(p)) < end) {}
return p;
case CTOKT_CHAR:
c = *((char *)tok->map->data);
findChar:
if (!(flags & CLF_STRICT)) {
/* should match the char or space */
while (*p != c && !isspace(UCHAR(*p)) &&
(p = Tcl_UtfNext(p)) < end) {}
} else {
/* should match the char */
while (*p != c && (p = Tcl_UtfNext(p)) < end) {}
}
return p;
}
}
return p;
}
/*
*----------------------------------------------------------------------
*
* DetermineGreedySearchLen --
*
* Determine min/max lengths as exact as possible (speed, greedy match).
*
* Results:
* None. Lengths are stored in *minLenPtr, *maxLenPtr.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
DetermineGreedySearchLen(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
ClockScanToken *tok,
int *minLenPtr,
int *maxLenPtr)
{
int minLen = tok->map->minSize;
int maxLen;
const char *p = yyInput + minLen;
const char *end = info->dateEnd;
/* if still tokens available, try to correct minimum length */
if ((tok + 1)->map) {
end -= tok->endDistance + yySpaceCount;
/* find position of next known token */
p = FindTokenBegin(p, end, tok + 1,
TCL_CLOCK_FULL_COMPAT ? opts->flags : CLF_STRICT);
if (p < end) {
minLen = p - yyInput;
}
}
/* max length to the end regarding distance to end (min-width of following tokens) */
maxLen = end - yyInput;
/* several amendments */
if (maxLen > tok->map->maxSize) {
maxLen = tok->map->maxSize;
}
if (minLen < tok->map->minSize) {
minLen = tok->map->minSize;
}
if (minLen > maxLen) {
maxLen = minLen;
}
if (maxLen > info->dateEnd - yyInput) {
maxLen = info->dateEnd - yyInput;
}
/* check digits rigth now */
if (tok->map->type == CTOKT_INT || tok->map->type == CTOKT_WIDE) {
p = yyInput;
end = p + maxLen;
if (end > info->dateEnd) {
end = info->dateEnd;
}
while (isdigit(UCHAR(*p)) && p < end) {
p++;
}
maxLen = p - yyInput;
}
/* try to get max length more precise for greedy match,
* check the next ahead token available there */
if (minLen < maxLen && tok->lookAhTok) {
ClockScanToken *laTok = tok + tok->lookAhTok + 1;
p = yyInput + maxLen;
/* regards all possible spaces here (because they are optional) */
end = p + tok->lookAhMax + yySpaceCount + 1;
if (end > info->dateEnd) {
end = info->dateEnd;
}
p += tok->lookAhMin;
if (laTok->map && p < end) {
/* try to find laTok between [lookAhMin, lookAhMax] */
while (minLen < maxLen) {
const char *f = FindTokenBegin(p, end, laTok,
TCL_CLOCK_FULL_COMPAT ? opts->flags : CLF_STRICT);
/* if found (not below lookAhMax) */
if (f < end) {
break;
}
/* try again with fewer length */
maxLen--;
p--;
end--;
}
} else if (p > end) {
maxLen -= (p - end);
if (maxLen < minLen) {
maxLen = minLen;
}
}
}
*minLenPtr = minLen;
*maxLenPtr = maxLen;
}
/*
*----------------------------------------------------------------------
*
* ObjListSearch --
*
* Find largest part of the input string from start regarding min and
* max lengths in the given list (utf-8, case sensitive).
*
* Results:
* TCL_OK - match found, TCL_RETURN - not matched, TCL_ERROR in error case.
*
* Side effects:
* Input points to end of the found token in string.
*
*----------------------------------------------------------------------
*/
static inline int
ObjListSearch(
DateInfo *info,
int *val,
Tcl_Obj **lstv,
Tcl_Size lstc,
int minLen,
int maxLen)
{
Tcl_Size i, l, lf = -1;
const char *s, *f, *sf;
/* search in list */
for (i = 0; i < lstc; i++) {
s = TclGetStringFromObj(lstv[i], &l);
if (l >= minLen
&& (f = TclUtfFindEqualNC(yyInput, yyInput + maxLen, s, s + l, &sf)) > yyInput) {
l = f - yyInput;
if (l < minLen) {
continue;
}
/* found, try to find longest value (greedy search) */
if (l < maxLen && minLen != maxLen) {
lf = i;
minLen = l + 1;
continue;
}
/* max possible - end of search */
*val = i;
yyInput += l;
break;
}
}
/* if found */
if (i < lstc) {
return TCL_OK;
}
if (lf >= 0) {
*val = lf;
yyInput += minLen - 1;
return TCL_OK;
}
return TCL_RETURN;
}
#if 0
/* currently unused */
static int
LocaleListSearch(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
int mcKey,
int *val,
int minLen,
int maxLen)
{
Tcl_Obj **lstv;
Tcl_Size lstc;
Tcl_Obj *valObj;
/* get msgcat value */
valObj = ClockMCGet(opts, mcKey);
if (valObj == NULL) {
return TCL_ERROR;
}
/* is a list */
if (TclListObjGetElements(opts->interp, valObj, &lstc, &lstv) != TCL_OK) {
return TCL_ERROR;
}
/* search in list */
return ObjListSearch(info, val, lstv, lstc,
minLen, maxLen);
}
#endif
/*
*----------------------------------------------------------------------
*
* ClockMCGetListIdxTree --
*
* Retrieves localized string indexed tree in the locale catalog for
* given literal index mcKey (and builds it on demand).
*
* Searches localized index in locale catalog, and if not yet exists,
* creates string indexed tree and stores it in the locale catalog.
*
* Results:
* Localized string index tree.
*
* Side effects:
* Caches the localized string index tree inside locale catalog.
*
*----------------------------------------------------------------------
*/
static TclStrIdxTree *
ClockMCGetListIdxTree(
ClockFmtScnCmdArgs *opts,
int mcKey)
{
TclStrIdxTree *idxTree;
Tcl_Obj *objPtr = ClockMCGetIdx(opts, mcKey);
if (objPtr != NULL
&& (idxTree = TclStrIdxTreeGetFromObj(objPtr)) != NULL) {
return idxTree;
} else {
/* build new index */
Tcl_Obj **lstv;
Tcl_Size lstc;
Tcl_Obj *valObj;
objPtr = TclStrIdxTreeNewObj();
if ((idxTree = TclStrIdxTreeGetFromObj(objPtr)) == NULL) {
goto done; /* unexpected, but ...*/
}
valObj = ClockMCGet(opts, mcKey);
if (valObj == NULL) {
goto done;
}
if (TclListObjGetElements(opts->interp, valObj, &lstc, &lstv) != TCL_OK) {
goto done;
}
if (TclStrIdxTreeBuildFromList(idxTree, lstc, lstv, NULL) != TCL_OK) {
goto done;
}
ClockMCSetIdx(opts, mcKey, objPtr);
objPtr = NULL;
}
done:
if (objPtr) {
Tcl_DecrRefCount(objPtr);
idxTree = NULL;
}
return idxTree;
}
/*
*----------------------------------------------------------------------
*
* ClockMCGetMultiListIdxTree --
*
* Retrieves localized string indexed tree in the locale catalog for
* multiple lists by literal indices mcKeys (and builds it on demand).
*
* Searches localized index in locale catalog for mcKey, and if not
* yet exists, creates string indexed tree and stores it in the
* locale catalog.
*
* Results:
* Localized string index tree.
*
* Side effects:
* Caches the localized string index tree inside locale catalog.
*
*----------------------------------------------------------------------
*/
static TclStrIdxTree *
ClockMCGetMultiListIdxTree(
ClockFmtScnCmdArgs *opts,
int mcKey,
int *mcKeys)
{
TclStrIdxTree * idxTree;
Tcl_Obj *objPtr = ClockMCGetIdx(opts, mcKey);
if (objPtr != NULL
&& (idxTree = TclStrIdxTreeGetFromObj(objPtr)) != NULL) {
return idxTree;
} else {
/* build new index */
Tcl_Obj **lstv;
Tcl_Size lstc;
Tcl_Obj *valObj;
objPtr = TclStrIdxTreeNewObj();
if ((idxTree = TclStrIdxTreeGetFromObj(objPtr)) == NULL) {
goto done; /* unexpected, but ...*/
}
while (*mcKeys) {
valObj = ClockMCGet(opts, *mcKeys);
if (valObj == NULL) {
goto done;
}
if (TclListObjGetElements(opts->interp, valObj, &lstc, &lstv) != TCL_OK) {
goto done;
}
if (TclStrIdxTreeBuildFromList(idxTree, lstc, lstv, NULL) != TCL_OK) {
goto done;
}
mcKeys++;
}
ClockMCSetIdx(opts, mcKey, objPtr);
objPtr = NULL;
}
done:
if (objPtr) {
Tcl_DecrRefCount(objPtr);
idxTree = NULL;
}
return idxTree;
}
/*
*----------------------------------------------------------------------
*
* ClockStrIdxTreeSearch --
*
* Find largest part of the input string from start regarding lengths
* in the given localized string indexed tree (utf-8, case sensitive).
*
* Results:
* TCL_OK - match found and the index stored in *val,
* TCL_RETURN - not matched or ambigous,
* TCL_ERROR - in error case.
*
* Side effects:
* Input points to end of the found token in string.
*
*----------------------------------------------------------------------
*/
static inline int
ClockStrIdxTreeSearch(
DateInfo *info,
TclStrIdxTree *idxTree,
int *val,
int minLen,
int maxLen)
{
TclStrIdx *foundItem;
const char *f = TclStrIdxTreeSearch(NULL, &foundItem, idxTree,
yyInput, yyInput + maxLen);
if (f <= yyInput || (f - yyInput) < minLen) {
/* not found */
return TCL_RETURN;
}
if (!foundItem->value) {
/* ambigous */
return TCL_RETURN;
}
*val = PTR2INT(foundItem->value);
/* shift input pointer */
yyInput = f;
return TCL_OK;
}
#if 0
/* currently unused */
static int
StaticListSearch(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
const char **lst,
int *val)
{
size_t len;
const char **s = lst;
while (*s != NULL) {
len = strlen(*s);
if (len <= info->dateEnd - yyInput
&& strncasecmp(yyInput, *s, len) == 0) {
*val = (s - lst);
yyInput += len;
break;
}
s++;
}
if (*s != NULL) {
return TCL_OK;
}
return TCL_RETURN;
}
#endif
static inline const char *
FindWordEnd(
ClockScanToken *tok,
const char *p,
const char *end)
{
const char *x = tok->tokWord.start;
const char *pfnd = p;
if (x == tok->tokWord.end - 1) { /* fast phase-out for single char word */
if (*p == *x) {
return ++p;
}
}
/* multi-char word */
x = TclUtfFindEqualNC(x, tok->tokWord.end, p, end, &pfnd);
if (x < tok->tokWord.end) {
/* no match -> error */
return NULL;
}
return pfnd;
}
static int
ClockScnToken_Month_Proc(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
ClockScanToken *tok)
{
#if 0
/* currently unused, test purposes only */
static const char * months[] = {
/* full */
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
/* abbr */
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
NULL
};
int val;
if (StaticListSearch(opts, info, months, &val) != TCL_OK) {
return TCL_RETURN;
}
yyMonth = (val % 12) + 1;
#else
static int monthsKeys[] = {MCLIT_MONTHS_FULL, MCLIT_MONTHS_ABBREV, 0};
int ret, val;
int minLen, maxLen;
TclStrIdxTree *idxTree;
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
/* get or create tree in msgcat dict */
idxTree = ClockMCGetMultiListIdxTree(opts, MCLIT_MONTHS_COMB, monthsKeys);
if (idxTree == NULL) {
return TCL_ERROR;
}
ret = ClockStrIdxTreeSearch(info, idxTree, &val, minLen, maxLen);
if (ret != TCL_OK) {
return ret;
}
yyMonth = val;
#endif
return TCL_OK;
}
static int
ClockScnToken_DayOfWeek_Proc(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
ClockScanToken *tok)
{
static int dowKeys[] = {MCLIT_DAYS_OF_WEEK_ABBREV, MCLIT_DAYS_OF_WEEK_FULL, 0};
int ret, val;
int minLen, maxLen;
char curTok = *tok->tokWord.start;
TclStrIdxTree *idxTree;
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
/* %u %w %Ou %Ow */
if (curTok != 'a' && curTok != 'A'
&& ((minLen <= 1 && maxLen >= 1) || PTR2INT(tok->map->data))) {
val = -1;
if (PTR2INT(tok->map->data) == 0) {
if (*yyInput >= '0' && *yyInput <= '9') {
val = *yyInput - '0';
}
} else {
idxTree = ClockMCGetListIdxTree(opts, PTR2INT(tok->map->data) /* mcKey */);
if (idxTree == NULL) {
return TCL_ERROR;
}
ret = ClockStrIdxTreeSearch(info, idxTree, &val, minLen, maxLen);
if (ret != TCL_OK) {
return ret;
}
--val;
}
if (val == -1) {
return TCL_RETURN;
}
if (val == 0) {
val = 7;
}
if (val > 7) {
Tcl_SetObjResult(opts->interp, Tcl_NewStringObj(
"day of week is greater than 7", TCL_AUTO_LENGTH));
Tcl_SetErrorCode(opts->interp, "CLOCK", "badDayOfWeek", (char *)NULL);
return TCL_ERROR;
}
info->date.dayOfWeek = val;
yyInput++;
return TCL_OK;
}
/* %a %A */
idxTree = ClockMCGetMultiListIdxTree(opts, MCLIT_DAYS_OF_WEEK_COMB, dowKeys);
if (idxTree == NULL) {
return TCL_ERROR;
}
ret = ClockStrIdxTreeSearch(info, idxTree, &val, minLen, maxLen);
if (ret != TCL_OK) {
return ret;
}
--val;
if (val == 0) {
val = 7;
}
info->date.dayOfWeek = val;
return TCL_OK;
}
static int
ClockScnToken_amPmInd_Proc(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
ClockScanToken *tok)
{
int ret, val;
int minLen, maxLen;
Tcl_Obj *amPmObj[2];
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
amPmObj[0] = ClockMCGet(opts, MCLIT_AM);
amPmObj[1] = ClockMCGet(opts, MCLIT_PM);
if (amPmObj[0] == NULL || amPmObj[1] == NULL) {
return TCL_ERROR;
}
ret = ObjListSearch(info, &val, amPmObj, 2, minLen, maxLen);
if (ret != TCL_OK) {
return ret;
}
if (val == 0) {
yyMeridian = MERam;
} else {
yyMeridian = MERpm;
}
return TCL_OK;
}
static int
ClockScnToken_LocaleERA_Proc(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
ClockScanToken *tok)
{
ClockClientData *dataPtr = opts->dataPtr;
int ret, val;
int minLen, maxLen;
Tcl_Obj *eraObj[6];
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
eraObj[0] = ClockMCGet(opts, MCLIT_BCE);
eraObj[1] = ClockMCGet(opts, MCLIT_CE);
eraObj[2] = dataPtr->mcLiterals[MCLIT_BCE2];
eraObj[3] = dataPtr->mcLiterals[MCLIT_CE2];
eraObj[4] = dataPtr->mcLiterals[MCLIT_BCE3];
eraObj[5] = dataPtr->mcLiterals[MCLIT_CE3];
if (eraObj[0] == NULL || eraObj[1] == NULL) {
return TCL_ERROR;
}
ret = ObjListSearch(info, &val, eraObj, 6, minLen, maxLen);
if (ret != TCL_OK) {
return ret;
}
if (val & 1) {
yydate.isBce = 0;
} else {
yydate.isBce = 1;
}
return TCL_OK;
}
static int
ClockScnToken_LocaleListMatcher_Proc(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
ClockScanToken *tok)
{
int ret, val;
int minLen, maxLen;
TclStrIdxTree *idxTree;
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
/* get or create tree in msgcat dict */
idxTree = ClockMCGetListIdxTree(opts, PTR2INT(tok->map->data) /* mcKey */);
if (idxTree == NULL) {
return TCL_ERROR;
}
ret = ClockStrIdxTreeSearch(info, idxTree, &val, minLen, maxLen);
if (ret != TCL_OK) {
return ret;
}
if (tok->map->offs > 0) {
*IntFieldAt(info, tok->map->offs) = --val;
}
return TCL_OK;
}
static int
ClockScnToken_JDN_Proc(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
ClockScanToken *tok)
{
int minLen, maxLen;
const char *p = yyInput, *end, *s;
Tcl_WideInt intJD;
int fractJD = 0, fractJDDiv = 1;
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
end = yyInput + maxLen;
/* currently positive astronomic dates only */
if (*p == '+' || *p == '-') {
p++;
}
s = p;
while (p < end && isdigit(UCHAR(*p))) {
p++;
}
if (Clock_str2wideInt(&intJD, s, p, (*yyInput != '-' ? 1 : -1)) != TCL_OK) {
return TCL_RETURN;
}
yyInput = p;
if (p >= end || *p++ != '.') { /* allow pure integer JDN */
/* by astronomical JD the seconds of day offs is 12 hours */
if (tok->map->offs) {
goto done;
}
/* calendar JD */
yydate.julianDay = intJD;
return TCL_OK;
}
s = p;
while (p < end && isdigit(UCHAR(*p))) {
fractJDDiv *= 10;
p++;
}
if (Clock_str2int(&fractJD, s, p, 1) != TCL_OK) {
return TCL_RETURN;
}
yyInput = p;
done:
/*
* Build a date from julian day (integer and fraction).
* Note, astronomical JDN starts at noon in opposite to calendar julianday.
*/
fractJD = (int)tok->map->offs /* 0 for calendar or 43200 for astro JD */
+ (int)((Tcl_WideInt)SECONDS_PER_DAY * fractJD / fractJDDiv);
if (fractJD >= SECONDS_PER_DAY) {
fractJD %= SECONDS_PER_DAY;
intJD += 1;
}
yydate.secondOfDay = fractJD;
yydate.julianDay = intJD;
yydate.seconds =
-210866803200LL
+ (SECONDS_PER_DAY * intJD)
+ fractJD;
info->flags |= CLF_POSIXSEC;
return TCL_OK;
}
static int
ClockScnToken_TimeZone_Proc(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
ClockScanToken *tok)
{
int minLen, maxLen;
int len = 0;
const char *p = yyInput;
Tcl_Obj *tzObjStor = NULL;
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
/* numeric timezone */
if (*p == '+' || *p == '-') {
/* max chars in numeric zone = "+00:00:00" */
#define MAX_ZONE_LEN 9
char buf[MAX_ZONE_LEN + 1];
char *bp = buf;
*bp++ = *p++;
len++;
if (maxLen > MAX_ZONE_LEN) {
maxLen = MAX_ZONE_LEN;
}
/* cumulate zone into buf without ':' */
while (len + 1 < maxLen) {
if (!isdigit(UCHAR(*p))) {
break;
}
*bp++ = *p++;
len++;
if (!isdigit(UCHAR(*p))) {
break;
}
*bp++ = *p++;
len++;
if (len + 2 < maxLen) {
if (*p == ':') {
p++;
len++;
}
}
}
*bp = '\0';
if (len < minLen) {
return TCL_RETURN;
}
#undef MAX_ZONE_LEN
/* timezone */
tzObjStor = Tcl_NewStringObj(buf, bp - buf);
} else {
/* legacy (alnum) timezone like CEST, etc. */
if (maxLen > 4) {
maxLen = 4;
}
while (len < maxLen) {
if ((*p & 0x80)
|| (!isalpha(UCHAR(*p)) && !isdigit(UCHAR(*p)))) { /* INTL: ISO only. */
break;
}
p++;
len++;
}
if (len < minLen) {
return TCL_RETURN;
}
/* timezone */
tzObjStor = Tcl_NewStringObj(yyInput, p - yyInput);
/* convert using dict */
}
/* try to apply new time zone */
Tcl_IncrRefCount(tzObjStor);
opts->timezoneObj = ClockSetupTimeZone(opts->dataPtr, opts->interp,
tzObjStor);
Tcl_DecrRefCount(tzObjStor);
if (opts->timezoneObj == NULL) {
return TCL_ERROR;
}
yyInput += len;
return TCL_OK;
}
static int
ClockScnToken_StarDate_Proc(
ClockFmtScnCmdArgs *opts,
DateInfo *info,
ClockScanToken *tok)
{
int minLen, maxLen;
const char *p = yyInput, *end, *s;
int year, fractYear, fractDayDiv, fractDay;
static const char *stardatePref = "stardate ";
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
end = yyInput + maxLen;
/* stardate string */
p = TclUtfFindEqualNCInLwr(p, end, stardatePref, stardatePref + 9, &s);
if (p >= end || p - yyInput < 9) {
return TCL_RETURN;
}
/* bypass spaces */
while (p < end && isspace(UCHAR(*p))) {
p++;
}
if (p >= end) {
return TCL_RETURN;
}
/* currently positive stardate only */
if (*p == '+') {
p++;
}
s = p;
while (p < end && isdigit(UCHAR(*p))) {
p++;
}
if (p >= end || p - s < 4) {
return TCL_RETURN;
}
if (Clock_str2int(&year, s, p - 3, 1) != TCL_OK
|| Clock_str2int(&fractYear, p - 3, p, 1) != TCL_OK) {
return TCL_RETURN;
}
if (*p++ != '.') {
return TCL_RETURN;
}
s = p;
fractDayDiv = 1;
while (p < end && isdigit(UCHAR(*p))) {
fractDayDiv *= 10;
p++;
}
if (Clock_str2int(&fractDay, s, p, 1) != TCL_OK) {
return TCL_RETURN;
}
yyInput = p;
/* Build a date from year and fraction. */
yydate.year = year + RODDENBERRY;
yydate.isBce = 0;
yydate.gregorian = 1;
if (IsGregorianLeapYear(&yydate)) {
fractYear *= 366;
} else {
fractYear *= 365;
}
yydate.dayOfYear = fractYear / 1000 + 1;
if (fractYear % 1000 >= 500) {
yydate.dayOfYear++;
}
GetJulianDayFromEraYearDay(&yydate, GREGORIAN_CHANGE_DATE);
yydate.localSeconds =
-210866803200LL
+ (SECONDS_PER_DAY * yydate.julianDay)
+ (SECONDS_PER_DAY * fractDay / fractDayDiv);
return TCL_OK;
}
/*
* Descriptors for the various fields in [clock scan].
*/
static const char *ScnSTokenMapIndex = "dmbyYHMSpJjCgGVazUsntQ";
static const ClockScanTokenMap ScnSTokenMap[] = {
/* %d %e */
{CTOKT_INT, CLF_DAYOFMONTH, 0, 1, 2, offsetof(DateInfo, date.dayOfMonth),
NULL, NULL},
/* %m %N */
{CTOKT_INT, CLF_MONTH, 0, 1, 2, offsetof(DateInfo, date.month),
NULL, NULL},
/* %b %B %h */
{CTOKT_PARSER, CLF_MONTH, 0, 0, 0xffff, 0,
ClockScnToken_Month_Proc, NULL},
/* %y */
{CTOKT_INT, CLF_YEAR, 0, 1, 2, offsetof(DateInfo, date.year),
NULL, NULL},
/* %Y */
{CTOKT_INT, CLF_YEAR | CLF_CENTURY, 0, 4, 4, offsetof(DateInfo, date.year),
NULL, NULL},
/* %H %k %I %l */
{CTOKT_INT, CLF_TIME, 0, 1, 2, offsetof(DateInfo, date.hour),
NULL, NULL},
/* %M */
{CTOKT_INT, CLF_TIME, 0, 1, 2, offsetof(DateInfo, date.minutes),
NULL, NULL},
/* %S */
{CTOKT_INT, CLF_TIME, 0, 1, 2, offsetof(DateInfo, date.secondOfMin),
NULL, NULL},
/* %p %P */
{CTOKT_PARSER, 0, 0, 0, 0xffff, 0,
ClockScnToken_amPmInd_Proc, NULL},
/* %J */
{CTOKT_WIDE, CLF_JULIANDAY | CLF_SIGNED, 0, 1, 0xffff, offsetof(DateInfo, date.julianDay),
NULL, NULL},
/* %j */
{CTOKT_INT, CLF_DAYOFYEAR, 0, 1, 3, offsetof(DateInfo, date.dayOfYear),
NULL, NULL},
/* %C */
{CTOKT_INT, CLF_CENTURY|CLF_ISO8601CENTURY, 0, 1, 2, offsetof(DateInfo, dateCentury),
NULL, NULL},
/* %g */
{CTOKT_INT, CLF_ISO8601YEAR, 0, 2, 2, offsetof(DateInfo, date.iso8601Year),
NULL, NULL},
/* %G */
{CTOKT_INT, CLF_ISO8601YEAR | CLF_ISO8601CENTURY, 0, 4, 4, offsetof(DateInfo, date.iso8601Year),
NULL, NULL},
/* %V */
{CTOKT_INT, CLF_ISO8601WEEK, 0, 1, 2, offsetof(DateInfo, date.iso8601Week),
NULL, NULL},
/* %a %A %u %w */
{CTOKT_PARSER, CLF_DAYOFWEEK, 0, 0, 0xffff, 0,
ClockScnToken_DayOfWeek_Proc, NULL},
/* %z %Z */
{CTOKT_PARSER, CLF_OPTIONAL, 0, 0, 0xffff, 0,
ClockScnToken_TimeZone_Proc, NULL},
/* %U %W */
{CTOKT_INT, CLF_OPTIONAL, 0, 1, 2, 0, /* currently no capture, parse only token */
NULL, NULL},
/* %s */
{CTOKT_WIDE, CLF_POSIXSEC | CLF_SIGNED, 0, 1, 0xffff, offsetof(DateInfo, date.seconds),
NULL, NULL},
/* %n */
{CTOKT_CHAR, 0, 0, 1, 1, 0, NULL, "\n"},
/* %t */
{CTOKT_CHAR, 0, 0, 1, 1, 0, NULL, "\t"},
/* %Q */
{CTOKT_PARSER, CLF_LOCALSEC, 0, 16, 30, 0,
ClockScnToken_StarDate_Proc, NULL},
};
static const char *ScnSTokenMapAliasIndex[2] = {
"eNBhkIlPAuwZW",
"dmbbHHHpaaazU"
};
static const char *ScnETokenMapIndex = "EJjys";
static const ClockScanTokenMap ScnETokenMap[] = {
/* %EE */
{CTOKT_PARSER, 0, 0, 0, 0xffff, offsetof(DateInfo, date.year),
ClockScnToken_LocaleERA_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %EJ */
{CTOKT_PARSER, CLF_JULIANDAY | CLF_SIGNED, 0, 1, 0xffff, 0, /* calendar JDN starts at midnight */
ClockScnToken_JDN_Proc, NULL},
/* %Ej */
{CTOKT_PARSER, CLF_JULIANDAY | CLF_SIGNED, 0, 1, 0xffff, (SECONDS_PER_DAY/2), /* astro JDN starts at noon */
ClockScnToken_JDN_Proc, NULL},
/* %Ey */
{CTOKT_PARSER, 0, 0, 0, 0xffff, 0, /* currently no capture, parse only token */
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %Es */
{CTOKT_WIDE, CLF_LOCALSEC | CLF_SIGNED, 0, 1, 0xffff, offsetof(DateInfo, date.localSeconds),
NULL, NULL},
};
static const char *ScnETokenMapAliasIndex[2] = {
"",
""
};
static const char *ScnOTokenMapIndex = "dmyHMSu";
static const ClockScanTokenMap ScnOTokenMap[] = {
/* %Od %Oe */
{CTOKT_PARSER, CLF_DAYOFMONTH, 0, 0, 0xffff, offsetof(DateInfo, date.dayOfMonth),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %Om */
{CTOKT_PARSER, CLF_MONTH, 0, 0, 0xffff, offsetof(DateInfo, date.month),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %Oy */
{CTOKT_PARSER, CLF_YEAR, 0, 0, 0xffff, offsetof(DateInfo, date.year),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %OH %Ok %OI %Ol */
{CTOKT_PARSER, CLF_TIME, 0, 0, 0xffff, offsetof(DateInfo, date.hour),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %OM */
{CTOKT_PARSER, CLF_TIME, 0, 0, 0xffff, offsetof(DateInfo, date.minutes),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %OS */
{CTOKT_PARSER, CLF_TIME, 0, 0, 0xffff, offsetof(DateInfo, date.secondOfMin),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %Ou Ow */
{CTOKT_PARSER, CLF_DAYOFWEEK, 0, 0, 0xffff, 0,
ClockScnToken_DayOfWeek_Proc, (void *)MCLIT_LOCALE_NUMERALS},
};
static const char *ScnOTokenMapAliasIndex[2] = {
"ekIlw",
"dHHHu"
};
/* Token map reserved for CTOKT_SPACE */
static const ClockScanTokenMap ScnSpaceTokenMap = {
CTOKT_SPACE, 0, 0, 1, 1, 0, NULL, NULL
};
static const ClockScanTokenMap ScnWordTokenMap = {
CTOKT_WORD, 0, 0, 1, 1, 0, NULL, NULL
};
static inline unsigned
EstimateTokenCount(
const char *fmt,
const char *end)
{
const char *p = fmt;
unsigned tokcnt;
/* estimate token count by % char and format length */
tokcnt = 0;
while (p <= end) {
if (*p++ == '%') {
tokcnt++;
p++;
}
}
p = fmt + tokcnt * 2;
if (p < end) {
if ((unsigned)(end - p) < tokcnt) {
tokcnt += (end - p);
} else {
tokcnt += tokcnt;
}
}
return ++tokcnt;
}
#define AllocTokenInChain(tok, chain, tokCnt, type) \
if (++(tok) >= (chain) + (tokCnt)) { \
chain = (type)Tcl_Realloc((char *)(chain), \
(tokCnt + CLOCK_MIN_TOK_CHAIN_BLOCK_SIZE) * sizeof(*(tok))); \
(tok) = (chain) + (tokCnt); \
(tokCnt) += CLOCK_MIN_TOK_CHAIN_BLOCK_SIZE; \
} \
memset(tok, 0, sizeof(*(tok)));
/*
*----------------------------------------------------------------------
*/
ClockFmtScnStorage *
ClockGetOrParseScanFormat(
Tcl_Interp *interp, /* Tcl interpreter */
Tcl_Obj *formatObj) /* Format container */
{
ClockFmtScnStorage *fss;
fss = Tcl_GetClockFrmScnFromObj(interp, formatObj);
if (fss == NULL) {
return NULL;
}
/* if format (scnTok) already tokenized */
if (fss->scnTok != NULL) {
return fss;
}
Tcl_MutexLock(&ClockFmtMutex);
/* first time scanning - tokenize format */
if (fss->scnTok == NULL) {
ClockScanToken *tok, *scnTok;
unsigned tokCnt;
const char *p, *e, *cp;
e = p = HashEntry4FmtScn(fss)->key.string;
e += strlen(p);
/* estimate token count by % char and format length */
fss->scnTokC = EstimateTokenCount(p, e);
fss->scnSpaceCount = 0;
scnTok = tok = (ClockScanToken *)Tcl_Alloc(sizeof(*tok) * fss->scnTokC);
memset(tok, 0, sizeof(*tok));
tokCnt = 1;
while (p < e) {
switch (*p) {
case '%': {
const ClockScanTokenMap *scnMap = ScnSTokenMap;
const char *mapIndex = ScnSTokenMapIndex;
const char **aliasIndex = ScnSTokenMapAliasIndex;
if (p + 1 >= e) {
goto word_tok;
}
p++;
/* try to find modifier: */
switch (*p) {
case '%':
/* begin new word token - don't join with previous word token,
* because current mapping should be "...%%..." -> "...%..." */
tok->map = &ScnWordTokenMap;
tok->tokWord.start = p;
tok->tokWord.end = p + 1;
AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *);
tokCnt++;
p++;
continue;
case 'E':
scnMap = ScnETokenMap,
mapIndex = ScnETokenMapIndex,
aliasIndex = ScnETokenMapAliasIndex;
p++;
break;
case 'O':
scnMap = ScnOTokenMap,
mapIndex = ScnOTokenMapIndex,
aliasIndex = ScnOTokenMapAliasIndex;
p++;
break;
}
/* search direct index */
cp = strchr(mapIndex, *p);
if (!cp || *cp == '\0') {
/* search wrapper index (multiple chars for same token) */
cp = strchr(aliasIndex[0], *p);
if (!cp || *cp == '\0') {
p--;
if (scnMap != ScnSTokenMap) {
p--;
}
goto word_tok;
}
cp = strchr(mapIndex, aliasIndex[1][cp - aliasIndex[0]]);
if (!cp || *cp == '\0') { /* unexpected, but ... */
#ifdef DEBUG
Tcl_Panic("token \"%c\" has no map in wrapper resolver", *p);
#endif
p--;
if (scnMap != ScnSTokenMap) {
p--;
}
goto word_tok;
}
}
tok->map = &scnMap[cp - mapIndex];
tok->tokWord.start = p;
/* calculate look ahead value by standing together tokens */
if (tok > scnTok) {
ClockScanToken *prevTok = tok - 1;
while (prevTok >= scnTok) {
if (prevTok->map->type != tok->map->type) {
break;
}
prevTok->lookAhMin += tok->map->minSize;
prevTok->lookAhMax += tok->map->maxSize;
prevTok->lookAhTok++;
prevTok--;
}
}
/* increase space count used in format */
if (tok->map->type == CTOKT_CHAR
&& isspace(UCHAR(*((char *)tok->map->data)))) {
fss->scnSpaceCount++;
}
/* next token */
AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *);
tokCnt++;
p++;
continue;
}
default:
if (isspace(UCHAR(*p))) {
tok->map = &ScnSpaceTokenMap;
tok->tokWord.start = p++;
while (p < e && isspace(UCHAR(*p))) {
p++;
}
tok->tokWord.end = p;
/* increase space count used in format */
fss->scnSpaceCount++;
/* next token */
AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *);
tokCnt++;
continue;
}
word_tok:
{
/* try continue with previous word token */
ClockScanToken *wordTok = tok - 1;
if (wordTok < scnTok || wordTok->map != &ScnWordTokenMap) {
/* start with new word token */
wordTok = tok;
wordTok->tokWord.start = p;
wordTok->map = &ScnWordTokenMap;
}
do {
if (isspace(UCHAR(*p))) {
fss->scnSpaceCount++;
}
p = Tcl_UtfNext(p);
} while (p < e && *p != '%');
wordTok->tokWord.end = p;
if (wordTok == tok) {
AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *);
tokCnt++;
}
}
break;
}
}
/* calculate end distance value for each tokens */
if (tok > scnTok) {
unsigned endDist = 0;
ClockScanToken *prevTok = tok - 1;
while (prevTok >= scnTok) {
prevTok->endDistance = endDist;
if (prevTok->map->type != CTOKT_WORD) {
endDist += prevTok->map->minSize;
} else {
endDist += prevTok->tokWord.end - prevTok->tokWord.start;
}
prevTok--;
}
}
/* correct count of real used tokens and free mem if desired
* (1 is acceptable delta to prevent memory fragmentation) */
if (fss->scnTokC > tokCnt + (CLOCK_MIN_TOK_CHAIN_BLOCK_SIZE / 2)) {
if ((tok = (ClockScanToken *)
Tcl_AttemptRealloc(scnTok, tokCnt * sizeof(*tok))) != NULL) {
scnTok = tok;
}
}
/* now we're ready - assign now to storage (note the threaded race condition) */
fss->scnTok = scnTok;
fss->scnTokC = tokCnt;
}
Tcl_MutexUnlock(&ClockFmtMutex);
return fss;
}
/*
*----------------------------------------------------------------------
*/
int
ClockScan(
DateInfo *info, /* Date fields used for parsing & converting */
Tcl_Obj *strObj, /* String containing the time to scan */
ClockFmtScnCmdArgs *opts) /* Command options */
{
ClockClientData *dataPtr = opts->dataPtr;
ClockFmtScnStorage *fss;
ClockScanToken *tok;
const ClockScanTokenMap *map;
const char *p, *x, *end;
unsigned short flags = 0;
int ret = TCL_ERROR;
/* get localized format */
if (ClockLocalizeFormat(opts) == NULL) {
return TCL_ERROR;
}
if (!(fss = ClockGetOrParseScanFormat(opts->interp, opts->formatObj))
|| !(tok = fss->scnTok)) {
return TCL_ERROR;
}
/* prepare parsing */
yyMeridian = MER24;
p = TclGetString(strObj);
end = p + strObj->length;
/* in strict mode - bypass spaces at begin / end only (not between tokens) */
if (opts->flags & CLF_STRICT) {
while (p < end && isspace(UCHAR(*p))) {
p++;
}
}
yyInput = p;
/* look ahead to count spaces (bypass it by count length and distances) */
x = end;
while (p < end) {
if (isspace(UCHAR(*p))) {
x = ++p; /* after first space in space block */
yySpaceCount++;
while (p < end && isspace(UCHAR(*p))) {
p++;
yySpaceCount++;
}
continue;
}
x = end;
p++;
}
/* ignore more as 1 space at end */
yySpaceCount -= (end - x);
end = x;
/* ignore mandatory spaces used in format */
yySpaceCount -= fss->scnSpaceCount;
if (yySpaceCount < 0) {
yySpaceCount = 0;
}
info->dateStart = p = yyInput;
info->dateEnd = end;
/* parse string */
for (; tok->map != NULL; tok++) {
map = tok->map;
/* bypass spaces at begin of input before parsing each token */
if (!(opts->flags & CLF_STRICT)
&& (map->type != CTOKT_SPACE
&& map->type != CTOKT_WORD
&& map->type != CTOKT_CHAR)) {
while (p < end && isspace(UCHAR(*p))) {
yySpaceCount--;
p++;
}
}
yyInput = p;
/* end of input string */
if (p >= end) {
break;
}
switch (map->type) {
case CTOKT_INT:
case CTOKT_WIDE: {
int minLen, size;
int sign = 1;
if (map->flags & CLF_SIGNED) {
if (*p == '+') {
yyInput = ++p;
} else if (*p == '-') {
yyInput = ++p;
sign = -1;
}
}
DetermineGreedySearchLen(opts, info, tok, &minLen, &size);
if (size < map->minSize) {
/* missing input -> error */
if ((map->flags & CLF_OPTIONAL)) {
continue;
}
goto not_match;
}
/* string 2 number, put number into info structure by offset */
if (map->offs) {
p = yyInput;
x = p + size;
if (map->type == CTOKT_INT) {
if (size <= 10) {
Clock_str2int_no(IntFieldAt(info, map->offs),
p, x, sign);
} else if (Clock_str2int(
IntFieldAt(info, map->offs), p, x, sign) != TCL_OK) {
goto overflow;
}
p = x;
} else {
if (size <= 18) {
Clock_str2wideInt_no(
WideFieldAt(info, map->offs), p, x, sign);
} else if (Clock_str2wideInt(
WideFieldAt(info, map->offs), p, x, sign) != TCL_OK) {
goto overflow;
}
p = x;
}
flags = (flags & ~map->clearFlags) | map->flags;
}
break;
}
case CTOKT_PARSER:
switch (map->parser(opts, info, tok)) {
case TCL_OK:
break;
case TCL_RETURN:
if ((map->flags & CLF_OPTIONAL)) {
yyInput = p;
continue;
}
goto not_match;
default:
goto done;
}
/* decrement count for possible spaces in match */
while (p < yyInput) {
if (isspace(UCHAR(*p))) {
yySpaceCount--;
}
p++;
}
p = yyInput;
flags = (flags & ~map->clearFlags) | map->flags;
break;
case CTOKT_SPACE:
/* at least one space */
if (!isspace(UCHAR(*p))) {
/* unmatched -> error */
goto not_match;
}
/* don't decrement yySpaceCount by regular (first expected space),
* already considered above with fss->scnSpaceCount */;
p++;
while (p < end && isspace(UCHAR(*p))) {
yySpaceCount--;
p++;
}
break;
case CTOKT_WORD:
x = FindWordEnd(tok, p, end);
if (!x) {
/* no match -> error */
goto not_match;
}
p = x;
break;
case CTOKT_CHAR:
x = (char *)map->data;
if (*x != *p) {
/* no match -> error */
goto not_match;
}
if (isspace(UCHAR(*x))) {
yySpaceCount--;
}
p++;
break;
}
}
/* check end was reached */
if (p < end) {
/* in non-strict mode bypass spaces at end of input */
if (!(opts->flags & CLF_STRICT) && isspace(UCHAR(*p))) {
p++;
while (p < end && isspace(UCHAR(*p))) {
p++;
}
}
/* something after last token - wrong format */
if (p < end) {
goto not_match;
}
}
/* end of string, check only optional tokens at end, otherwise - not match */
while (tok->map != NULL) {
if (!(opts->flags & CLF_STRICT) && (tok->map->type == CTOKT_SPACE)) {
tok++;
if (tok->map == NULL) {
/* no tokens anymore - trailing spaces are mandatory */
goto not_match;
}
}
if (!(tok->map->flags & CLF_OPTIONAL)) {
goto not_match;
}
tok++;
}
/*
* Invalidate result
*/
flags |= info->flags;
/* seconds token (%s) take precedence over all other tokens */
if ((opts->flags & CLF_EXTENDED) || !(flags & CLF_POSIXSEC)) {
if (flags & CLF_DATE) {
if (!(flags & CLF_JULIANDAY)) {
info->flags |= CLF_ASSEMBLE_SECONDS|CLF_ASSEMBLE_JULIANDAY;
/* dd precedence below ddd */
switch (flags & (CLF_MONTH|CLF_DAYOFYEAR|CLF_DAYOFMONTH)) {
case (CLF_DAYOFYEAR | CLF_DAYOFMONTH):
/* miss month: ddd over dd (without month) */
flags &= ~CLF_DAYOFMONTH;
/* fallthrough */
case CLF_DAYOFYEAR:
/* ddd over naked weekday */
if (!(flags & CLF_ISO8601YEAR)) {
flags &= ~CLF_ISO8601WEEK;
}
break;
case CLF_MONTH | CLF_DAYOFYEAR | CLF_DAYOFMONTH:
/* both available: mmdd over ddd */
case CLF_MONTH | CLF_DAYOFMONTH:
case CLF_DAYOFMONTH:
/* mmdd / dd over naked weekday */
if (!(flags & CLF_ISO8601YEAR)) {
flags &= ~CLF_ISO8601WEEK;
}
break;
/* neither mmdd nor ddd available */
case 0:
/* but we have day of the week, which can be used */
if (flags & CLF_DAYOFWEEK) {
/* prefer week based calculation of julianday */
flags |= CLF_ISO8601WEEK;
}
}
/* YearWeekDay below YearMonthDay */
if ((flags & CLF_ISO8601WEEK)
&& ((flags & (CLF_YEAR | CLF_DAYOFYEAR)) == (CLF_YEAR | CLF_DAYOFYEAR)
|| (flags & (CLF_YEAR | CLF_DAYOFMONTH | CLF_MONTH)) == (
CLF_YEAR | CLF_DAYOFMONTH | CLF_MONTH))) {
/* yy precedence below yyyy */
if (!(flags & CLF_ISO8601CENTURY) && (flags & CLF_CENTURY)) {
/* normally precedence of ISO is higher, but no century - so put it down */
flags &= ~CLF_ISO8601WEEK;
} else if (!(flags & CLF_ISO8601YEAR)) {
/* yymmdd or yyddd over naked weekday */
flags &= ~CLF_ISO8601WEEK;
}
}
if (flags & CLF_YEAR) {
if (yyYear < 100) {
if (!(flags & CLF_CENTURY)) {
if (yyYear >= dataPtr->yearOfCenturySwitch) {
yyYear -= 100;
}
yyYear += dataPtr->currentYearCentury;
} else {
yyYear += info->dateCentury * 100;
}
}
}
if (flags & (CLF_ISO8601WEEK | CLF_ISO8601YEAR)) {
if ((flags & (CLF_ISO8601YEAR | CLF_YEAR)) == CLF_YEAR) {
/* for calculations expected iso year */
info->date.iso8601Year = yyYear;
} else if (info->date.iso8601Year < 100) {
if (!(flags & CLF_ISO8601CENTURY)) {
if (info->date.iso8601Year >= dataPtr->yearOfCenturySwitch) {
info->date.iso8601Year -= 100;
}
info->date.iso8601Year += dataPtr->currentYearCentury;
} else {
info->date.iso8601Year += info->dateCentury * 100;
}
}
if ((flags & (CLF_ISO8601YEAR | CLF_YEAR)) == CLF_ISO8601YEAR) {
/* for calculations expected year (e. g. CLF_ISO8601WEEK not set) */
yyYear = info->date.iso8601Year;
}
}
}
}
/* if no time - reset time */
if (!(flags & (CLF_TIME | CLF_LOCALSEC | CLF_POSIXSEC))) {
info->flags |= CLF_ASSEMBLE_SECONDS;
yydate.localSeconds = 0;
}
if (flags & CLF_TIME) {
info->flags |= CLF_ASSEMBLE_SECONDS;
yySecondOfDay = ToSeconds(yyHour, yyMinutes,
yySeconds, yyMeridian);
} else if (!(flags & (CLF_LOCALSEC | CLF_POSIXSEC))) {
info->flags |= CLF_ASSEMBLE_SECONDS;
yySecondOfDay = yydate.localSeconds % SECONDS_PER_DAY;
}
}
/* tell caller which flags were set */
info->flags |= flags;
ret = TCL_OK;
done:
return ret;
/* Error case reporting. */
overflow:
Tcl_SetObjResult(opts->interp, Tcl_NewStringObj(
"integer value too large to represent", TCL_AUTO_LENGTH));
Tcl_SetErrorCode(opts->interp, "CLOCK", "dateTooLarge", (char *)NULL);
goto done;
not_match:
#if 1
Tcl_SetObjResult(opts->interp, Tcl_NewStringObj(
"input string does not match supplied format", TCL_AUTO_LENGTH));
#else
/* to debug where exactly scan breaks */
Tcl_SetObjResult(opts->interp, Tcl_ObjPrintf(
"input string \"%s\" does not match supplied format \"%s\","
" locale \"%s\" - token \"%s\"",
info->dateStart, HashEntry4FmtScn(fss)->key.string,
TclGetString(opts->localeObj),
tok && tok->tokWord.start ? tok->tokWord.start : "NULL"));
#endif
Tcl_SetErrorCode(opts->interp, "CLOCK", "badInputString", (char *)NULL);
goto done;
}
#define FrmResultIsAllocated(dateFmt) \
(dateFmt->resEnd - dateFmt->resMem > MIN_FMT_RESULT_BLOCK_ALLOC)
static inline int
FrmResultAllocate(
DateFormat *dateFmt,
int len)
{
int needed = dateFmt->output + len - dateFmt->resEnd;
if (needed >= 0) { /* >= 0 - regards NTS zero */
int newsize = dateFmt->resEnd - dateFmt->resMem
+ needed + MIN_FMT_RESULT_BLOCK_ALLOC * 2;
char *newRes;
/* differentiate between stack and memory */
if (!FrmResultIsAllocated(dateFmt)) {
newRes = (char *)Tcl_AttemptAlloc(newsize);
if (newRes == NULL) {
return TCL_ERROR;
}
memcpy(newRes, dateFmt->resMem, dateFmt->output - dateFmt->resMem);
} else {
newRes = (char *)Tcl_AttemptRealloc(dateFmt->resMem, newsize);
if (newRes == NULL) {
return TCL_ERROR;
}
}
dateFmt->output = newRes + (dateFmt->output - dateFmt->resMem);
dateFmt->resMem = newRes;
dateFmt->resEnd = newRes + newsize;
}
return TCL_OK;
}
static int
ClockFmtToken_HourAMPM_Proc(
TCL_UNUSED(ClockFmtScnCmdArgs *),
TCL_UNUSED(DateFormat *),
TCL_UNUSED(ClockFormatToken *),
int *val)
{
*val = ((*val + SECONDS_PER_DAY - 3600) / 3600) % 12 + 1;
return TCL_OK;
}
static int
ClockFmtToken_AMPM_Proc(
ClockFmtScnCmdArgs *opts,
DateFormat *dateFmt,
ClockFormatToken *tok,
int *val)
{
Tcl_Obj *mcObj;
const char *s;
Tcl_Size len;
if (*val < (SECONDS_PER_DAY / 2)) {
mcObj = ClockMCGet(opts, MCLIT_AM);
} else {
mcObj = ClockMCGet(opts, MCLIT_PM);
}
if (mcObj == NULL) {
return TCL_ERROR;
}
s = TclGetStringFromObj(mcObj, &len);
if (FrmResultAllocate(dateFmt, len) != TCL_OK) {
return TCL_ERROR;
}
memcpy(dateFmt->output, s, len + 1);
if (*tok->tokWord.start == 'p') {
len = Tcl_UtfToUpper(dateFmt->output);
}
dateFmt->output += len;
return TCL_OK;
}
static int
ClockFmtToken_StarDate_Proc(
TCL_UNUSED(ClockFmtScnCmdArgs *),
DateFormat *dateFmt,
TCL_UNUSED(ClockFormatToken *),
TCL_UNUSED(int *))
{
int fractYear;
/* Get day of year, zero based */
int v = dateFmt->date.dayOfYear - 1;
/* Convert day of year to a fractional year */
if (IsGregorianLeapYear(&dateFmt->date)) {
fractYear = 1000 * v / 366;
} else {
fractYear = 1000 * v / 365;
}
/* Put together the StarDate as "Stardate %02d%03d.%1d" */
if (FrmResultAllocate(dateFmt, 30) != TCL_OK) {
return TCL_ERROR;
}
memcpy(dateFmt->output, "Stardate ", 9);
dateFmt->output += 9;
dateFmt->output = Clock_itoaw(dateFmt->output,
dateFmt->date.year - RODDENBERRY, '0', 2);
dateFmt->output = Clock_itoaw(dateFmt->output,
fractYear, '0', 3);
*dateFmt->output++ = '.';
/* be sure positive after decimal point (note: clock-value can be negative) */
v = dateFmt->date.secondOfDay / (SECONDS_PER_DAY / 10);
if (v < 0) {
v = 10 + v;
}
dateFmt->output = Clock_itoaw(dateFmt->output, v, '0', 1);
return TCL_OK;
}
static int
ClockFmtToken_WeekOfYear_Proc(
TCL_UNUSED(ClockFmtScnCmdArgs *),
DateFormat *dateFmt,
ClockFormatToken *tok,
int *val)
{
int dow = dateFmt->date.dayOfWeek;
if (*tok->tokWord.start == 'U') {
if (dow == 7) {
dow = 0;
}
dow++;
}
*val = (dateFmt->date.dayOfYear - dow + 7) / 7;
return TCL_OK;
}
static int
ClockFmtToken_JDN_Proc(
TCL_UNUSED(ClockFmtScnCmdArgs *),
DateFormat *dateFmt,
ClockFormatToken *tok,
TCL_UNUSED(int *))
{
Tcl_WideInt intJD = dateFmt->date.julianDay;
int fractJD;
/* Convert to JDN parts (regarding start offset) and time fraction */
fractJD = dateFmt->date.secondOfDay
- (int)tok->map->offs; /* 0 for calendar or 43200 for astro JD */
if (fractJD < 0) {
intJD--;
fractJD += SECONDS_PER_DAY;
}
if (fractJD && intJD < 0) { /* avoid jump over 0, by negative JD's */
intJD++;
if (intJD == 0) {
/* -0.0 / -0.9 has zero integer part, so append "-" extra */
if (FrmResultAllocate(dateFmt, 1) != TCL_OK) {
return TCL_ERROR;
}
*dateFmt->output++ = '-';
}
/* and inverse seconds of day, -0(75) -> -0.25 as float */
fractJD = SECONDS_PER_DAY - fractJD;
}
/* 21 is max width of (negative) wide-int (rather smaller, but anyway a time fraction below) */
if (FrmResultAllocate(dateFmt, 21) != TCL_OK) {
return TCL_ERROR;
}
dateFmt->output = Clock_witoaw(dateFmt->output, intJD, '0', 1);
/* simplest cases .0 and .5 */
if (!fractJD || fractJD == (SECONDS_PER_DAY / 2)) {
/* point + 0 or 5 */
if (FrmResultAllocate(dateFmt, 1 + 1) != TCL_OK) {
return TCL_ERROR;
}
*dateFmt->output++ = '.';
*dateFmt->output++ = !fractJD ? '0' : '5';
*dateFmt->output = '\0';
return TCL_OK;
} else {
/* wrap the time fraction */
#define JDN_MAX_PRECISION 8
#define JDN_MAX_PRECBOUND 100000000 /* 10**JDN_MAX_PRECISION */
char *p;
/* to float (part after floating point, + 0.5 to round it up) */
fractJD = (int)(
(double)fractJD * JDN_MAX_PRECBOUND / SECONDS_PER_DAY + 0.5);
/* point + integer (as time fraction after floating point) */
if (FrmResultAllocate(dateFmt, 1 + JDN_MAX_PRECISION) != TCL_OK) {
return TCL_ERROR;
}
*dateFmt->output++ = '.';
p = Clock_itoaw(dateFmt->output, fractJD, '0', JDN_MAX_PRECISION);
/* remove trailing zero's */
dateFmt->output++;
while (p > dateFmt->output && p[-1] == '0') {
p--;
}
*p = '\0';
dateFmt->output = p;
}
return TCL_OK;
}
static int
ClockFmtToken_TimeZone_Proc(
ClockFmtScnCmdArgs *opts,
DateFormat *dateFmt,
ClockFormatToken *tok,
TCL_UNUSED(int *))
{
if (*tok->tokWord.start == 'z') {
int z = dateFmt->date.tzOffset;
char sign = '+';
if (z < 0) {
z = -z;
sign = '-';
}
if (FrmResultAllocate(dateFmt, 7) != TCL_OK) {
return TCL_ERROR;
}
*dateFmt->output++ = sign;
dateFmt->output = Clock_itoaw(dateFmt->output, z / 3600, '0', 2);
z %= 3600;
dateFmt->output = Clock_itoaw(dateFmt->output, z / 60, '0', 2);
z %= 60;
if (z != 0) {
dateFmt->output = Clock_itoaw(dateFmt->output, z, '0', 2);
}
} else {
Tcl_Obj * objPtr;
const char *s;
Tcl_Size len;
/* convert seconds to local seconds to obtain tzName object */
if (ConvertUTCToLocal(opts->dataPtr, opts->interp,
&dateFmt->date, opts->timezoneObj,
GREGORIAN_CHANGE_DATE) != TCL_OK) {
return TCL_ERROR;
}
objPtr = dateFmt->date.tzName;
s = TclGetStringFromObj(objPtr, &len);
if (FrmResultAllocate(dateFmt, len) != TCL_OK) {
return TCL_ERROR;
}
memcpy(dateFmt->output, s, len + 1);
dateFmt->output += len;
}
return TCL_OK;
}
static int
ClockFmtToken_LocaleERA_Proc(
ClockFmtScnCmdArgs *opts,
DateFormat *dateFmt,
TCL_UNUSED(ClockFormatToken *),
TCL_UNUSED(int *))
{
Tcl_Obj *mcObj;
const char *s;
Tcl_Size len;
if (dateFmt->date.isBce) {
mcObj = ClockMCGet(opts, MCLIT_BCE);
} else {
mcObj = ClockMCGet(opts, MCLIT_CE);
}
if (mcObj == NULL) {
return TCL_ERROR;
}
s = TclGetStringFromObj(mcObj, &len);
if (FrmResultAllocate(dateFmt, len) != TCL_OK) {
return TCL_ERROR;
}
memcpy(dateFmt->output, s, len + 1);
dateFmt->output += len;
return TCL_OK;
}
static int
ClockFmtToken_LocaleERAYear_Proc(
ClockFmtScnCmdArgs *opts,
DateFormat *dateFmt,
ClockFormatToken *tok,
int *val)
{
Tcl_Size rowc;
Tcl_Obj **rowv;
if (dateFmt->localeEra == NULL) {
Tcl_Obj *mcObj = ClockMCGet(opts, MCLIT_LOCALE_ERAS);
if (mcObj == NULL) {
return TCL_ERROR;
}
if (TclListObjGetElements(opts->interp, mcObj, &rowc, &rowv) != TCL_OK) {
return TCL_ERROR;
}
if (rowc != 0) {
dateFmt->localeEra = LookupLastTransition(opts->interp,
dateFmt->date.localSeconds, rowc, rowv, NULL);
}
if (dateFmt->localeEra == NULL) {
dateFmt->localeEra = (Tcl_Obj*)1;
}
}
/* if no LOCALE_ERAS in catalog or era not found */
if (dateFmt->localeEra == (Tcl_Obj*)1) {
if (FrmResultAllocate(dateFmt, 11) != TCL_OK) {
return TCL_ERROR;
}
if (*tok->tokWord.start == 'C') { /* %EC */
*val = dateFmt->date.year / 100;
dateFmt->output = Clock_itoaw(dateFmt->output, *val, '0', 2);
} else { /* %Ey */
*val = dateFmt->date.year % 100;
dateFmt->output = Clock_itoaw(dateFmt->output, *val, '0', 2);
}
} else {
Tcl_Obj *objPtr;
const char *s;
Tcl_Size len;
if (*tok->tokWord.start == 'C') { /* %EC */
if (Tcl_ListObjIndex(opts->interp, dateFmt->localeEra, 1,
&objPtr) != TCL_OK) {
return TCL_ERROR;
}
} else { /* %Ey */
if (Tcl_ListObjIndex(opts->interp, dateFmt->localeEra, 2,
&objPtr) != TCL_OK) {
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(opts->interp, objPtr, val) != TCL_OK) {
return TCL_ERROR;
}
*val = dateFmt->date.year - *val;
/* if year in locale numerals */
if (*val >= 0 && *val < 100) {
/* year as integer */
Tcl_Obj * mcObj = ClockMCGet(opts, MCLIT_LOCALE_NUMERALS);
if (mcObj == NULL) {
return TCL_ERROR;
}
if (Tcl_ListObjIndex(opts->interp, mcObj, *val, &objPtr) != TCL_OK) {
return TCL_ERROR;
}
} else {
/* year as integer */
if (FrmResultAllocate(dateFmt, 11) != TCL_OK) {
return TCL_ERROR;
}
dateFmt->output = Clock_itoaw(dateFmt->output, *val, '0', 2);
return TCL_OK;
}
}
s = TclGetStringFromObj(objPtr, &len);
if (FrmResultAllocate(dateFmt, len) != TCL_OK) {
return TCL_ERROR;
}
memcpy(dateFmt->output, s, len + 1);
dateFmt->output += len;
}
return TCL_OK;
}
/*
* Descriptors for the various fields in [clock format].
*/
static const char *FmtSTokenMapIndex =
"demNbByYCHMSIklpaAuwUVzgGjJsntQ";
static const ClockFormatTokenMap FmtSTokenMap[] = {
/* %d */
{CTOKT_INT, "0", 2, 0, 0, 0, offsetof(DateFormat, date.dayOfMonth), NULL, NULL},
/* %e */
{CTOKT_INT, " ", 2, 0, 0, 0, offsetof(DateFormat, date.dayOfMonth), NULL, NULL},
/* %m */
{CTOKT_INT, "0", 2, 0, 0, 0, offsetof(DateFormat, date.month), NULL, NULL},
/* %N */
{CTOKT_INT, " ", 2, 0, 0, 0, offsetof(DateFormat, date.month), NULL, NULL},
/* %b %h */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX | CLFMT_DECR, 0, 12, offsetof(DateFormat, date.month),
NULL, (void *)MCLIT_MONTHS_ABBREV},
/* %B */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX | CLFMT_DECR, 0, 12, offsetof(DateFormat, date.month),
NULL, (void *)MCLIT_MONTHS_FULL},
/* %y */
{CTOKT_INT, "0", 2, 0, 0, 100, offsetof(DateFormat, date.year), NULL, NULL},
/* %Y */
{CTOKT_INT, "0", 4, 0, 0, 0, offsetof(DateFormat, date.year), NULL, NULL},
/* %C */
{CTOKT_INT, "0", 2, 0, 100, 0, offsetof(DateFormat, date.year), NULL, NULL},
/* %H */
{CTOKT_INT, "0", 2, 0, 3600, 24, offsetof(DateFormat, date.secondOfDay), NULL, NULL},
/* %M */
{CTOKT_INT, "0", 2, 0, 60, 60, offsetof(DateFormat, date.secondOfDay), NULL, NULL},
/* %S */
{CTOKT_INT, "0", 2, 0, 0, 60, offsetof(DateFormat, date.secondOfDay), NULL, NULL},
/* %I */
{CTOKT_INT, "0", 2, CLFMT_CALC, 0, 0, offsetof(DateFormat, date.secondOfDay),
ClockFmtToken_HourAMPM_Proc, NULL},
/* %k */
{CTOKT_INT, " ", 2, 0, 3600, 24, offsetof(DateFormat, date.secondOfDay), NULL, NULL},
/* %l */
{CTOKT_INT, " ", 2, CLFMT_CALC, 0, 0, offsetof(DateFormat, date.secondOfDay),
ClockFmtToken_HourAMPM_Proc, NULL},
/* %p %P */
{CTOKT_INT, NULL, 0, 0, 0, 0, offsetof(DateFormat, date.secondOfDay),
ClockFmtToken_AMPM_Proc, NULL},
/* %a */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 0, 7, offsetof(DateFormat, date.dayOfWeek),
NULL, (void *)MCLIT_DAYS_OF_WEEK_ABBREV},
/* %A */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 0, 7, offsetof(DateFormat, date.dayOfWeek),
NULL, (void *)MCLIT_DAYS_OF_WEEK_FULL},
/* %u */
{CTOKT_INT, " ", 1, 0, 0, 0, offsetof(DateFormat, date.dayOfWeek), NULL, NULL},
/* %w */
{CTOKT_INT, " ", 1, 0, 0, 7, offsetof(DateFormat, date.dayOfWeek), NULL, NULL},
/* %U %W */
{CTOKT_INT, "0", 2, CLFMT_CALC, 0, 0, offsetof(DateFormat, date.dayOfYear),
ClockFmtToken_WeekOfYear_Proc, NULL},
/* %V */
{CTOKT_INT, "0", 2, 0, 0, 0, offsetof(DateFormat, date.iso8601Week), NULL, NULL},
/* %z %Z */
{CFMTT_PROC, NULL, 0, 0, 0, 0, 0,
ClockFmtToken_TimeZone_Proc, NULL},
/* %g */
{CTOKT_INT, "0", 2, 0, 0, 100, offsetof(DateFormat, date.iso8601Year), NULL, NULL},
/* %G */
{CTOKT_INT, "0", 4, 0, 0, 0, offsetof(DateFormat, date.iso8601Year), NULL, NULL},
/* %j */
{CTOKT_INT, "0", 3, 0, 0, 0, offsetof(DateFormat, date.dayOfYear), NULL, NULL},
/* %J */
{CTOKT_WIDE, "0", 7, 0, 0, 0, offsetof(DateFormat, date.julianDay), NULL, NULL},
/* %s */
{CTOKT_WIDE, "0", 1, 0, 0, 0, offsetof(DateFormat, date.seconds), NULL, NULL},
/* %n */
{CTOKT_CHAR, "\n", 0, 0, 0, 0, 0, NULL, NULL},
/* %t */
{CTOKT_CHAR, "\t", 0, 0, 0, 0, 0, NULL, NULL},
/* %Q */
{CFMTT_PROC, NULL, 0, 0, 0, 0, 0,
ClockFmtToken_StarDate_Proc, NULL},
};
static const char *FmtSTokenMapAliasIndex[2] = {
"hPWZ",
"bpUz"
};
static const char *FmtETokenMapIndex = "EJjys";
static const ClockFormatTokenMap FmtETokenMap[] = {
/* %EE */
{CFMTT_PROC, NULL, 0, 0, 0, 0, 0,
ClockFmtToken_LocaleERA_Proc, NULL},
/* %EJ */
{CFMTT_PROC, NULL, 0, 0, 0, 0, 0, /* calendar JDN starts at midnight */
ClockFmtToken_JDN_Proc, NULL},
/* %Ej */
{CFMTT_PROC, NULL, 0, 0, 0, 0, (SECONDS_PER_DAY/2), /* astro JDN starts at noon */
ClockFmtToken_JDN_Proc, NULL},
/* %Ey %EC */
{CTOKT_INT, NULL, 0, 0, 0, 0, offsetof(DateFormat, date.year),
ClockFmtToken_LocaleERAYear_Proc, NULL},
/* %Es */
{CTOKT_WIDE, "0", 1, 0, 0, 0, offsetof(DateFormat, date.localSeconds), NULL, NULL},
};
static const char *FmtETokenMapAliasIndex[2] = {
"C",
"y"
};
static const char *FmtOTokenMapIndex = "dmyHIMSuw";
static const ClockFormatTokenMap FmtOTokenMap[] = {
/* %Od %Oe */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 0, 100, offsetof(DateFormat, date.dayOfMonth),
NULL, (void *)MCLIT_LOCALE_NUMERALS},
/* %Om */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 0, 100, offsetof(DateFormat, date.month),
NULL, (void *)MCLIT_LOCALE_NUMERALS},
/* %Oy */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 0, 100, offsetof(DateFormat, date.year),
NULL, (void *)MCLIT_LOCALE_NUMERALS},
/* %OH %Ok */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 3600, 24, offsetof(DateFormat, date.secondOfDay),
NULL, (void *)MCLIT_LOCALE_NUMERALS},
/* %OI %Ol */
{CTOKT_INT, NULL, 0, CLFMT_CALC | CLFMT_LOCALE_INDX, 0, 0, offsetof(DateFormat, date.secondOfDay),
ClockFmtToken_HourAMPM_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %OM */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 60, 60, offsetof(DateFormat, date.secondOfDay),
NULL, (void *)MCLIT_LOCALE_NUMERALS},
/* %OS */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 0, 60, offsetof(DateFormat, date.secondOfDay),
NULL, (void *)MCLIT_LOCALE_NUMERALS},
/* %Ou */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 0, 100, offsetof(DateFormat, date.dayOfWeek),
NULL, (void *)MCLIT_LOCALE_NUMERALS},
/* %Ow */
{CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 0, 7, offsetof(DateFormat, date.dayOfWeek),
NULL, (void *)MCLIT_LOCALE_NUMERALS},
};
static const char *FmtOTokenMapAliasIndex[2] = {
"ekl",
"dHI"
};
static const ClockFormatTokenMap FmtWordTokenMap = {
CTOKT_WORD, NULL, 0, 0, 0, 0, 0, NULL, NULL
};
/*
*----------------------------------------------------------------------
*/
ClockFmtScnStorage *
ClockGetOrParseFmtFormat(
Tcl_Interp *interp, /* Tcl interpreter */
Tcl_Obj *formatObj) /* Format container */
{
ClockFmtScnStorage *fss;
fss = Tcl_GetClockFrmScnFromObj(interp, formatObj);
if (fss == NULL) {
return NULL;
}
/* if format (fmtTok) already tokenized */
if (fss->fmtTok != NULL) {
return fss;
}
Tcl_MutexLock(&ClockFmtMutex);
/* first time formatting - tokenize format */
if (fss->fmtTok == NULL) {
ClockFormatToken *tok, *fmtTok;
unsigned tokCnt;
const char *p, *e, *cp;
e = p = HashEntry4FmtScn(fss)->key.string;
e += strlen(p);
/* estimate token count by % char and format length */
fss->fmtTokC = EstimateTokenCount(p, e);
fmtTok = tok = (ClockFormatToken *)Tcl_Alloc(sizeof(*tok) * fss->fmtTokC);
memset(tok, 0, sizeof(*tok));
tokCnt = 1;
while (p < e) {
switch (*p) {
case '%': {
const ClockFormatTokenMap *fmtMap = FmtSTokenMap;
const char *mapIndex = FmtSTokenMapIndex;
const char **aliasIndex = FmtSTokenMapAliasIndex;
if (p + 1 >= e) {
goto word_tok;
}
p++;
/* try to find modifier: */
switch (*p) {
case '%':
/* begin new word token - don't join with previous word token,
* because current mapping should be "...%%..." -> "...%..." */
tok->map = &FmtWordTokenMap;
tok->tokWord.start = p;
tok->tokWord.end = p + 1;
AllocTokenInChain(tok, fmtTok, fss->fmtTokC, ClockFormatToken *);
tokCnt++;
p++;
continue;
case 'E':
fmtMap = FmtETokenMap,
mapIndex = FmtETokenMapIndex,
aliasIndex = FmtETokenMapAliasIndex;
p++;
break;
case 'O':
fmtMap = FmtOTokenMap,
mapIndex = FmtOTokenMapIndex,
aliasIndex = FmtOTokenMapAliasIndex;
p++;
break;
}
/* search direct index */
cp = strchr(mapIndex, *p);
if (!cp || *cp == '\0') {
/* search wrapper index (multiple chars for same token) */
cp = strchr(aliasIndex[0], *p);
if (!cp || *cp == '\0') {
p--;
if (fmtMap != FmtSTokenMap) {
p--;
}
goto word_tok;
}
cp = strchr(mapIndex, aliasIndex[1][cp - aliasIndex[0]]);
if (!cp || *cp == '\0') { /* unexpected, but ... */
#ifdef DEBUG
Tcl_Panic("token \"%c\" has no map in wrapper resolver", *p);
#endif
p--;
if (fmtMap != FmtSTokenMap) {
p--;
}
goto word_tok;
}
}
tok->map = &fmtMap[cp - mapIndex];
tok->tokWord.start = p;
/* next token */
AllocTokenInChain(tok, fmtTok, fss->fmtTokC, ClockFormatToken *);
tokCnt++;
p++;
continue;
}
default:
word_tok:
{
/* try continue with previous word token */
ClockFormatToken *wordTok = tok - 1;
if (wordTok < fmtTok || wordTok->map != &FmtWordTokenMap) {
/* start with new word token */
wordTok = tok;
wordTok->tokWord.start = p;
wordTok->map = &FmtWordTokenMap;
}
do {
p = Tcl_UtfNext(p);
} while (p < e && *p != '%');
wordTok->tokWord.end = p;
if (wordTok == tok) {
AllocTokenInChain(tok, fmtTok, fss->fmtTokC, ClockFormatToken *);
tokCnt++;
}
}
break;
}
}
/* correct count of real used tokens and free mem if desired
* (1 is acceptable delta to prevent memory fragmentation) */
if (fss->fmtTokC > tokCnt + (CLOCK_MIN_TOK_CHAIN_BLOCK_SIZE / 2)) {
if ((tok = (ClockFormatToken *)
Tcl_AttemptRealloc(fmtTok, tokCnt * sizeof(*tok))) != NULL) {
fmtTok = tok;
}
}
/* now we're ready - assign now to storage (note the threaded race condition) */
fss->fmtTok = fmtTok;
fss->fmtTokC = tokCnt;
}
Tcl_MutexUnlock(&ClockFmtMutex);
return fss;
}
/*
*----------------------------------------------------------------------
*/
int
ClockFormat(
DateFormat *dateFmt, /* Date fields used for parsing & converting */
ClockFmtScnCmdArgs *opts) /* Command options */
{
ClockFmtScnStorage *fss;
ClockFormatToken *tok;
const ClockFormatTokenMap *map;
char resMem[MIN_FMT_RESULT_BLOCK_ALLOC];
/* get localized format */
if (ClockLocalizeFormat(opts) == NULL) {
return TCL_ERROR;
}
if (!(fss = ClockGetOrParseFmtFormat(opts->interp, opts->formatObj))
|| !(tok = fss->fmtTok)) {
return TCL_ERROR;
}
/* result container object */
dateFmt->resMem = resMem;
dateFmt->resEnd = dateFmt->resMem + sizeof(resMem);
if (fss->fmtMinAlloc > sizeof(resMem)) {
dateFmt->resMem = (char *)Tcl_AttemptAlloc(fss->fmtMinAlloc);
if (dateFmt->resMem == NULL) {
return TCL_ERROR;
}
dateFmt->resEnd = dateFmt->resMem + fss->fmtMinAlloc;
}
dateFmt->output = dateFmt->resMem;
*dateFmt->output = '\0';
/* do format each token */
for (; tok->map != NULL; tok++) {
map = tok->map;
switch (map->type) {
case CTOKT_INT: {
int val = *IntFieldAt(dateFmt, map->offs);
if (map->fmtproc == NULL) {
if (map->flags & CLFMT_DECR) {
val--;
}
if (map->flags & CLFMT_INCR) {
val++;
}
if (map->divider) {
val /= map->divider;
}
if (map->divmod) {
val %= map->divmod;
}
} else {
if (map->fmtproc(opts, dateFmt, tok, &val) != TCL_OK) {
goto done;
}
/* if not calculate only (output inside fmtproc) */
if (!(map->flags & CLFMT_CALC)) {
continue;
}
}
if (!(map->flags & CLFMT_LOCALE_INDX)) {
if (FrmResultAllocate(dateFmt, 11) != TCL_OK) {
goto error;
}
if (map->width) {
dateFmt->output = Clock_itoaw(
dateFmt->output, val, *map->tostr, map->width);
} else {
dateFmt->output += sprintf(dateFmt->output, map->tostr, val);
}
} else {
const char *s;
Tcl_Obj * mcObj = ClockMCGet(opts, PTR2INT(map->data) /* mcKey */);
if (mcObj == NULL) {
goto error;
}
if (Tcl_ListObjIndex(opts->interp, mcObj, val, &mcObj) != TCL_OK
|| mcObj == NULL) {
goto error;
}
s = TclGetString(mcObj);
if (FrmResultAllocate(dateFmt, mcObj->length) != TCL_OK) {
goto error;
}
memcpy(dateFmt->output, s, mcObj->length + 1);
dateFmt->output += mcObj->length;
}
break;
}
case CTOKT_WIDE: {
Tcl_WideInt val = *WideFieldAt(dateFmt, map->offs);
if (FrmResultAllocate(dateFmt, 21) != TCL_OK) {
goto error;
}
if (map->width) {
dateFmt->output = Clock_witoaw(dateFmt->output, val, *map->tostr, map->width);
} else {
dateFmt->output += sprintf(dateFmt->output, map->tostr, val);
}
break;
}
case CTOKT_CHAR:
if (FrmResultAllocate(dateFmt, 1) != TCL_OK) {
goto error;
}
*dateFmt->output++ = *map->tostr;
break;
case CFMTT_PROC:
if (map->fmtproc(opts, dateFmt, tok, NULL) != TCL_OK) {
goto error;
}
break;
case CTOKT_WORD: {
Tcl_Size len = tok->tokWord.end - tok->tokWord.start;
if (FrmResultAllocate(dateFmt, len) != TCL_OK) {
goto error;
}
if (len == 1) {
*dateFmt->output++ = *tok->tokWord.start;
} else {
memcpy(dateFmt->output, tok->tokWord.start, len);
dateFmt->output += len;
}
break;
}
}
}
goto done;
error:
if (dateFmt->resMem != resMem) {
Tcl_Free(dateFmt->resMem);
}
dateFmt->resMem = NULL;
done:
if (dateFmt->resMem) {
size_t size;
Tcl_Obj *result;
TclNewObj(result);
result->length = dateFmt->output - dateFmt->resMem;
size = result->length + 1;
if (dateFmt->resMem == resMem) {
result->bytes = (char *)Tcl_AttemptAlloc(size);
if (result->bytes == NULL) {
return TCL_ERROR;
}
memcpy(result->bytes, dateFmt->resMem, size);
} else if ((dateFmt->resEnd - dateFmt->resMem) / size > MAX_FMT_RESULT_THRESHOLD) {
result->bytes = (char *)Tcl_AttemptRealloc(dateFmt->resMem, size);
if (result->bytes == NULL) {
result->bytes = dateFmt->resMem;
}
} else {
result->bytes = dateFmt->resMem;
}
/* save last used buffer length */
if (dateFmt->resMem != resMem
&& fss->fmtMinAlloc < size + MIN_FMT_RESULT_BLOCK_DELTA) {
fss->fmtMinAlloc = size + MIN_FMT_RESULT_BLOCK_DELTA;
}
result->bytes[result->length] = '\0';
Tcl_SetObjResult(opts->interp, result);
return TCL_OK;
}
return TCL_ERROR;
}
void
ClockFrmScnClearCaches(void)
{
Tcl_MutexLock(&ClockFmtMutex);
/* clear caches ... */
Tcl_MutexUnlock(&ClockFmtMutex);
}
void
ClockFrmScnFinalize(void)
{
if (!initialized) {
return;
}
Tcl_MutexLock(&ClockFmtMutex);
#if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0
/* clear GC */
ClockFmtScnStorage_GC.stackPtr = NULL;
ClockFmtScnStorage_GC.stackBound = NULL;
ClockFmtScnStorage_GC.count = 0;
#endif
if (initialized) {
initialized = 0;
Tcl_DeleteHashTable(&FmtScnHashTable);
}
Tcl_MutexUnlock(&ClockFmtMutex);
Tcl_MutexFinalize(&ClockFmtMutex);
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|