1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 CERN
* Copyright (C) 2016-2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* @author Wayne Stambaugh <stambaughw@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <algorithm>
#include <wx/mstream.h>
#include <wx/filename.h>
#include <wx/tokenzr.h>
#include <draw_graphic_text.h>
#include <kiway.h>
#include <kicad_string.h>
#include <richio.h>
#include <core/typeinfo.h>
#include <properties.h>
#include <trace_helpers.h>
#include <general.h>
#include <sch_bitmap.h>
#include <sch_bus_entry.h>
#include <sch_component.h>
#include <sch_junction.h>
#include <sch_line.h>
#include <sch_marker.h>
#include <sch_no_connect.h>
#include <sch_text.h>
#include <sch_sheet.h>
#include <sch_legacy_plugin.h>
#include <template_fieldnames.h>
#include <sch_screen.h>
#include <class_libentry.h>
#include <class_library.h>
#include <lib_arc.h>
#include <lib_bezier.h>
#include <lib_circle.h>
#include <lib_field.h>
#include <lib_pin.h>
#include <lib_polyline.h>
#include <lib_rectangle.h>
#include <lib_text.h>
#include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
#include <symbol_lib_table.h> // for PropPowerSymsOnly definintion.
// Must be the first line of part library document (.dcm) files.
#define DOCFILE_IDENT "EESchema-DOCLIB Version 2.0"
#define SCH_PARSE_ERROR( text, reader, pos ) \
THROW_PARSE_ERROR( text, reader.GetSource(), reader.Line(), \
reader.LineNumber(), pos - reader.Line() )
// Token delimiters.
const char* delims = " \t\r\n";
// Tokens to read/save graphic lines style
#define T_STYLE "style"
#define T_COLOR "rgb" // cannot be modifed (used by wxWidgets)
#define T_COLORA "rgba" // cannot be modifed (used by wxWidgets)
#define T_WIDTH "width"
static bool is_eol( char c )
{
// The default file eol character used internally by KiCad.
// |
// | Possible eol if someone edited the file by hand on certain platforms.
// | |
// | | May have gone past eol with strtok().
// | | |
if( c == '\n' || c == '\r' || c == 0 )
return true;
return false;
}
/**
* Compare \a aString to the string starting at \a aLine and advances the character point to
* the end of \a String and returns the new pointer position in \a aOutput if it is not NULL.
*
* @param aString - A pointer to the string to compare.
* @param aLine - A pointer to string to begin the comparison.
* @param aOutput - A pointer to a string pointer to the end of the comparison if not NULL.
* @return true if \a aString was found starting at \a aLine. Otherwise false.
*/
static bool strCompare( const char* aString, const char* aLine, const char** aOutput = NULL )
{
size_t len = strlen( aString );
bool retv = ( strncasecmp( aLine, aString, len ) == 0 ) &&
( isspace( aLine[ len ] ) || aLine[ len ] == 0 );
if( retv && aOutput )
{
const char* tmp = aLine;
// Move past the end of the token.
tmp += len;
// Move to the beginning of the next token.
while( *tmp && isspace( *tmp ) )
tmp++;
*aOutput = tmp;
}
return retv;
}
/**
* Parse an ASCII integer string with possible leading whitespace into
* an integer and updates the pointer at \a aOutput if it is not NULL, just
* like "man strtol()".
*
* @param aReader - The line reader used to generate exception throw information.
* @param aLine - A pointer the current position in a string.
* @param aOutput - The pointer to a string pointer to copy the string pointer position when
* the parsing is complete.
* @return A valid integer value.
* @throw An #IO_ERROR on an unexpected end of line.
* @throw A #PARSE_ERROR if the parsed token is not a valid integer.
*/
static int parseInt( FILE_LINE_READER& aReader, const char* aLine, const char** aOutput = NULL )
{
if( !*aLine )
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
// Clear errno before calling strtol() in case some other crt call set it.
errno = 0;
long retv = strtol( aLine, (char**) aOutput, 10 );
// Make sure no error occurred when calling strtol().
if( errno == ERANGE )
SCH_PARSE_ERROR( "invalid integer value", aReader, aLine );
// strtol does not strip off whitespace before the next token.
if( aOutput )
{
const char* next = *aOutput;
while( *next && isspace( *next ) )
next++;
*aOutput = next;
}
return (int) retv;
}
/**
* Parse an ASCII hex integer string with possible leading whitespace into
* a long integer and updates the pointer at \a aOutput if it is not NULL, just
* like "man strtol".
*
* @param aReader - The line reader used to generate exception throw information.
* @param aLine - A pointer the current position in a string.
* @param aOutput - The pointer to a string pointer to copy the string pointer position when
* the parsing is complete.
* @return A valid integer value.
* @throw IO_ERROR on an unexpected end of line.
* @throw PARSE_ERROR if the parsed token is not a valid integer.
*/
static unsigned long parseHex( FILE_LINE_READER& aReader, const char* aLine,
const char** aOutput = NULL )
{
if( !*aLine )
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
unsigned long retv;
// Clear errno before calling strtoul() in case some other crt call set it.
errno = 0;
retv = strtoul( aLine, (char**) aOutput, 16 );
// Make sure no error occurred when calling strtoul().
if( errno == ERANGE )
SCH_PARSE_ERROR( "invalid hexadecimal number", aReader, aLine );
// Strip off whitespace before the next token.
if( aOutput )
{
// const char* next = aLine + strlen( token );
const char* next = *aOutput;
while( *next && isspace( *next ) )
next++;
*aOutput = next;
}
return retv;
}
/**
* Parses an ASCII point string with possible leading whitespace into a double precision
* floating point number and updates the pointer at \a aOutput if it is not NULL, just
* like "man strtod".
*
* @param aReader - The line reader used to generate exception throw information.
* @param aLine - A pointer the current position in a string.
* @param aOutput - The pointer to a string pointer to copy the string pointer position when
* the parsing is complete.
* @return A valid double value.
* @throw IO_ERROR on an unexpected end of line.
* @throw PARSE_ERROR if the parsed token is not a valid integer.
*/
static double parseDouble( FILE_LINE_READER& aReader, const char* aLine,
const char** aOutput = NULL )
{
if( !*aLine )
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
// Clear errno before calling strtod() in case some other crt call set it.
errno = 0;
double retv = strtod( aLine, (char**) aOutput );
// Make sure no error occurred when calling strtod().
if( errno == ERANGE )
SCH_PARSE_ERROR( "invalid floating point number", aReader, aLine );
// strtod does not strip off whitespace before the next token.
if( aOutput )
{
const char* next = *aOutput;
while( *next && isspace( *next ) )
next++;
*aOutput = next;
}
return retv;
}
/**
* Parse a single ASCII character and updates the pointer at \a aOutput if it is not NULL.
*
* @param aReader - The line reader used to generate exception throw information.
* @param aCurrentToken - A pointer the current position in a string.
* @param aNextToken - The pointer to a string pointer to copy the string pointer position when
* the parsing is complete.
* @return A valid ASCII character.
* @throw IO_ERROR on an unexpected end of line.
* @throw PARSE_ERROR if the parsed token is not a a single character token.
*/
static char parseChar( FILE_LINE_READER& aReader, const char* aCurrentToken,
const char** aNextToken = NULL )
{
while( *aCurrentToken && isspace( *aCurrentToken ) )
aCurrentToken++;
if( !*aCurrentToken )
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
if( !isspace( *( aCurrentToken + 1 ) ) )
SCH_PARSE_ERROR( "expected single character token", aReader, aCurrentToken );
if( aNextToken )
{
const char* next = aCurrentToken + 2;
while( *next && isspace( *next ) )
next++;
*aNextToken = next;
}
return *aCurrentToken;
}
/**
* Parse an unquoted utf8 string and updates the pointer at \a aOutput if it is not NULL.
*
* The parsed string must be a continuous string with no white space.
*
* @param aString - A reference to the parsed string.
* @param aReader - The line reader used to generate exception throw information.
* @param aCurrentToken - A pointer the current position in a string.
* @param aNextToken - The pointer to a string pointer to copy the string pointer position when
* the parsing is complete.
* @param aCanBeEmpty - True if the parsed string is optional. False if it is mandatory.
* @throw IO_ERROR on an unexpected end of line.
* @throw PARSE_ERROR if the \a aCanBeEmpty is false and no string was parsed.
*/
static void parseUnquotedString( wxString& aString, FILE_LINE_READER& aReader,
const char* aCurrentToken, const char** aNextToken = NULL,
bool aCanBeEmpty = false )
{
if( !*aCurrentToken )
{
if( aCanBeEmpty )
return;
else
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
}
const char* tmp = aCurrentToken;
while( *tmp && isspace( *tmp ) )
tmp++;
if( !*tmp )
{
if( aCanBeEmpty )
return;
else
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
}
std::string utf8;
while( *tmp && !isspace( *tmp ) )
utf8 += *tmp++;
aString = FROM_UTF8( utf8.c_str() );
if( aString.IsEmpty() && !aCanBeEmpty )
SCH_PARSE_ERROR( _( "expected unquoted string" ), aReader, aCurrentToken );
if( aNextToken )
{
const char* next = tmp;
while( *next && isspace( *next ) )
next++;
*aNextToken = next;
}
}
/**
* Parse an quoted ASCII utf8 and updates the pointer at \a aOutput if it is not NULL.
*
* The parsed string must be contained within a single line. There are no multi-line
* quoted strings in the legacy schematic file format.
*
* @param aString - A reference to the parsed string.
* @param aReader - The line reader used to generate exception throw information.
* @param aCurrentToken - A pointer the current position in a string.
* @param aNextToken - The pointer to a string pointer to copy the string pointer position when
* the parsing is complete.
* @param aCanBeEmpty - True if the parsed string is optional. False if it is mandatory.
* @throw IO_ERROR on an unexpected end of line.
* @throw PARSE_ERROR if the \a aCanBeEmpty is false and no string was parsed.
*/
static void parseQuotedString( wxString& aString, FILE_LINE_READER& aReader,
const char* aCurrentToken, const char** aNextToken = NULL,
bool aCanBeEmpty = false )
{
if( !*aCurrentToken )
{
if( aCanBeEmpty )
return;
else
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
}
const char* tmp = aCurrentToken;
while( *tmp && isspace( *tmp ) )
tmp++;
if( !*tmp )
{
if( aCanBeEmpty )
return;
else
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
}
// Verify opening quote.
if( *tmp != '"' )
SCH_PARSE_ERROR( "expecting opening quote", aReader, aCurrentToken );
tmp++;
std::string utf8; // utf8 without escapes and quotes.
// Fetch everything up to closing quote.
while( *tmp )
{
if( *tmp == '\\' )
{
tmp++;
if( !*tmp )
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
// Do not copy the escape byte if it is followed by \ or "
if( *tmp != '"' && *tmp != '\\' )
utf8 += '\\';
utf8 += *tmp;
}
else if( *tmp == '"' ) // Closing double quote.
{
break;
}
else
{
utf8 += *tmp;
}
tmp++;
}
aString = FROM_UTF8( utf8.c_str() );
if( aString.IsEmpty() && !aCanBeEmpty )
SCH_PARSE_ERROR( "expected quoted string", aReader, aCurrentToken );
if( *tmp && *tmp != '"' )
SCH_PARSE_ERROR( "no closing quote for string found", aReader, tmp );
// Move past the closing quote.
tmp++;
if( aNextToken )
{
const char* next = tmp;
while( *next && *next == ' ' )
next++;
*aNextToken = next;
}
}
/**
* A cache assistant for the part library portion of the #SCH_PLUGIN API, and only for the
* #SCH_LEGACY_PLUGIN, so therefore is private to this implementation file, i.e. not placed
* into a header.
*/
class SCH_LEGACY_PLUGIN_CACHE
{
static int m_modHash; // Keep track of the modification status of the library.
wxString m_fileName; // Absolute path and file name.
wxFileName m_libFileName; // Absolute path and file name is required here.
wxDateTime m_fileModTime;
LIB_ALIAS_MAP m_aliases; // Map of names of LIB_ALIAS pointers.
bool m_isWritable;
bool m_isModified;
int m_versionMajor;
int m_versionMinor;
int m_libType; // Is this cache a component or symbol library.
LIB_PART* loadPart( FILE_LINE_READER& aReader );
void loadHeader( FILE_LINE_READER& aReader );
void loadAliases( std::unique_ptr< LIB_PART >& aPart, FILE_LINE_READER& aReader );
void loadField( std::unique_ptr< LIB_PART >& aPart, FILE_LINE_READER& aReader );
void loadDrawEntries( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader );
void loadFootprintFilters( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader );
void loadDocs();
LIB_ARC* loadArc( std::unique_ptr< LIB_PART >& aPart, FILE_LINE_READER& aReader );
LIB_CIRCLE* loadCircle( std::unique_ptr< LIB_PART >& aPart, FILE_LINE_READER& aReader );
LIB_TEXT* loadText( std::unique_ptr< LIB_PART >& aPart, FILE_LINE_READER& aReader );
LIB_RECTANGLE* loadRectangle( std::unique_ptr< LIB_PART >& aPart, FILE_LINE_READER& aReader );
LIB_PIN* loadPin( std::unique_ptr< LIB_PART >& aPart, FILE_LINE_READER& aReader );
LIB_POLYLINE* loadPolyLine( std::unique_ptr< LIB_PART >& aPart, FILE_LINE_READER& aReader );
LIB_BEZIER* loadBezier( std::unique_ptr< LIB_PART >& aPart, FILE_LINE_READER& aReader );
FILL_T parseFillMode( FILE_LINE_READER& aReader, const char* aLine,
const char** aOutput );
bool checkForDuplicates( wxString& aAliasName );
LIB_ALIAS* removeAlias( LIB_ALIAS* aAlias );
void saveDocFile();
void saveSymbol( LIB_PART* aSymbol,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter );
void saveArc( LIB_ARC* aArc, std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter );
void saveBezier( LIB_BEZIER* aBezier,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter );
void saveCircle( LIB_CIRCLE* aCircle,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter );
void saveField( LIB_FIELD* aField,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter );
void savePin( LIB_PIN* aPin, std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter );
void savePolyLine( LIB_POLYLINE* aPolyLine,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter );
void saveRectangle( LIB_RECTANGLE* aRectangle,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter );
void saveText( LIB_TEXT* aText,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter );
friend SCH_LEGACY_PLUGIN;
public:
SCH_LEGACY_PLUGIN_CACHE( const wxString& aLibraryPath );
~SCH_LEGACY_PLUGIN_CACHE();
int GetModifyHash() const { return m_modHash; }
// Most all functions in this class throw IO_ERROR exceptions. There are no
// error codes nor user interface calls from here, nor in any SCH_PLUGIN objects.
// Catch these exceptions higher up please.
/// Save the entire library to file m_libFileName;
void Save( bool aSaveDocFile = true );
void Load();
void AddSymbol( const LIB_PART* aPart );
void DeleteAlias( const wxString& aAliasName );
void DeleteSymbol( const wxString& aAliasName );
wxDateTime GetLibModificationTime();
bool IsFile( const wxString& aFullPathAndFileName ) const;
bool IsFileChanged() const;
void SetModified( bool aModified = true ) { m_isModified = aModified; }
wxString GetLogicalName() const { return m_libFileName.GetName(); }
void SetFileName( const wxString& aFileName ) { m_libFileName = aFileName; }
wxString GetFileName() const { return m_libFileName.GetFullPath(); }
};
SCH_LEGACY_PLUGIN::SCH_LEGACY_PLUGIN()
{
init( NULL );
}
SCH_LEGACY_PLUGIN::~SCH_LEGACY_PLUGIN()
{
delete m_cache;
}
void SCH_LEGACY_PLUGIN::init( KIWAY* aKiway, const PROPERTIES* aProperties )
{
m_version = 0;
m_rootSheet = NULL;
m_props = aProperties;
m_kiway = aKiway;
m_cache = NULL;
m_out = NULL;
}
SCH_SHEET* SCH_LEGACY_PLUGIN::Load( const wxString& aFileName, KIWAY* aKiway,
SCH_SHEET* aAppendToMe, const PROPERTIES* aProperties )
{
wxASSERT( !aFileName || aKiway != NULL );
LOCALE_IO toggle; // toggles on, then off, the C locale.
SCH_SHEET* sheet;
wxFileName fn = aFileName;
// Unfortunately child sheet file names the legacy schematic file format are not fully
// qualified and are always appended to the project path. The aFileName attribute must
// always be an absolute path so the project path can be used for load child sheet files.
wxASSERT( fn.IsAbsolute() );
if( aAppendToMe )
{
wxLogTrace( traceSchLegacyPlugin, "Append \"%s\" to sheet \"%s\".",
aFileName, aAppendToMe->GetFileName() );
wxFileName normedFn = aAppendToMe->GetFileName();
if( !normedFn.IsAbsolute() )
{
if( aFileName.Right( normedFn.GetFullPath().Length() ) == normedFn.GetFullPath() )
m_path = aFileName.Left( aFileName.Length() - normedFn.GetFullPath().Length() );
}
if( m_path.IsEmpty() )
m_path = aKiway->Prj().GetProjectPath();
wxLogTrace( traceSchLegacyPlugin, "Normalized append path \"%s\".", m_path );
}
else
{
m_path = aKiway->Prj().GetProjectPath();
}
m_currentPath.push( m_path );
init( aKiway, aProperties );
if( aAppendToMe == NULL )
{
// Clean up any allocated memory if an exception occurs loading the schematic.
std::unique_ptr< SCH_SHEET > newSheet( new SCH_SHEET );
newSheet->SetFileName( aFileName );
m_rootSheet = newSheet.get();
loadHierarchy( newSheet.get() );
// If we got here, the schematic loaded successfully.
sheet = newSheet.release();
}
else
{
m_rootSheet = aAppendToMe->GetRootSheet();
wxASSERT( m_rootSheet != NULL );
sheet = aAppendToMe;
loadHierarchy( sheet );
}
wxASSERT( m_currentPath.size() == 1 ); // only the project path should remain
return sheet;
}
// Everything below this comment is recursive. Modify with care.
void SCH_LEGACY_PLUGIN::loadHierarchy( SCH_SHEET* aSheet )
{
SCH_SCREEN* screen = NULL;
if( !aSheet->GetScreen() )
{
// SCH_SCREEN objects store the full path and file name where the SCH_SHEET object only
// stores the file name and extension. Add the project path to the file name and
// extension to compare when calling SCH_SHEET::SearchHierarchy().
wxFileName fileName = aSheet->GetFileName();
if( !fileName.IsAbsolute() )
fileName.MakeAbsolute( m_currentPath.top() );
// Save the current path so that it gets restored when decending and ascending the
// sheet hierarchy which allows for sheet schematic files to be nested in folders
// relative to the last path a schematic was loaded from.
wxLogTrace( traceSchLegacyPlugin, "Saving path \"%s\"", m_currentPath.top() );
m_currentPath.push( fileName.GetPath() );
wxLogTrace( traceSchLegacyPlugin, "Current path \"%s\"", m_currentPath.top() );
wxLogTrace( traceSchLegacyPlugin, "Loading \"%s\"", fileName.GetFullPath() );
m_rootSheet->SearchHierarchy( fileName.GetFullPath(), &screen );
if( screen )
{
aSheet->SetScreen( screen );
// Do not need to load the sub-sheets - this has already been done.
}
else
{
aSheet->SetScreen( new SCH_SCREEN( m_kiway ) );
aSheet->GetScreen()->SetFileName( fileName.GetFullPath() );
try
{
loadFile( fileName.GetFullPath(), aSheet->GetScreen() );
EDA_ITEM* item = aSheet->GetScreen()->GetDrawItems();
while( item )
{
if( item->Type() == SCH_SHEET_T )
{
SCH_SHEET* sheet = (SCH_SHEET*) item;
// Set the parent to aSheet. This effectively creates a method to find
// the root sheet from any sheet so a pointer to the root sheet does not
// need to be stored globally. Note: this is not the same as a hierarchy.
// Complex hierarchies can have multiple copies of a sheet. This only
// provides a simple tree to find the root sheet.
sheet->SetParent( aSheet );
// Recursion starts here.
loadHierarchy( sheet );
}
item = item->Next();
}
}
catch( const IO_ERROR& ioe )
{
// If there is a problem loading the root sheet, there is no recovery.
if( aSheet == m_rootSheet )
throw( ioe );
// For all subsheets, queue up the error message for the caller.
if( !m_error.IsEmpty() )
m_error += "\n";
m_error += ioe.What();
}
}
m_currentPath.pop();
wxLogTrace( traceSchLegacyPlugin, "Restoring path \"%s\"", m_currentPath.top() );
}
}
void SCH_LEGACY_PLUGIN::loadFile( const wxString& aFileName, SCH_SCREEN* aScreen )
{
FILE_LINE_READER reader( aFileName );
loadHeader( reader, aScreen );
while( reader.ReadLine() )
{
char* line = reader.Line();
while( *line && *line == ' ' )
line++;
// Either an object will be loaded properly or the file load will fail and raise
// an exception.
if( strCompare( "$Descr", line ) )
loadPageSettings( reader, aScreen );
else if( strCompare( "$Comp", line ) )
aScreen->Append( loadComponent( reader ) );
else if( strCompare( "$Sheet", line ) )
aScreen->Append( loadSheet( reader ) );
else if( strCompare( "$Bitmap", line ) )
aScreen->Append( loadBitmap( reader ) );
else if( strCompare( "Connection", line ) )
aScreen->Append( loadJunction( reader ) );
else if( strCompare( "NoConn", line ) )
aScreen->Append( loadNoConnect( reader ) );
else if( strCompare( "Wire", line ) )
aScreen->Append( loadWire( reader ) );
else if( strCompare( "Entry", line ) )
aScreen->Append( loadBusEntry( reader ) );
else if( strCompare( "Text", line ) )
aScreen->Append( loadText( reader ) );
else if( strCompare( "$EndSCHEMATC", line ) )
return;
}
// Unfortunately schematic files prior to version 2 are not terminated with $EndSCHEMATC
// so checking for it's existance will fail so just exit here and take our chances. :(
if( m_version > 1 )
THROW_IO_ERROR( "'$EndSCHEMATC' not found" );
}
void SCH_LEGACY_PLUGIN::loadHeader( FILE_LINE_READER& aReader, SCH_SCREEN* aScreen )
{
const char* line = aReader.ReadLine();
if( !line || !strCompare( "Eeschema Schematic File Version", line, &line ) )
{
m_error.Printf( _( "\"%s\" does not appear to be an Eeschema file" ),
GetChars( aScreen->GetFileName() ) );
THROW_IO_ERROR( m_error );
}
// get the file version here.
m_version = parseInt( aReader, line, &line );
// The next lines are the lib list section, and are mainly comments, like:
// LIBS:power
// the lib list is not used, but is in schematic file just in case.
// It is usually not empty, but we accept empty list.
// If empty, there is a legacy section, not used
// EELAYER i j
// and the last line is
// EELAYER END
// Skip all lines until the end of header "EELAYER END" is found
while( aReader.ReadLine() )
{
line = aReader.Line();
while( *line == ' ' )
line++;
if( strCompare( "EELAYER END", line ) )
return;
}
THROW_IO_ERROR( _( "Missing 'EELAYER END'" ) );
}
void SCH_LEGACY_PLUGIN::loadPageSettings( FILE_LINE_READER& aReader, SCH_SCREEN* aScreen )
{
wxASSERT( aScreen != NULL );
wxString buf;
const char* line = aReader.Line();
PAGE_INFO pageInfo;
TITLE_BLOCK tb;
wxCHECK_RET( strCompare( "$Descr", line, &line ), "Invalid sheet description" );
parseUnquotedString( buf, aReader, line, &line );
if( !pageInfo.SetType( buf ) )
SCH_PARSE_ERROR( "invalid page size", aReader, line );
int pagew = parseInt( aReader, line, &line );
int pageh = parseInt( aReader, line, &line );
if( buf == PAGE_INFO::Custom )
{
pageInfo.SetWidthMils( pagew );
pageInfo.SetHeightMils( pageh );
}
else
{
wxString orientation;
// Non custom size, set portrait if its present. Can be empty string which defaults
// to landscape.
parseUnquotedString( orientation, aReader, line, &line, true );
if( orientation == "portrait" )
pageInfo.SetPortrait( true );
}
aScreen->SetPageSettings( pageInfo );
while( line != NULL )
{
buf.clear();
if( !aReader.ReadLine() )
SCH_PARSE_ERROR( _( "unexpected end of file" ), aReader, line );
line = aReader.Line();
if( strCompare( "Sheet", line, &line ) )
{
aScreen->m_ScreenNumber = parseInt( aReader, line, &line );
aScreen->m_NumberOfScreens = parseInt( aReader, line, &line );
}
else if( strCompare( "Title", line, &line ) )
{
parseQuotedString( buf, aReader, line, &line, true );
tb.SetTitle( buf );
}
else if( strCompare( "Date", line, &line ) )
{
parseQuotedString( buf, aReader, line, &line, true );
tb.SetDate( buf );
}
else if( strCompare( "Rev", line, &line ) )
{
parseQuotedString( buf, aReader, line, &line, true );
tb.SetRevision( buf );
}
else if( strCompare( "Comp", line, &line ) )
{
parseQuotedString( buf, aReader, line, &line, true );
tb.SetCompany( buf );
}
else if( strCompare( "Comment1", line, &line ) )
{
parseQuotedString( buf, aReader, line, &line, true );
tb.SetComment1( buf );
}
else if( strCompare( "Comment2", line, &line ) )
{
parseQuotedString( buf, aReader, line, &line, true );
tb.SetComment2( buf );
}
else if( strCompare( "Comment3", line, &line ) )
{
parseQuotedString( buf, aReader, line, &line, true );
tb.SetComment3( buf );
}
else if( strCompare( "Comment4", line, &line ) )
{
parseQuotedString( buf, aReader, line, &line, true );
tb.SetComment4( buf );
}
else if( strCompare( "$EndDescr", line ) )
{
aScreen->SetTitleBlock( tb );
return;
}
}
SCH_PARSE_ERROR( "missing 'EndDescr'", aReader, line );
}
SCH_SHEET* SCH_LEGACY_PLUGIN::loadSheet( FILE_LINE_READER& aReader )
{
std::unique_ptr< SCH_SHEET > sheet( new SCH_SHEET() );
sheet->SetTimeStamp( GetNewTimeStamp() );
const char* line = aReader.ReadLine();
while( line != NULL )
{
if( strCompare( "S", line, &line ) ) // Sheet dimensions.
{
wxPoint position;
position.x = parseInt( aReader, line, &line );
position.y = parseInt( aReader, line, &line );
sheet->SetPosition( position );
wxSize size;
size.SetWidth( parseInt( aReader, line, &line ) );
size.SetHeight( parseInt( aReader, line, &line ) );
sheet->SetSize( size );
}
else if( strCompare( "U", line, &line ) ) // Sheet time stamp.
{
sheet->SetTimeStamp( parseHex( aReader, line ) );
}
else if( *line == 'F' ) // Sheet field.
{
line++;
wxString text;
int size;
int fieldId = parseInt( aReader, line, &line );
if( fieldId == 0 || fieldId == 1 ) // Sheet name and file name.
{
parseQuotedString( text, aReader, line, &line );
size = parseInt( aReader, line, &line );
if( fieldId == 0 )
{
sheet->SetName( text );
sheet->SetSheetNameSize( size );
}
else
{
sheet->SetFileName( text );
sheet->SetFileNameSize( size );
}
}
else // Sheet pin.
{
std::unique_ptr< SCH_SHEET_PIN > sheetPin( new SCH_SHEET_PIN( sheet.get() ) );
sheetPin->SetNumber( fieldId );
// Can be empty fields.
parseQuotedString( text, aReader, line, &line, true );
sheetPin->SetText( text );
if( line == NULL )
THROW_IO_ERROR( _( "unexpected end of line" ) );
switch( parseChar( aReader, line, &line ) )
{
case 'I':
sheetPin->SetShape( NET_INPUT );
break;
case 'O':
sheetPin->SetShape( NET_OUTPUT );
break;
case 'B':
sheetPin->SetShape( NET_BIDI );
break;
case 'T':
sheetPin->SetShape( NET_TRISTATE );
break;
case 'U':
sheetPin->SetShape( NET_UNSPECIFIED );
break;
default:
SCH_PARSE_ERROR( "invalid sheet pin type", aReader, line );
}
switch( parseChar( aReader, line, &line ) )
{
case 'R': /* pin on right side */
sheetPin->SetEdge( SCH_SHEET_PIN::SHEET_RIGHT_SIDE );
break;
case 'T': /* pin on top side */
sheetPin->SetEdge( SCH_SHEET_PIN::SHEET_TOP_SIDE );
break;
case 'B': /* pin on bottom side */
sheetPin->SetEdge( SCH_SHEET_PIN::SHEET_BOTTOM_SIDE );
break;
case 'L': /* pin on left side */
sheetPin->SetEdge( SCH_SHEET_PIN::SHEET_LEFT_SIDE );
break;
default:
SCH_PARSE_ERROR( "invalid sheet pin side", aReader, line );
}
wxPoint position;
position.x = parseInt( aReader, line, &line );
position.y = parseInt( aReader, line, &line );
sheetPin->SetPosition( position );
size = parseInt( aReader, line, &line );
sheetPin->SetTextSize( wxSize( size, size ) );
sheet->AddPin( sheetPin.release() );
}
}
else if( strCompare( "$EndSheet", line ) )
return sheet.release();
line = aReader.ReadLine();
}
SCH_PARSE_ERROR( "missing '$EndSheet`", aReader, line );
return NULL; // Prevents compiler warning. Should never get here.
}
SCH_BITMAP* SCH_LEGACY_PLUGIN::loadBitmap( FILE_LINE_READER& aReader )
{
std::unique_ptr< SCH_BITMAP > bitmap( new SCH_BITMAP );
const char* line = aReader.Line();
wxCHECK( strCompare( "$Bitmap", line, &line ), NULL );
line = aReader.ReadLine();
while( line != NULL )
{
if( strCompare( "Pos", line, &line ) )
{
wxPoint position;
position.x = parseInt( aReader, line, &line );
position.y = parseInt( aReader, line, &line );
bitmap->SetPosition( position );
}
else if( strCompare( "Scale", line, &line ) )
{
/// @todo Make m_scale private and add accessors.
bitmap->GetImage()->SetScale( parseDouble( aReader, line, &line ) );
}
else if( strCompare( "Data", line, &line ) )
{
wxMemoryOutputStream stream;
while( line )
{
if( !aReader.ReadLine() )
SCH_PARSE_ERROR( _( "Unexpected end of file" ), aReader, line );
line = aReader.Line();
if( strCompare( "EndData", line ) )
{
// all the PNG date is read.
// We expect here m_image and m_bitmap are void
wxImage* image = new wxImage();
wxMemoryInputStream istream( stream );
image->LoadFile( istream, wxBITMAP_TYPE_PNG );
bitmap->GetImage()->SetImage( image );
bitmap->GetImage()->SetBitmap( new wxBitmap( *image ) );
break;
}
// Read PNG data, stored in hexadecimal,
// each byte = 2 hexadecimal digits and a space between 2 bytes
// and put it in memory stream buffer
int len = strlen( line );
for( ; len > 0 && !isspace( *line ); len -= 3, line += 3 )
{
int value = 0;
if( sscanf( line, "%X", &value ) == 1 )
stream.PutC( (char) value );
else
THROW_IO_ERROR( "invalid PNG data" );
}
}
if( line == NULL )
THROW_IO_ERROR( _( "unexpected end of file" ) );
}
else if( strCompare( "$EndBitmap", line ) )
return bitmap.release();
line = aReader.ReadLine();
}
THROW_IO_ERROR( _( "unexpected end of file" ) );
}
SCH_JUNCTION* SCH_LEGACY_PLUGIN::loadJunction( FILE_LINE_READER& aReader )
{
std::unique_ptr< SCH_JUNCTION > junction( new SCH_JUNCTION );
const char* line = aReader.Line();
wxCHECK( strCompare( "Connection", line, &line ), NULL );
wxString name;
parseUnquotedString( name, aReader, line, &line );
wxPoint position;
position.x = parseInt( aReader, line, &line );
position.y = parseInt( aReader, line, &line );
junction->SetPosition( position );
return junction.release();
}
SCH_NO_CONNECT* SCH_LEGACY_PLUGIN::loadNoConnect( FILE_LINE_READER& aReader )
{
std::unique_ptr< SCH_NO_CONNECT > no_connect( new SCH_NO_CONNECT );
const char* line = aReader.Line();
wxCHECK( strCompare( "NoConn", line, &line ), NULL );
wxString name;
parseUnquotedString( name, aReader, line, &line );
wxPoint position;
position.x = parseInt( aReader, line, &line );
position.y = parseInt( aReader, line, &line );
no_connect->SetPosition( position );
return no_connect.release();
}
SCH_LINE* SCH_LEGACY_PLUGIN::loadWire( FILE_LINE_READER& aReader )
{
std::unique_ptr< SCH_LINE > wire( new SCH_LINE );
const char* line = aReader.Line();
wxCHECK( strCompare( "Wire", line, &line ), NULL );
if( strCompare( "Wire", line, &line ) )
wire->SetLayer( LAYER_WIRE );
else if( strCompare( "Bus", line, &line ) )
wire->SetLayer( LAYER_BUS );
else if( strCompare( "Notes", line, &line ) )
wire->SetLayer( LAYER_NOTES );
else
SCH_PARSE_ERROR( "invalid line type", aReader, line );
if( !strCompare( "Line", line, &line ) )
SCH_PARSE_ERROR( "invalid wire definition", aReader, line );
// Since Sept 15, 2017, a line style is alloved (width, style, color)
// Only non default values are stored
while( !is_eol( *line ) )
{
wxString buf;
parseUnquotedString( buf, aReader, line, &line );
if( buf == ")" )
continue;
else if( buf == T_WIDTH )
{
int size = parseInt( aReader, line, &line );
wire->SetLineWidth( size );
}
else if( buf == T_STYLE )
{
parseUnquotedString( buf, aReader, line, &line );
int style = SCH_LINE::GetLineStyleInternalId( buf );
wire->SetLineStyle( style );
}
else // should be the color parameter.
{
// The color param is something like rgb(150, 40, 191)
// and because there is no space between ( and 150
// the first param is inside buf.
// So break keyword and the first param into 2 separate strings.
wxString prm, keyword;
keyword = buf.BeforeLast( '(', &prm );
if( ( keyword == T_COLOR ) || ( keyword == T_COLORA ) )
{
long color[4] = { 0 };
int ii = 0;
if( !prm.IsEmpty() )
{
prm.ToLong( &color[ii] );
ii++;
}
int prm_count = ( keyword == T_COLORA ) ? 4 : 3;
// fix opacity to 1.0 or 255, when not exists in file
color[3] = 255;
for(; ii < prm_count && !is_eol( *line ); ii++ )
color[ii] = parseInt( aReader, line, &line );
wire->SetLineColor( color[0]/255.0, color[1]/255.0, color[2]/255.0,color[3]/255.0 );
}
}
}
// Read the segment en points coordinates:
line = aReader.ReadLine();
wxPoint begin, end;
begin.x = parseInt( aReader, line, &line );
begin.y = parseInt( aReader, line, &line );
end.x = parseInt( aReader, line, &line );
end.y = parseInt( aReader, line, &line );
wire->SetStartPoint( begin );
wire->SetEndPoint( end );
return wire.release();
}
SCH_BUS_ENTRY_BASE* SCH_LEGACY_PLUGIN::loadBusEntry( FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK( strCompare( "Entry", line, &line ), NULL );
std::unique_ptr< SCH_BUS_ENTRY_BASE > busEntry;
if( strCompare( "Wire", line, &line ) )
{
busEntry.reset( new SCH_BUS_WIRE_ENTRY );
if( !strCompare( "Line", line, &line ) )
SCH_PARSE_ERROR( "invalid bus entry definition expected 'Line'", aReader, line );
}
else if( strCompare( "Bus", line, &line ) )
{
busEntry.reset( new SCH_BUS_BUS_ENTRY );
if( !strCompare( "Bus", line, &line ) )
SCH_PARSE_ERROR( "invalid bus entry definition expected 'Bus'", aReader, line );
}
else
SCH_PARSE_ERROR( "invalid bus entry type", aReader, line );
line = aReader.ReadLine();
wxPoint pos;
wxSize size;
pos.x = parseInt( aReader, line, &line );
pos.y = parseInt( aReader, line, &line );
size.x = parseInt( aReader, line, &line );
size.y = parseInt( aReader, line, &line );
size.x -= pos.x;
size.y -= pos.y;
busEntry->SetPosition( pos );
busEntry->SetSize( size );
return busEntry.release();
}
SCH_TEXT* SCH_LEGACY_PLUGIN::loadText( FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK( strCompare( "Text", line, &line ), NULL );
std::unique_ptr< SCH_TEXT> text;
if( strCompare( "Notes", line, &line ) )
text.reset( new SCH_TEXT );
else if( strCompare( "Label", line, &line ) )
text.reset( new SCH_LABEL );
else if( strCompare( "HLabel", line, &line ) )
text.reset( new SCH_HIERLABEL );
else if( strCompare( "GLabel", line, &line ) )
{
// Prior to version 2, the SCH_GLOBALLABEL object did not exist.
if( m_version == 1 )
text.reset( new SCH_HIERLABEL );
else
text.reset( new SCH_GLOBALLABEL );
}
else
SCH_PARSE_ERROR( "unknown Text type", aReader, line );
// Parse the parameters common to all text objects.
wxPoint position;
position.x = parseInt( aReader, line, &line );
position.y = parseInt( aReader, line, &line );
text->SetPosition( position );
text->SetLabelSpinStyle( parseInt( aReader, line, &line ) );
int size = parseInt( aReader, line, &line );
text->SetTextSize( wxSize( size, size ) );
// Parse the global and hierarchical label type.
if( text->Type() == SCH_HIERARCHICAL_LABEL_T || text->Type() == SCH_GLOBAL_LABEL_T )
{
if( strCompare( SheetLabelType[NET_INPUT], line, &line ) )
text->SetShape( NET_INPUT );
else if( strCompare( SheetLabelType[NET_OUTPUT], line, &line ) )
text->SetShape( NET_OUTPUT );
else if( strCompare( SheetLabelType[NET_BIDI], line, &line ) )
text->SetShape( NET_BIDI );
else if( strCompare( SheetLabelType[NET_TRISTATE], line, &line ) )
text->SetShape( NET_TRISTATE );
else if( strCompare( SheetLabelType[NET_UNSPECIFIED], line, &line ) )
text->SetShape( NET_UNSPECIFIED );
else
SCH_PARSE_ERROR( "invalid label type", aReader, line );
}
int thickness = 0;
// The following tokens do not exist in version 1 schematic files,
// and not always in version 2 for HLabels and GLabels
if( m_version > 1 )
{
if( m_version > 2 || *line >= ' ' )
{
if( strCompare( "Italic", line, &line ) )
text->SetItalic( true );
else if( !strCompare( "~", line, &line ) )
SCH_PARSE_ERROR( _( "expected 'Italics' or '~'" ), aReader, line );
}
// The thickness token does not exist in older versions of the schematic file format
// so calling parseInt will be made only if the EOL is not reached.
if( *line >= ' ' )
thickness = parseInt( aReader, line, &line );
}
text->SetBold( thickness != 0 );
text->SetThickness( thickness != 0 ? GetPenSizeForBold( size ) : 0 );
// Read the text string for the text.
char* tmp = aReader.ReadLine();
tmp = strtok( tmp, "\r\n" );
wxString val = FROM_UTF8( tmp );
for( ; ; )
{
int i = val.find( wxT( "\\n" ) );
if( i == wxNOT_FOUND )
break;
val.erase( i, 2 );
val.insert( i, wxT( "\n" ) );
}
text->SetText( val );
return text.release();
}
SCH_COMPONENT* SCH_LEGACY_PLUGIN::loadComponent( FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK( strCompare( "$Comp", line, &line ), NULL );
std::unique_ptr< SCH_COMPONENT > component( new SCH_COMPONENT() );
line = aReader.ReadLine();
while( line != NULL )
{
if( strCompare( "L", line, &line ) )
{
wxString libName;
parseUnquotedString( libName, aReader, line, &line );
libName.Replace( "~", " " );
LIB_ID libId;
// Prior to schematic version 4, library IDs did not have a library nickname so
// parsing the symbol name with LIB_ID::Parse() would break symbol library links
// that contained '/' and ':' characters.
if( m_version > 3 )
libId.Parse( libName, LIB_ID::ID_SCH, true );
else
libId.SetLibItemName( libName, false );
component->SetLibId( libId );
wxString refDesignator;
parseUnquotedString( refDesignator, aReader, line, &line );
refDesignator.Replace( "~", " " );
wxString prefix = refDesignator;
while( prefix.Length() )
{
if( ( prefix.Last() < '0' || prefix.Last() > '9') && prefix.Last() != '?' )
break;
prefix.RemoveLast();
}
// Avoid a prefix containing trailing/leading spaces
prefix.Trim( true );
prefix.Trim( false );
if( prefix.IsEmpty() )
component->SetPrefix( wxString( "U" ) );
else
component->SetPrefix( prefix );
}
else if( strCompare( "U", line, &line ) )
{
// This fixes a potentially buggy files caused by unit being set to zero which
// causes netlist issues. See https://bugs.launchpad.net/kicad/+bug/1677282.
int unit = parseInt( aReader, line, &line );
if( unit == 0 )
{
unit = 1;
// Set the file as modified so the user can be warned.
if( m_rootSheet && m_rootSheet->GetScreen() )
m_rootSheet->GetScreen()->SetModify();
}
component->SetUnit( unit );
component->SetConvert( parseInt( aReader, line, &line ) );
component->SetTimeStamp( parseHex( aReader, line, &line ) );
}
else if( strCompare( "P", line, &line ) )
{
wxPoint pos;
pos.x = parseInt( aReader, line, &line );
pos.y = parseInt( aReader, line, &line );
component->SetPosition( pos );
}
else if( strCompare( "AR", line, &line ) )
{
const char* strCompare = "Path=";
int len = strlen( strCompare );
if( strncasecmp( strCompare, line, len ) != 0 )
SCH_PARSE_ERROR( "missing 'Path=' token", aReader, line );
line += len;
wxString path, reference, unit;
parseQuotedString( path, aReader, line, &line );
strCompare = "Ref=";
len = strlen( strCompare );
if( strncasecmp( strCompare, line, len ) != 0 )
SCH_PARSE_ERROR( "missing 'Ref=' token", aReader, line );
line+= len;
parseQuotedString( reference, aReader, line, &line );
strCompare = "Part=";
len = strlen( strCompare );
if( strncasecmp( strCompare, line, len ) != 0 )
SCH_PARSE_ERROR( "missing 'Part=' token", aReader, line );
line+= len;
parseQuotedString( unit, aReader, line, &line );
long tmp;
if( !unit.ToLong( &tmp, 10 ) )
SCH_PARSE_ERROR( "expected integer value", aReader, line );
if( tmp < 0 || tmp > MAX_UNIT_COUNT_PER_PACKAGE )
SCH_PARSE_ERROR( "unit value out of range", aReader, line );
component->AddHierarchicalReference( path, reference, (int)tmp );
component->GetField( REFERENCE )->SetText( reference );
}
else if( strCompare( "F", line, &line ) )
{
int index = parseInt( aReader, line, &line );
wxString text, name;
parseQuotedString( text, aReader, line, &line, true );
char orientation = parseChar( aReader, line, &line );
wxPoint pos;
pos.x = parseInt( aReader, line, &line );
pos.y = parseInt( aReader, line, &line );
int size = parseInt( aReader, line, &line );
int attributes = parseHex( aReader, line, &line );
if( index >= component->GetFieldCount() )
{
// The first MANDATOR_FIELDS _must_ be constructed within
// the SCH_COMPONENT constructor. This assert is simply here
// to guard against a change in that constructor.
wxASSERT( component->GetFieldCount() >= MANDATORY_FIELDS );
// Ignore the _supplied_ fieldNdx. It is not important anymore
// if within the user defined fields region (i.e. >= MANDATORY_FIELDS).
// We freely renumber the index to fit the next available field slot.
index = component->GetFieldCount(); // new has this index after insertion
SCH_FIELD field( wxPoint( 0, 0 ), -1, component.get(), name );
component->AddField( field );
}
// Prior to version 2 of the schematic file format, none of the following existed.
if( m_version > 1 )
{
wxString textAttrs;
char hjustify = parseChar( aReader, line, &line );
parseUnquotedString( textAttrs, aReader, line, &line );
// The name of the field is optional.
parseQuotedString( name, aReader, line, &line, true );
if( hjustify == 'L' )
component->GetField( index )->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
else if( hjustify == 'R' )
component->GetField( index )->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
else if( hjustify != 'C' )
SCH_PARSE_ERROR( "component field text horizontal justification must be "
"L, R, or C", aReader, line );
// We are guaranteed to have a least one character here for older file formats
// otherwise an exception would have been raised..
if( textAttrs[0] == 'T' )
component->GetField( index )->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
else if( textAttrs[0] == 'B' )
component->GetField( index )->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
else if( textAttrs[0] != 'C' )
SCH_PARSE_ERROR( "component field text vertical justification must be "
"B, T, or C", aReader, line );
// Newer file formats include the bold and italics text attribute.
if( textAttrs.Length() > 1 )
{
if( textAttrs.Length() != 3 )
SCH_PARSE_ERROR( _( "component field text attributes must be 3 characters wide" ),
aReader, line );
if( textAttrs[1] == 'I' )
component->GetField( index )->SetItalic( true );
else if( textAttrs[1] != 'N' )
SCH_PARSE_ERROR( "component field text italics indicator must be I or N",
aReader, line );
if( textAttrs[2] == 'B' )
component->GetField( index )->SetBold( true );
else if( textAttrs[2] != 'N' )
SCH_PARSE_ERROR( "component field text bold indicator must be B or N",
aReader, line );
}
}
component->GetField( index )->SetText( text );
component->GetField( index )->SetTextPos( pos );
component->GetField( index )->SetVisible( !attributes );
component->GetField( index )->SetTextSize( wxSize( size, size ) );
if( orientation == 'H' )
component->GetField( index )->SetTextAngle( TEXT_ANGLE_HORIZ );
else if( orientation == 'V' )
component->GetField( index )->SetTextAngle( TEXT_ANGLE_VERT );
else
SCH_PARSE_ERROR( "component field orientation must be H or V",
aReader, line );
if( name.IsEmpty() )
name = TEMPLATE_FIELDNAME::GetDefaultFieldName( index );
component->GetField( index )->SetName( name );
}
else if( strCompare( "$EndComp", line ) )
{
// Ensure all flags (some are set by previous initializations) are reset:
component->ClearFlags();
return component.release();
}
else
{
// There are two lines that begin with a tab or spaces that includes a line with the
// redundant position information and the transform matrix settings.
// Parse the redundant position information just the same to check for formatting
// errors.
parseInt( aReader, line, &line ); // Always 1.
parseInt( aReader, line, &line ); // The X coordinate.
parseInt( aReader, line, &line ); // The Y coordinate.
line = aReader.ReadLine();
TRANSFORM transform;
transform.x1 = parseInt( aReader, line, &line );
if( transform.x1 < -1 || transform.x1 > 1 )
SCH_PARSE_ERROR( "invalid component X1 transform value", aReader, line );
transform.y1 = parseInt( aReader, line, &line );
if( transform.y1 < -1 || transform.y1 > 1 )
SCH_PARSE_ERROR( "invalid component Y1 transform value", aReader, line );
transform.x2 = parseInt( aReader, line, &line );
if( transform.x2 < -1 || transform.x2 > 1 )
SCH_PARSE_ERROR( "invalid component X2 transform value", aReader, line );
transform.y2 = parseInt( aReader, line, &line );
if( transform.y2 < -1 || transform.y2 > 1 )
SCH_PARSE_ERROR( "invalid component Y2 transform value", aReader, line );
component->SetTransform( transform );
}
line = aReader.ReadLine();
}
SCH_PARSE_ERROR( "invalid component line", aReader, line );
return NULL; // Prevents compiler warning. Should never get here.
}
void SCH_LEGACY_PLUGIN::Save( const wxString& aFileName, SCH_SCREEN* aScreen, KIWAY* aKiway,
const PROPERTIES* aProperties )
{
wxCHECK_RET( aScreen != NULL, "NULL SCH_SCREEN object." );
wxCHECK_RET( !aFileName.IsEmpty(), "No schematic file name defined." );
LOCALE_IO toggle; // toggles on, then off, the C locale, to write floating point values.
init( aKiway, aProperties );
wxFileName fn = aFileName;
// File names should be absolute. Don't assume everything relative to the project path
// works properly.
wxASSERT( fn.IsAbsolute() );
FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
m_out = &formatter; // no ownership
Format( aScreen );
}
void SCH_LEGACY_PLUGIN::Format( SCH_SCREEN* aScreen )
{
wxCHECK_RET( aScreen != NULL, "NULL SCH_SCREEN* object." );
wxCHECK_RET( m_kiway != NULL, "NULL KIWAY* object." );
// Write the header
m_out->Print( 0, "%s %s %d\n", "EESchema", SCHEMATIC_HEAD_STRING, EESCHEMA_VERSION );
// Write the project libraries.
for( const PART_LIB& lib : *m_kiway->Prj().SchLibs() )
m_out->Print( 0, "LIBS:%s\n", TO_UTF8( lib.GetName() ) );
// This section is not used, but written for file compatibility
m_out->Print( 0, "EELAYER %d %d\n", SCH_LAYER_ID_COUNT, 0 );
m_out->Print( 0, "EELAYER END\n" );
/* Write page info, ScreenNumber and NumberOfScreen; not very meaningful for
* SheetNumber and Sheet Count in a complex hierarchy, but useful in
* simple hierarchy and flat hierarchy. Used also to search the root
* sheet ( ScreenNumber = 1 ) within the files
*/
const TITLE_BLOCK& tb = aScreen->GetTitleBlock();
const PAGE_INFO& page = aScreen->GetPageSettings();
m_out->Print( 0, "$Descr %s %d %d%s\n", TO_UTF8( page.GetType() ),
page.GetWidthMils(),
page.GetHeightMils(),
!page.IsCustom() && page.IsPortrait() ? " portrait" : "" );
m_out->Print( 0, "encoding utf-8\n" );
m_out->Print( 0, "Sheet %d %d\n", aScreen->m_ScreenNumber, aScreen->m_NumberOfScreens );
m_out->Print( 0, "Title %s\n", EscapedUTF8( tb.GetTitle() ).c_str() );
m_out->Print( 0, "Date %s\n", EscapedUTF8( tb.GetDate() ).c_str() );
m_out->Print( 0, "Rev %s\n", EscapedUTF8( tb.GetRevision() ).c_str() );
m_out->Print( 0, "Comp %s\n", EscapedUTF8( tb.GetCompany() ).c_str() );
m_out->Print( 0, "Comment1 %s\n", EscapedUTF8( tb.GetComment1() ).c_str() );
m_out->Print( 0, "Comment2 %s\n", EscapedUTF8( tb.GetComment2() ).c_str() );
m_out->Print( 0, "Comment3 %s\n", EscapedUTF8( tb.GetComment3() ).c_str() );
m_out->Print( 0, "Comment4 %s\n", EscapedUTF8( tb.GetComment4() ).c_str() );
m_out->Print( 0, "$EndDescr\n" );
for( SCH_ITEM* item = aScreen->GetDrawItems(); item; item = item->Next() )
{
switch( item->Type() )
{
case SCH_COMPONENT_T:
saveComponent( static_cast< SCH_COMPONENT* >( item ) );
break;
case SCH_BITMAP_T:
saveBitmap( static_cast< SCH_BITMAP* >( item ) );
break;
case SCH_SHEET_T:
saveSheet( static_cast< SCH_SHEET* >( item ) );
break;
case SCH_JUNCTION_T:
saveJunction( static_cast< SCH_JUNCTION* >( item ) );
break;
case SCH_NO_CONNECT_T:
saveNoConnect( static_cast< SCH_NO_CONNECT* >( item ) );
break;
case SCH_BUS_WIRE_ENTRY_T:
case SCH_BUS_BUS_ENTRY_T:
saveBusEntry( static_cast< SCH_BUS_ENTRY_BASE* >( item ) );
break;
case SCH_LINE_T:
saveLine( static_cast< SCH_LINE* >( item ) );
break;
case SCH_TEXT_T:
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIERARCHICAL_LABEL_T:
saveText( static_cast< SCH_TEXT* >( item ) );
break;
default:
wxASSERT( "Unexpected schematic object type in SCH_LEGACY_PLUGIN::Format()" );
}
}
m_out->Print( 0, "$EndSCHEMATC\n" );
}
void SCH_LEGACY_PLUGIN::saveComponent( SCH_COMPONENT* aComponent )
{
std::string name1;
std::string name2;
wxArrayString reference_fields;
static wxString delimiters( wxT( " " ) );
// This is redundant with the AR entries below, but it makes the files backwards-compatible.
if( aComponent->GetPathsAndReferences().GetCount() > 0 )
{
reference_fields = wxStringTokenize( aComponent->GetPathsAndReferences()[0], delimiters );
name1 = toUTFTildaText( reference_fields[1] );
}
else
{
if( aComponent->GetField( REFERENCE )->GetText().IsEmpty() )
name1 = toUTFTildaText( aComponent->GetPrefix() );
else
name1 = toUTFTildaText( aComponent->GetField( REFERENCE )->GetText() );
}
wxString part_name = aComponent->GetLibId().Format();
if( part_name.size() )
{
name2 = toUTFTildaText( part_name );
}
else
{
name2 = "_NONAME_";
}
m_out->Print( 0, "$Comp\n" );
m_out->Print( 0, "L %s %s\n", name2.c_str(), name1.c_str() );
// Generate unit number, convert and time stamp
m_out->Print( 0, "U %d %d %8.8lX\n", aComponent->GetUnit(), aComponent->GetConvert(),
(unsigned long)aComponent->GetTimeStamp() );
// Save the position
m_out->Print( 0, "P %d %d\n", aComponent->GetPosition().x, aComponent->GetPosition().y );
/* If this is a complex hierarchy; save hierarchical references.
* but for simple hierarchies it is not necessary.
* the reference inf is already saved
* this is useful for old Eeschema version compatibility
*/
if( aComponent->GetPathsAndReferences().GetCount() > 1 )
{
for( unsigned int ii = 0; ii < aComponent->GetPathsAndReferences().GetCount(); ii++ )
{
/*format:
* AR Path="/140/2" Ref="C99" Part="1"
* where 140 is the uid of the containing sheet
* and 2 is the timestamp of this component.
* (timestamps are actually 8 hex chars)
* Ref is the conventional component reference for this 'path'
* Part is the conventional component part selection for this 'path'
*/
reference_fields = wxStringTokenize( aComponent->GetPathsAndReferences()[ii],
delimiters );
m_out->Print( 0, "AR Path=\"%s\" Ref=\"%s\" Part=\"%s\" \n",
TO_UTF8( reference_fields[0] ),
TO_UTF8( reference_fields[1] ),
TO_UTF8( reference_fields[2] ) );
}
}
// update the ugly field index, which I would like to see go away someday soon.
for( int i = 0; i < aComponent->GetFieldCount(); ++i )
aComponent->GetField( i )->SetId( i );
// Fixed fields:
// Save mandatory fields even if they are blank,
// because the visibility, size and orientation are set from libary editor.
for( unsigned i = 0; i < MANDATORY_FIELDS; ++i )
saveField( aComponent->GetField( i ) );
// User defined fields:
// The *policy* about which user defined fields are part of a symbol is now
// only in the dialog editors. No policy should be enforced here, simply
// save all the user defined fields, they are present because a dialog editor
// thought they should be. If you disagree, go fix the dialog editors.
for( int i = MANDATORY_FIELDS; i < aComponent->GetFieldCount(); ++i )
saveField( aComponent->GetField( i ) );
// Unit number, position, box ( old standard )
m_out->Print( 0, "\t%-4d %-4d %-4d\n", aComponent->GetUnit(), aComponent->GetPosition().x,
aComponent->GetPosition().y );
TRANSFORM transform = aComponent->GetTransform();
m_out->Print( 0, "\t%-4d %-4d %-4d %-4d\n",
transform.x1, transform.y1, transform.x2, transform.y2 );
m_out->Print( 0, "$EndComp\n" );
}
void SCH_LEGACY_PLUGIN::saveField( SCH_FIELD* aField )
{
char hjustify = 'C';
if( aField->GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
hjustify = 'L';
else if( aField->GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
hjustify = 'R';
char vjustify = 'C';
if( aField->GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
vjustify = 'B';
else if( aField->GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
vjustify = 'T';
m_out->Print( 0, "F %d %s %c %-3d %-3d %-3d %4.4X %c %c%c%c",
aField->GetId(),
EscapedUTF8( aField->GetText() ).c_str(), // wraps in quotes too
aField->GetTextAngle() == TEXT_ANGLE_HORIZ ? 'H' : 'V',
aField->GetLibPosition().x, aField->GetLibPosition().y,
aField->GetTextWidth(),
!aField->IsVisible(),
hjustify, vjustify,
aField->IsItalic() ? 'I' : 'N',
aField->IsBold() ? 'B' : 'N' );
// Save field name, if the name is user definable
if( aField->GetId() >= FIELD1 )
{
m_out->Print( 0, " %s", EscapedUTF8( aField->GetName() ).c_str() );
}
m_out->Print( 0, "\n" );
}
void SCH_LEGACY_PLUGIN::saveBitmap( SCH_BITMAP* aBitmap )
{
wxCHECK_RET( aBitmap != NULL, "SCH_BITMAP* is NULL" );
wxImage* image = aBitmap->GetImage()->GetImageData();
wxCHECK_RET( image != NULL, "wxImage* is NULL" );
m_out->Print( 0, "$Bitmap\n" );
m_out->Print( 0, "Pos %-4d %-4d\n", aBitmap->GetPosition().x, aBitmap->GetPosition().y );
m_out->Print( 0, "Scale %f\n", aBitmap->GetImage()->GetScale() );
m_out->Print( 0, "Data\n" );
wxMemoryOutputStream stream;
image->SaveFile( stream, wxBITMAP_TYPE_PNG );
// Write binary data in hexadecimal form (ASCII)
wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
char* begin = (char*) buffer->GetBufferStart();
for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
{
if( ii >= 32 )
{
ii = 0;
m_out->Print( 0, "\n" );
}
m_out->Print( 0, "%2.2X ", *begin & 0xFF );
}
m_out->Print( 0, "\nEndData\n" );
m_out->Print( 0, "$EndBitmap\n" );
}
void SCH_LEGACY_PLUGIN::saveSheet( SCH_SHEET* aSheet )
{
wxCHECK_RET( aSheet != NULL, "SCH_SHEET* is NULL" );
m_out->Print( 0, "$Sheet\n" );
m_out->Print( 0, "S %-4d %-4d %-4d %-4d\n",
aSheet->GetPosition().x, aSheet->GetPosition().y,
aSheet->GetSize().x, aSheet->GetSize().y );
m_out->Print( 0, "U %8.8lX\n", (unsigned long) aSheet->GetTimeStamp() );
if( !aSheet->GetName().IsEmpty() )
m_out->Print( 0, "F0 %s %d\n", EscapedUTF8( aSheet->GetName() ).c_str(),
aSheet->GetSheetNameSize() );
if( !aSheet->GetFileName().IsEmpty() )
m_out->Print( 0, "F1 %s %d\n", EscapedUTF8( aSheet->GetFileName() ).c_str(),
aSheet->GetFileNameSize() );
for( const SCH_SHEET_PIN& pin : aSheet->GetPins() )
{
int type, side;
if( pin.GetText().IsEmpty() )
break;
switch( pin.GetEdge() )
{
default:
case SCH_SHEET_PIN::SHEET_LEFT_SIDE:
side = 'L';
break;
case SCH_SHEET_PIN::SHEET_RIGHT_SIDE:
side = 'R';
break;
case SCH_SHEET_PIN::SHEET_TOP_SIDE:
side = 'T';
break;
case SCH_SHEET_PIN::SHEET_BOTTOM_SIDE:
side = 'B';
break;
}
switch( pin.GetShape() )
{
case NET_INPUT:
type = 'I'; break;
case NET_OUTPUT:
type = 'O'; break;
case NET_BIDI:
type = 'B'; break;
case NET_TRISTATE:
type = 'T'; break;
default:
case NET_UNSPECIFIED:
type = 'U'; break;
}
m_out->Print( 0, "F%d %s %c %c %-3d %-3d %-3d\n", pin.GetNumber(),
EscapedUTF8( pin.GetText() ).c_str(), // supplies wrapping quotes
type, side, pin.GetPosition().x, pin.GetPosition().y,
pin.GetTextWidth() );
}
m_out->Print( 0, "$EndSheet\n" );
}
void SCH_LEGACY_PLUGIN::saveJunction( SCH_JUNCTION* aJunction )
{
wxCHECK_RET( aJunction != NULL, "SCH_JUNCTION* is NULL" );
m_out->Print( 0, "Connection ~ %-4d %-4d\n",
aJunction->GetPosition().x, aJunction->GetPosition().y );
}
void SCH_LEGACY_PLUGIN::saveNoConnect( SCH_NO_CONNECT* aNoConnect )
{
wxCHECK_RET( aNoConnect != NULL, "SCH_NOCONNECT* is NULL" );
m_out->Print( 0, "NoConn ~ %-4d %-4d\n", aNoConnect->GetPosition().x,
aNoConnect->GetPosition().y );
}
void SCH_LEGACY_PLUGIN::saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry )
{
wxCHECK_RET( aBusEntry != NULL, "SCH_BUS_ENTRY_BASE* is NULL" );
if( aBusEntry->GetLayer() == LAYER_WIRE )
m_out->Print( 0, "Entry Wire Line\n\t%-4d %-4d %-4d %-4d\n",
aBusEntry->GetPosition().x, aBusEntry->GetPosition().y,
aBusEntry->m_End().x, aBusEntry->m_End().y );
else
m_out->Print( 0, "Entry Bus Bus\n\t%-4d %-4d %-4d %-4d\n",
aBusEntry->GetPosition().x, aBusEntry->GetPosition().y,
aBusEntry->m_End().x, aBusEntry->m_End().y );
}
void SCH_LEGACY_PLUGIN::saveLine( SCH_LINE* aLine )
{
wxCHECK_RET( aLine != NULL, "SCH_LINE* is NULL" );
const char* layer = "Notes";
const char* width = "Line";
if( aLine->GetLayer() == LAYER_WIRE )
layer = "Wire";
else if( aLine->GetLayer() == LAYER_BUS )
layer = "Bus";
m_out->Print( 0, "Wire %s %s", layer, width );
// Write line style (width, type, color) only for non default values
if( aLine->GetLayer() == LAYER_NOTES )
{
if( aLine->GetPenSize() != aLine->GetDefaultWidth() )
m_out->Print( 0, " %s %d", T_WIDTH, aLine->GetLineSize() );
if( aLine->GetLineStyle() != aLine->GetDefaultStyle() )
m_out->Print( 0, " %s %s", T_STYLE, SCH_LINE::GetLineStyleName( aLine->GetLineStyle() ) );
if( aLine->GetLineColor() != aLine->GetDefaultColor() )
m_out->Print( 0, " %s",
TO_UTF8( aLine->GetLineColor().ToColour().GetAsString( wxC2S_CSS_SYNTAX ) ) );
}
m_out->Print( 0, "\n" );
m_out->Print( 0, "\t%-4d %-4d %-4d %-4d",
aLine->GetStartPoint().x, aLine->GetStartPoint().y,
aLine->GetEndPoint().x, aLine->GetEndPoint().y );
m_out->Print( 0, "\n");
}
void SCH_LEGACY_PLUGIN::saveText( SCH_TEXT* aText )
{
wxCHECK_RET( aText != NULL, "SCH_TEXT* is NULL" );
const char* italics = "~";
const char* textType = "Notes";
if( aText->IsItalic() )
italics = "Italic";
wxString text = aText->GetText();
SCH_LAYER_ID layer = aText->GetLayer();
if( layer == LAYER_NOTES || layer == LAYER_LOCLABEL )
{
if( layer == LAYER_NOTES )
{
// For compatibility reasons, the text must be saved in only one text line
// so replace all EOLs with \\n
text.Replace( wxT( "\n" ), wxT( "\\n" ) );
// Here we should have no CR or LF character in line
// This is not always the case if a multiline text was copied (using a copy/paste
// function) from a text that uses E.O.L characters that differs from the current
// EOL format. This is mainly the case under Linux using LF symbol when copying
// a text from Windows (using CRLF symbol) so we must just remove the extra CR left
// (or LF left under MacOSX)
for( unsigned ii = 0; ii < text.Len(); )
{
if( text[ii] == 0x0A || text[ii] == 0x0D )
text.erase( ii, 1 );
else
ii++;
}
}
else
{
textType = "Label";
}
m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %d\n%s\n", textType,
aText->GetPosition().x, aText->GetPosition().y,
aText->GetLabelSpinStyle(),
aText->GetTextWidth(),
italics, aText->GetThickness(), TO_UTF8( text ) );
}
else if( layer == LAYER_GLOBLABEL || layer == LAYER_HIERLABEL )
{
textType = ( layer == LAYER_GLOBLABEL ) ? "GLabel" : "HLabel";
m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %s %d\n%s\n", textType,
aText->GetPosition().x, aText->GetPosition().y,
aText->GetLabelSpinStyle(),
aText->GetTextWidth(),
SheetLabelType[aText->GetShape()],
italics,
aText->GetThickness(), TO_UTF8( text ) );
}
}
int SCH_LEGACY_PLUGIN_CACHE::m_modHash = 1; // starts at 1 and goes up
SCH_LEGACY_PLUGIN_CACHE::SCH_LEGACY_PLUGIN_CACHE( const wxString& aFullPathAndFileName ) :
m_fileName( aFullPathAndFileName ),
m_libFileName( aFullPathAndFileName ),
m_isWritable( true ),
m_isModified( false )
{
m_versionMajor = -1;
m_versionMinor = -1;
m_libType = LIBRARY_TYPE_EESCHEMA;
}
SCH_LEGACY_PLUGIN_CACHE::~SCH_LEGACY_PLUGIN_CACHE()
{
// When the cache is destroyed, all of the alias objects on the heap should be deleted.
for( LIB_ALIAS_MAP::iterator it = m_aliases.begin(); it != m_aliases.end(); ++it )
{
wxLogTrace( traceSchLegacyPlugin, wxT( "Removing alias %s from library %s." ),
GetChars( it->second->GetName() ), GetChars( GetLogicalName() ) );
LIB_PART* part = it->second->GetPart();
LIB_ALIAS* alias = it->second;
delete alias;
// When the last alias of a part is destroyed, the part is no longer required and it
// too is destroyed.
if( part && part->GetAliasCount() == 0 )
delete part;
}
m_aliases.clear();
}
wxDateTime SCH_LEGACY_PLUGIN_CACHE::GetLibModificationTime()
{
// update the writable flag while we have a wxFileName, in a network this
// is possibly quite dynamic anyway.
m_isWritable = m_libFileName.IsFileWritable();
return m_libFileName.GetModificationTime();
}
bool SCH_LEGACY_PLUGIN_CACHE::IsFile( const wxString& aFullPathAndFileName ) const
{
return m_fileName == aFullPathAndFileName;
}
bool SCH_LEGACY_PLUGIN_CACHE::IsFileChanged() const
{
if( m_fileModTime.IsValid() && m_libFileName.IsOk() && m_libFileName.FileExists() )
return m_libFileName.GetModificationTime() != m_fileModTime;
return false;
}
LIB_ALIAS* SCH_LEGACY_PLUGIN_CACHE::removeAlias( LIB_ALIAS* aAlias )
{
wxCHECK_MSG( aAlias != NULL, NULL, "NULL pointer cannot be removed from library." );
LIB_ALIAS_MAP::iterator it = m_aliases.find( aAlias->GetName() );
if( it == m_aliases.end() )
return NULL;
// If the entry pointer doesn't match the name it is mapped to in the library, we
// have done something terribly wrong.
wxCHECK_MSG( *it->second == aAlias, NULL,
"Pointer mismatch while attempting to remove alias entry <" + aAlias->GetName() +
"> from library cache <" + m_libFileName.GetName() + ">." );
LIB_ALIAS* alias = aAlias;
LIB_PART* part = alias->GetPart();
alias = part->RemoveAlias( alias );
if( !alias )
{
delete part;
if( m_aliases.size() > 1 )
{
LIB_ALIAS_MAP::iterator next = it;
next++;
if( next == m_aliases.end() )
next = m_aliases.begin();
alias = next->second;
}
}
m_aliases.erase( it );
m_isModified = true;
++m_modHash;
return alias;
}
void SCH_LEGACY_PLUGIN_CACHE::AddSymbol( const LIB_PART* aPart )
{
// aPart is cloned in PART_LIB::AddPart(). The cache takes ownership of aPart.
wxArrayString aliasNames = aPart->GetAliasNames();
for( size_t i = 0; i < aliasNames.size(); i++ )
{
LIB_ALIAS_MAP::iterator it = m_aliases.find( aliasNames[i] );
if( it != m_aliases.end() )
removeAlias( it->second );
LIB_ALIAS* alias = const_cast< LIB_PART* >( aPart )->GetAlias( aliasNames[i] );
wxASSERT_MSG( alias != NULL, "No alias <" + aliasNames[i] + "> found in symbol <" +
aPart->GetName() +">." );
m_aliases[ aliasNames[i] ] = alias;
}
m_isModified = true;
++m_modHash;
}
void SCH_LEGACY_PLUGIN_CACHE::Load()
{
wxCHECK_RET( m_libFileName.IsAbsolute(),
wxString::Format( "Cannot use relative file paths in legacy plugin to "
"open library \"%s\".", m_libFileName.GetFullPath() ) );
wxLogTrace( traceSchLegacyPlugin, "Loading legacy symbol file \"%s\"",
m_libFileName.GetFullPath() );
FILE_LINE_READER reader( m_libFileName.GetFullPath() );
if( !reader.ReadLine() )
THROW_IO_ERROR( _( "unexpected end of file" ) );
const char* line = reader.Line();
if( !strCompare( "EESchema-LIBRARY Version", line, &line ) )
{
// Old .sym files (which are libraries with only one symbol, used to store and reuse shapes)
// EESchema-LIB Version x.x SYMBOL. They are valid files.
if( !strCompare( "EESchema-LIB Version", line, &line ) )
SCH_PARSE_ERROR( "file is not a valid component or symbol library file", reader, line );
}
m_versionMajor = parseInt( reader, line, &line );
if( *line != '.' )
SCH_PARSE_ERROR( "invalid file version formatting in header", reader, line );
line++;
m_versionMinor = parseInt( reader, line, &line );
if( m_versionMajor < 1 || m_versionMinor < 0 || m_versionMinor > 99 )
SCH_PARSE_ERROR( "invalid file version in header", reader, line );
// Check if this is a symbol library which is the same as a component library but without
// any alias, documentation, footprint filters, etc.
if( strCompare( "SYMBOL", line, &line ) )
{
// Symbol files add date and time stamp info to the header.
m_libType = LIBRARY_TYPE_SYMBOL;
/// @todo Probably should check for a valid date and time stamp even though it's not used.
}
else
{
m_libType = LIBRARY_TYPE_EESCHEMA;
}
while( reader.ReadLine() )
{
line = reader.Line();
if( *line == '#' || isspace( *line ) ) // Skip comments and blank lines.
continue;
// Headers where only supported in older library file formats.
if( m_libType == LIBRARY_TYPE_EESCHEMA && strCompare( "$HEADER", line ) )
loadHeader( reader );
if( strCompare( "DEF", line ) )
{
// Read one DEF/ENDDEF part entry from library:
loadPart( reader );
}
}
++m_modHash;
// Remember the file modification time of library file when the
// cache snapshot was made, so that in a networked environment we will
// reload the cache as needed.
m_fileModTime = GetLibModificationTime();
if( USE_OLD_DOC_FILE_FORMAT( m_versionMajor, m_versionMinor ) )
loadDocs();
}
void SCH_LEGACY_PLUGIN_CACHE::loadDocs()
{
const char* line;
wxString text;
wxString aliasName;
wxFileName fn = m_libFileName;
LIB_ALIAS* alias = NULL;;
fn.SetExt( DOC_EXT );
// Not all libraries will have a document file.
if( !fn.FileExists() )
return;
if( !fn.IsFileReadable() )
THROW_IO_ERROR( wxString::Format( _( "user does not have permission to read library "
"document file \"%s\"" ), fn.GetFullPath() ) );
FILE_LINE_READER reader( fn.GetFullPath() );
line = reader.ReadLine();
if( !line )
THROW_IO_ERROR( _( "symbol document library file is empty" ) );
if( !strCompare( DOCFILE_IDENT, line, &line ) )
SCH_PARSE_ERROR( "invalid document library file version formatting in header",
reader, line );
while( reader.ReadLine() )
{
line = reader.Line();
if( *line == '#' ) // Comment line.
continue;
if( !strCompare( "$CMP", line, &line ) != 0 )
SCH_PARSE_ERROR( "$CMP command expected", reader, line );
parseUnquotedString( aliasName, reader, line, &line ); // Alias name.
aliasName = LIB_ID::FixIllegalChars( aliasName, LIB_ID::ID_SCH );
LIB_ALIAS_MAP::iterator it = m_aliases.find( aliasName );
if( it == m_aliases.end() )
wxLogWarning( "Alias '%s' not found in library:\n\n"
"'%s'\n\nat line %d offset %d", aliasName, fn.GetFullPath(),
reader.LineNumber(), (int) (line - reader.Line() ) );
else
alias = it->second;
// Read the curent alias associated doc.
// if the alias does not exist, just skip the description
// (Can happen if a .dcm is not synchronized with the corresponding .lib file)
while( reader.ReadLine() )
{
line = reader.Line();
if( !line )
SCH_PARSE_ERROR( "unexpected end of file", reader, line );
if( strCompare( "$ENDCMP", line, &line ) )
break;
text = FROM_UTF8( line + 2 );
text = text.Trim();
switch( line[0] )
{
case 'D':
if( alias )
alias->SetDescription( text );
break;
case 'K':
if( alias )
alias->SetKeyWords( text );
break;
case 'F':
if( alias )
alias->SetDocFileName( text );
break;
case '#':
break;
default:
SCH_PARSE_ERROR( "expected token in symbol definition", reader, line );
}
}
}
}
void SCH_LEGACY_PLUGIN_CACHE::loadHeader( FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxASSERT( strCompare( "$HEADER", line, &line ) );
while( aReader.ReadLine() )
{
line = (char*) aReader;
// The time stamp saved in old library files is not used or saved in the latest
// library file version.
if( strCompare( "TimeStamp", line, &line ) )
continue;
else if( strCompare( "$ENDHEADER", line, &line ) )
return;
}
SCH_PARSE_ERROR( "$ENDHEADER not found", aReader, line );
}
LIB_PART* SCH_LEGACY_PLUGIN_CACHE::loadPart( FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK( strCompare( "DEF", line, &line ), NULL );
// Read DEF line:
char yes_no = 0;
std::unique_ptr< LIB_PART > part( new LIB_PART( wxEmptyString ) );
wxString name, prefix;
parseUnquotedString( name, aReader, line, &line ); // Part name.
parseUnquotedString( prefix, aReader, line, &line ); // Prefix name
parseInt( aReader, line, &line ); // NumOfPins, unused.
part->SetPinNameOffset( parseInt( aReader, line, &line ) ); // Pin name offset.
yes_no = parseChar( aReader, line, &line ); // Show pin numbers.
if( !( yes_no == 'Y' || yes_no == 'N') )
SCH_PARSE_ERROR( "expected Y or N", aReader, line );
part->SetShowPinNumbers( ( yes_no == 'N' ) ? false : true );
yes_no = parseChar( aReader, line, &line ); // Show pin numbers.
if( !( yes_no == 'Y' || yes_no == 'N') )
SCH_PARSE_ERROR( "expected Y or N", aReader, line );
part->SetShowPinNames( ( yes_no == 'N' ) ? false : true ); // Show pin names.
part->SetUnitCount( parseInt( aReader, line, &line ) ); // Number of units.
// Ensure m_unitCount is >= 1. Could be read as 0 in old libraries.
if( part->GetUnitCount() < 1 )
part->SetUnitCount( 1 );
// Copy part name and prefix.
// The root alias is added to the alias list by SetName() which is called by SetText().
if( name.IsEmpty() )
{
part->SetName( "~" );
}
else if( name[0] != '~' )
{
part->SetName( name );
}
else
{
part->SetName( name.Right( name.Length() - 1 ) );
part->GetValueField().SetVisible( false );
}
// Don't set the library alias, this is determined by the symbol library table.
part->SetLibId( LIB_ID( wxEmptyString, part->GetName() ) );
// There are some code paths in SetText() that do not set the root alias to the
// alias list so add it here if it didn't get added by SetText().
if( !part->HasAlias( part->GetName() ) )
part->AddAlias( part->GetName() );
LIB_FIELD& reference = part->GetReferenceField();
if( prefix == "~" )
{
reference.Empty();
reference.SetVisible( false );
}
else
{
reference.SetText( prefix );
}
// In version 2.2 and earlier, this parameter was a '0' which was just a place holder.
// The was no concept of interchangeable multiple unit symbols.
if( LIB_VERSION( m_versionMajor, m_versionMinor ) <= LIB_VERSION( 2, 2 ) )
{
// Nothing needs to be set since the default setting for symbols with multiple
// units were never interchangeable. Just parse the 0 an move on.
parseInt( aReader, line, &line );
}
else
{
char locked = parseChar( aReader, line, &line );
if( locked == 'L' )
part->LockUnits( true );
else if( locked == 'F' || locked == '0' )
part->LockUnits( false );
else
SCH_PARSE_ERROR( "expected L, F, or 0", aReader, line );
}
// There is the optional power component flag.
if( *line )
{
char power = parseChar( aReader, line, &line );
if( power == 'P' )
part->SetPower();
else if( power == 'N' )
part->SetNormal();
else
SCH_PARSE_ERROR( "expected P or N", aReader, line );
}
line = aReader.ReadLine();
// Read lines until "ENDDEF" is found.
while( line )
{
if( *line == '#' ) // Comment
;
else if( strCompare( "Ti", line, &line ) ) // Modification date is ignored.
continue;
else if( strCompare( "ALIAS", line, &line ) ) // Aliases
loadAliases( part, aReader );
else if( *line == 'F' ) // Fields
loadField( part, aReader );
else if( strCompare( "DRAW", line, &line ) ) // Drawing objects.
loadDrawEntries( part, aReader );
else if( strCompare( "$FPLIST", line, &line ) ) // Footprint filter list
loadFootprintFilters( part, aReader );
else if( strCompare( "ENDDEF", line, &line ) ) // End of part description
{
// Add aliases
for( size_t ii = 0; ii < part->GetAliasCount(); ++ii )
{
LIB_ALIAS* alias = part->GetAlias( ii );
const wxString& aliasName = alias->GetName();
auto it = m_aliases.find( aliasName );
if( it != m_aliases.end() )
{
// Find a new name for the alias
wxString newName;
int idx = 0;
LIB_ALIAS_MAP::const_iterator jt;
do
{
newName = wxString::Format( "%s_%d", aliasName, idx );
jt = m_aliases.find( newName );
++idx;
}
while( jt != m_aliases.end() );
wxLogWarning( "Symbol name conflict in library:\n%s\n"
"'%s' has been renamed to '%s'",
m_fileName, aliasName, newName );
if( alias->IsRoot() )
part->SetName( newName );
else
alias->SetName( newName );
m_aliases[newName] = alias;
}
else
{
m_aliases[aliasName] = alias;
}
}
return part.release();
}
line = aReader.ReadLine();
}
SCH_PARSE_ERROR( "missing ENDDEF", aReader, line );
}
bool SCH_LEGACY_PLUGIN_CACHE::checkForDuplicates( wxString& aAliasName )
{
wxCHECK_MSG( !aAliasName.IsEmpty(), false, "alias name cannot be empty" );
// The alias name is not a duplicate so don't change it.
if( m_aliases.find( aAliasName ) == m_aliases.end() )
return false;
int dupCounter = 1;
wxString newAlias = aAliasName;
// If the alias is already loaded, the library is broken. It may have been possible in
// the past that this could happen so we assign a new alias name to prevent any conflicts
// rather than throw an exception.
while( m_aliases.find( newAlias ) != m_aliases.end() )
{
newAlias = aAliasName << dupCounter;
dupCounter++;
}
aAliasName = newAlias;
return true;
}
void SCH_LEGACY_PLUGIN_CACHE::loadAliases( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
wxString newAlias;
const char* line = aReader.Line();
wxCHECK_RET( strCompare( "ALIAS", line, &line ), "Invalid ALIAS section" );
// Parse the ALIAS list.
wxString alias;
parseUnquotedString( alias, aReader, line, &line );
while( !alias.IsEmpty() )
{
newAlias = alias;
checkForDuplicates( newAlias );
aPart->AddAlias( newAlias );
alias.clear();
parseUnquotedString( alias, aReader, line, &line, true );
}
}
void SCH_LEGACY_PLUGIN_CACHE::loadField( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_RET( *line == 'F', "Invalid field line" );
int id;
if( sscanf( line + 1, "%d", &id ) != 1 || id < 0 )
SCH_PARSE_ERROR( "invalid field ID", aReader, line + 1 );
std::unique_ptr< LIB_FIELD > field( new LIB_FIELD( aPart.get(), id ) );
// Skip to the first double quote.
while( *line != '"' && *line != 0 )
line++;
if( *line == 0 )
SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, line );
wxString text;
parseQuotedString( text, aReader, line, &line, true );
// Doctor the *.lib file field which has a "~" in blank fields. New saves will
// not save like this.
if( text.size() == 1 && text[0] == '~' )
text.clear();
field->m_Text = text;
wxPoint pos;
pos.x = parseInt( aReader, line, &line );
pos.y = parseInt( aReader, line, &line );
field->SetPosition( pos );
wxSize textSize;
textSize.x = textSize.y = parseInt( aReader, line, &line );
field->SetTextSize( textSize );
char textOrient = parseChar( aReader, line, &line );
if( textOrient == 'H' )
field->SetTextAngle( TEXT_ANGLE_HORIZ );
else if( textOrient == 'V' )
field->SetTextAngle( TEXT_ANGLE_VERT );
else
SCH_PARSE_ERROR( "invalid field text orientation parameter", aReader, line );
char textVisible = parseChar( aReader, line, &line );
if( textVisible == 'V' )
field->SetVisible( true );
else if ( textVisible == 'I' )
field->SetVisible( false );
else
SCH_PARSE_ERROR( "invalid field text visibility parameter", aReader, line );
// It may be technically correct to use the library version to determine if the field text
// attributes are present. If anyone knows if that is valid and what version that would be,
// please change this to test the library version rather than an EOL or the quoted string
// of the field name.
if( *line != 0 && *line != '"' )
{
char textHJustify = parseChar( aReader, line, &line );
if( textHJustify == 'C' )
field->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER );
else if( textHJustify == 'L' )
field->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
else if( textHJustify == 'R' )
field->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
else
SCH_PARSE_ERROR( "invalid field text horizontal justification parameter",
aReader, line );
wxString attributes;
parseUnquotedString( attributes, aReader, line, &line );
if( !(attributes.size() == 3 || attributes.size() == 1 ) )
SCH_PARSE_ERROR( "invalid field text attributes size",
aReader, line );
if( attributes[0] == 'C' )
field->SetVertJustify( GR_TEXT_VJUSTIFY_CENTER );
else if( attributes[0] == 'B' )
field->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
else if( attributes[0] == 'T' )
field->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
else
SCH_PARSE_ERROR( "invalid field text vertical justification parameter",
aReader, line );
if( attributes.size() == 3 )
{
if( attributes[1] == 'I' ) // Italic
field->SetItalic( true );
else if( attributes[1] != 'N' ) // No italics is default, check for error.
SCH_PARSE_ERROR( "invalid field text italic parameter", aReader, line );
if ( attributes[2] == 'B' ) // Bold
field->SetBold( true );
else if( attributes[2] != 'N' ) // No bold is default, check for error.
SCH_PARSE_ERROR( "invalid field text bold parameter", aReader, line );
}
}
// Fields in RAM must always have names.
if( (unsigned) id < MANDATORY_FIELDS )
{
// Fields in RAM must always have names, because we are trying to get
// less dependent on field ids and more dependent on names.
// Plus assumptions are made in the field editors.
field->m_name = TEMPLATE_FIELDNAME::GetDefaultFieldName( id );
LIB_FIELD* fixedField = aPart->GetField( field->GetId() );
// this will fire only if somebody broke a constructor or editor.
// MANDATORY_FIELDS are always present in ram resident components, no
// exceptions, and they always have their names set, even fixed fields.
wxASSERT( fixedField );
*fixedField = *field;
// Ensure the VALUE field = the part name (can be not the case
// with malformed libraries: edited by hand, or converted from other tools)
if( fixedField->GetId() == VALUE )
fixedField->m_Text = aPart->GetName();
}
else
{
wxString name;
parseQuotedString( name, aReader, line, &line, true ); // Optional.
if( !name.IsEmpty() )
field->m_name = name;
aPart->AddDrawItem( field.release() ); // LIB_FIELD* is now owned by the LIB_PART.
}
}
void SCH_LEGACY_PLUGIN_CACHE::loadDrawEntries( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_RET( strCompare( "DRAW", line, &line ), "Invalid DRAW section" );
line = aReader.ReadLine();
while( line )
{
if( strCompare( "ENDDRAW", line, &line ) )
return;
switch( line[0] )
{
case 'A': // Arc
aPart->AddDrawItem( loadArc( aPart, aReader ) );
break;
case 'C': // Circle
aPart->AddDrawItem( loadCircle( aPart, aReader ) );
break;
case 'T': // Text
aPart->AddDrawItem( loadText( aPart, aReader ) );
break;
case 'S': // Square
aPart->AddDrawItem( loadRectangle( aPart, aReader ) );
break;
case 'X': // Pin Description
aPart->AddDrawItem( loadPin( aPart, aReader ) );
break;
case 'P': // Polyline
aPart->AddDrawItem( loadPolyLine( aPart, aReader ) );
break;
case 'B': // Bezier Curves
aPart->AddDrawItem( loadBezier( aPart, aReader ) );
break;
case '#': // Comment
case '\n': // Empty line
case '\r':
case 0:
break;
default:
SCH_PARSE_ERROR( "undefined DRAW entry", aReader, line );
}
line = aReader.ReadLine();
}
SCH_PARSE_ERROR( "file ended prematurely loading component draw element", aReader, line );
}
FILL_T SCH_LEGACY_PLUGIN_CACHE::parseFillMode( FILE_LINE_READER& aReader, const char* aLine,
const char** aOutput )
{
FILL_T mode;
switch( parseChar( aReader, aLine, aOutput ) )
{
case 'F':
mode = FILLED_SHAPE;
break;
case 'f':
mode = FILLED_WITH_BG_BODYCOLOR;
break;
case 'N':
mode = NO_FILL;
break;
default:
SCH_PARSE_ERROR( "invalid fill type, expected f, F, or N", aReader, aLine );
}
return mode;
}
LIB_ARC* SCH_LEGACY_PLUGIN_CACHE::loadArc( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_MSG( strCompare( "A", line, &line ), NULL, "Invalid LIB_ARC definition" );
std::unique_ptr< LIB_ARC > arc( new LIB_ARC( aPart.get() ) );
wxPoint center;
center.x = parseInt( aReader, line, &line );
center.y = parseInt( aReader, line, &line );
arc->SetPosition( center );
arc->SetRadius( parseInt( aReader, line, &line ) );
int angle1 = parseInt( aReader, line, &line );
int angle2 = parseInt( aReader, line, &line );
NORMALIZE_ANGLE_POS( angle1 );
NORMALIZE_ANGLE_POS( angle2 );
arc->SetFirstRadiusAngle( angle1 );
arc->SetSecondRadiusAngle( angle2 );
arc->SetUnit( parseInt( aReader, line, &line ) );
arc->SetConvert( parseInt( aReader, line, &line ) );
arc->SetWidth( parseInt( aReader, line, &line ) );
// Old libraries (version <= 2.2) do not have always this FILL MODE param
// when fill mode is no fill (default mode).
if( *line != 0 )
arc->SetFillMode( parseFillMode( aReader, line, &line ) );
// Actual Coordinates of arc ends are read from file
if( *line != 0 )
{
wxPoint arcStart, arcEnd;
arcStart.x = parseInt( aReader, line, &line );
arcStart.y = parseInt( aReader, line, &line );
arcEnd.x = parseInt( aReader, line, &line );
arcEnd.y = parseInt( aReader, line, &line );
arc->SetStart( arcStart );
arc->SetEnd( arcEnd );
}
else
{
// Actual Coordinates of arc ends are not read from file
// (old library), calculate them
wxPoint arcStart( arc->GetRadius(), 0 );
wxPoint arcEnd( arc->GetRadius(), 0 );
RotatePoint( &arcStart.x, &arcStart.y, -angle1 );
arcStart += arc->GetPosition();
arc->SetStart( arcStart );
RotatePoint( &arcEnd.x, &arcEnd.y, -angle2 );
arcEnd += arc->GetPosition();
arc->SetEnd( arcEnd );
}
return arc.release();
}
LIB_CIRCLE* SCH_LEGACY_PLUGIN_CACHE::loadCircle( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_MSG( strCompare( "C", line, &line ), NULL, "Invalid LIB_CIRCLE definition" );
std::unique_ptr< LIB_CIRCLE > circle( new LIB_CIRCLE( aPart.get() ) );
wxPoint center;
center.x = parseInt( aReader, line, &line );
center.y = parseInt( aReader, line, &line );
circle->SetPosition( center );
circle->SetRadius( parseInt( aReader, line, &line ) );
circle->SetUnit( parseInt( aReader, line, &line ) );
circle->SetConvert( parseInt( aReader, line, &line ) );
circle->SetWidth( parseInt( aReader, line, &line ) );
if( *line != 0 )
circle->SetFillMode( parseFillMode( aReader, line, &line ) );
return circle.release();
}
LIB_TEXT* SCH_LEGACY_PLUGIN_CACHE::loadText( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_MSG( strCompare( "T", line, &line ), NULL, "Invalid LIB_TEXT definition" );
std::unique_ptr< LIB_TEXT > text( new LIB_TEXT( aPart.get() ) );
text->SetTextAngle( (double) parseInt( aReader, line, &line ) );
wxPoint center;
center.x = parseInt( aReader, line, &line );
center.y = parseInt( aReader, line, &line );
text->SetPosition( center );
wxSize size;
size.x = size.y = parseInt( aReader, line, &line );
text->SetTextSize( size );
text->SetVisible( !parseInt( aReader, line, &line ) );
text->SetUnit( parseInt( aReader, line, &line ) );
text->SetConvert( parseInt( aReader, line, &line ) );
wxString str;
// If quoted string loading fails, load as not quoted string.
if( *line == '"' )
parseQuotedString( str, aReader, line, &line );
else
{
parseUnquotedString( str, aReader, line, &line );
// In old libs, "spaces" are replaced by '~' in unquoted strings:
str.Replace( "~", " " );
}
if( !str.IsEmpty() )
{
// convert two apostrophes back to double quote
str.Replace( "''", "\"" );
}
text->SetText( str );
// Here things are murky and not well defined. At some point it appears the format
// was changed to add text properties. However rather than add the token to the end of
// the text definition, it was added after the string and no mention if the file
// verion was bumped or not so this code make break on very old component libraries.
//
// Update: apparently even in the latest version this can be different so added a test
// for end of line before checking for the text properties.
if( LIB_VERSION( m_versionMajor, m_versionMinor ) > LIB_VERSION( 2, 0 ) && !is_eol( *line ) )
{
if( strCompare( "Italic", line, &line ) )
text->SetItalic( true );
else if( !strCompare( "Normal", line, &line ) )
SCH_PARSE_ERROR( "invalid text stype, expected 'Normal' or 'Italic'",
aReader, line );
if( parseInt( aReader, line, &line ) > 0 )
text->SetBold( true );
// Some old libaries version > 2.0 do not have these options for text justification:
if( !is_eol( *line ) )
{
switch( parseChar( aReader, line, &line ) )
{
case 'L':
text->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
break;
case 'C':
text->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER );
break;
case 'R':
text->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
break;
default:
SCH_PARSE_ERROR( "invalid horizontal text justication parameter, expected L, C, or R",
aReader, line );
}
switch( parseChar( aReader, line, &line ) )
{
case 'T':
text->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
break;
case 'C':
text->SetVertJustify( GR_TEXT_VJUSTIFY_CENTER );
break;
case 'B':
text->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
break;
default:
SCH_PARSE_ERROR( "invalid vertical text justication parameter, expected T, C, or B",
aReader, line );
}
}
}
return text.release();
}
LIB_RECTANGLE* SCH_LEGACY_PLUGIN_CACHE::loadRectangle( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_MSG( strCompare( "S", line, &line ), NULL, "Invalid LIB_RECTANGLE definition" );
std::unique_ptr< LIB_RECTANGLE > rectangle( new LIB_RECTANGLE( aPart.get() ) );
wxPoint pos;
pos.x = parseInt( aReader, line, &line );
pos.y = parseInt( aReader, line, &line );
rectangle->SetPosition( pos );
wxPoint end;
end.x = parseInt( aReader, line, &line );
end.y = parseInt( aReader, line, &line );
rectangle->SetEnd( end );
rectangle->SetUnit( parseInt( aReader, line, &line ) );
rectangle->SetConvert( parseInt( aReader, line, &line ) );
rectangle->SetWidth( parseInt( aReader, line, &line ) );
if( *line != 0 )
rectangle->SetFillMode( parseFillMode( aReader, line, &line ) );
return rectangle.release();
}
LIB_PIN* SCH_LEGACY_PLUGIN_CACHE::loadPin( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_MSG( strCompare( "X", line, &line ), NULL, "Invalid LIB_PIN definition" );
std::unique_ptr< LIB_PIN > pin( new LIB_PIN( aPart.get() ) );
wxString name, number;
parseUnquotedString( name, aReader, line, &line );
parseUnquotedString( number, aReader, line, &line );
// Unlike most of the other LIB_ITEMs, the SetXXX() routines on LIB_PINs are at the UI
// level, performing additional pin checking, multi-pin editing, and setting the modified
// flag. So we must set the member fields directly.
pin->m_name = name;
pin->m_number = number;
wxPoint pos;
pos.x = parseInt( aReader, line, &line );
pos.y = parseInt( aReader, line, &line );
pin->m_position = pos;
pin->m_length = parseInt( aReader, line, &line );
pin->m_orientation = parseChar( aReader, line, &line );
pin->m_numTextSize = parseInt( aReader, line, &line );
pin->m_nameTextSize = parseInt( aReader, line, &line );
pin->m_Unit = parseInt( aReader, line, &line );
pin->m_Convert = parseInt( aReader, line, &line );
char type = parseChar( aReader, line, &line );
wxString attributes;
// Optional
parseUnquotedString( attributes, aReader, line, &line, true );
switch( type )
{
case 'I':
pin->m_type = PIN_INPUT;
break;
case 'O':
pin->m_type = PIN_OUTPUT;
break;
case 'B':
pin->m_type = PIN_BIDI;
break;
case 'T':
pin->m_type = PIN_TRISTATE;
break;
case 'P':
pin->m_type = PIN_PASSIVE;
break;
case 'U':
pin->m_type = PIN_UNSPECIFIED;
break;
case 'W':
pin->m_type = PIN_POWER_IN;
break;
case 'w':
pin->m_type = PIN_POWER_OUT;
break;
case 'C':
pin->m_type = PIN_OPENCOLLECTOR;
break;
case 'E':
pin->m_type = PIN_OPENEMITTER;
break;
case 'N':
pin->m_type = PIN_NC;
break;
default:
SCH_PARSE_ERROR( "unknown pin type", aReader, line );
}
if( !attributes.IsEmpty() ) /* Special Symbol defined */
{
enum
{
INVERTED = 1 << 0,
CLOCK = 1 << 1,
LOWLEVEL_IN = 1 << 2,
LOWLEVEL_OUT = 1 << 3,
FALLING_EDGE = 1 << 4,
NONLOGIC = 1 << 5
};
int flags = 0;
for( int j = attributes.size(); j > 0; )
{
switch( attributes[--j].GetValue() )
{
case '~':
break;
case 'N':
pin->m_attributes |= PIN_INVISIBLE;
break;
case 'I':
flags |= INVERTED;
break;
case 'C':
flags |= CLOCK;
break;
case 'L':
flags |= LOWLEVEL_IN;
break;
case 'V':
flags |= LOWLEVEL_OUT;
break;
case 'F':
flags |= FALLING_EDGE;
break;
case 'X':
flags |= NONLOGIC;
break;
default:
SCH_PARSE_ERROR( "unknown pin attribute", aReader, line );
}
}
switch( flags )
{
case 0:
pin->m_shape = PINSHAPE_LINE;
break;
case INVERTED:
pin->m_shape = PINSHAPE_INVERTED;
break;
case CLOCK:
pin->m_shape = PINSHAPE_CLOCK;
break;
case INVERTED | CLOCK:
pin->m_shape = PINSHAPE_INVERTED_CLOCK;
break;
case LOWLEVEL_IN:
pin->m_shape = PINSHAPE_INPUT_LOW;
break;
case LOWLEVEL_IN | CLOCK:
pin->m_shape = PINSHAPE_CLOCK_LOW;
break;
case LOWLEVEL_OUT:
pin->m_shape = PINSHAPE_OUTPUT_LOW;
break;
case FALLING_EDGE:
pin->m_shape = PINSHAPE_FALLING_EDGE_CLOCK;
break;
case NONLOGIC:
pin->m_shape = PINSHAPE_NONLOGIC;
break;
default:
SCH_PARSE_ERROR( "pin attributes do not define a valid pin shape", aReader, line );
}
}
return pin.release();
}
LIB_POLYLINE* SCH_LEGACY_PLUGIN_CACHE::loadPolyLine( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_MSG( strCompare( "P", line, &line ), NULL, "Invalid LIB_POLYLINE definition" );
std::unique_ptr< LIB_POLYLINE > polyLine( new LIB_POLYLINE( aPart.get() ) );
int points = parseInt( aReader, line, &line );
polyLine->SetUnit( parseInt( aReader, line, &line ) );
polyLine->SetConvert( parseInt( aReader, line, &line ) );
polyLine->SetWidth( parseInt( aReader, line, &line ) );
wxPoint pt;
for( int i = 0; i < points; i++ )
{
pt.x = parseInt( aReader, line, &line );
pt.y = parseInt( aReader, line, &line );
polyLine->AddPoint( pt );
}
if( *line != 0 )
polyLine->SetFillMode( parseFillMode( aReader, line, &line ) );
return polyLine.release();
}
LIB_BEZIER* SCH_LEGACY_PLUGIN_CACHE::loadBezier( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_MSG( strCompare( "B", line, &line ), NULL, "Invalid LIB_BEZIER definition" );
std::unique_ptr< LIB_BEZIER > bezier( new LIB_BEZIER( aPart.get() ) );
int points = parseInt( aReader, line, &line );
bezier->SetUnit( parseInt( aReader, line, &line ) );
bezier->SetConvert( parseInt( aReader, line, &line ) );
bezier->SetWidth( parseInt( aReader, line, &line ) );
wxPoint pt;
for( int i = 0; i < points; i++ )
{
pt.x = parseInt( aReader, line, &line );
pt.y = parseInt( aReader, line, &line );
bezier->AddPoint( pt );
}
if( *line != 0 )
bezier->SetFillMode( parseFillMode( aReader, line, &line ) );
return bezier.release();
}
void SCH_LEGACY_PLUGIN_CACHE::loadFootprintFilters( std::unique_ptr< LIB_PART >& aPart,
FILE_LINE_READER& aReader )
{
const char* line = aReader.Line();
wxCHECK_RET( strCompare( "$FPLIST", line, &line ), "Invalid footprint filter list" );
line = aReader.ReadLine();
while( line )
{
if( strCompare( "$ENDFPLIST", line, &line ) )
return;
wxString footprint;
parseUnquotedString( footprint, aReader, line, &line );
aPart->GetFootprints().Add( footprint );
line = aReader.ReadLine();
}
SCH_PARSE_ERROR( "file ended prematurely while loading footprint filters", aReader, line );
}
void SCH_LEGACY_PLUGIN_CACHE::Save( bool aSaveDocFile )
{
if( !m_isModified )
return;
std::unique_ptr< FILE_OUTPUTFORMATTER > formatter( new FILE_OUTPUTFORMATTER( m_libFileName.GetFullPath() ) );
formatter->Print( 0, "%s %d.%d\n", LIBFILE_IDENT, LIB_VERSION_MAJOR, LIB_VERSION_MINOR );
formatter->Print( 0, "#encoding utf-8\n");
for( LIB_ALIAS_MAP::iterator it = m_aliases.begin(); it != m_aliases.end(); it++ )
{
if( !it->second->IsRoot() )
continue;
saveSymbol( it->second->GetPart(), formatter );
}
formatter->Print( 0, "#\n#End Library\n" );
formatter.reset();
m_fileModTime = m_libFileName.GetModificationTime();
m_isModified = false;
if( aSaveDocFile )
saveDocFile();
}
void SCH_LEGACY_PLUGIN_CACHE::saveSymbol( LIB_PART* aSymbol,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter )
{
wxCHECK_RET( aSymbol, "Invalid LIB_PART pointer." );
LIB_FIELD& value = aSymbol->GetValueField();
// First line: it s a comment (component name for readers)
aFormatter->Print( 0, "#\n# %s\n#\n", TO_UTF8( value.GetText() ) );
// Save data
aFormatter->Print( 0, "DEF" );
if( value.IsVisible() )
{
aFormatter->Print( 0, " %s", TO_UTF8( value.GetText() ) );
}
else
{
aFormatter->Print( 0, " ~%s", TO_UTF8( value.GetText() ) );
}
LIB_FIELD& reference = aSymbol->GetReferenceField();
if( !reference.GetText().IsEmpty() )
{
aFormatter->Print( 0, " %s", TO_UTF8( reference.GetText() ) );
}
else
{
aFormatter->Print( 0, " ~" );
}
aFormatter->Print( 0, " %d %d %c %c %d %c %c\n",
0, aSymbol->GetPinNameOffset(),
aSymbol->ShowPinNumbers() ? 'Y' : 'N',
aSymbol->ShowPinNames() ? 'Y' : 'N',
aSymbol->GetUnitCount(), aSymbol->UnitsLocked() ? 'L' : 'F',
aSymbol->IsPower() ? 'P' : 'N' );
timestamp_t dateModified = aSymbol->GetDateLastEdition();
if( dateModified != 0 )
{
int sec = dateModified & 63;
int min = ( dateModified >> 6 ) & 63;
int hour = ( dateModified >> 12 ) & 31;
int day = ( dateModified >> 17 ) & 31;
int mon = ( dateModified >> 22 ) & 15;
int year = ( dateModified >> 26 ) + 1990;
aFormatter->Print( 0, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec );
}
LIB_FIELDS fields;
aSymbol->GetFields( fields );
// Mandatory fields:
// may have their own save policy so there is a separate loop for them.
// Empty fields are saved, because the user may have set visibility,
// size and orientation
for( int i = 0; i < MANDATORY_FIELDS; ++i )
{
saveField( &fields[i], aFormatter );
}
// User defined fields:
// may have their own save policy so there is a separate loop for them.
int fieldId = MANDATORY_FIELDS; // really wish this would go away.
for( unsigned i = MANDATORY_FIELDS; i < fields.size(); ++i )
{
// There is no need to save empty fields, i.e. no reason to preserve field
// names now that fields names come in dynamically through the template
// fieldnames.
if( !fields[i].GetText().IsEmpty() )
{
fields[i].SetId( fieldId++ );
saveField( &fields[i], aFormatter );
}
}
// Save the alias list: a line starting by "ALIAS". The first alias is the root
// and has the same name as the component. In the old library file format this
// alias does not get added to the alias list.
if( aSymbol->GetAliasCount() > 1 )
{
wxArrayString aliases = aSymbol->GetAliasNames();
aFormatter->Print( 0, "ALIAS" );
for( unsigned i = 1; i < aliases.size(); i++ )
{
aFormatter->Print( 0, " %s", TO_UTF8( aliases[i] ) );
}
aFormatter->Print( 0, "\n" );
}
wxArrayString footprints = aSymbol->GetFootprints();
// Write the footprint filter list
if( footprints.GetCount() != 0 )
{
aFormatter->Print( 0, "$FPLIST\n" );
for( unsigned i = 0; i < footprints.GetCount(); i++ )
{
aFormatter->Print( 0, " %s\n", TO_UTF8( footprints[i] ) );
}
aFormatter->Print( 0, "$ENDFPLIST\n" );
}
// Save graphics items (including pins)
if( !aSymbol->GetDrawItems().empty() )
{
// Sort the draw items in order to editing a file editing by hand.
aSymbol->GetDrawItems().sort();
aFormatter->Print( 0, "DRAW\n" );
for( LIB_ITEM& item : aSymbol->GetDrawItems() )
{
switch( item.Type() )
{
case LIB_FIELD_T: // Fields have already been saved above.
continue;
case LIB_ARC_T:
saveArc( (LIB_ARC*) &item, aFormatter );
break;
case LIB_BEZIER_T:
saveBezier( (LIB_BEZIER*) &item, aFormatter );
break;
case LIB_CIRCLE_T:
saveCircle( ( LIB_CIRCLE* ) &item, aFormatter );
break;
case LIB_PIN_T:
savePin( (LIB_PIN* ) &item, aFormatter );
break;
case LIB_POLYLINE_T:
savePolyLine( ( LIB_POLYLINE* ) &item, aFormatter );
break;
case LIB_RECTANGLE_T:
saveRectangle( ( LIB_RECTANGLE* ) &item, aFormatter );
break;
case LIB_TEXT_T:
saveText( ( LIB_TEXT* ) &item, aFormatter );
break;
default:
;
}
}
aFormatter->Print( 0, "ENDDRAW\n" );
}
aFormatter->Print( 0, "ENDDEF\n" );
}
void SCH_LEGACY_PLUGIN_CACHE::saveArc( LIB_ARC* aArc,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter )
{
wxCHECK_RET( aArc && aArc->Type() == LIB_ARC_T, "Invalid LIB_ARC object." );
int x1 = aArc->GetFirstRadiusAngle();
if( x1 > 1800 )
x1 -= 3600;
int x2 = aArc->GetSecondRadiusAngle();
if( x2 > 1800 )
x2 -= 3600;
aFormatter->Print( 0, "A %d %d %d %d %d %d %d %d %c %d %d %d %d\n",
aArc->GetPosition().x, aArc->GetPosition().y,
aArc->GetRadius(), x1, x2, aArc->GetUnit(), aArc->GetConvert(),
aArc->GetWidth(), fill_tab[aArc->GetFillMode()],
aArc->GetStart().x, aArc->GetStart().y,
aArc->GetEnd().x, aArc->GetEnd().y );
}
void SCH_LEGACY_PLUGIN_CACHE::saveBezier( LIB_BEZIER* aBezier,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter )
{
wxCHECK_RET( aBezier && aBezier->Type() == LIB_BEZIER_T, "Invalid LIB_BEZIER object." );
aFormatter->Print( 0, "B %u %d %d %d", (unsigned)aBezier->GetPoints().size(),
aBezier->GetUnit(), aBezier->GetConvert(), aBezier->GetWidth() );
for( const auto& pt : aBezier->GetPoints() )
aFormatter->Print( 0, " %d %d", pt.x, pt.y );
aFormatter->Print( 0, " %c\n", fill_tab[aBezier->GetFillMode()] );
}
void SCH_LEGACY_PLUGIN_CACHE::saveCircle( LIB_CIRCLE* aCircle,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter )
{
wxCHECK_RET( aCircle && aCircle->Type() == LIB_CIRCLE_T, "Invalid LIB_CIRCLE object." );
aFormatter->Print( 0, "C %d %d %d %d %d %d %c\n",
aCircle->GetPosition().x, aCircle->GetPosition().y,
aCircle->GetRadius(), aCircle->GetUnit(), aCircle->GetConvert(),
aCircle->GetWidth(), fill_tab[aCircle->GetFillMode()] );
}
void SCH_LEGACY_PLUGIN_CACHE::saveField( LIB_FIELD* aField,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter )
{
wxCHECK_RET( aField && aField->Type() == LIB_FIELD_T, "Invalid LIB_FIELD object." );
int hjustify, vjustify;
int id = aField->GetId();
wxString text = aField->m_Text;
hjustify = 'C';
if( aField->GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
hjustify = 'L';
else if( aField->GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
hjustify = 'R';
vjustify = 'C';
if( aField->GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
vjustify = 'B';
else if( aField->GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
vjustify = 'T';
aFormatter->Print( 0, "F%d %s %d %d %d %c %c %c %c%c%c",
id,
EscapedUTF8( text ).c_str(), // wraps in quotes
aField->GetTextPos().x, aField->GetTextPos().y, aField->GetTextWidth(),
aField->GetTextAngle() == 0 ? 'H' : 'V',
aField->IsVisible() ? 'V' : 'I',
hjustify, vjustify,
aField->IsItalic() ? 'I' : 'N',
aField->IsBold() ? 'B' : 'N' );
/* Save field name, if necessary
* Field name is saved only if it is not the default name.
* Just because default name depends on the language and can change from
* a country to another
*/
wxString defName = TEMPLATE_FIELDNAME::GetDefaultFieldName( id );
if( id >= FIELD1 && !aField->m_name.IsEmpty() && aField->m_name != defName )
aFormatter->Print( 0, " %s", EscapedUTF8( aField->m_name ).c_str() );
aFormatter->Print( 0, "\n" );
}
void SCH_LEGACY_PLUGIN_CACHE::savePin( LIB_PIN* aPin,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter )
{
wxCHECK_RET( aPin && aPin->Type() == LIB_PIN_T, "Invalid LIB_PIN object." );
int Etype;
switch( aPin->GetType() )
{
default:
case PIN_INPUT:
Etype = 'I';
break;
case PIN_OUTPUT:
Etype = 'O';
break;
case PIN_BIDI:
Etype = 'B';
break;
case PIN_TRISTATE:
Etype = 'T';
break;
case PIN_PASSIVE:
Etype = 'P';
break;
case PIN_UNSPECIFIED:
Etype = 'U';
break;
case PIN_POWER_IN:
Etype = 'W';
break;
case PIN_POWER_OUT:
Etype = 'w';
break;
case PIN_OPENCOLLECTOR:
Etype = 'C';
break;
case PIN_OPENEMITTER:
Etype = 'E';
break;
case PIN_NC:
Etype = 'N';
break;
}
if( !aPin->GetName().IsEmpty() )
aFormatter->Print( 0, "X %s", TO_UTF8( aPin->GetName() ) );
else
aFormatter->Print( 0, "X ~" );
aFormatter->Print( 0, " %s %d %d %d %c %d %d %d %d %c",
aPin->GetNumber().IsEmpty() ? "~" : TO_UTF8( aPin->GetNumber() ),
aPin->GetPosition().x, aPin->GetPosition().y,
(int) aPin->GetLength(), (int) aPin->GetOrientation(),
aPin->GetNumberTextSize(), aPin->GetNameTextSize(),
aPin->GetUnit(), aPin->GetConvert(), Etype );
if( aPin->GetShape() || !aPin->IsVisible() )
aFormatter->Print( 0, " " );
if( !aPin->IsVisible() )
aFormatter->Print( 0, "N" );
switch( aPin->GetShape() )
{
case PINSHAPE_LINE:
break;
case PINSHAPE_INVERTED:
aFormatter->Print( 0, "I" );
break;
case PINSHAPE_CLOCK:
aFormatter->Print( 0, "C" );
break;
case PINSHAPE_INVERTED_CLOCK:
aFormatter->Print( 0, "IC" );
break;
case PINSHAPE_INPUT_LOW:
aFormatter->Print( 0, "L" );
break;
case PINSHAPE_CLOCK_LOW:
aFormatter->Print( 0, "CL" );
break;
case PINSHAPE_OUTPUT_LOW:
aFormatter->Print( 0, "V" );
break;
case PINSHAPE_FALLING_EDGE_CLOCK:
aFormatter->Print( 0, "F" );
break;
case PINSHAPE_NONLOGIC:
aFormatter->Print( 0, "X" );
break;
default:
assert( !"Invalid pin shape" );
}
aFormatter->Print( 0, "\n" );
aPin->ClearFlags( IS_CHANGED );
}
void SCH_LEGACY_PLUGIN_CACHE::savePolyLine( LIB_POLYLINE* aPolyLine,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter )
{
wxCHECK_RET( aPolyLine && aPolyLine->Type() == LIB_POLYLINE_T, "Invalid LIB_POLYLINE object." );
int ccount = aPolyLine->GetCornerCount();
aFormatter->Print( 0, "P %d %d %d %d", ccount, aPolyLine->GetUnit(), aPolyLine->GetConvert(),
aPolyLine->GetWidth() );
for( const auto& pt : aPolyLine->GetPolyPoints() )
{
aFormatter->Print( 0, " %d %d", pt.x, pt.y );
}
aFormatter->Print( 0, " %c\n", fill_tab[aPolyLine->GetFillMode()] );
}
void SCH_LEGACY_PLUGIN_CACHE::saveRectangle( LIB_RECTANGLE* aRectangle,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter )
{
wxCHECK_RET( aRectangle && aRectangle->Type() == LIB_RECTANGLE_T,
"Invalid LIB_RECTANGLE object." );
aFormatter->Print( 0, "S %d %d %d %d %d %d %d %c\n",
aRectangle->GetPosition().x, aRectangle->GetPosition().y,
aRectangle->GetEnd().x, aRectangle->GetEnd().y,
aRectangle->GetUnit(), aRectangle->GetConvert(),
aRectangle->GetWidth(), fill_tab[aRectangle->GetFillMode()] );
}
void SCH_LEGACY_PLUGIN_CACHE::saveText( LIB_TEXT* aText,
std::unique_ptr< FILE_OUTPUTFORMATTER >& aFormatter )
{
wxCHECK_RET( aText && aText->Type() == LIB_TEXT_T, "Invalid LIB_TEXT object." );
wxString text = aText->GetText();
if( text.Contains( wxT( " " ) ) || text.Contains( wxT( "~" ) ) || text.Contains( wxT( "\"" ) ) )
{
// convert double quote to similar-looking two apostrophes
text.Replace( wxT( "\"" ), wxT( "''" ) );
text = wxT( "\"" ) + text + wxT( "\"" );
}
aFormatter->Print( 0, "T %g %d %d %d %d %d %d %s", aText->GetTextAngle(),
aText->GetTextPos().x, aText->GetTextPos().y,
aText->GetTextWidth(), !aText->IsVisible(),
aText->GetUnit(), aText->GetConvert(), TO_UTF8( text ) );
aFormatter->Print( 0, " %s %d", aText->IsItalic() ? "Italic" : "Normal", aText->IsBold() );
char hjustify = 'C';
if( aText->GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
hjustify = 'L';
else if( aText->GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
hjustify = 'R';
char vjustify = 'C';
if( aText->GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
vjustify = 'B';
else if( aText->GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
vjustify = 'T';
aFormatter->Print( 0, " %c %c\n", hjustify, vjustify );
}
void SCH_LEGACY_PLUGIN_CACHE::saveDocFile()
{
wxFileName fileName = m_libFileName;
fileName.SetExt( DOC_EXT );
FILE_OUTPUTFORMATTER formatter( fileName.GetFullPath() );
formatter.Print( 0, "%s\n", DOCFILE_IDENT );
for( LIB_ALIAS_MAP::iterator it = m_aliases.begin(); it != m_aliases.end(); it++ )
{
wxString description = it->second->GetDescription();
wxString keyWords = it->second->GetKeyWords();
wxString docFileName = it->second->GetDocFileName();
if( description.IsEmpty() && keyWords.IsEmpty() && docFileName.IsEmpty() )
continue;
formatter.Print( 0, "#\n$CMP %s\n", TO_UTF8( it->second->GetName() ) );
if( !description.IsEmpty() )
formatter.Print( 0, "D %s\n", TO_UTF8( description ) );
if( !keyWords.IsEmpty() )
formatter.Print( 0, "K %s\n", TO_UTF8( keyWords ) );
if( !docFileName.IsEmpty() )
formatter.Print( 0, "F %s\n", TO_UTF8( docFileName ) );
formatter.Print( 0, "$ENDCMP\n" );
}
formatter.Print( 0, "#\n#End Doc Library\n" );
}
void SCH_LEGACY_PLUGIN_CACHE::DeleteAlias( const wxString& aAliasName )
{
LIB_ALIAS_MAP::iterator it = m_aliases.find( aAliasName );
if( it == m_aliases.end() )
THROW_IO_ERROR( wxString::Format( _( "library %s does not contain an alias %s" ),
m_libFileName.GetFullName(), aAliasName ) );
LIB_ALIAS* alias = it->second;
LIB_PART* part = alias->GetPart();
alias = part->RemoveAlias( alias );
if( !alias )
{
delete part;
if( m_aliases.size() > 1 )
{
LIB_ALIAS_MAP::iterator next = it;
next++;
if( next == m_aliases.end() )
next = m_aliases.begin();
alias = next->second;
}
}
m_aliases.erase( it );
++m_modHash;
m_isModified = true;
}
void SCH_LEGACY_PLUGIN_CACHE::DeleteSymbol( const wxString& aAliasName )
{
LIB_ALIAS_MAP::iterator it = m_aliases.find( aAliasName );
if( it == m_aliases.end() )
THROW_IO_ERROR( wxString::Format( _( "library %s does not contain an alias %s" ),
m_libFileName.GetFullName(), aAliasName ) );
LIB_ALIAS* alias = it->second;
LIB_PART* part = alias->GetPart();
wxArrayString aliasNames = part->GetAliasNames();
// Deleting all of the aliases deletes the symbol from the library.
for( size_t i = 0; i < aliasNames.Count(); i++ )
DeleteAlias( aliasNames[i] );
}
void SCH_LEGACY_PLUGIN::cacheLib( const wxString& aLibraryFileName )
{
if( !m_cache || !m_cache->IsFile( aLibraryFileName ) || m_cache->IsFileChanged() )
{
// a spectacular episode in memory management:
delete m_cache;
m_cache = new SCH_LEGACY_PLUGIN_CACHE( aLibraryFileName );
// Because m_cache is rebuilt, increment PART_LIBS::s_modify_generation
// to modify the hash value that indicate component to symbol links
// must be updated.
PART_LIBS::s_modify_generation++;
if( !isBuffering( m_props ) )
m_cache->Load();
}
}
bool SCH_LEGACY_PLUGIN::writeDocFile( const PROPERTIES* aProperties )
{
std::string propName( SCH_LEGACY_PLUGIN::PropNoDocFile );
if( aProperties && aProperties->find( propName ) != aProperties->end() )
return false;
return true;
}
bool SCH_LEGACY_PLUGIN::isBuffering( const PROPERTIES* aProperties )
{
return ( aProperties && aProperties->Exists( SCH_LEGACY_PLUGIN::PropBuffering ) );
}
int SCH_LEGACY_PLUGIN::GetModifyHash() const
{
if( m_cache )
return m_cache->GetModifyHash();
// If the cache hasn't been loaded, it hasn't been modified.
return 0;
}
size_t SCH_LEGACY_PLUGIN::GetSymbolLibCount( const wxString& aLibraryPath,
const PROPERTIES* aProperties )
{
LOCALE_IO toggle;
m_props = aProperties;
cacheLib( aLibraryPath );
return m_cache->m_aliases.size();
}
void SCH_LEGACY_PLUGIN::EnumerateSymbolLib( wxArrayString& aAliasNameList,
const wxString& aLibraryPath,
const PROPERTIES* aProperties )
{
LOCALE_IO toggle; // toggles on, then off, the C locale.
m_props = aProperties;
bool powerSymbolsOnly = ( aProperties &&
aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
cacheLib( aLibraryPath );
const LIB_ALIAS_MAP& aliases = m_cache->m_aliases;
for( LIB_ALIAS_MAP::const_iterator it = aliases.begin(); it != aliases.end(); ++it )
{
if( !powerSymbolsOnly || it->second->GetPart()->IsPower() )
aAliasNameList.Add( it->first );
}
}
void SCH_LEGACY_PLUGIN::EnumerateSymbolLib( std::vector<LIB_ALIAS*>& aAliasList,
const wxString& aLibraryPath,
const PROPERTIES* aProperties )
{
LOCALE_IO toggle; // toggles on, then off, the C locale.
m_props = aProperties;
bool powerSymbolsOnly = ( aProperties &&
aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
cacheLib( aLibraryPath );
const LIB_ALIAS_MAP& aliases = m_cache->m_aliases;
for( LIB_ALIAS_MAP::const_iterator it = aliases.begin(); it != aliases.end(); ++it )
{
if( !powerSymbolsOnly || it->second->GetPart()->IsPower() )
aAliasList.push_back( it->second );
}
}
LIB_ALIAS* SCH_LEGACY_PLUGIN::LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
const PROPERTIES* aProperties )
{
LOCALE_IO toggle; // toggles on, then off, the C locale.
m_props = aProperties;
cacheLib( aLibraryPath );
LIB_ALIAS_MAP::const_iterator it = m_cache->m_aliases.find( aAliasName );
if( it == m_cache->m_aliases.end() )
return NULL;
return it->second;
}
void SCH_LEGACY_PLUGIN::SaveSymbol( const wxString& aLibraryPath, const LIB_PART* aSymbol,
const PROPERTIES* aProperties )
{
m_props = aProperties;
cacheLib( aLibraryPath );
m_cache->AddSymbol( aSymbol );
if( !isBuffering( aProperties ) )
m_cache->Save( writeDocFile( aProperties ) );
}
void SCH_LEGACY_PLUGIN::DeleteAlias( const wxString& aLibraryPath, const wxString& aAliasName,
const PROPERTIES* aProperties )
{
m_props = aProperties;
cacheLib( aLibraryPath );
m_cache->DeleteAlias( aAliasName );
if( !isBuffering( aProperties ) )
m_cache->Save( writeDocFile( aProperties ) );
}
void SCH_LEGACY_PLUGIN::DeleteSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
const PROPERTIES* aProperties )
{
m_props = aProperties;
cacheLib( aLibraryPath );
m_cache->DeleteSymbol( aAliasName );
if( !isBuffering( aProperties ) )
m_cache->Save( writeDocFile( aProperties ) );
}
void SCH_LEGACY_PLUGIN::CreateSymbolLib( const wxString& aLibraryPath,
const PROPERTIES* aProperties )
{
if( wxFileExists( aLibraryPath ) )
{
THROW_IO_ERROR( wxString::Format(
_( "symbol library \"%s\" already exists, cannot create a new library" ),
aLibraryPath.GetData() ) );
}
LOCALE_IO toggle;
m_props = aProperties;
delete m_cache;
m_cache = new SCH_LEGACY_PLUGIN_CACHE( aLibraryPath );
m_cache->SetModified();
m_cache->Save( writeDocFile( aProperties ) );
m_cache->Load(); // update m_writable and m_mod_time
}
bool SCH_LEGACY_PLUGIN::DeleteSymbolLib( const wxString& aLibraryPath,
const PROPERTIES* aProperties )
{
wxFileName fn = aLibraryPath;
if( !fn.FileExists() )
return false;
// Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
// we don't want that. we want bare metal portability with no UI here.
if( wxRemove( aLibraryPath ) )
{
THROW_IO_ERROR( wxString::Format( _( "library \"%s\" cannot be deleted" ),
aLibraryPath.GetData() ) );
}
if( m_cache && m_cache->IsFile( aLibraryPath ) )
{
delete m_cache;
m_cache = 0;
}
return true;
}
void SCH_LEGACY_PLUGIN::SaveLibrary( const wxString& aLibraryPath, const PROPERTIES* aProperties )
{
if( !m_cache )
m_cache = new SCH_LEGACY_PLUGIN_CACHE( aLibraryPath );
wxString oldFileName = m_cache->GetFileName();
if( !m_cache->IsFile( aLibraryPath ) )
{
m_cache->SetFileName( aLibraryPath );
}
// This is a forced save.
m_cache->SetModified();
m_cache->Save( writeDocFile( aProperties ) );
m_cache->SetFileName( oldFileName );
}
bool SCH_LEGACY_PLUGIN::CheckHeader( const wxString& aFileName )
{
// Open file and check first line
wxTextFile tempFile;
tempFile.Open( aFileName );
wxString firstline;
// read the first line
firstline = tempFile.GetFirstLine();
tempFile.Close();
return firstline.StartsWith( "EESchema" );
}
bool SCH_LEGACY_PLUGIN::IsSymbolLibWritable( const wxString& aLibraryPath )
{
return wxFileName::IsFileWritable( aLibraryPath );
}
const char* SCH_LEGACY_PLUGIN::PropBuffering = "buffering";
const char* SCH_LEGACY_PLUGIN::PropNoDocFile = "no_doc_file";
|