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
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1993-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave 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 3 of the License, or
// (at your option) any later version.
//
// Octave 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 Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
// Originally written by John C. Campbell <jcc@bevo.che.wisc.edu>
//
// Thomas Baier <baier@ci.tuwien.ac.at> added the original versions of
// the following functions:
//
// popen
// pclose
// execute (now popen2.m)
// sync_system (now merged with system)
// async_system (now merged with system)
// Extensively revised by John W. Eaton <jwe@octave.org>,
// April 1996.
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include <cerrno>
#include <cstdio>
#include <iomanip>
#include <string>
#if defined (HAVE_ZLIB_H)
# include <zlib.h>
#endif
#include "file-ops.h"
#include "file-stat.h"
#include "iconv-wrappers.h"
#include "lo-ieee.h"
#include "lo-sysdep.h"
#include "localcharset-wrapper.h"
#include "mkostemp-wrapper.h"
#include "oct-env.h"
#include "oct-locbuf.h"
#include "tmpfile-wrapper.h"
#include "unistd-wrappers.h"
#include "builtin-defun-decls.h"
#include "defun.h"
#include "error.h"
#include "errwarn.h"
#include "file-io.h"
#include "interpreter-private.h"
#include "interpreter.h"
#include "load-path.h"
#include "oct-fstrm.h"
#include "oct-iostrm.h"
#include "oct-map.h"
#include "oct-prcstrm.h"
#include "oct-stream.h"
#include "oct-strstrm.h"
#include "ov.h"
#include "ovl.h"
#include "pager.h"
#include "sysdep.h"
#include "utils.h"
#include "variables.h"
static void
normalize_fopen_mode (std::string& mode, bool& use_zlib)
{
use_zlib = false;
if (! mode.empty ())
{
// Matlab uses 'A' and 'W' to indicate that buffered writing should
// take place. Octave already does that. Theoretically, we should
// warn about using 'a', 'r', or 'w' because Octave does not enable
// automatic flushing with these modes. The performance hit is ~4X
// when using automatic flushing and seems completely unnecessary.
// See bug #52644.
size_t pos = mode.find ('W');
if (pos != std::string::npos)
mode[pos] = 'w';
pos = mode.find ('R');
if (pos != std::string::npos)
mode[pos] = 'r';
pos = mode.find ('A');
if (pos != std::string::npos)
mode[pos] = 'a';
pos = mode.find ('z');
if (pos != std::string::npos)
{
#if defined (HAVE_ZLIB)
use_zlib = true;
mode.erase (pos, 1);
#else
err_disabled_feature ("", "gzipped files (zlib)");
#endif
}
// Use binary mode if 't' is not specified, but don't add
// 'b' if it is already present.
size_t bpos = mode.find ('b');
size_t tpos = mode.find ('t');
if (bpos == std::string::npos && tpos == std::string::npos)
mode += 'b';
}
}
static std::ios::openmode
fopen_mode_to_ios_mode (const std::string& mode)
{
std::ios::openmode retval = std::ios::in;
if (mode == "rt")
retval = std::ios::in;
else if (mode == "wt")
retval = std::ios::out | std::ios::trunc;
else if (mode == "at")
retval = std::ios::out | std::ios::app;
else if (mode == "r+t" || mode == "rt+")
retval = std::ios::in | std::ios::out;
else if (mode == "w+t" || mode == "wt+")
retval = std::ios::in | std::ios::out | std::ios::trunc;
else if (mode == "a+t" || mode == "at+")
retval = std::ios::in | std::ios::out | std::ios::app;
else if (mode == "rb" || mode == "r")
retval = std::ios::in | std::ios::binary;
else if (mode == "wb" || mode == "w")
retval = std::ios::out | std::ios::trunc | std::ios::binary;
else if (mode == "ab" || mode == "a")
retval = std::ios::out | std::ios::app | std::ios::binary;
else if (mode == "r+b" || mode == "rb+" || mode == "r+")
retval = std::ios::in | std::ios::out | std::ios::binary;
else if (mode == "w+b" || mode == "wb+" || mode == "w+")
retval = (std::ios::in | std::ios::out | std::ios::trunc
| std::ios::binary);
else if (mode == "a+b" || mode == "ab+" || mode == "a+")
retval = (std::ios::in | std::ios::out | std::ios::app
| std::ios::binary);
else
error ("invalid mode specified");
return retval;
}
DEFMETHOD (fclose, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} fclose (@var{fid})
@deftypefnx {} {} fclose ("all")
@deftypefnx {} {@var{status} =} fclose ("all")
Close the file specified by the file descriptor @var{fid}.
If successful, @code{fclose} returns 0, otherwise, it returns -1. The
second form of the @code{fclose} call closes all open files except
@code{stdin}, @code{stdout}, @code{stderr}, and any FIDs associated
with gnuplot.
@seealso{fopen, fflush, freport}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
return ovl (streams.remove (args(0), "fclose"));
}
DEFMETHOD (fclear, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} fclear (@var{fid})
Clear the stream state for the file specified by the file descriptor
@var{fid}.
@seealso{ferror, fopen}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
int fid = streams.get_file_number (args(0));
octave::stream os = streams.lookup (fid, "fclear");
os.clearerr ();
return ovl ();
}
DEFMETHOD (fflush, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} fflush (@var{fid})
Flush output to file descriptor @var{fid}.
@code{fflush} returns 0 on success and an OS dependent error value
(@minus{}1 on Unix) on error.
Programming Note: Flushing is useful for ensuring that all pending output
makes it to the screen before some other event occurs. For example, it is
always a good idea to flush the standard output stream before calling
@code{input}.
@seealso{fopen, fclose}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
octave_value retval = -1;
octave::stream_list& streams = interp.get_stream_list ();
// FIXME: any way to avoid special case for stdout?
int fid = streams.get_file_number (args(0));
if (fid == 1)
{
octave::flush_stdout ();
retval = 0;
}
else
{
octave::stream os = streams.lookup (fid, "fflush");
retval = os.flush ();
}
return retval;
}
DEFMETHOD (fgetl, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{str} =} fgetl (@var{fid})
@deftypefnx {} {@var{str} =} fgetl (@var{fid}, @var{len})
Read characters from a file, stopping after a newline, or EOF,
or @var{len} characters have been read.
The characters read, excluding the possible trailing newline, are returned
as a string.
If @var{len} is omitted, @code{fgetl} reads until the next newline
character.
If there are no more characters to read, @code{fgetl} returns @minus{}1.
To read a line and return the terminating newline see @code{fgets}.
@seealso{fgets, fscanf, fread, fopen}
@end deftypefn */)
{
static const std::string who = "fgetl";
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), who);
octave_value len_arg = (nargin == 2) ? args(1) : octave_value ();
bool err = false;
std::string tmp = os.getl (len_arg, err, who);
if (! err)
return ovl (tmp, tmp.length ());
else
return ovl (-1, 0);
}
DEFMETHOD (fgets, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{str} =} fgets (@var{fid})
@deftypefnx {} {@var{str} =} fgets (@var{fid}, @var{len})
Read characters from a file, stopping after a newline, or EOF,
or @var{len} characters have been read.
The characters read, including the possible trailing newline, are returned
as a string.
If @var{len} is omitted, @code{fgets} reads until the next newline
character.
If there are no more characters to read, @code{fgets} returns @minus{}1.
To read a line and discard the terminating newline see @code{fgetl}.
@seealso{fputs, fgetl, fscanf, fread, fopen}
@end deftypefn */)
{
static const std::string who = "fgets";
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), who);
octave_value len_arg = (nargin == 2) ? args(1) : octave_value ();
bool err = false;
std::string tmp = os.gets (len_arg, err, who);
if (! err)
return ovl (tmp, tmp.length ());
else
return ovl (-1.0, 0.0);
}
DEFMETHOD (fskipl, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{nlines} =} fskipl (@var{fid})
@deftypefnx {} {@var{nlines} =} fskipl (@var{fid}, @var{count})
@deftypefnx {} {@var{nlines} =} fskipl (@var{fid}, Inf)
Read and skip @var{count} lines from the file specified by the file
descriptor @var{fid}.
@code{fskipl} discards characters until an end-of-line is encountered
exactly @var{count}-times, or until the end-of-file marker is found.
If @var{count} is omitted, it defaults to 1. @var{count} may also be
@code{Inf}, in which case lines are skipped until the end of the file.
This form is suitable for counting the number of lines in a file.
Returns the number of lines skipped (end-of-line sequences encountered).
@seealso{fgetl, fgets, fscanf, fopen}
@end deftypefn */)
{
static const std::string who = "fskipl";
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), who);
octave_value count_arg = (nargin == 2) ? args(1) : octave_value ();
bool err = false;
off_t tmp = os.skipl (count_arg, err, who);
if (! err)
return ovl (tmp);
else
return ovl ();
}
static octave::stream
do_stream_open (const std::string& name, const std::string& mode_arg,
const std::string& arch, std::string encoding, int& fid)
{
octave::stream retval;
fid = -1;
// Valid names for encodings consist of ASCII characters only.
std::transform (encoding.begin (), encoding.end (), encoding.begin (),
::tolower);
if (encoding.compare ("utf-8"))
{
// check if encoding is valid
void *codec = octave_iconv_open_wrapper (encoding.c_str (), "utf-8");
if (codec == reinterpret_cast<void *> (-1))
{
if (errno == EINVAL)
error ("fopen: conversion from codepage '%s' not supported",
encoding.c_str ());
}
else
octave_iconv_close_wrapper (codec);
}
std::string mode = mode_arg;
bool use_zlib = false;
normalize_fopen_mode (mode, use_zlib);
std::ios::openmode md = fopen_mode_to_ios_mode (mode);
octave::mach_info::float_format flt_fmt
= octave::mach_info::string_to_float_format (arch);
std::string fname = octave::sys::file_ops::tilde_expand (name);
octave::sys::file_stat fs (fname);
if (! (md & std::ios::out))
fname = octave::find_data_file_in_load_path ("fopen", fname);
if (! fs.is_dir ())
{
#if defined (HAVE_ZLIB)
if (use_zlib)
{
FILE *fptr = octave::sys::fopen (fname.c_str (), mode.c_str ());
if (fptr)
{
int fd = fileno (fptr);
gzFile gzf = ::gzdopen (fd, mode.c_str ());
retval = octave_zstdiostream::create (fname, gzf, fd, md,
flt_fmt, encoding);
}
else
retval.error (std::strerror (errno));
}
else
#endif
{
FILE *fptr = octave::sys::fopen (fname, mode);
retval = octave_stdiostream::create (fname, fptr, md, flt_fmt,
encoding);
if (! fptr)
retval.error (std::strerror (errno));
}
}
return retval;
}
static octave::stream
do_stream_open (const octave_value& tc_name, const octave_value& tc_mode,
const octave_value& tc_arch, const octave_value& tc_encoding,
const char *fcn, int& fid)
{
octave::stream retval;
fid = -1;
std::string name = tc_name.xstring_value ("%s: filename must be a string", fcn);
std::string mode = tc_mode.xstring_value ("%s: file mode must be a string", fcn);
std::string arch = tc_arch.xstring_value ("%s: architecture type must be a string", fcn);
std::string encoding = tc_encoding.xstring_value ("%s: ENCODING must be a string", fcn);
retval = do_stream_open (name, mode, arch, encoding, fid);
return retval;
}
DEFMETHOD (fopen, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{fid} =} fopen (@var{name})
@deftypefnx {} {@var{fid} =} fopen (@var{name}, @var{mode})
@deftypefnx {} {@var{fid} =} fopen (@var{name}, @var{mode}, @var{arch})
@deftypefnx {} {@var{fid} =} fopen (@var{name}, @var{mode}, @var{arch}, @var{encoding})
@deftypefnx {} {[@var{fid}, @var{msg}] =} fopen (@dots{})
@deftypefnx {} {@var{fid_list} =} fopen ("all")
@deftypefnx {} {[@var{file}, @var{mode}, @var{arch}, @var{encoding}] =} fopen (@var{fid})
Open a file for low-level I/O or query open files and file descriptors.
The first form of the @code{fopen} function opens the named file with
the specified mode (read-write, read-only, etc.@:), architecture
interpretation (IEEE big endian, IEEE little endian, etc.@:) and file encoding,
and returns an integer value that may be used to refer to the file later. If
an error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the
corresponding system error message. The @var{mode} is a one or two
character string that specifies whether the file is to be opened for
reading, writing, or both. The @var{encoding} is a character string with a
valid encoding identifier. This encoding is used when strings are read from
or written to the file. By default, the same encoding specified for reading
@file{.m} files is used for interpreting user files.
The second form of the @code{fopen} function returns a vector of file ids
corresponding to all the currently open files, excluding the
@code{stdin}, @code{stdout}, and @code{stderr} streams.
The third form of the @code{fopen} function returns information about the
open file given its file id.
For example,
@example
myfile = fopen ("splat.dat", "r", "ieee-le");
@end example
@noindent
opens the file @file{splat.dat} for reading. If necessary, binary
numeric values will be read assuming they are stored in IEEE format with
the least significant bit first, and then converted to the native
representation.
Opening a file that is already open simply opens it again and returns a
separate file id. It is not an error to open a file several times,
though writing to the same file through several different file ids may
produce unexpected results.
The possible values of @var{mode} are
@table @asis
@item @samp{r} (default)
Open a file for reading.
@item @samp{w}
Open a file for writing. The previous contents are discarded.
@item @samp{a}
Open or create a file for writing at the end of the file.
@item @samp{r+}
Open an existing file for reading and writing.
@item @samp{w+}
Open a file for reading or writing. The previous contents are
discarded.
@item @samp{a+}
Open or create a file for reading or writing at the end of the
file.
@end table
Append a @qcode{"t"} to the mode string to open the file in text mode or a
@qcode{"b"} to open in binary mode. On Windows systems,
text mode reading and writing automatically converts linefeeds to the
appropriate line end character for the system (carriage-return linefeed on
Windows). The default when no mode is specified is binary.
Additionally, you may append a @qcode{"z"} to the mode string to open a
gzipped file for reading or writing. For this to be successful, you
must also open the file in binary mode.
The parameter @var{arch} is a string specifying the default data format
for the file. Valid values for @var{arch} are:
@table @asis
@item @qcode{"native"} or @qcode{"n"} (default)
The format of the current machine.
@item @qcode{"ieee-be"} or @qcode{"b"}
IEEE big endian format.
@item @qcode{"ieee-le"} or @qcode{"l"}
IEEE little endian format.
@end table
When opening a new file that does not yet exist, permissions will be set to
@code{0666 - @var{umask}}.
Compatibility Note: Octave opens files using buffered I/O. Small writes are
accumulated until an internal buffer is filled, and then everything is written
in a single operation. This is very efficient and improves performance.
@sc{matlab}, however, opens files using flushed I/O where every write operation
is immediately performed. If the write operation must be performed immediately
after data has been written then the write should be followed by a call to
@code{fflush} to flush the internal buffer.
@seealso{fclose, fgets, fgetl, fscanf, fread, fputs, fdisp, fprintf, fwrite, fskipl, fseek, frewind, ftell, feof, ferror, fclear, fflush, freport, umask}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 1 || nargin > 4)
print_usage ();
octave_value_list retval = ovl (-1.0);
octave::stream_list& streams = interp.get_stream_list ();
if (nargin == 1)
{
if (args(0).is_string ())
{
// If there is only one argument and it is a string but it
// is not the string "all", we assume it is a file to open
// with MODE = "r". To open a file called "all", you have
// to supply more than one argument.
if (nargout < 2 && args(0).string_value () == "all")
return streams.open_file_numbers ();
}
else
{
string_vector tmp = streams.get_info (args(0));
retval = ovl (tmp(0), tmp(1), tmp(2), tmp(3));
return retval;
}
}
octave_value mode = (nargin > 1) ? args(1) : octave_value ("r");
octave_value arch = (nargin > 2) ? args(2) : octave_value ("native");
octave::input_system& input_sys = interp.get_input_system ();
octave_value encoding = (nargin > 3) ? args(3)
: octave_value (input_sys.mfile_encoding ());
if (encoding.string_value () == "system")
encoding = octave_value (octave_locale_charset_wrapper ());
int fid = -1;
octave::stream os = do_stream_open (args(0), mode, arch, encoding, "fopen",
fid);
if (os)
retval = ovl (streams.insert (os), "");
else
{
int error_number = 0;
retval = ovl (-1.0, os.error (false, error_number));
}
return retval;
}
/*
## Further tests are in io.tst
%!test # Uses hardcoded value of 1 for stdout
%! [name, mode, arch, encoding] = fopen (1);
%! assert (name, "stdout");
%! assert (mode, "w");
%! assert (encoding, "utf-8");
%!test # Query of non-existent stream returns all ""
%! [name, mode, arch] = fopen (-1);
%! assert (name, "");
%! assert (mode, "");
%! assert (arch, "");
*/
DEFMETHOD (freport, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} freport ()
Print a list of which files have been opened, and whether they are open
for reading, writing, or both.
For example:
@example
@group
freport ()
@print{} number mode arch name
@print{} ------ ---- ---- ----
@print{} 0 r ieee-le stdin
@print{} 1 w ieee-le stdout
@print{} 2 w ieee-le stderr
@print{} 3 r ieee-le myfile
@end group
@end example
@seealso{fopen, fclose, is_valid_file_id}
@end deftypefn */)
{
if (args.length () > 0)
warning ("freport: ignoring extra arguments");
octave::stream_list& streams = interp.get_stream_list ();
octave_stdout << streams.list_open_files ();
return ovl ();
}
DEFMETHOD (frewind, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {} frewind (@var{fid})
@deftypefnx {} {@var{status} =} frewind (@var{fid})
Move the file pointer to the beginning of the file specified by file
descriptor @var{fid}.
@code{frewind} returns 0 for success, and -1 if an error is encountered. It
is equivalent to @code{fseek (@var{fid}, 0, SEEK_SET)}.
@seealso{fseek, ftell, fopen}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
int result = -1;
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), "frewind");
result = os.rewind ();
if (nargout > 0)
return ovl (result);
else
return ovl ();
}
DEFMETHOD (fseek, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} fseek (@var{fid}, @var{offset})
@deftypefnx {} {} fseek (@var{fid}, @var{offset}, @var{origin})
@deftypefnx {} {@var{status} =} fseek (@dots{})
Set the file pointer to the location @var{offset} within the file @var{fid}.
The pointer is positioned @var{offset} characters from the @var{origin}, which
may be one of the predefined variables @w{@qcode{SEEK_SET}} (beginning),
@w{@qcode{SEEK_CUR}} (current position), or @w{@qcode{SEEK_END}} (end of file)
or strings @nospell{@qcode{"bof"}}, @nospell{@qcode{"cof"}}, or
@nospell{@qcode{"eof"}}. If @var{origin} is omitted, @w{@qcode{SEEK_SET}} is
assumed. @var{offset} may be positive, negative, or zero but not all
combinations of @var{origin} and @var{offset} can be realized.
@code{fseek} returns 0 on success and -1 on error.
@seealso{fskipl, frewind, ftell, fopen, SEEK_SET, SEEK_CUR, SEEK_END}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 2 || nargin > 3)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), "fseek");
octave_value origin_arg = (nargin == 3) ? args(2) : octave_value (-1.0);
return ovl (os.seek (args(1), origin_arg));
}
DEFMETHOD (ftell, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{pos} =} ftell (@var{fid})
Return the position of the file pointer as the number of characters from the
beginning of the file specified by file descriptor @var{fid}.
@seealso{fseek, frewind, feof, fopen}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), "ftell");
return ovl (os.tell ());
}
static octave_value_list
printf_internal (octave::interpreter& interp, const std::string& who,
const octave_value_list& args, int nargout)
{
int nargin = args.length ();
if (! (nargin > 1 || (nargin > 0 && args(0).is_string ())))
print_usage ();
int result;
octave::stream os;
int fmt_n = 0;
octave::stream_list& streams = interp.get_stream_list ();
if (args(0).is_string ())
os = streams.lookup (1, who);
else
{
fmt_n = 1;
os = streams.lookup (args(0), who);
}
if (! args(fmt_n).is_string ())
error ("%s: format TEMPLATE must be a string", who.c_str ());
octave_value_list tmp_args;
if (nargin > 1 + fmt_n)
{
tmp_args.resize (nargin-fmt_n-1, octave_value ());
for (int i = fmt_n + 1; i < nargin; i++)
tmp_args(i-fmt_n-1) = args(i);
}
result = os.printf (args(fmt_n), tmp_args, who);
if (nargout > 0)
return ovl (result);
else
return ovl ();
}
DEFMETHOD (fprintf, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {} fprintf (@var{fid}, @var{template}, @dots{})
@deftypefnx {} {} fprintf (@var{template}, @dots{})
@deftypefnx {} {@var{numbytes} =} fprintf (@dots{})
This function is equivalent to @code{printf}, except that the output is
written to the file descriptor @var{fid} instead of @code{stdout}.
If @var{fid} is omitted, the output is written to @code{stdout} making the
function exactly equivalent to @code{printf}.
The optional output returns the number of bytes written to the file.
Implementation Note: For compatibility with @sc{matlab}, escape sequences in
the template string (e.g., @qcode{"@xbackslashchar{}n"} => newline) are
expanded even when the template string is defined with single quotes.
@seealso{fputs, fdisp, fwrite, fscanf, printf, sprintf, fopen}
@end deftypefn */)
{
static const std::string who = "fprintf";
return printf_internal (interp, who, args, nargout);
}
DEFMETHOD (printf, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {} printf (@var{template}, @dots{})
Print optional arguments under the control of the template string
@var{template} to the stream @code{stdout} and return the number of
characters printed.
@ifclear OCTAVE_MANUAL
See the Formatted Output section of the GNU Octave manual for a
complete description of the syntax of the template string.
@end ifclear
Implementation Note: For compatibility with @sc{matlab}, escape sequences in
the template string (e.g., @qcode{"@xbackslashchar{}n"} => newline) are
expanded even when the template string is defined with single quotes.
@seealso{fprintf, sprintf, scanf}
@end deftypefn */)
{
static const std::string who = "printf";
octave_value_list tmp_args = args;
return printf_internal (interp, who, tmp_args.prepend (octave_value (1)),
nargout);
}
static octave_value_list
puts_internal (octave::interpreter& interp, const std::string& who,
const octave_value_list& args)
{
if (args.length () != 2)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), who);
return ovl (os.puts (args(1), who));
}
DEFMETHOD (fputs, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} fputs (@var{fid}, @var{string})
@deftypefnx {} {@var{status} =} fputs (@var{fid}, @var{string})
Write the string @var{string} to the file with file descriptor @var{fid}.
The string is written to the file with no additional formatting. Use
@code{fdisp} instead to automatically append a newline character appropriate
for the local machine.
Return a non-negative number on success or EOF on error.
@seealso{fdisp, fprintf, fwrite, fopen}
@end deftypefn */)
{
static const std::string who = "fputs";
return puts_internal (interp, who, args);
}
DEFMETHOD (puts, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} puts (@var{string})
@deftypefnx {} {@var{status} =} puts (@var{string})
Write a string to the standard output with no formatting.
The string is written verbatim to the standard output. Use @code{disp} to
automatically append a newline character appropriate for the local machine.
Return a non-negative number on success and EOF on error.
@seealso{fputs, disp}
@end deftypefn */)
{
static const std::string who = "puts";
octave_value_list tmp_args = args;
return puts_internal (interp, who, tmp_args.prepend (octave_value (1)));
}
DEFUN (sprintf, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} sprintf (@var{template}, @dots{})
This is like @code{printf}, except that the output is returned as a
string.
Unlike the C library function, which requires you to provide a suitably
sized string as an argument, Octave's @code{sprintf} function returns the
string, automatically sized to hold all of the items converted.
Implementation Note: For compatibility with @sc{matlab}, escape sequences in
the template string (e.g., @qcode{"@xbackslashchar{}n"} => newline) are
expanded even when the template string is defined with single quotes.
@seealso{printf, fprintf, sscanf}
@end deftypefn */)
{
static const std::string who = "sprintf";
int nargin = args.length ();
if (nargin == 0)
print_usage ();
// We don't use octave_ostrstream::create here because need direct
// access to the OSTR object so that we can extract a string object
// from it to return.
octave_ostrstream *ostr = new octave_ostrstream ();
// The octave::stream destructor will delete OSTR for us.
octave::stream os (ostr);
if (! os.is_valid ())
error ("%s: unable to create output buffer", who.c_str ());
octave_value fmt_arg = args(0);
if (! fmt_arg.is_string ())
error ("%s: format TEMPLATE must be a string", who.c_str ());
octave_value_list retval (3);
octave_value_list tmp_args;
if (nargin > 1)
{
tmp_args.resize (nargin-1, octave_value ());
for (int i = 1; i < nargin; i++)
tmp_args(i-1) = args(i);
}
// NOTE: Call to os.error must precede next call to ostr which might reset it.
retval(2) = os.printf (fmt_arg, tmp_args, who);
retval(1) = os.error ();
std::string result = ostr->str ();
char type = (fmt_arg.is_sq_string () ? '\'' : '"');
retval(0) = (result.empty () ? octave_value (charMatrix (1, 0), type)
: octave_value (result, type));
return retval;
}
static octave_value_list
scanf_internal (octave::interpreter& interp, const std::string& who,
const octave_value_list& args)
{
int nargin = args.length ();
if (nargin < 2 || nargin > 3)
print_usage ();
octave_value_list retval;
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), who);
if (! args(1).is_string ())
error ("%s: format TEMPLATE must be a string", who.c_str ());
if (nargin == 3 && args(2).is_string ())
{
retval = os.oscanf (args(1), who);
}
else
{
octave_idx_type count = 0;
Array<double> size
= (nargin == 3
? args(2).vector_value ()
: Array<double> (dim_vector (1, 1), lo_ieee_inf_value ()));
octave_value tmp = os.scanf (args(1), size, count, who);
retval = ovl (tmp, count, os.error ());
}
return retval;
}
DEFMETHOD (fscanf, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {[@var{val}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, @var{size})
@deftypefnx {} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, "C")
In the first form, read from @var{fid} according to @var{template},
returning the result in the matrix @var{val}.
The optional argument @var{size} specifies the amount of data to read
and may be one of
@table @code
@item Inf
Read as much as possible, returning a column vector.
@item @var{nr}
Read up to @var{nr} elements, returning a column vector.
@item [@var{nr}, Inf]
Read as much as possible, returning a matrix with @var{nr} rows. If the
number of elements read is not an exact multiple of @var{nr}, the last
column is padded with zeros.
@item [@var{nr}, @var{nc}]
Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with
@var{nr} rows. If the number of elements read is not an exact multiple
of @var{nr}, the last column is padded with zeros.
@end table
@noindent
If @var{size} is omitted, a value of @code{Inf} is assumed.
A string is returned if @var{template} specifies only character conversions.
The number of items successfully read is returned in @var{count}.
If an error occurs, @var{errmsg} contains a system-dependent error message.
In the second form, read from @var{fid} according to @var{template},
with each conversion specifier in @var{template} corresponding to a
single scalar return value. This form is more ``C-like'', and also
compatible with previous versions of Octave. The number of successful
conversions is returned in @var{count}
@ifclear OCTAVE_MANUAL
See the Formatted Input section of the GNU Octave manual for a
complete description of the syntax of the template string.
@end ifclear
@seealso{fgets, fgetl, fread, scanf, sscanf, fopen}
@end deftypefn */)
{
static const std::string who = "fscanf";
return scanf_internal (interp, who, args);
}
static std::string
get_scan_string_data (const octave_value& val, const std::string& who)
{
std::string retval;
if (! val.is_string ())
error ("%s: argument STRING must be a string", who.c_str ());
octave_value tmp = val.reshape (dim_vector (1, val.numel ()));
retval = tmp.string_value ();
return retval;
}
DEFUN (sscanf, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {[@var{val}, @var{count}, @var{errmsg}, @var{pos}] =} sscanf (@var{string}, @var{template}, @var{size})
@deftypefnx {} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} sscanf (@var{string}, @var{template}, "C")
This is like @code{fscanf}, except that the characters are taken from the
string @var{string} instead of from a stream.
Reaching the end of the string is treated as an end-of-file condition. In
addition to the values returned by @code{fscanf}, the index of the next
character to be read is returned in @var{pos}.
@seealso{fscanf, scanf, sprintf}
@end deftypefn */)
{
static const std::string who = "sscanf";
int nargin = args.length ();
if (nargin < 2 || nargin > 3)
print_usage ();
octave_value_list retval;
std::string data = get_scan_string_data (args(0), who);
octave::stream os = octave_istrstream::create (data);
if (! os.is_valid ())
error ("%s: unable to create temporary input buffer", who.c_str ());
if (! args(1).is_string ())
error ("%s: format TEMPLATE must be a string", who.c_str ());
if (nargin == 3 && args(2).is_string ())
{
retval = os.oscanf (args(1), who);
}
else
{
octave_idx_type count = 0;
Array<double> size = (nargin == 3) ? args(2).vector_value ()
: Array<double> (dim_vector (1, 1),
lo_ieee_inf_value ());
octave_value tmp = os.scanf (args(1), size, count, who);
// FIXME: is this the right thing to do?
// Extract error message first, because getting
// position will clear it.
std::string errmsg = os.error ();
retval = ovl (tmp, count, errmsg,
(os.eof () ? data.length () : os.tell ()) + 1);
}
return retval;
}
/*
%!test <*56396>
%! [val, count, errmsg, nextpos] = sscanf ('1234a6', '%2d', 3);
%! assert (val, [12; 34]);
%! assert (count, 2);
%! assert (errmsg, "sscanf: format failed to match");
%! assert (nextpos, 5);
*/
DEFMETHOD (scanf, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {[@var{val}, @var{count}, @var{errmsg}] =} scanf (@var{template}, @var{size})
@deftypefnx {} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} scanf (@var{template}, "C")
This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.
It is currently not useful to call @code{scanf} in interactive programs.
@seealso{fscanf, sscanf, printf}
@end deftypefn */)
{
static const std::string who = "scanf";
octave_value_list tmp_args = args;
return scanf_internal (interp, who, tmp_args.prepend (octave_value (0)));
}
static octave_value_list
textscan_internal (octave::interpreter& interp, const std::string& who,
const octave_value_list& args)
{
if (args.length () < 1)
print_usage (who);
octave::stream os;
if (args(0).is_string ())
{
std::string data = get_scan_string_data (args(0), who);
os = octave_istrstream::create (data);
if (! os.is_valid ())
error ("%s: unable to create temporary input buffer", who.c_str ());
}
else
{
octave::stream_list& streams = interp.get_stream_list ();
os = streams.lookup (args(0), who);
}
int nskip = 1;
std::string fmt;
if (args.length () == 1)
{
// omitted format = %f. explicit "" = width from file
fmt = "%f";
}
else if (args(1).is_string ())
{
fmt = args(1).string_value ();
if (args(1).is_sq_string ())
fmt = octave::do_string_escapes (fmt);
nskip++;
}
else
error ("%s: FORMAT must be a string", who.c_str ());
octave_idx_type ntimes = -1;
if (args.length () > 2)
{
if (args(2).isnumeric ())
{
ntimes = args(2).idx_type_value ();
if (ntimes < args(2).double_value ())
error ("%s: REPEAT = %g is too large",
who.c_str (), args(2).double_value ());
nskip++;
}
}
octave_value_list options = args.splice (0, nskip);
octave_idx_type count = 0;
octave_value result = os.textscan (fmt, ntimes, options, who, count);
std::string errmsg = os.error ();
return ovl (result, count, errmsg);
}
DEFMETHOD (textscan, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{C} =} textscan (@var{fid}, @var{format})
@deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{repeat})
@deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{param}, @var{value}, @dots{})
@deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{repeat}, @var{param}, @var{value}, @dots{})
@deftypefnx {} {@var{C} =} textscan (@var{str}, @dots{})
@deftypefnx {} {[@var{C}, @var{position}, @var{errmsg}] =} textscan (@dots{})
Read data from a text file or string.
The string @var{str} or file associated with @var{fid} is read from and
parsed according to @var{format}. The function is an extension of
@code{strread} and @code{textread}. Differences include: the ability to
read from either a file or a string, additional options, and additional
format specifiers.
The input is interpreted as a sequence of words, delimiters (such as
whitespace), and literals. The characters that form delimiters and
whitespace are determined by the options. The format consists of format
specifiers interspersed between literals. In the format, whitespace forms
a delimiter between consecutive literals, but is otherwise ignored.
The output @var{C} is a cell array where the number of columns is determined
by the number of format specifiers.
The first word of the input is matched to the first specifier of the format
and placed in the first column of the output; the second is matched to the
second specifier and placed in the second column and so forth. If there
are more words than specifiers then the process is repeated until all words
have been processed or the limit imposed by @var{repeat} has been met (see
below).
The string @var{format} describes how the words in @var{str} should be
parsed. As in @var{fscanf}, any (non-whitespace) text in the format that is
not one of these specifiers is considered a literal. If there is a literal
between two format specifiers then that same literal must appear in the
input stream between the matching words.
The following specifiers are valid:
@table @code
@item %f
@itemx %f64
@itemx %n
The word is parsed as a number and converted to double.
@item %f32
The word is parsed as a number and converted to single (float).
@item %d
@itemx %d8
@itemx %d16
@itemx %d32
@itemx %d64
The word is parsed as a number and converted to int8, int16, int32, or
int64. If no size is specified then int32 is used.
@item %u
@itemx %u8
@itemx %u16
@itemx %u32
@itemx %u64
The word is parsed as a number and converted to uint8, uint16, uint32, or
uint64. If no size is specified then uint32 is used.
@item %s
The word is parsed as a string ending at the last character before
whitespace, an end-of-line, or a delimiter specified in the options.
@item %q
The word is parsed as a "quoted string".
If the first character of the string is a double quote (") then the string
includes everything until a matching double quote---including whitespace,
delimiters, and end-of-line characters. If a pair of consecutive double
quotes appears in the input, it is replaced in the output by a single
double quote. For examples, the input "He said ""Hello""" would
return the value 'He said "Hello"'.
@item %c
The next character of the input is read.
This includes delimiters, whitespace, and end-of-line characters.
@item %[@dots{}]
@itemx %[^@dots{}]
In the first form, the word consists of the longest run consisting of only
characters between the brackets. Ranges of characters can be specified by
a hyphen; for example, %[0-9a-zA-Z] matches all alphanumeric characters (if
the underlying character set is ASCII). Since @sc{matlab} treats hyphens
literally, this expansion only applies to alphanumeric characters. To
include '-' in the set, it should appear first or last in the brackets; to
include ']', it should be the first character. If the first character is
'^' then the word consists of characters @strong{not} listed.
@item %N@dots{}
For %s, %c %d, %f, %n, %u, an optional width can be specified as %Ns, etc.
where N is an integer > 1. For %c, this causes exactly N characters to be
read instead of a single character. For the other specifiers, it is an
upper bound on the number of characters read; normal delimiters can cause
fewer characters to be read. For complex numbers, this limit applies to
the real and imaginary components individually. For %f and %n, format
specifiers like %N.Mf are allowed, where M is an upper bound on number of
characters after the decimal point to be considered; subsequent digits are
skipped. For example, the specifier %8.2f would read 12.345e6 as 1.234e7.
@item %*@dots{}
The word specified by the remainder of the conversion specifier is skipped.
@item literals
In addition the format may contain literal character strings; these will be
skipped during reading. If the input string does not match this literal,
the processing terminates.
@end table
Parsed words corresponding to the first specifier are returned in the first
output argument and likewise for the rest of the specifiers.
By default, if there is only one input argument, @var{format} is @t{"%f"}.
This means that numbers are read from the input into a single column vector.
If @var{format} is explicitly empty (@qcode{""}) then textscan will
return data in a number of columns matching the number of fields on the
first data line of the input. Either of these is suitable only when the
input is exclusively numeric.
For example, the string
@smallexample
@group
@var{str} = "\
Bunny Bugs 5.5\n\
Duck Daffy -7.5e-5\n\
Penguin Tux 6"
@end group
@end smallexample
@noindent
can be read using
@example
@var{a} = textscan (@var{str}, "%s %s %f");
@end example
The optional numeric argument @var{repeat} can be used for limiting the
number of items read:
@table @asis
@item -1
Read all of the string or file until the end (default).
@item N
Read until the first of two conditions occurs: 1) the format has been
processed N times, or 2) N lines of the input have been processed. Zero
(0) is an acceptable value for @var{repeat}. Currently, end-of-line
characters inside %q, %c, and %[@dots{}]$ conversions do not contribute to
the line count. This is incompatible with @sc{matlab} and may change in
future.
@end table
The behavior of @code{textscan} can be changed via property/value pairs.
The following properties are recognized:
@table @asis
@item @qcode{"BufSize"}
This specifies the number of bytes to use for the internal buffer.
A modest speed improvement may be obtained by setting this to a large value
when reading a large file, especially if the input contains long strings.
The default is 4096, or a value dependent on @var{n} if that is specified.
@item @qcode{"CollectOutput"}
A value of 1 or true instructs @code{textscan} to concatenate consecutive
columns of the same class in the output cell array. A value of 0 or false
(default) leaves output in distinct columns.
@item @qcode{"CommentStyle"}
Specify parts of the input which are considered comments and will be
skipped. @var{value} is the comment style and can be either (1) A string
or 1x1 cell string, to skip everything to the right of it; (2) A cell array
of two strings, to skip everything between the first and second strings.
Comments are only parsed where whitespace is accepted and do not act as
delimiters.
@item @qcode{"Delimiter"}
If @var{value} is a string, any character in @var{value} will be used to
split the input into words. If @var{value} is a cell array of strings,
any string in the array will be used to split the input into words.
(default value = any whitespace.)
@item @qcode{"EmptyValue"}
Value to return for empty numeric values in non-whitespace delimited data.
The default is NaN@. When the data type does not support NaN (int32 for
example), then the default is zero.
@item @qcode{"EndOfLine"}
@var{value} can be either an empty or one character specifying the
end-of-line character, or the pair
@qcode{"@xbackslashchar{}r@xbackslashchar{}n"} (CRLF).
In the latter case, any of
@qcode{"@xbackslashchar{}r"}, @qcode{"@xbackslashchar{}n"} or
@qcode{"@xbackslashchar{}r@xbackslashchar{}n"} is counted as a (single)
newline. If no value is given,
@qcode{"@xbackslashchar{}r@xbackslashchar{}n"} is used.
@c If set to "" (empty string) EOLs are ignored as delimiters and added
@c to whitespace.
@c When reading from a character string, optional input argument @var{n}
@c specifies the number of times @var{format} should be used (i.e., to limit
@c the amount of data read).
@c When reading from file, @var{n} specifies the number of data lines to read;
@c in this sense it differs slightly from the format repeat count in strread.
@item @qcode{"HeaderLines"}
The first @var{value} number of lines of @var{fid} are skipped. Note that
this does not refer to the first non-comment lines, but the first lines of
any type.
@item @qcode{"MultipleDelimsAsOne"}
If @var{value} is nonzero, treat a series of consecutive delimiters,
without whitespace in between, as a single delimiter. Consecutive
delimiter series need not be vertically aligned. Without this option, a
single delimiter before the end of the line does not cause the line to be
considered to end with an empty value, but a single delimiter at the start
of a line causes the line to be considered to start with an empty value.
@item @qcode{"TreatAsEmpty"}
Treat single occurrences (surrounded by delimiters or whitespace) of the
string(s) in @var{value} as missing values.
@item @qcode{"ReturnOnError"}
If set to numerical 1 or true, return normally as soon as an error is
encountered, such as trying to read a string using @code{%f}.
If set to 0 or false, return an error and no data.
@item @qcode{"Whitespace"}
Any character in @var{value} will be interpreted as whitespace and trimmed;
The default value for whitespace is
@c Note: the next line specifically has a newline which generates a space
@c in the output of qcode, but keeps the next line < 80 characters.
@qcode{"
@xbackslashchar{}b@xbackslashchar{}r@xbackslashchar{}n@xbackslashchar{}t"}
(note the space). Unless whitespace is set to @qcode{""} (empty) AND at
least one @qcode{"%s"} format conversion specifier is supplied, a space is
always part of whitespace.
@end table
When the number of words in @var{str} or @var{fid} doesn't match an exact
multiple of the number of format conversion specifiers, @code{textscan}'s
behavior depends on whether the last character of the string or file is an
end-of-line as specified by the @code{EndOfLine} option:
@table @asis
@item last character = end-of-line
Data columns are padded with empty fields, NaN or 0 (for integer fields) so
that all columns have equal length
@item last character is not end-of-line
Data columns are not padded; @code{textscan} returns columns of unequal
length
@end table
The second output @var{position} provides the location, in characters
from the beginning of the file or string, where processing stopped.
@seealso{dlmread, fscanf, load, strread, textread}
@end deftypefn */)
{
static const std::string who = "textscan";
return textscan_internal (interp, who, args);
}
DEFMETHOD (__textscan__, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{C} =} __textscan__ (@var{who}, @dots{})
Like @code{textscan} but accept additional argument @var{who} to use
as the name of the function when reporting errors.
@end deftypefn */)
{
if (args.length () == 0)
print_usage ();
return textscan_internal (interp, args(0).string_value (),
args.splice (0, 1));
}
/*
%!test
%! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12";
%! fmtstr = "%f %d %f %s";
%! c = textscan (str, fmtstr, 2, "delimiter", ",", "emptyvalue", -Inf);
%! assert (c{1}, [1;5]);
%! assert (c{3}, [3; -Inf]);
%! assert (iscellstr (c{4}));
%!test
%! b = [10:10:100];
%! b = [b; 8*b/5];
%! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b);
%! fmt = "%f miles/hr = %f kilometers/hr";
%! c = textscan (str, fmt);
%! assert (c{1}, b(1,:)', 1e-5);
%! assert (c{2}, b(2,:)', 1e-5);
%!test
%! str = "13, -, NA, str1, -25\r\n// Middle line\r\n36, na, 05, str3, 6";
%! c = textscan (str, "%d %n %f %s %n", "delimiter", ",",
%! "treatAsEmpty", {"NA", "na", "-"}, "commentStyle", "//");
%! assert (c{1}, int32 ([13; 36]));
%! assert (c{2}, [NaN; NaN]);
%! assert (c{3}, [NaN; 5]);
%! assert (c{4}, {"str1"; "str3"});
%! assert (c{5}, [-25; 6]);
%!test
%! str = "Km:10 = hhhBjjj miles16hour\r\n";
%! str = [str "Km:15 = hhhJjjj miles241hour\r\n"];
%! str = [str "Km:2 = hhhRjjj miles3hour\r\n"];
%! str = [str "Km:25 = hhhZ\r\n"];
%! fmt = "Km:%d = hhh%1sjjj miles%dhour";
%! c = textscan (str, fmt, "delimiter", " ");
%! assert (c{1}', int32 ([10, 15, 2, 25]));
%! assert (c{2}', {'B' 'J' 'R' 'Z'});
%! assert (c{3}', int32 ([16, 241, 3, 0]));
## Test with default EndOfLine parameter
%!test
%! c = textscan ("L1\nL2", "%s");
%! assert (c{:}, {"L1"; "L2"});
## Test with EndofLine parameter set to "" (empty) - newline should be in word
%!test
%! c = textscan ("L1\nL2", "%s", "endofline", "");
%! assert (int8 ([c{:}{:}]), int8 ([76, 49, 10, 76, 50]));
## Matlab fails this test. A literal after a conversion is not a delimiter
%!#test
%! ## No delimiters at all besides EOL. Skip fields, even empty fields
%! str = "Text1Text2Text\nTextText4Text\nText57Text";
%! c = textscan (str, "Text%*dText%dText");
%! assert (c{1}, int32 ([2; 4; 0]));
## CollectOutput test
%!test
%! b = [10:10:100];
%! b = [b; 8*b/5; 8*b*1000/5];
%! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b);
%! fmt = "%f miles%s %s %f (%f) kilometers %*s";
%! c = textscan (str, fmt, "collectoutput", 1);
%! assert (size (c{3}), [10, 2]);
%! assert (size (c{2}), [10, 2]);
## CollectOutput test with uneven column length files
%!test
%! b = [10:10:100];
%! b = [b; 8*b/5; 8*b*1000/5];
%! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b);
%! str = [str "110 miles/hr"];
%! fmt = "%f miles%s %s %f (%f) kilometers %*s";
%! c = textscan (str, fmt, "collectoutput", 1);
%! assert (size (c{1}), [11, 1]);
%! assert (size (c{3}), [11, 2]);
%! assert (size (c{2}), [11, 2]);
%! assert (c{3}(end), NaN);
%! assert (c{2}{11, 1}, "/hr");
%! assert (isempty (c{2}{11, 2}), true);
## Double quoted string
%!test
%! str = 'First "the second called ""the middle""" third';
%! fmt = "%q";
%! c = textscan (str, fmt);
%! assert (c{1}, {"First"; 'the second called "the middle"'; "third"});
## Arbitrary character
%!test
%! c = textscan ("a first, \n second, third", "%s %c %11c", "delimiter", " ,");
%! assert (c{1}, {"a"; "ond"});
%! assert (c{2}, {"f"; "t"});
%! assert (c{3}, {"irst, \n sec"; "hird"});
## Field width and non-standard delimiters
%!test
%! str = "12;34;123456789;7";
%! c = textscan (str, "%4d %4d", "delimiter", ";", "collectOutput", 1);
%! assert (c, {[12, 34; 1234, 5678; 9, 7]});
## Field width and non-standard delimiters (2)
%!test
%! str = "12;34;123456789;7";
%! c = textscan (str, "%4f %f", "delimiter", ";", "collectOutput", 1);
%! assert (c, {[12, 34; 1234, 56789; 7, NaN]});
## Ignore trailing delimiter, but use leading one
%!test
%! str = "12.234e+2,34, \n12345.789-9876j,78\n,10|3";
%! c = textscan (str, "%10.2f %f", "delimiter", ",", "collectOutput", 1,
%! "expChars", "e|");
%! assert (c, {[1223, 34; 12345.79-9876j, 78; NaN, 10000]}, 1e-6);
## Multi-character delimiter
%!test
%! str = "99end2 space88gap 4564";
%! c = textscan (str, "%d %s", "delimiter", {"end", "gap", "space"});
%! assert (c{1}, int32 ([99; 88]));
%! assert (c{2}, {"2 "; "4564"});
## FIXME: Following two tests still fail (4/13/2016).
## Delimiters as part of literals, and following literals
%!#test
%! str = "12 R&D & 7";
%! c = textscan (str, "%f R&D %f", "delimiter", "&", "collectOutput", 1,
%! "EmptyValue", -99);
%! assert (c, {[12, -99; 7, -99]});
## Delimiters as part of literals, and before literals
%!#test
%! str = "12 & R&D 7";
%! c = textscan (str, "%f R&D %f", "delimiter", "&", "collectOutput", 1);
%! assert (c, {[12 7]});
## Check number of lines read, not number of passes through format string
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid, "1\n2\n3\n4\n5\n6");
%! fseek (fid, 0, "bof");
%! c = textscan (fid, "%f %f", 2);
%! E = feof (fid);
%! fclose (fid);
%! unlink (f);
%! assert (c, {1, 2});
%! assert (! E);
## Check number of lines read, not number of passes through format string
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid, "1\r\n2\r3\n4\r\n5\n6");
%! fseek (fid, 0, "bof");
%! c = textscan (fid, "%f %f", 4);
%! fclose (fid);
%! unlink (f);
%! assert (c, {[1;3], [2;4]});
## Check number of lines read, with multiple delimiters
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid, "1-\r\n-2\r3-\n-4\r\n5\n6");
%! fseek (fid, 0, "bof");
%! c = textscan (fid, "%f %f", 4, "delimiter", "-", "multipleDelimsAsOne", 1);
%! fclose (fid);
%! unlink (f);
%! assert (c, {[1;3], [2;4]});
## Check ReturnOnError
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! str = "1 2 3\n4 s 6";
%! fprintf (fid, str);
%! fseek (fid, 0, "bof");
%! c = textscan (fid, "%f %f %f", "ReturnOnError", 1);
%! fseek (fid, 0, "bof");
%! fclose (fid);
%! unlink (f);
%! u = textscan (str, "%f %f %f", "ReturnOnError", 1);
%! assert (c, {[1;4], [2], [3]});
%! assert (u, {[1;4], [2], [3]});
%! ## Check ReturnOnError (2)
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! str = "1 2 3\n4 s 6\n";
%! fprintf (fid, str);
%! fseek (fid, 0, "bof");
%! c = textscan (fid, "%f %f %f", "ReturnOnError", 1);
%! fseek (fid, 0, "bof");
%! fclose (fid);
%! unlink (f);
%! u = textscan (str, "%f %f %f", "ReturnOnError", 1);
%! assert (c, {[1;4], 2, 3});
%! assert (u, {[1;4], 2, 3});
%!error <Read error in field 2 of row 2>
%! textscan ("1 2 3\n4 s 6", "%f %f %f", "ReturnOnError", 0);
## Check ReturnOnError (3)
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid, "1 s 3\n4 5 6");
%! fseek (fid, 0, "bof");
%! c = textscan (fid, "", "ReturnOnError", 1);
%! fseek (fid, 0, "bof");
%! fclose (fid);
%! unlink (f);
%! assert (c, {1});
## Check ReturnOnError with empty fields
%!test
%! c = textscan ("1,,3\n4,5,6", "", "Delimiter", ",", "ReturnOnError", 1);
%! assert (c, {[1;4], [NaN;5], [3;6]});
## Check ReturnOnError with empty fields (2)
%!test
%! c = textscan ("1,,3\n4,5,6", "%f %f %f", "Delimiter", ",",
%! "ReturnOnError", 1);
%! assert (c, {[1;4], [NaN;5], [3;6]});
## Check ReturnOnError in first column
%!test
%! c = textscan ("1 2 3\ns 5 6", "", "ReturnOnError", 1);
%! assert (c, {1, 2, 3});
## FIXME: This test fails (4/14/16)
## Test incomplete first data line
%!#test
%! R = textscan (['Empty1' char(10)], 'Empty%d %f');
%! assert (R{1}, int32 (1));
%! assert (isempty (R{2}), true);
%!test <*37023>
%! data = textscan (" 1. 1 \n 2 3\n", '%f %f');
%! assert (data{1}, [1; 2], 1e-15);
%! assert (data{2}, [1; 3], 1e-15);
## Whitespace test using delimiter ";"
%!test <*37333>
%! tc{1, 1} = "C:/code;";
%! tc{1, end+1} = "C:/code/meas;";
%! tc{1, end+1} = " C:/code/sim;";
%! tc{1, end+1} = "C:/code/utils;";
%! string = [tc{:}];
%! c = textscan (string, "%s", "delimiter", ";");
%! for k = 1:max (numel (c{1}), numel (tc))
%! lh = c{1}{k};
%! rh = tc{k};
%! rh(rh == ";") = "";
%! rh = strtrim (rh);
%! assert (strcmp (lh, rh));
%! endfor
## Whitespace test, adding multipleDelimsAsOne true arg
%!test <*37333>
%! tc{1, 1} = "C:/code;";
%! tc{1, end+1} = " C:/code/meas;";
%! tc{1, end+1} = "C:/code/sim;;";
%! tc{1, end+1} = "C:/code/utils;";
%! string = [tc{:}];
%! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 1);
%! for k = 1:max (numel (c{1}), numel (tc))
%! lh = c{1}{k};
%! rh = tc{k};
%! rh(rh == ";") = "";
%! rh = strtrim (rh);
%! assert (strcmp (lh, rh));
%! endfor
## Whitespace test (bug #37333), adding multipleDelimsAsOne false arg
%!test <*37333>
%! tc{1, 1} = "C:/code;";
%! tc{1, end+1} = " C:/code/meas;";
%! tc{1, end+1} = "C:/code/sim;;";
%! tc{1, end+1} = "";
%! tc{1, end+1} = "C:/code/utils;";
%! string = [tc{:}];
%! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 0);
%! for k = 1:max (numel (c{1}), numel (tc))
%! lh = c{1}{k};
%! rh = tc{k};
%! rh(rh == ";") = "";
%! rh = strtrim (rh);
%! assert (strcmp (lh, rh));
%! endfor
## Whitespace test (bug #37333) whitespace "" arg
%!test <*37333>
%! tc{1, 1} = "C:/code;";
%! tc{1, end+1} = " C:/code/meas;";
%! tc{1, end+1} = "C:/code/sim;";
%! tc{1, end+1} = "C:/code/utils;";
%! string = [tc{:}];
%! c = textscan (string, "%s", "delimiter", ";", "whitespace", "");
%! for k = 1:max (numel (c{1}), numel (tc))
%! lh = c{1}{k};
%! rh = tc{k};
%! rh(rh == ";") = "";
%! assert (strcmp (lh, rh));
%! endfor
## Whitespace test (bug #37333), whitespace " " arg
%!test <*37333>
%! tc{1, 1} = "C:/code;";
%! tc{1, end+1} = " C:/code/meas;";
%! tc{1, end+1} = "C:/code/sim;";
%! tc{1, end+1} = "C:/code/utils;";
%! string = [tc{:}];
%! c = textscan (string, "%s", "delimiter", ";", "whitespace", " ");
%! for k = 1:max (numel (c{1}), numel (tc))
%! lh = c{1}{k};
%! rh = tc{k};
%! rh(rh == ";") = "";
%! rh = strtrim (rh);
%! assert (strcmp (lh, rh));
%! endfor
## Tests reading with empty format, should return proper nr of columns
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid, " 1 2 3 4\n5 6 7 8");
%! fseek (fid, 0, "bof");
%! C = textscan (fid, "");
%! E = feof (fid);
%! fclose (fid);
%! unlink (f);
%! assert (C{1}, [1 ; 5], 1e-6);
%! assert (C{2}, [2 ; 6], 1e-6);
%! assert (C{3}, [3 ; 7], 1e-6);
%! assert (C{4}, [4 ; 8], 1e-6);
%! assert (E);
## Test leaving the file at the correct position on exit
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid, "1,2\n3,4\n");
%! fseek (fid, 0, "bof");
%! C = textscan (fid, "%s %f", 2, "Delimiter", ",");
%! E = ftell (fid);
%! fclose (fid);
%! unlink (f);
%! assert (E, 8);
## Tests reading with empty format; empty fields & incomplete lower row
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid, " ,2,,4\n5,6");
%! fseek (fid, 0, "bof");
%! C = textscan (fid, "", "delimiter", ",", "EmptyValue", 999,
%! "CollectOutput" , 1);
%! fclose (fid);
%! unlink (f);
%! assert (C{1}, [999, 2, 999, 4; 5, 6, 999, 999], 1e-6);
## Error message tests
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! msg1 = "textscan: 1 parameters given, but only 0 values";
%! try
%! C = textscan (fid, "", "headerlines");
%! end_try_catch;
%! assert (! feof (fid));
%! fclose (fid);
%! unlink (f);
%! assert (msg1, lasterr);
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! msg1 = "textscan: HeaderLines must be numeric";
%! try
%! C = textscan (fid, "", "headerlines", "hh");
%! end_try_catch;
%! fclose (fid);
%! unlink (f);
%! assert (msg1, lasterr);
## Skip headerlines
%!test
%! C = textscan ("field 1 field2\n 1 2\n3 4", "", "headerlines", 1,
%! "collectOutput", 1);
%! assert (C, {[1 2; 3 4]});
## Skip headerlines with non-default EOL
%!test
%! C = textscan ("field 1 field2\r 1 2\r3 4", "", "headerlines", 2,
%! "collectOutput", 1, "EndOfLine", '\r');
%! assert (C, {[3 4]});
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid,"some_string");
%! fseek (fid, 0, "bof");
%! msg1 = "textscan: EndOfLine must be at most one character or '\\r\\n'";
%! try
%! C = textscan (fid, "%f", "EndOfLine", "\n\r");
%! end_try_catch;
%! fclose (fid);
%! unlink (f);
%! assert (msg1, lasterr);
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid,"some_string");
%! fseek (fid, 0, "bof");
%! msg1 = "textscan: EndOfLine must be at most one character or '\\r\\n'";
%! try
%! C = textscan (fid, "%f", "EndOfLine", 33);
%! end_try_catch;
%! fclose (fid);
%! unlink (f);
%! assert (msg1, lasterr);
%!assert <*41824> (textscan ("123", "", "whitespace", " "){:}, 123);
## just test supplied emptyvalue
%!assert <*42343> (textscan (",NaN", "", "delimiter", "," ,"emptyValue" ,Inf),
%! {Inf, NaN})
## test padding with supplied emptyvalue
%!test <*42343>
%! c = textscan (",1,,4\nInf, ,NaN\n", "", "delimiter", ",",
%! "emptyvalue", -10);
%! assert (cell2mat (c), [-10, 1, -10, 4; Inf, -10, NaN, -10]);
%!test <*42528>
%! assert (textscan ("1i", ""){1}, 0+1i);
%! C = textscan ("3, 2-4i, NaN\n -i, 1, 23.4+2.2i\n 1+1 1+1j", "",
%! "delimiter", ",");
%! assert (cell2mat (C), [3+0i, 2-4i, NaN+0i; 0-i, 1+0i, 23.4+2.2i; 1 1 1+1i]);
%!test
%! ## TreatAsEmpty
%! C = textscan ("1,2,3,NN,5,6\n", "%d%d%d%f", "delimiter", ",",
%! "TreatAsEmpty", "NN");
%! assert (C{3}(1), int32 (3));
%! assert (C{4}(1), NaN);
## MultipleDelimsAsOne
%!test
%! str = "11, 12, 13,, 15\n21,, 23, 24, 25\n,, 33, 34, 35\n";
%! C = textscan (str, "%f %f %f %f", "delimiter", ",",
%! "multipledelimsasone", 1, "endofline", "\n");
%! assert (C{1}', [11, 21, 33]);
%! assert (C{2}', [12, 23, 34]);
%! assert (C{3}', [13, 24, 35]);
%! assert (C{4}', [15, 25, NaN]);
## Single-quoted escape sequences
%!test
%! str = "11\t12\t13\r21\t22\t23";
%! c = textscan (str, "", "delimiter", '\t', "EndOfLine", '\r');
%! assert (c{1}', [11, 21]);
%! assert (c{2}', [12, 22]);
%! assert (c{3}', [13, 23]);
%!test <*44750>
%! c = textscan ("/home/foo/", "%s", "delimiter", "/",
%! "MultipleDelimsAsOne", 1);
%! assert (c{1}, {"home"; "foo"});
## FIXME: Test still fails (4/13/2016).
## Allow cuddling %sliteral, but warn it is ambiguous
%!#test
%! C = textscan ("abcxyz51\nxyz83\n##xyz101", "%s xyz %d");
%! assert (C{1}([1 3]), {"abc"; "##"});
%! assert (isempty (C{1}{2}), true);
%! assert (C{2}, int32 ([51; 83; 101]));
## Literals are not delimiters.
## Test for false positives in check for non-supported format specifiers
%!test
%! c = textscan ("Total: 32.5 % (of cm values)",
%! "Total: %f %% (of cm values)");
%! assert (c{1}, 32.5, 1e-5);
## Test various forms of string format specifiers
%!test <*45712>
%! str = "14 :1 z:2 z:3 z:5 z:11";
%! C = textscan (str, "%f %s %*s %3s %*3s %f", "delimiter", ":");
%! assert (C, {14, {"1 z"}, {"3 z"}, 11});
## Bit width, fixed width conversion specifiers
%!test
%! str2 = "123456789012345 ";
%! str2 = [str2 str2 str2 str2 str2 str2 str2 str2];
%! str2 = [str2 "123456789.01234 1234567890.1234 12345.678901234 12345.678901234"];
%! pttrn = "%3u8%*s %5u16%*s %10u32%*s %15u64 %3d8%*s %5d16%*s %10d32%*s %15d64 %9f32%*s %14f64%*s %10.2f32%*s %12.2f64%*s";
%! C = textscan (str2, pttrn, "delimiter", " ");
%! assert (C{1}, uint8 (123));
%! assert (C{2}, uint16 (12345));
%! assert (C{3}, uint32 (1234567890));
%! assert (C{4}, uint64 (123456789012345));
%! assert (C{5}, int8 (123));
%! assert (C{6}, int16 (12345));
%! assert (C{7}, int32 (1234567890));
%! assert (C{8}, int64 (123456789012345));
%! assert (C{9}, single (123456789), 1e-12);
%! assert (C{10}, double (1234567890.123), 1e-15);
%! assert (C{11}, single (12345.68), 1e-5);
%! assert (C{12}, double (12345.68), 1e-11);
## Bit width, fixed width conv. specifiers -- check the right amount is left
%!test
%! str2 = "123456789012345 ";
%! str2 = [str2 str2 "123456789.01234"];
%! pttrn = "%3u8 %5u16 %10u32 %3d8 %5d16 %10d32 %9f32 %9f";
%! C = textscan (str2, pttrn, "delimiter", " ");
%! assert (C{1}, uint8 (123));
%! assert (C{2}, uint16 (45678));
%! assert (C{3}, uint32 (9012345));
%! assert (C{4}, int8 (123));
%! assert (C{5}, int16 (45678));
%! assert (C{6}, int32 (9012345));
%! assert (C{7}, single (123456789), 1e-12);
%! assert (C{8}, double (0.01234), 1e-12);
%!test
%! C = textscan ("123.123", "%2f %3f %3f");
%! assert (C{1}, 12);
%! assert (C{2}, 3.1, 1e-11);
%! assert (C{3}, 23);
%!test
%! C = textscan ("123.123", "%3f %3f %3f");
%! assert (C{1}, 123);
%! assert (C{2}, 0.12, 1e-11);
%! assert (C{3}, 3);
%!test
%! C = textscan ("123.123", "%4f %3f");
%! assert (C{1}, 123);
%! assert (C{2}, 123);
## field width interrupts exponent. (Matlab incorrectly gives [12, 2e12])
%!test
%! assert (textscan ("12e12", "%4f"), {[120; 2]});
%! assert (textscan ("12e+12", "%5f"), {[120; 2]});
%! assert (textscan ("125e-12","%6f"), {[12.5; 2]});
## %[] tests
## Plain [..] and *[..]
%!test
%! ar = "abcdefguvwxAny\nacegxyzTrailing\nJunk";
%! C = textscan (ar, "%[abcdefg] %*[uvwxyz] %s");
%! assert (C{1}, {"abcdefg"; "aceg"; ""});
%! assert (C{2}, {"Any"; "Trailing"; "Junk"});
%!test
%! assert (textscan ("A2 B2 C3", "%*[ABC]%d", 3), {int32([2; 2; 3])});
## [^..] and *[^..]
%!test
%! br = "abcdefguvwx1Any\nacegxyz2Trailing\n3Junk";
%! C = textscan (br, "%[abcdefg] %*[^0123456789] %s");
%! assert (C{1}, {"abcdefg"; "aceg"; ""});
%! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
## [..] and [^..] containing delimiters
%!test
%! cr = "ab cd efguv wx1Any\na ce gx yz2Trailing\n 3Junk";
%! C = textscan (cr, "%[ abcdefg] %*[^0123456789] %s", "delimiter", " \n",
%! "whitespace", "");
%! assert (C{1}, {"ab cd efg"; "a ce g"; " "});
%! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
%!assert <*36464> (textscan ("1 2 3 4 5 6", "%*n%n%*[^\n]"){1}, 2);
## test %[]] and %[^]]
%!test
%! assert (textscan ("345]", "%*[123456]%[]]"){1}{1}, "]");
%! assert (textscan ("345]", "%*[^]]%s"){1}{1}, "]");
## Test that "-i" checks the next two characters
%!test
%! C = textscan ("-i -in -inf -infinity", "%f %f%s %f %f %s");
%! assert (C, {-i, -i, {"n"}, -Inf, -Inf, {"inity"}});
## Again for "+i", this time with custom parser
%!test
%! C = textscan ("+i +in +inf +infinity", "%f %f%s %f %f %s", "ExpChars", "eE");
%! assert (C, {i, i, {"n"}, Inf, Inf, {"inity"}});
## Check single quoted format interprets control sequences
%!test
%! C = textscan ("1 2\t3 4", '%f %[^\t] %f %f');
%! assert (C, {1, {"2"}, 3, 4});
## Check a non-empty line with no valid conversion registers empytValue
%!test
%! C = textscan ("Empty\n", "Empty%f %f");
%! assert (C, { NaN, NaN });
## Check overflow and underflow of integer types
%!test
%! a = "-1e90 ";
%! b = "1e90 ";
%! fmt = "%d8 %d16 %d32 %d64 %u8 %u16 %u32 %u64 ";
%! C = textscan ([a a a a a a a a b b b b b b b b], fmt);
%! assert (C{1}, int8 ([-128; 127]));
%! assert (C{2}, int16 ([-32768; 32767]));
%! assert (C{3}, int32 ([-2147483648; 2147483647]));
%! assert (C{4}, int64 ([-9223372036854775808; 9223372036854775807]));
%! assert (C{5}, uint8 ([0; 255]));
%! assert (C{6}, uint16 ([0; 65535]));
%! assert (C{7}, uint32 ([0; 4294967295]));
%! assert (C{8}, uint64 ([0; 18446744073709551615]));
## Tests from Matlab (does The MathWorks have any copyright over the input?)
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid,"09/12/2005 Level1 12.34 45 1.23e10 inf Nan Yes 5.1+3i\n");
%! fprintf (fid,"10/12/2005 Level2 23.54 60 9e19 -inf 0.001 No 2.2-.5i\n");
%! fprintf (fid,"11/12/2005 Level3 34.90 12 2e5 10 100 No 3.1+.1i\n");
%! fseek (fid, 0, "bof");
%! C = textscan (fid,"%s %s %f32 %d8 %u %f %f %s %f");
%! %assert (C{1}, {"09/12/2005";"10/12/2005";"11/12/2005"});
%! assert (C{2}, {"Level1";"Level2";"Level3"});
%! assert (C{3}, [single(12.34);single(23.54);single(34.90)]);
%! assert (C{4}, [int8(45);int8(60);int8(12)]);
%! assert (C{5}, [uint32(4294967295);uint32(4294967295);uint32(200000)]);
%! assert (C{6}, [inf;-inf;10]);
%! assert (C{7}, [NaN;0.001;100], eps);
%! assert (C{8}, {"Yes";"No";"No"});
%! assert (C{9}, [5.1+3i;2.2-0.5i;3.1+0.1i]);
%! fseek (fid, 0, "bof");
%! C = textscan (fid,"%s Level%d %f32 %d8 %u %f %f %s %f");
%! assert (C{2}, [int32(1);int32(2);int32(3)]);
%! assert (C{3}, [single(12.34);single(23.54);single(34.90)]);
%! fseek (fid, 0, "bof");
%! C = textscan (fid, '%s %*[^\n]');
%! fclose (fid);
%! unlink (f);
%! assert (C, {{"09/12/2005";"10/12/2005";"11/12/2005"}});
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid,"1, 2, 3, 4, , 6\n");
%! fprintf (fid,"7, 8, 9, , 11, 12\n");
%! fseek (fid, 0, "bof");
%! C = textscan (fid,"%f %f %f %f %u8 %f", "Delimiter",",","EmptyValue",-Inf);
%! fclose (fid);
%! unlink (f);
%! assert (C{4}, [4; -Inf]);
%! assert (C{5}, uint8 ([0; 11]));
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! fprintf (fid,"abc, 2, NA, 3, 4\n");
%! fprintf (fid,"// Comment Here\n");
%! fprintf (fid,"def, na, 5, 6, 7\n");
%! fseek (fid, 0, "bof");
%! C = textscan (fid, "%s %n %n %n %n", "Delimiter", ",",
%! "TreatAsEmpty", {"NA","na"}, "CommentStyle", "//");
%! fclose (fid);
%! unlink (f);
%! assert (C{1}, {"abc";"def"});
%! assert (C{2}, [2; NaN]);
%! assert (C{3}, [NaN; 5]);
%! assert (C{4}, [3; 6]);
%! assert (C{5}, [4; 7]);
## FIXME: Almost passes. Second return value is {"/"}. Tested 4/14/16.
## Test start of comment as string
%!#test
%! c = textscan ("1 / 2 // 3", "%n %s %u8", "CommentStyle", {"//"});
%! assert (c(1), {1, "/", 2});
%!assert (textscan (["1 2 3 4"; "5 6 7 8"], "%f"), {[15; 26; 37; 48]})
## Check for delimiter after exponent
%!assert (textscan ("1e-3|42", "%f", "delimiter", "|"), {[1e-3; 42]})
%!test <*52479>
%! str = "\t\ta\tb\tc\n";
%! ret = textscan (str, "%s", "delimiter", "\t");
%! assert (ret, { {''; ''; 'a'; 'b'; 'c'} });
%!test <*52479>
%! str = "\t\ta\tb\tc\n";
%! ret = textscan (str, "%s", "delimiter", {"\t"});
%! assert (ret, { {''; ''; 'a'; 'b'; 'c'} });
%!test <*52550>
%! str = ",,1,2,3\n";
%! obs = textscan (str, "%d", "delimiter", ",");
%! assert (obs, { [0; 0; 1; 2; 3] });
%! obs = textscan (str, "%d", "delimiter", {","});
%! assert (obs, { [0; 0; 1; 2; 3] });
%!test <*52550>
%! str = " , ,1,2,3\n";
%! obs = textscan (str, "%d", "delimiter", ",");
%! assert (obs, { [0; 0; 1; 2; 3] });
%! textscan (str, "%d", "delimiter", {","});
%! assert (obs, { [0; 0; 1; 2; 3] });
%!test <*52550>
%! str = " 0 , 5+6j , -INF+INFj ,NaN,3\n";
%! obs = textscan (str, "%f", "delimiter", ",");
%! assert (obs, { [0; 5+6i; complex(-Inf,Inf); NaN; 3] });
%! obs = textscan (str, "%f", "delimiter", {","});
%! assert (obs, { [0; 5+6i; complex(-Inf,Inf); NaN; 3] });
%!test <*52550>
%! str = " 0;,;,1;,2;,3\n";
%! assert (textscan (str, "%f", "delimiter", {";,"}), { [0; NaN; 1; 2; 3] });
%!test <*52550>
%! str = " 0 ;1 , $ 2 ;3\n";
%! obs = textscan (str, "%f", "delimiter", ",;$");
%! assert (obs, { [0; 1; NaN; 2; 3] });
%! obs = textscan (str, "%f", "delimiter", {",",";","$"});
%! assert (obs, { [0; 1; NaN; 2; 3] });
## file stream with encoding
%!test
%! f = tempname ();
%! fid = fopen (f, "w+", "n", "iso-8859-1");
%! unwind_protect
%! fprintf (fid, "abc,äöü\n");
%! fseek (fid, 0, "bof");
%! obs = textscan (fid, "%s", "delimiter", ",");
%! fclose (fid);
%! assert (obs, { {"abc"; "äöü"} });
%! unwind_protect_cleanup
%! unlink (f);
%! end_unwind_protect
%!test <*56917>
%! str = '"a,b","c"';
%! obs = textscan (str, "%q", "delimiter", ",");
%! assert (obs, { { "a,b"; "c" } });
*/
// These tests have end-comment sequences, so can't just be in a comment
#if 0
## Test unfinished comment
%!test
%! c = textscan ("1 2 /* half comment", "%n %u8", "CommentStyle", {"/*", "*/"});
%! assert (c, {1, 2});
## Test reading from a real file
%!test
%! f = tempname ();
%! fid = fopen (f, "w+");
%! d = rand (1, 4);
%! fprintf (fid, " %f %f /* comment */ %f %f ", d);
%! fseek (fid, 0, "bof");
%! A = textscan (fid, "%f %f", "CommentStyle", {"/*", "*/"});
%! E = feof (fid);
%! fclose (fid);
%! unlink (f);
%! assert (A{1}, [d(1); d(3)], 1e-6);
%! assert (A{2}, [d(2); d(4)], 1e-6);
%! assert (E);
#endif
/*
## Test input validation
%!error textscan ()
%!error <file id must be> textscan (single (4))
%!error <file id must be> textscan ({4})
%!error <must be a string> textscan ("Hello World", 2)
%!error <at most one character or>
%! textscan ("Hello World", "%s", "EndOfLine", 3);
%!error <'%z' is not a valid format specifier> textscan ("1.0", "%z")
%!error <no valid format conversion specifiers> textscan ("1.0", "foo")
*/
static octave_value
do_fread (octave::stream& os, const octave_value& size_arg,
const octave_value& prec_arg, const octave_value& skip_arg,
const octave_value& arch_arg, octave_idx_type& count)
{
count = -1;
Array<double> size = size_arg.xvector_value ("fread: invalid SIZE specified");
std::string prec = prec_arg.xstring_value ("fread: PRECISION must be a string");
int block_size = 1;
oct_data_conv::data_type input_type;
oct_data_conv::data_type output_type;
try
{
oct_data_conv::string_to_data_type (prec, block_size,
input_type, output_type);
}
catch (octave::execution_exception& e)
{
error (e, "fread: invalid PRECISION specified");
}
int skip = 0;
try
{
skip = skip_arg.int_value (true);
}
catch (octave::execution_exception& e)
{
error (e, "fread: SKIP must be an integer");
}
std::string arch = arch_arg.xstring_value ("fread: ARCH architecture type must be a string");
octave::mach_info::float_format flt_fmt
= octave::mach_info::string_to_float_format (arch);
return os.read (size, block_size, input_type, output_type, skip,
flt_fmt, count);
}
DEFMETHOD (fread, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{val} =} fread (@var{fid})
@deftypefnx {} {@var{val} =} fread (@var{fid}, @var{size})
@deftypefnx {} {@var{val} =} fread (@var{fid}, @var{size}, @var{precision})
@deftypefnx {} {@var{val} =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip})
@deftypefnx {} {@var{val} =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})
@deftypefnx {} {[@var{val}, @var{count}] =} fread (@dots{})
Read binary data from the file specified by the file descriptor @var{fid}.
The optional argument @var{size} specifies the amount of data to read
and may be one of
@table @code
@item Inf
Read as much as possible, returning a column vector.
@item @var{nr}
Read up to @var{nr} elements, returning a column vector.
@item [@var{nr}, Inf]
Read as much as possible, returning a matrix with @var{nr} rows. If the
number of elements read is not an exact multiple of @var{nr}, the last
column is padded with zeros.
@item [@var{nr}, @var{nc}]
Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with
@var{nr} rows. If the number of elements read is not an exact multiple
of @var{nr}, the last column is padded with zeros.
@end table
@noindent
If @var{size} is omitted, a value of @code{Inf} is assumed.
The optional argument @var{precision} is a string specifying the type of
data to read and may be one of
@table @asis
@item @qcode{"uint8"} (default)
8-bit unsigned integer.
@item @qcode{"int8"}
@itemx @qcode{"integer*1"}
8-bit signed integer.
@item @qcode{"uint16"}
@itemx @qcode{"ushort"}
@itemx @qcode{"unsigned short"}
16-bit unsigned integer.
@item @qcode{"int16"}
@itemx @qcode{"integer*2"}
@itemx @qcode{"short"}
16-bit signed integer.
@item @qcode{"uint"}
@itemx @qcode{"uint32"}
@itemx @qcode{"unsigned int"}
@itemx @qcode{"ulong"}
@itemx @qcode{"unsigned long"}
32-bit unsigned integer.
@item @qcode{"int"}
@itemx @qcode{"int32"}
@itemx @qcode{"integer*4"}
@itemx @qcode{"long"}
32-bit signed integer.
@item @qcode{"uint64"}
64-bit unsigned integer.
@item @qcode{"int64"}
@itemx @qcode{"integer*8"}
64-bit signed integer.
@item @qcode{"single"}
@itemx @qcode{"float"}
@itemx @qcode{"float32"}
@itemx @qcode{"real*4"}
32-bit floating point number.
@item @qcode{"double"}
@itemx @qcode{"float64"}
@itemx @qcode{"real*8"}
64-bit floating point number.
@item @qcode{"char"}
@itemx @qcode{"char*1"}
8-bit single character.
@item @qcode{"uchar"}
@itemx @qcode{"unsigned char"}
8-bit unsigned character.
@item @qcode{"schar"}
@itemx @qcode{"signed char"}
8-bit signed character.
@end table
@noindent
The default precision is @qcode{"uint8"}.
The @var{precision} argument may also specify an optional repeat
count. For example, @samp{32*single} causes @code{fread} to read
a block of 32 single precision floating point numbers. Reading in
blocks is useful in combination with the @var{skip} argument.
The @var{precision} argument may also specify a type conversion.
For example, @samp{int16=>int32} causes @code{fread} to read 16-bit
integer values and return an array of 32-bit integer values. By
default, @code{fread} returns a double precision array. The special
form @samp{*TYPE} is shorthand for @samp{TYPE=>TYPE}.
The conversion and repeat counts may be combined. For example, the
specification @samp{32*single=>single} causes @code{fread} to read
blocks of single precision floating point values and return an array
of single precision values instead of the default array of double
precision values.
The optional argument @var{skip} specifies the number of bytes to skip
after each element (or block of elements) is read. If it is not
specified, a value of 0 is assumed. If the final block read is not
complete, the final skip is omitted. For example,
@example
fread (f, 10, "3*single=>single", 8)
@end example
@noindent
will omit the final 8-byte skip because the last read will not be
a complete block of 3 values.
The optional argument @var{arch} is a string specifying the data format
for the file. Valid values are
@table @asis
@item @qcode{"native"} or @qcode{"n"}
The format of the current machine.
@item @qcode{"ieee-be"} or @qcode{"b"}
IEEE big endian.
@item @qcode{"ieee-le"} or @qcode{"l"}
IEEE little endian.
@end table
If no @var{arch} is given the value used in the call to @code{fopen} which
created the file descriptor is used. Otherwise, the value specified with
@code{fread} overrides that of @code{fopen} and determines the data format.
The output argument @var{val} contains the data read from the file.
The optional return value @var{count} contains the number of elements read.
@seealso{fwrite, fgets, fgetl, fscanf, fopen}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 1 || nargin > 5)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), "fread");
octave_value size = lo_ieee_inf_value ();
octave_value prec = "uint8";
octave_value skip = 0;
octave_value arch = "unknown";
int idx = 1;
if (nargin > idx && ! args(idx).is_string ())
size = args(idx++);
if (nargin > idx)
prec = args(idx++);
if (nargin > idx)
skip = args(idx++);
if (nargin > idx)
arch = args(idx++);
else if (skip.is_string ())
{
arch = skip;
skip = 0;
}
octave_idx_type count = -1;
octave_value tmp = do_fread (os, size, prec, skip, arch, count);
return ovl (tmp, count);
}
static int
do_fwrite (octave::stream& os, const octave_value& data,
const octave_value& prec_arg, const octave_value& skip_arg,
const octave_value& arch_arg)
{
std::string prec = prec_arg.xstring_value ("fwrite: PRECISION must be a string");
int block_size = 1;
oct_data_conv::data_type output_type;
try
{
oct_data_conv::string_to_data_type (prec, block_size, output_type);
}
catch (octave::execution_exception& e)
{
error (e, "fwrite: invalid PRECISION specified");
}
int skip = 0;
try
{
skip = skip_arg.int_value (true);
}
catch (octave::execution_exception& e)
{
error (e, "fwrite: SKIP must be an integer");
}
std::string arch = arch_arg.xstring_value ("fwrite: ARCH architecture type must be a string");
octave::mach_info::float_format flt_fmt
= octave::mach_info::string_to_float_format (arch);
return os.write (data, block_size, output_type, skip, flt_fmt);
}
DEFMETHOD (fwrite, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} fwrite (@var{fid}, @var{data})
@deftypefnx {} {} fwrite (@var{fid}, @var{data}, @var{precision})
@deftypefnx {} {} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip})
@deftypefnx {} {} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})
@deftypefnx {} {@var{count} =} fwrite (@dots{})
Write data in binary form to the file specified by the file descriptor
@var{fid}, returning the number of values @var{count} successfully written
to the file.
The argument @var{data} is a matrix of values that are to be written to
the file. The values are extracted in column-major order.
The remaining arguments @var{precision}, @var{skip}, and @var{arch} are
optional, and are interpreted as described for @code{fread}.
The behavior of @code{fwrite} is undefined if the values in @var{data}
are too large to fit in the specified precision.
@seealso{fread, fputs, fprintf, fopen}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 2 || nargin > 5)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), "fwrite");
octave_value prec = "uchar";
octave_value skip = 0;
octave_value arch = "unknown";
int idx = 1;
octave_value data = args(idx++);
if (nargin > idx)
prec = args(idx++);
if (nargin > idx)
skip = args(idx++);
if (nargin > idx)
arch = args(idx++);
else if (skip.is_string ())
{
arch = skip;
skip = 0;
}
return ovl (do_fwrite (os, data, prec, skip, arch));
}
DEFMETHODX ("feof", Ffeof, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{status} =} feof (@var{fid})
Return 1 if an end-of-file condition has been encountered for the file
specified by file descriptor @var{fid} and 0 otherwise.
Note that @code{feof} will only return 1 if the end of the file has already
been encountered, not if the next read operation will result in an
end-of-file condition.
@seealso{fread, frewind, fseek, fclear, fopen}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), "feof");
return ovl (os.eof () ? 1.0 : 0.0);
}
DEFMETHODX ("ferror", Fferror, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{msg} =} ferror (@var{fid})
@deftypefnx {} {[@var{msg}, @var{err}] =} ferror (@var{fid})
@deftypefnx {} {[@dots{}] =} ferror (@var{fid}, "clear")
Query the error status of the stream specified by file descriptor @var{fid}.
If an error condition exists then return a string @var{msg} describing the
error. Otherwise, return an empty string @qcode{""}.
The second input @qcode{"clear"} is optional. If supplied, the error
state on the stream will be cleared.
The optional second output is a numeric indication of the error status.
@var{err} is 1 if an error condition has been encountered and 0 otherwise.
Note that @code{ferror} indicates if an error has already occurred, not
whether the next operation will result in an error condition.
@seealso{fclear, fopen}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
octave::stream os = streams.lookup (args(0), "ferror");
bool clear = false;
if (nargin == 2)
{
std::string opt = args(1).string_value ();
clear = (opt == "clear");
}
int error_number = 0;
std::string error_message = os.error (clear, error_number);
return ovl (error_message, error_number);
}
DEFMETHODX ("popen", Fpopen, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{fid} =} popen (@var{command}, @var{mode})
Start a process and create a pipe.
The name of the command to run is given by @var{command}. The argument
@var{mode} may be
@table @asis
@item @qcode{"r"}
The pipe will be connected to the standard output of the process, and
open for reading.
@item @qcode{"w"}
The pipe will be connected to the standard input of the process, and
open for writing.
@end table
The file identifier corresponding to the input or output stream of the
process is returned in @var{fid}.
For example:
@example
@group
fid = popen ("ls -ltr / | tail -3", "r");
while (ischar (s = fgets (fid)))
fputs (stdout, s);
endwhile
@print{} drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc
@print{} drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib
@print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp
@end group
@end example
@seealso{popen2}
@end deftypefn */)
{
if (args.length () != 2)
print_usage ();
std::string name = args(0).xstring_value ("popen: COMMAND must be a string");
std::string mode = args(1).xstring_value ("popen: MODE must be a string");
octave_value retval;
octave::stream_list& streams = interp.get_stream_list ();
if (mode == "r")
{
octave::stream ips = octave_iprocstream::create (name);
retval = streams.insert (ips);
}
else if (mode == "w")
{
octave::stream ops = octave_oprocstream::create (name);
retval = streams.insert (ops);
}
else
error ("popen: invalid MODE specified");
return retval;
}
DEFMETHODX ("pclose", Fpclose, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} pclose (@var{fid})
Close a file identifier that was opened by @code{popen}.
The function @code{fclose} may also be used for the same purpose.
@seealso{fclose, popen}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
octave::stream_list& streams = interp.get_stream_list ();
return ovl (streams.remove (args(0), "pclose"));
}
DEFUN (tempname, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{fname} =} tempname ()
@deftypefnx {} {@var{fname} =} tempname (@var{dir})
@deftypefnx {} {@var{fname} =} tempname (@var{dir}, @var{prefix})
Return a unique temporary filename as a string.
If @var{prefix} is omitted, a value of @qcode{"oct-"} is used.
If @var{dir} is also omitted, the default directory for temporary files
(@code{P_tmpdir}) is used. If @var{dir} is provided, it must exist,
otherwise the default directory for temporary files is used.
Programming Note: Because the named file is not opened by @code{tempname},
it is possible, though relatively unlikely, that it will not be available
by the time your program attempts to open it. If this is a concern,
see @code{tmpfile}.
@seealso{mkstemp, tempdir, P_tmpdir, tmpfile}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin > 2)
print_usage ();
std::string dir;
if (nargin > 0)
dir = args(0).xstring_value ("tempname: DIR must be a string");
else
dir = octave::sys::env::getenv ("TMPDIR");
std::string pfx ("oct-");
if (nargin > 1)
pfx = args(1).xstring_value ("tempname: PREFIX must be a string");
return ovl (octave::sys::tempnam (dir, pfx));
}
/*
%!test
%! envvar = {"TMPDIR", "TMP"};
%! envdir = cellfun (@(x) getenv (x), envvar, "uniformoutput", false);
%! unwind_protect
%! cellfun (@(x) unsetenv (x), envvar);
%! envname = "TMPDIR";
%! def_tmpdir = P_tmpdir;
%! ## Strip trailing file separators from P_tmpdir
%! while (length (def_tmpdir) > 2 && any (def_tmpdir(end) == filesep ("all")))
%! def_tmpdir(end) = [];
%! endwhile
%!
%! ## Test 0-argument form
%! fname = tempname ();
%! [tmpdir, tmpfname] = fileparts (fname);
%! assert (tmpdir, def_tmpdir);
%! assert (tmpfname (1:4), "oct-");
%! ## Test 1-argument form
%! tmp_tmpdir = [def_tmpdir filesep() substr(tmpfname, -5)];
%! mkdir (tmp_tmpdir) || error ("Unable to create tmp dir");
%! setenv (envname, def_tmpdir);
%! fname = tempname (tmp_tmpdir);
%! [tmpdir, tmpfname] = fileparts (fname);
%! assert (tmpdir, tmp_tmpdir);
%! assert (tmpfname (1:4), "oct-");
%! ## Test 1-argument form w/null tmpdir
%! fname = tempname ("");
%! [tmpdir, tmpfname] = fileparts (fname);
%! assert (tmpdir, def_tmpdir);
%! assert (tmpfname (1:4), "oct-");
%! ## Test 2-argument form
%! fname = tempname (tmp_tmpdir, "pfx-");
%! [tmpdir, tmpfname] = fileparts (fname);
%! assert (tmpdir, tmp_tmpdir);
%! assert (tmpfname (1:4), "pfx-");
%! ## Test 2-argument form w/null prefix
%! fname = tempname (tmp_tmpdir, "");
%! [tmpdir, tmpfname] = fileparts (fname);
%! assert (tmpdir, tmp_tmpdir);
%! assert (tmpfname (1:4), "file");
%! unwind_protect_cleanup
%! rmdir (tmp_tmpdir);
%! for i = 1:numel (envvar)
%! if (isempty (envdir{i}))
%! unsetenv (envvar{i});
%! else
%! setenv (envvar{i}, envdir{i});
%! endif
%! endfor
%! end_unwind_protect
*/
DEFMETHOD (tmpfile, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {[@var{fid}, @var{msg}] =} tmpfile ()
Return the file ID corresponding to a new temporary file with a unique
name.
The file is opened in binary read/write (@qcode{"w+b"}) mode and will be
deleted automatically when it is closed or when Octave exits.
If successful, @var{fid} is a valid file ID and @var{msg} is an empty
string. Otherwise, @var{fid} is -1 and @var{msg} contains a
system-dependent error message.
@seealso{tempname, mkstemp, tempdir, P_tmpdir}
@end deftypefn */)
{
if (args.length () != 0)
print_usage ();
octave_value_list retval;
FILE *fid = octave_tmpfile_wrapper ();
if (fid)
{
std::string nm;
std::ios::openmode md = fopen_mode_to_ios_mode ("w+b");
octave::stream s = octave_stdiostream::create (nm, fid, md);
if (! s)
{
fclose (fid);
error ("tmpfile: failed to create octave_stdiostream object");
}
octave::stream_list& streams = interp.get_stream_list ();
retval = ovl (streams.insert (s), "");
}
else
retval = ovl (-1, std::strerror (errno));
return retval;
}
DEFMETHOD (mkstemp, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {[@var{fid}, @var{name}, @var{msg}] =} mkstemp ("@var{template}")
@deftypefnx {} {[@var{fid}, @var{name}, @var{msg}] =} mkstemp ("@var{template}", @var{delete})
Return the file descriptor @var{fid} corresponding to a new temporary file
with a unique name created from @var{template}.
The last six characters of @var{template} must be @qcode{"XXXXXX"} and
these are replaced with a string that makes the filename unique. The file
is then created with mode read/write and permissions that are system
dependent (on GNU/Linux systems, the permissions will be 0600 for versions
of glibc 2.0.7 and later). The file is opened in binary mode and with the
@w{@code{O_EXCL}} flag.
If the optional argument @var{delete} is supplied and is true, the file will
be deleted automatically when Octave exits.
If successful, @var{fid} is a valid file ID, @var{name} is the name of the
file, and @var{msg} is an empty string. Otherwise, @var{fid} is -1,
@var{name} is empty, and @var{msg} contains a system-dependent error
message.
@seealso{tempname, tempdir, P_tmpdir, tmpfile, fopen}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ();
std::string tmpl8 = args(0).xstring_value ("mkstemp: TEMPLATE argument must be a string");
octave_value_list retval = ovl (-1, "", "");
OCTAVE_LOCAL_BUFFER (char, tmp, tmpl8.size () + 1);
strcpy (tmp, tmpl8.c_str ());
int fd = octave_mkostemp_wrapper (tmp);
if (fd < 0)
{
retval(0) = fd;
retval(2) = std::strerror (errno);
}
else
{
const char *fopen_mode = "w+b";
FILE *fid = fdopen (fd, fopen_mode);
if (! fid)
{
retval(0) = -1;
retval(2) = std::strerror (errno);
}
else
{
std::string nm = tmp;
std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode);
octave::stream s = octave_stdiostream::create (nm, fid, md);
if (! s)
error ("mkstemp: failed to create octave_stdiostream object");
octave::stream_list& streams = interp.get_stream_list ();
retval(0) = streams.insert (s);
retval(1) = nm;
if (nargin == 2 && args(1).is_true ())
interp.mark_for_deletion (nm);
}
}
return retval;
}
// FIXME: This routine also exists verbatim in syscalls.cc.
// Maybe change to be a general utility routine.
static int
convert (int x, int ibase, int obase)
{
int retval = 0;
int tmp = x % obase;
if (tmp > ibase - 1)
error ("umask: invalid digit");
retval = tmp;
int mult = ibase;
while ((x = (x - tmp) / obase))
{
tmp = x % obase;
if (tmp > ibase - 1)
error ("umask: invalid digit");
retval += mult * tmp;
mult *= ibase;
}
return retval;
}
DEFUNX ("umask", Fumask, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} umask (@var{mask})
Set the permission mask for file creation.
The parameter @var{mask} is an integer, interpreted as an octal number.
If successful, returns the previous value of the mask (as an integer to be
interpreted as an octal number); otherwise an error message is printed.
The permission mask is a UNIX concept used when creating new objects on a
file system such as files, directories, or named FIFOs. The object to be
created has base permissions in an octal number @var{mode} which are
modified according to the octal value of @var{mask}. The final permissions
for the new object are @code{@var{mode} - @var{mask}}.
@seealso{fopen, mkdir, mkfifo}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
int mask = args(0).xint_value ("umask: MASK must be an integer");
if (mask < 0)
error ("umask: MASK must be a positive integer value");
int oct_mask = convert (mask, 8, 10);
int status = convert (octave::sys::umask (oct_mask), 10, 8);
if (status >= 0)
return ovl (status);
else
return ovl ();
}
static octave_value
const_value (const char *, const octave_value_list& args, int val)
{
if (args.length () != 0)
print_usage ();
return octave_value (val);
}
DEFUNX ("P_tmpdir", FP_tmpdir, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} P_tmpdir ()
Return the name of the host system's @strong{default} directory for
temporary files.
Programming Note: The value returned by @code{P_tmpdir} is always the
default location. This value may not agree with that returned from
@code{tempdir} if the user has overridden the default with the @env{TMPDIR}
environment variable.
@seealso{tempdir, tempname, mkstemp, tmpfile}
@end deftypefn */)
{
if (args.length () != 0)
print_usage ();
return ovl (octave::get_P_tmpdir ());
}
// NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be
// this way for Matlab compatibility.
DEFUNX ("SEEK_SET", FSEEK_SET, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} SEEK_SET ()
Return the numerical value to pass to @code{fseek} to position the file pointer
relative to the beginning of the file.
@seealso{SEEK_CUR, SEEK_END, fseek}
@end deftypefn */)
{
return const_value ("SEEK_SET", args, -1);
}
DEFUNX ("SEEK_CUR", FSEEK_CUR, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} SEEK_CUR ()
Return the numerical value to pass to @code{fseek} to position the file pointer
relative to the current position.
@seealso{SEEK_SET, SEEK_END, fseek}
@end deftypefn */)
{
return const_value ("SEEK_CUR", args, 0);
}
DEFUNX ("SEEK_END", FSEEK_END, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} SEEK_END ()
Return the numerical value to pass to @code{fseek} to position the file pointer
relative to the end of the file.
@seealso{SEEK_SET, SEEK_CUR, fseek}
@end deftypefn */)
{
return const_value ("SEEK_END", args, 1);
}
static octave_value
const_value (const char *, const octave_value_list& args,
const octave_value& val)
{
if (args.length () != 0)
print_usage ();
return octave_value (val);
}
DEFMETHODX ("stdin", Fstdin, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} stdin ()
Return the numeric value corresponding to the standard input stream.
When Octave is used interactively, stdin is filtered through the command
line editing functions.
@seealso{stdout, stderr}
@end deftypefn */)
{
octave::stream_list& streams = interp.get_stream_list ();
return const_value ("stdin", args, streams.stdin_file ());
}
DEFMETHODX ("stdout", Fstdout, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} stdout ()
Return the numeric value corresponding to the standard output stream.
Data written to the standard output may be filtered through the pager.
@seealso{stdin, stderr, page_screen_output}
@end deftypefn */)
{
octave::stream_list& streams = interp.get_stream_list ();
return const_value ("stdout", args, streams.stdout_file ());
}
DEFMETHODX ("stderr", Fstderr, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} stderr ()
Return the numeric value corresponding to the standard error stream.
Even if paging is turned on, the standard error is not sent to the pager.
It is useful for error messages and prompts.
@seealso{stdin, stdout}
@end deftypefn */)
{
octave::stream_list& streams = interp.get_stream_list ();
return const_value ("stderr", args, streams.stderr_file ());
}
// Deprecated variables and functions.
// Deprecated in Octave 6.
void
mark_for_deletion (const std::string& file)
{
octave::interpreter& interp
= octave::__get_interpreter__ ("mark_for_deletion");
interp.mark_for_deletion (file);
}
// Deprecated in Octave 6.
void
cleanup_tmp_files (void)
{
octave::interpreter& interp
= octave::__get_interpreter__ ("cleanup_tmp_files");
interp.cleanup_tmp_files ();
}
|