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
|
------------------------------------------------------------------------------
-- --
-- GNAT RUN-TIME COMPONENTS --
-- --
-- A D A . T E X T _ I O --
-- --
-- B o d y --
-- --
-- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
with Ada.Streams; use Ada.Streams;
with Interfaces.C_Streams; use Interfaces.C_Streams;
with System.File_IO;
with System.CRTL;
with System.WCh_Cnv; use System.WCh_Cnv;
with System.WCh_Con; use System.WCh_Con;
with Ada.Unchecked_Conversion;
with Ada.Unchecked_Deallocation;
pragma Elaborate_All (System.File_IO);
-- Needed because of calls to Chain_File in package body elaboration
package body Ada.Text_IO is
package FIO renames System.File_IO;
subtype AP is FCB.AFCB_Ptr;
function To_FCB is new Ada.Unchecked_Conversion (File_Mode, FCB.File_Mode);
function To_TIO is new Ada.Unchecked_Conversion (FCB.File_Mode, File_Mode);
use type FCB.File_Mode;
use type System.CRTL.size_t;
WC_Encoding : Character;
pragma Import (C, WC_Encoding, "__gl_wc_encoding");
-- Default wide character encoding
Err_Name : aliased String := "*stderr" & ASCII.NUL;
In_Name : aliased String := "*stdin" & ASCII.NUL;
Out_Name : aliased String := "*stdout" & ASCII.NUL;
-- Names of standard files
--
-- Use "preallocated" strings to avoid calling "new" during the elaboration
-- of the run time. This is needed in the tasking case to avoid calling
-- Task_Lock too early. A filename is expected to end with a null character
-- in the runtime, here the null characters are added just to have a
-- correct filename length.
--
-- Note: the names for these files are bogus, and probably it would be
-- better for these files to have no names, but the ACVC tests insist.
-- We use names that are bound to fail in open etc.
Null_Str : aliased constant String := "";
-- Used as form string for standard files
-----------------------
-- Local Subprograms --
-----------------------
function Get_Upper_Half_Char
(C : Character;
File : File_Type) return Character;
-- This function is shared by Get and Get_Immediate to extract an encoded
-- upper half character value from the given File. The first byte has
-- already been read and is passed in C. The character value is returned as
-- the result, and the file pointer is bumped past the character.
-- Constraint_Error is raised if the encoded value is outside the bounds of
-- type Character.
function Get_Upper_Half_Char_Immed
(C : Character;
File : File_Type) return Character;
-- This routine is identical to Get_Upper_Half_Char, except that the reads
-- are done in Get_Immediate mode (i.e. without waiting for a line return).
function Getc (File : File_Type) return int;
-- Gets next character from file, which has already been checked for being
-- in read status, and returns the character read if no error occurs. The
-- result is EOF if the end of file was read.
function Getc_Immed (File : File_Type) return int;
-- This routine is identical to Getc, except that the read is done in
-- Get_Immediate mode (i.e. without waiting for a line return).
function Has_Upper_Half_Character (Item : String) return Boolean;
-- Returns True if any of the characters is in the range 16#80#-16#FF#
function Nextc (File : File_Type) return int;
-- Returns next character from file without skipping past it (i.e. it is a
-- combination of Getc followed by an Ungetc).
procedure Put_Encoded (File : File_Type; Char : Character);
-- Called to output a character Char to the given File, when the encoding
-- method for the file is other than brackets, and Char is upper half.
procedure Putc (ch : int; File : File_Type);
-- Outputs the given character to the file, which has already been checked
-- for being in output status. Device_Error is raised if the character
-- cannot be written.
procedure Set_WCEM (File : in out File_Type);
-- Called by Open and Create to set the wide character encoding method for
-- the file, processing a WCEM form parameter if one is present. File is
-- IN OUT because it may be closed in case of an error.
procedure Terminate_Line (File : File_Type);
-- If the file is in Write_File or Append_File mode, and the current line
-- is not terminated, then a line terminator is written using New_Line.
-- Note that there is no Terminate_Page routine, because the page mark at
-- the end of the file is implied if necessary.
procedure Ungetc (ch : int; File : File_Type);
-- Pushes back character into stream, using ungetc. The caller has checked
-- that the file is in read status. Device_Error is raised if the character
-- cannot be pushed back. An attempt to push back and end of file character
-- (EOF) is ignored.
-------------------
-- AFCB_Allocate --
-------------------
function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr is
pragma Unreferenced (Control_Block);
begin
return new Text_AFCB;
end AFCB_Allocate;
----------------
-- AFCB_Close --
----------------
procedure AFCB_Close (File : not null access Text_AFCB) is
begin
-- If the file being closed is one of the current files, then close
-- the corresponding current file. It is not clear that this action
-- is required (RM A.10.3(23)) but it seems reasonable, and besides
-- ACVC test CE3208A expects this behavior.
if File_Type (File) = Current_In then
Current_In := null;
elsif File_Type (File) = Current_Out then
Current_Out := null;
elsif File_Type (File) = Current_Err then
Current_Err := null;
end if;
Terminate_Line (File_Type (File));
end AFCB_Close;
---------------
-- AFCB_Free --
---------------
procedure AFCB_Free (File : not null access Text_AFCB) is
type FCB_Ptr is access all Text_AFCB;
FT : FCB_Ptr := FCB_Ptr (File);
procedure Free is new Ada.Unchecked_Deallocation (Text_AFCB, FCB_Ptr);
begin
Free (FT);
end AFCB_Free;
-----------
-- Close --
-----------
procedure Close (File : in out File_Type) is
begin
FIO.Close (AP (File)'Unrestricted_Access);
end Close;
---------
-- Col --
---------
-- Note: we assume that it is impossible in practice for the column
-- to exceed the value of Count'Last, i.e. no check is required for
-- overflow raising layout error.
function Col (File : File_Type) return Positive_Count is
begin
FIO.Check_File_Open (AP (File));
return File.Col;
end Col;
function Col return Positive_Count is
begin
return Col (Current_Out);
end Col;
------------
-- Create --
------------
procedure Create
(File : in out File_Type;
Mode : File_Mode := Out_File;
Name : String := "";
Form : String := "")
is
Dummy_File_Control_Block : Text_AFCB;
pragma Warnings (Off, Dummy_File_Control_Block);
-- Yes, we know this is never assigned a value, only the tag
-- is used for dispatching purposes, so that's expected.
begin
FIO.Open (File_Ptr => AP (File),
Dummy_FCB => Dummy_File_Control_Block,
Mode => To_FCB (Mode),
Name => Name,
Form => Form,
Amethod => 'T',
Creat => True,
Text => True);
File.Self := File;
Set_WCEM (File);
end Create;
-------------------
-- Current_Error --
-------------------
function Current_Error return File_Type is
begin
return Current_Err;
end Current_Error;
function Current_Error return File_Access is
begin
return Current_Err.Self'Access;
end Current_Error;
-------------------
-- Current_Input --
-------------------
function Current_Input return File_Type is
begin
return Current_In;
end Current_Input;
function Current_Input return File_Access is
begin
return Current_In.Self'Access;
end Current_Input;
--------------------
-- Current_Output --
--------------------
function Current_Output return File_Type is
begin
return Current_Out;
end Current_Output;
function Current_Output return File_Access is
begin
return Current_Out.Self'Access;
end Current_Output;
------------
-- Delete --
------------
procedure Delete (File : in out File_Type) is
begin
FIO.Delete (AP (File)'Unrestricted_Access);
end Delete;
-----------------
-- End_Of_File --
-----------------
function End_Of_File (File : File_Type) return Boolean is
ch : int;
begin
FIO.Check_Read_Status (AP (File));
if File.Before_Upper_Half_Character then
return False;
elsif File.Before_LM then
if File.Before_LM_PM then
return Nextc (File) = EOF;
end if;
else
ch := Getc (File);
if ch = EOF then
return True;
elsif ch /= LM then
Ungetc (ch, File);
return False;
else -- ch = LM
File.Before_LM := True;
end if;
end if;
-- Here we are just past the line mark with Before_LM set so that we
-- do not have to try to back up past the LM, thus avoiding the need
-- to back up more than one character.
ch := Getc (File);
if ch = EOF then
return True;
elsif ch = PM and then File.Is_Regular_File then
File.Before_LM_PM := True;
return Nextc (File) = EOF;
-- Here if neither EOF nor PM followed end of line
else
Ungetc (ch, File);
return False;
end if;
end End_Of_File;
function End_Of_File return Boolean is
begin
return End_Of_File (Current_In);
end End_Of_File;
-----------------
-- End_Of_Line --
-----------------
function End_Of_Line (File : File_Type) return Boolean is
ch : int;
begin
FIO.Check_Read_Status (AP (File));
if File.Before_Upper_Half_Character then
return False;
elsif File.Before_LM then
return True;
else
ch := Getc (File);
if ch = EOF then
return True;
else
Ungetc (ch, File);
return (ch = LM);
end if;
end if;
end End_Of_Line;
function End_Of_Line return Boolean is
begin
return End_Of_Line (Current_In);
end End_Of_Line;
-----------------
-- End_Of_Page --
-----------------
function End_Of_Page (File : File_Type) return Boolean is
ch : int;
begin
FIO.Check_Read_Status (AP (File));
if not File.Is_Regular_File then
return False;
elsif File.Before_Upper_Half_Character then
return False;
elsif File.Before_LM then
if File.Before_LM_PM then
return True;
end if;
else
ch := Getc (File);
if ch = EOF then
return True;
elsif ch /= LM then
Ungetc (ch, File);
return False;
else -- ch = LM
File.Before_LM := True;
end if;
end if;
-- Here we are just past the line mark with Before_LM set so that we
-- do not have to try to back up past the LM, thus avoiding the need
-- to back up more than one character.
ch := Nextc (File);
return ch = PM or else ch = EOF;
end End_Of_Page;
function End_Of_Page return Boolean is
begin
return End_Of_Page (Current_In);
end End_Of_Page;
--------------
-- EOF_Char --
--------------
function EOF_Char return Integer is
begin
return EOF;
end EOF_Char;
-----------
-- Flush --
-----------
procedure Flush (File : File_Type) is
begin
FIO.Flush (AP (File));
end Flush;
procedure Flush is
begin
Flush (Current_Out);
end Flush;
----------
-- Form --
----------
function Form (File : File_Type) return String is
begin
return FIO.Form (AP (File));
end Form;
---------
-- Get --
---------
procedure Get
(File : File_Type;
Item : out Character)
is
ch : int;
begin
FIO.Check_Read_Status (AP (File));
if File.Before_Upper_Half_Character then
File.Before_Upper_Half_Character := False;
Item := File.Saved_Upper_Half_Character;
elsif File.Before_LM then
File.Before_LM := False;
File.Col := 1;
if File.Before_LM_PM then
File.Line := 1;
File.Page := File.Page + 1;
File.Before_LM_PM := False;
else
File.Line := File.Line + 1;
end if;
end if;
loop
ch := Getc (File);
if ch = EOF then
raise End_Error;
elsif ch = LM then
File.Line := File.Line + 1;
File.Col := 1;
elsif ch = PM and then File.Is_Regular_File then
File.Page := File.Page + 1;
File.Line := 1;
else
Item := Character'Val (ch);
File.Col := File.Col + 1;
return;
end if;
end loop;
end Get;
procedure Get (Item : out Character) is
begin
Get (Current_In, Item);
end Get;
procedure Get
(File : File_Type;
Item : out String)
is
ch : int;
J : Natural;
begin
FIO.Check_Read_Status (AP (File));
if File.Before_LM then
File.Before_LM := False;
File.Before_LM_PM := False;
File.Col := 1;
if File.Before_LM_PM then
File.Line := 1;
File.Page := File.Page + 1;
File.Before_LM_PM := False;
else
File.Line := File.Line + 1;
end if;
end if;
J := Item'First;
while J <= Item'Last loop
ch := Getc (File);
if ch = EOF then
raise End_Error;
elsif ch = LM then
File.Line := File.Line + 1;
File.Col := 1;
elsif ch = PM and then File.Is_Regular_File then
File.Page := File.Page + 1;
File.Line := 1;
else
Item (J) := Character'Val (ch);
J := J + 1;
File.Col := File.Col + 1;
end if;
end loop;
end Get;
procedure Get (Item : out String) is
begin
Get (Current_In, Item);
end Get;
-------------------
-- Get_Immediate --
-------------------
procedure Get_Immediate
(File : File_Type;
Item : out Character)
is
ch : int;
begin
FIO.Check_Read_Status (AP (File));
if File.Before_Upper_Half_Character then
File.Before_Upper_Half_Character := False;
Item := File.Saved_Upper_Half_Character;
elsif File.Before_LM then
File.Before_LM := False;
File.Before_LM_PM := False;
Item := Character'Val (LM);
else
ch := Getc_Immed (File);
if ch = EOF then
raise End_Error;
else
Item :=
(if not Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
then Character'Val (ch)
else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
end if;
end if;
end Get_Immediate;
procedure Get_Immediate
(Item : out Character)
is
begin
Get_Immediate (Current_In, Item);
end Get_Immediate;
procedure Get_Immediate
(File : File_Type;
Item : out Character;
Available : out Boolean)
is
ch : int;
end_of_file : int;
avail : int;
procedure getc_immediate_nowait
(stream : FILEs;
ch : out int;
end_of_file : out int;
avail : out int);
pragma Import (C, getc_immediate_nowait, "getc_immediate_nowait");
begin
FIO.Check_Read_Status (AP (File));
Available := True;
if File.Before_Upper_Half_Character then
File.Before_Upper_Half_Character := False;
Item := File.Saved_Upper_Half_Character;
elsif File.Before_LM then
File.Before_LM := False;
File.Before_LM_PM := False;
Item := Character'Val (LM);
else
getc_immediate_nowait (File.Stream, ch, end_of_file, avail);
if ferror (File.Stream) /= 0 then
raise Device_Error;
elsif end_of_file /= 0 then
raise End_Error;
elsif avail = 0 then
Available := False;
Item := ASCII.NUL;
else
Available := True;
Item :=
(if not Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
then Character'Val (ch)
else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
end if;
end if;
end Get_Immediate;
procedure Get_Immediate
(Item : out Character;
Available : out Boolean)
is
begin
Get_Immediate (Current_In, Item, Available);
end Get_Immediate;
--------------
-- Get_Line --
--------------
procedure Get_Line
(File : File_Type;
Item : out String;
Last : out Natural) is separate;
-- The implementation of Ada.Text_IO.Get_Line is split into a subunit so
-- that different implementations can be used on different systems.
procedure Get_Line
(Item : out String;
Last : out Natural)
is
begin
Get_Line (Current_In, Item, Last);
end Get_Line;
function Get_Line (File : File_Type) return String is
function Get_Rest (S : String) return String;
-- This is a recursive function that reads the rest of the line and
-- returns it. S is the part read so far.
--------------
-- Get_Rest --
--------------
function Get_Rest (S : String) return String is
-- The first time we allocate a buffer of size 500. Each following
-- time we allocate a buffer the same size as what we have read so
-- far. This limits us to a logarithmic number of calls to Get_Rest
-- and also ensures only a linear use of stack space.
Buffer : String (1 .. Integer'Max (500, S'Length));
Last : Natural;
begin
Get_Line (File, Buffer, Last);
declare
R : constant String := S & Buffer (1 .. Last);
begin
if Last < Buffer'Last then
return R;
else
pragma Assert (Last = Buffer'Last);
-- If the String has the same length as the buffer, and there
-- is no end of line, check whether we are at the end of file,
-- in which case we have the full String in the buffer.
if End_Of_File (File) then
return R;
else
return Get_Rest (R);
end if;
end if;
end;
end Get_Rest;
-- Start of processing for Get_Line
begin
return Get_Rest ("");
end Get_Line;
function Get_Line return String is
begin
return Get_Line (Current_In);
end Get_Line;
-------------------------
-- Get_Upper_Half_Char --
-------------------------
function Get_Upper_Half_Char
(C : Character;
File : File_Type) return Character
is
Result : Wide_Character;
function In_Char return Character;
-- Function used to obtain additional characters it the wide character
-- sequence is more than one character long.
function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
-------------
-- In_Char --
-------------
function In_Char return Character is
ch : constant Integer := Getc (File);
begin
if ch = EOF then
raise End_Error;
else
return Character'Val (ch);
end if;
end In_Char;
-- Start of processing for Get_Upper_Half_Char
begin
Result := WC_In (C, File.WC_Method);
if Wide_Character'Pos (Result) > 16#FF# then
raise Constraint_Error with
"invalid wide character in Text_'I'O input";
else
return Character'Val (Wide_Character'Pos (Result));
end if;
end Get_Upper_Half_Char;
-------------------------------
-- Get_Upper_Half_Char_Immed --
-------------------------------
function Get_Upper_Half_Char_Immed
(C : Character;
File : File_Type) return Character
is
Result : Wide_Character;
function In_Char return Character;
-- Function used to obtain additional characters it the wide character
-- sequence is more than one character long.
function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
-------------
-- In_Char --
-------------
function In_Char return Character is
ch : constant Integer := Getc_Immed (File);
begin
if ch = EOF then
raise End_Error;
else
return Character'Val (ch);
end if;
end In_Char;
-- Start of processing for Get_Upper_Half_Char_Immed
begin
Result := WC_In (C, File.WC_Method);
if Wide_Character'Pos (Result) > 16#FF# then
raise Constraint_Error with
"invalid wide character in Text_'I'O input";
else
return Character'Val (Wide_Character'Pos (Result));
end if;
end Get_Upper_Half_Char_Immed;
----------
-- Getc --
----------
function Getc (File : File_Type) return int is
ch : int;
begin
ch := fgetc (File.Stream);
if ch = EOF and then ferror (File.Stream) /= 0 then
raise Device_Error;
else
return ch;
end if;
end Getc;
----------------
-- Getc_Immed --
----------------
function Getc_Immed (File : File_Type) return int is
ch : int;
end_of_file : int;
procedure getc_immediate
(stream : FILEs; ch : out int; end_of_file : out int);
pragma Import (C, getc_immediate, "getc_immediate");
begin
FIO.Check_Read_Status (AP (File));
if File.Before_LM then
File.Before_LM := False;
File.Before_LM_PM := False;
ch := LM;
else
getc_immediate (File.Stream, ch, end_of_file);
if ferror (File.Stream) /= 0 then
raise Device_Error;
elsif end_of_file /= 0 then
return EOF;
end if;
end if;
return ch;
end Getc_Immed;
------------------------------
-- Has_Upper_Half_Character --
------------------------------
function Has_Upper_Half_Character (Item : String) return Boolean is
begin
for J in Item'Range loop
if Character'Pos (Item (J)) >= 16#80# then
return True;
end if;
end loop;
return False;
end Has_Upper_Half_Character;
-------------------------------
-- Initialize_Standard_Files --
-------------------------------
procedure Initialize_Standard_Files is
begin
Standard_Err.Stream := stderr;
Standard_Err.Name := Err_Name'Access;
Standard_Err.Form := Null_Str'Unrestricted_Access;
Standard_Err.Mode := FCB.Out_File;
Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
Standard_Err.Is_Temporary_File := False;
Standard_Err.Is_System_File := True;
Standard_Err.Text_Encoding := Default_Text;
Standard_Err.Access_Method := 'T';
Standard_Err.Self := Standard_Err;
Standard_Err.WC_Method := Default_WCEM;
Standard_In.Stream := stdin;
Standard_In.Name := In_Name'Access;
Standard_In.Form := Null_Str'Unrestricted_Access;
Standard_In.Mode := FCB.In_File;
Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
Standard_In.Is_Temporary_File := False;
Standard_In.Is_System_File := True;
Standard_In.Text_Encoding := Default_Text;
Standard_In.Access_Method := 'T';
Standard_In.Self := Standard_In;
Standard_In.WC_Method := Default_WCEM;
Standard_Out.Stream := stdout;
Standard_Out.Name := Out_Name'Access;
Standard_Out.Form := Null_Str'Unrestricted_Access;
Standard_Out.Mode := FCB.Out_File;
Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
Standard_Out.Is_Temporary_File := False;
Standard_Out.Is_System_File := True;
Standard_Out.Text_Encoding := Default_Text;
Standard_Out.Access_Method := 'T';
Standard_Out.Self := Standard_Out;
Standard_Out.WC_Method := Default_WCEM;
FIO.Make_Unbuffered (AP (Standard_Out));
FIO.Make_Unbuffered (AP (Standard_Err));
end Initialize_Standard_Files;
-------------
-- Is_Open --
-------------
function Is_Open (File : File_Type) return Boolean is
begin
return FIO.Is_Open (AP (File));
end Is_Open;
----------
-- Line --
----------
-- Note: we assume that it is impossible in practice for the line
-- to exceed the value of Count'Last, i.e. no check is required for
-- overflow raising layout error.
function Line (File : File_Type) return Positive_Count is
begin
FIO.Check_File_Open (AP (File));
return File.Line;
end Line;
function Line return Positive_Count is
begin
return Line (Current_Out);
end Line;
-----------------
-- Line_Length --
-----------------
function Line_Length (File : File_Type) return Count is
begin
FIO.Check_Write_Status (AP (File));
return File.Line_Length;
end Line_Length;
function Line_Length return Count is
begin
return Line_Length (Current_Out);
end Line_Length;
----------------
-- Look_Ahead --
----------------
procedure Look_Ahead
(File : File_Type;
Item : out Character;
End_Of_Line : out Boolean)
is
ch : int;
begin
FIO.Check_Read_Status (AP (File));
-- If we are logically before a line mark, we can return immediately
if File.Before_LM then
End_Of_Line := True;
Item := ASCII.NUL;
-- If we are before an upper half character just return it (this can
-- happen if there are two calls to Look_Ahead in a row).
elsif File.Before_Upper_Half_Character then
End_Of_Line := False;
Item := File.Saved_Upper_Half_Character;
-- Otherwise we must read a character from the input stream
else
ch := Getc (File);
if ch = LM
or else ch = EOF
or else (ch = PM and then File.Is_Regular_File)
then
End_Of_Line := True;
Ungetc (ch, File);
Item := ASCII.NUL;
-- Case where character obtained does not represent the start of an
-- encoded sequence so it stands for itself and we can unget it with
-- no difficulty.
elsif not Is_Start_Of_Encoding
(Character'Val (ch), File.WC_Method)
then
End_Of_Line := False;
Ungetc (ch, File);
Item := Character'Val (ch);
-- For the start of an encoding, we read the character using the
-- Get_Upper_Half_Char routine. It will occupy more than one byte
-- so we can't put it back with ungetc. Instead we save it in the
-- control block, setting a flag that everyone interested in reading
-- characters must test before reading the stream.
else
Item := Get_Upper_Half_Char (Character'Val (ch), File);
End_Of_Line := False;
File.Saved_Upper_Half_Character := Item;
File.Before_Upper_Half_Character := True;
end if;
end if;
end Look_Ahead;
procedure Look_Ahead
(Item : out Character;
End_Of_Line : out Boolean)
is
begin
Look_Ahead (Current_In, Item, End_Of_Line);
end Look_Ahead;
----------
-- Mode --
----------
function Mode (File : File_Type) return File_Mode is
begin
return To_TIO (FIO.Mode (AP (File)));
end Mode;
----------
-- Name --
----------
function Name (File : File_Type) return String is
begin
return FIO.Name (AP (File));
end Name;
--------------
-- New_Line --
--------------
procedure New_Line
(File : File_Type;
Spacing : Positive_Count := 1)
is
begin
-- Raise Constraint_Error if out of range value. The reason for this
-- explicit test is that we don't want junk values around, even if
-- checks are off in the caller.
if not Spacing'Valid then
raise Constraint_Error;
end if;
FIO.Check_Write_Status (AP (File));
for K in 1 .. Spacing loop
Putc (LM, File);
File.Line := File.Line + 1;
if File.Page_Length /= 0
and then File.Line > File.Page_Length
then
Putc (PM, File);
File.Line := 1;
File.Page := File.Page + 1;
end if;
end loop;
File.Col := 1;
end New_Line;
procedure New_Line (Spacing : Positive_Count := 1) is
begin
New_Line (Current_Out, Spacing);
end New_Line;
--------------
-- New_Page --
--------------
procedure New_Page (File : File_Type) is
begin
FIO.Check_Write_Status (AP (File));
if File.Col /= 1 or else File.Line = 1 then
Putc (LM, File);
end if;
Putc (PM, File);
File.Page := File.Page + 1;
File.Line := 1;
File.Col := 1;
end New_Page;
procedure New_Page is
begin
New_Page (Current_Out);
end New_Page;
-----------
-- Nextc --
-----------
function Nextc (File : File_Type) return int is
ch : int;
begin
ch := fgetc (File.Stream);
if ch = EOF then
if ferror (File.Stream) /= 0 then
raise Device_Error;
end if;
else
if ungetc (ch, File.Stream) = EOF then
raise Device_Error;
end if;
end if;
return ch;
end Nextc;
----------
-- Open --
----------
procedure Open
(File : in out File_Type;
Mode : File_Mode;
Name : String;
Form : String := "")
is
Dummy_File_Control_Block : Text_AFCB;
pragma Warnings (Off, Dummy_File_Control_Block);
-- Yes, we know this is never assigned a value, only the tag
-- is used for dispatching purposes, so that's expected.
begin
FIO.Open (File_Ptr => AP (File),
Dummy_FCB => Dummy_File_Control_Block,
Mode => To_FCB (Mode),
Name => Name,
Form => Form,
Amethod => 'T',
Creat => False,
Text => True);
File.Self := File;
Set_WCEM (File);
end Open;
----------
-- Page --
----------
-- Note: we assume that it is impossible in practice for the page
-- to exceed the value of Count'Last, i.e. no check is required for
-- overflow raising layout error.
function Page (File : File_Type) return Positive_Count is
begin
FIO.Check_File_Open (AP (File));
return File.Page;
end Page;
function Page return Positive_Count is
begin
return Page (Current_Out);
end Page;
-----------------
-- Page_Length --
-----------------
function Page_Length (File : File_Type) return Count is
begin
FIO.Check_Write_Status (AP (File));
return File.Page_Length;
end Page_Length;
function Page_Length return Count is
begin
return Page_Length (Current_Out);
end Page_Length;
---------
-- Put --
---------
procedure Put
(File : File_Type;
Item : Character)
is
begin
FIO.Check_Write_Status (AP (File));
if File.Line_Length /= 0 and then File.Col > File.Line_Length then
New_Line (File);
end if;
-- If lower half character, or brackets encoding, output directly
if Character'Pos (Item) < 16#80#
or else File.WC_Method = WCEM_Brackets
then
if fputc (Character'Pos (Item), File.Stream) = EOF then
raise Device_Error;
end if;
-- Case of upper half character with non-brackets encoding
else
Put_Encoded (File, Item);
end if;
File.Col := File.Col + 1;
end Put;
procedure Put (Item : Character) is
begin
Put (Current_Out, Item);
end Put;
---------
-- Put --
---------
procedure Put
(File : File_Type;
Item : String)
is
begin
FIO.Check_Write_Status (AP (File));
-- Only have something to do if string is non-null
if Item'Length > 0 then
-- If we have bounded lines, or if the file encoding is other than
-- Brackets and the string has at least one upper half character,
-- then output the string character by character.
if File.Line_Length /= 0
or else (File.WC_Method /= WCEM_Brackets
and then Has_Upper_Half_Character (Item))
then
for J in Item'Range loop
Put (File, Item (J));
end loop;
-- Otherwise we can output the entire string at once. Note that if
-- there are LF or FF characters in the string, we do not bother to
-- count them as line or page terminators.
else
FIO.Write_Buf (AP (File), Item'Address, Item'Length);
File.Col := File.Col + Item'Length;
end if;
end if;
end Put;
procedure Put (Item : String) is
begin
Put (Current_Out, Item);
end Put;
-----------------
-- Put_Encoded --
-----------------
procedure Put_Encoded (File : File_Type; Char : Character) is
procedure Out_Char (C : Character);
-- Procedure to output one character of an upper half encoded sequence
procedure WC_Out is new Wide_Char_To_Char_Sequence (Out_Char);
--------------
-- Out_Char --
--------------
procedure Out_Char (C : Character) is
begin
Putc (Character'Pos (C), File);
end Out_Char;
-- Start of processing for Put_Encoded
begin
WC_Out (Wide_Character'Val (Character'Pos (Char)), File.WC_Method);
end Put_Encoded;
--------------
-- Put_Line --
--------------
procedure Put_Line
(File : File_Type;
Item : String)
is
Ilen : Natural := Item'Length;
Istart : Natural := Item'First;
begin
FIO.Check_Write_Status (AP (File));
-- If we have bounded lines, or if the file encoding is other than
-- Brackets and the string has at least one upper half character, then
-- output the string character by character.
if File.Line_Length /= 0
or else (File.WC_Method /= WCEM_Brackets
and then Has_Upper_Half_Character (Item))
then
for J in Item'Range loop
Put (File, Item (J));
end loop;
New_Line (File);
return;
end if;
-- Normal case where we do not need to output character by character
-- We setup a single string that has the necessary terminators and
-- then write it with a single call. The reason for doing this is
-- that it gives better behavior for the use of Put_Line in multi-
-- tasking programs, since often the OS will treat the entire put
-- operation as an atomic operation.
-- We only do this if the message is 512 characters or less in length,
-- since otherwise Put_Line would use an unbounded amount of stack
-- space and could cause undetected stack overflow. If we have a
-- longer string, then output the first part separately to avoid this.
if Ilen > 512 then
FIO.Write_Buf (AP (File), Item'Address, size_t (Ilen - 512));
Istart := Istart + Ilen - 512;
Ilen := 512;
end if;
-- Now prepare the string with its terminator
declare
Buffer : String (1 .. Ilen + 2);
Plen : size_t;
begin
Buffer (1 .. Ilen) := Item (Istart .. Item'Last);
Buffer (Ilen + 1) := Character'Val (LM);
if File.Page_Length /= 0
and then File.Line > File.Page_Length
then
Buffer (Ilen + 2) := Character'Val (PM);
Plen := size_t (Ilen) + 2;
File.Line := 1;
File.Page := File.Page + 1;
else
Plen := size_t (Ilen) + 1;
File.Line := File.Line + 1;
end if;
FIO.Write_Buf (AP (File), Buffer'Address, Plen);
File.Col := 1;
end;
end Put_Line;
procedure Put_Line (Item : String) is
begin
Put_Line (Current_Out, Item);
end Put_Line;
----------
-- Putc --
----------
procedure Putc (ch : int; File : File_Type) is
begin
if fputc (ch, File.Stream) = EOF then
raise Device_Error;
end if;
end Putc;
----------
-- Read --
----------
-- This is the primitive Stream Read routine, used when a Text_IO file
-- is treated directly as a stream using Text_IO.Streams.Stream.
procedure Read
(File : in out Text_AFCB;
Item : out Stream_Element_Array;
Last : out Stream_Element_Offset)
is
Discard_ch : int;
pragma Warnings (Off, Discard_ch);
begin
-- Need to deal with Before_Upper_Half_Character ???
if File.Mode /= FCB.In_File then
raise Mode_Error;
end if;
-- Deal with case where our logical and physical position do not match
-- because of being after an LM or LM-PM sequence when in fact we are
-- logically positioned before it.
if File.Before_LM then
-- If we are before a PM, then it is possible for a stream read
-- to leave us after the LM and before the PM, which is a bit
-- odd. The easiest way to deal with this is to unget the PM,
-- so we are indeed positioned between the characters. This way
-- further stream read operations will work correctly, and the
-- effect on text processing is a little weird, but what can
-- be expected if stream and text input are mixed this way?
if File.Before_LM_PM then
Discard_ch := ungetc (PM, File.Stream);
File.Before_LM_PM := False;
end if;
File.Before_LM := False;
Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
if Item'Length = 1 then
Last := Item'Last;
else
Last :=
Item'First +
Stream_Element_Offset
(fread (buffer => Item'Address,
index => size_t (Item'First + 1),
size => 1,
count => Item'Length - 1,
stream => File.Stream));
end if;
return;
end if;
-- Now we do the read. Since this is a text file, it is normally in
-- text mode, but stream data must be read in binary mode, so we
-- temporarily set binary mode for the read, resetting it after.
-- These calls have no effect in a system (like Unix) where there is
-- no distinction between text and binary files.
set_binary_mode (fileno (File.Stream));
Last :=
Item'First +
Stream_Element_Offset
(fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
if Last < Item'Last then
if ferror (File.Stream) /= 0 then
raise Device_Error;
end if;
end if;
set_text_mode (fileno (File.Stream));
end Read;
-----------
-- Reset --
-----------
procedure Reset
(File : in out File_Type;
Mode : File_Mode)
is
begin
-- Don't allow change of mode for current file (RM A.10.2(5))
if (File = Current_In or else
File = Current_Out or else
File = Current_Error)
and then To_FCB (Mode) /= File.Mode
then
raise Mode_Error;
end if;
Terminate_Line (File);
FIO.Reset (AP (File)'Unrestricted_Access, To_FCB (Mode));
File.Page := 1;
File.Line := 1;
File.Col := 1;
File.Line_Length := 0;
File.Page_Length := 0;
File.Before_LM := False;
File.Before_LM_PM := False;
end Reset;
procedure Reset (File : in out File_Type) is
begin
Terminate_Line (File);
FIO.Reset (AP (File)'Unrestricted_Access);
File.Page := 1;
File.Line := 1;
File.Col := 1;
File.Line_Length := 0;
File.Page_Length := 0;
File.Before_LM := False;
File.Before_LM_PM := False;
end Reset;
-------------
-- Set_Col --
-------------
procedure Set_Col
(File : File_Type;
To : Positive_Count)
is
ch : int;
begin
-- Raise Constraint_Error if out of range value. The reason for this
-- explicit test is that we don't want junk values around, even if
-- checks are off in the caller.
if not To'Valid then
raise Constraint_Error;
end if;
FIO.Check_File_Open (AP (File));
-- Output case
if Mode (File) >= Out_File then
-- Error if we attempt to set Col to a value greater than the
-- maximum permissible line length.
if File.Line_Length /= 0 and then To > File.Line_Length then
raise Layout_Error;
end if;
-- If we are behind current position, then go to start of new line
if To < File.Col then
New_Line (File);
end if;
-- Loop to output blanks till we are at the required column
while File.Col < To loop
Put (File, ' ');
end loop;
-- Input case
else
-- If we are logically before a LM, but physically after it, the
-- file position still reflects the position before the LM, so eat
-- it now and adjust the file position appropriately.
if File.Before_LM then
File.Before_LM := False;
File.Before_LM_PM := False;
File.Line := File.Line + 1;
File.Col := 1;
end if;
-- Loop reading characters till we get one at the required Col value
loop
-- Read next character. The reason we have to read ahead is to
-- skip formatting characters, the effect of Set_Col is to set
-- us to a real character with the right Col value, and format
-- characters don't count.
ch := Getc (File);
-- Error if we hit an end of file
if ch = EOF then
raise End_Error;
-- If line mark, eat it and adjust file position
elsif ch = LM then
File.Line := File.Line + 1;
File.Col := 1;
-- If recognized page mark, eat it, and adjust file position
elsif ch = PM and then File.Is_Regular_File then
File.Page := File.Page + 1;
File.Line := 1;
File.Col := 1;
-- Otherwise this is the character we are looking for, so put it
-- back in the input stream (we have not adjusted the file
-- position yet, so everything is set right after this ungetc).
elsif To = File.Col then
Ungetc (ch, File);
return;
-- Keep skipping characters if we are not there yet, updating the
-- file position past the skipped character.
else
File.Col := File.Col + 1;
end if;
end loop;
end if;
end Set_Col;
procedure Set_Col (To : Positive_Count) is
begin
Set_Col (Current_Out, To);
end Set_Col;
---------------
-- Set_Error --
---------------
procedure Set_Error (File : File_Type) is
begin
FIO.Check_Write_Status (AP (File));
Current_Err := File;
end Set_Error;
---------------
-- Set_Input --
---------------
procedure Set_Input (File : File_Type) is
begin
FIO.Check_Read_Status (AP (File));
Current_In := File;
end Set_Input;
--------------
-- Set_Line --
--------------
procedure Set_Line
(File : File_Type;
To : Positive_Count)
is
begin
-- Raise Constraint_Error if out of range value. The reason for this
-- explicit test is that we don't want junk values around, even if
-- checks are off in the caller.
if not To'Valid then
raise Constraint_Error;
end if;
FIO.Check_File_Open (AP (File));
if To = File.Line then
return;
end if;
if Mode (File) >= Out_File then
if File.Page_Length /= 0 and then To > File.Page_Length then
raise Layout_Error;
end if;
if To < File.Line then
New_Page (File);
end if;
while File.Line < To loop
New_Line (File);
end loop;
else
while To /= File.Line loop
Skip_Line (File);
end loop;
end if;
end Set_Line;
procedure Set_Line (To : Positive_Count) is
begin
Set_Line (Current_Out, To);
end Set_Line;
---------------------
-- Set_Line_Length --
---------------------
procedure Set_Line_Length (File : File_Type; To : Count) is
begin
-- Raise Constraint_Error if out of range value. The reason for this
-- explicit test is that we don't want junk values around, even if
-- checks are off in the caller.
if not To'Valid then
raise Constraint_Error;
end if;
FIO.Check_Write_Status (AP (File));
File.Line_Length := To;
end Set_Line_Length;
procedure Set_Line_Length (To : Count) is
begin
Set_Line_Length (Current_Out, To);
end Set_Line_Length;
----------------
-- Set_Output --
----------------
procedure Set_Output (File : File_Type) is
begin
FIO.Check_Write_Status (AP (File));
Current_Out := File;
end Set_Output;
---------------------
-- Set_Page_Length --
---------------------
procedure Set_Page_Length (File : File_Type; To : Count) is
begin
-- Raise Constraint_Error if out of range value. The reason for this
-- explicit test is that we don't want junk values around, even if
-- checks are off in the caller.
if not To'Valid then
raise Constraint_Error;
end if;
FIO.Check_Write_Status (AP (File));
File.Page_Length := To;
end Set_Page_Length;
procedure Set_Page_Length (To : Count) is
begin
Set_Page_Length (Current_Out, To);
end Set_Page_Length;
--------------
-- Set_WCEM --
--------------
procedure Set_WCEM (File : in out File_Type) is
Start : Natural;
Stop : Natural;
begin
FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
if Start = 0 then
File.WC_Method := Default_WCEM;
else
if Stop = Start then
for J in WC_Encoding_Letters'Range loop
if File.Form (Start) = WC_Encoding_Letters (J) then
File.WC_Method := J;
return;
end if;
end loop;
end if;
Close (File);
raise Use_Error with "invalid WCEM form parameter";
end if;
end Set_WCEM;
---------------
-- Skip_Line --
---------------
procedure Skip_Line
(File : File_Type;
Spacing : Positive_Count := 1)
is
ch : int;
begin
-- Raise Constraint_Error if out of range value. The reason for this
-- explicit test is that we don't want junk values around, even if
-- checks are off in the caller.
if not Spacing'Valid then
raise Constraint_Error;
end if;
FIO.Check_Read_Status (AP (File));
for L in 1 .. Spacing loop
if File.Before_LM then
File.Before_LM := False;
-- Note that if File.Before_LM_PM is currently set, we also have
-- to reset it (because it makes sense for Before_LM_PM to be set
-- only when Before_LM is also set). This is done later on in this
-- subprogram, as soon as Before_LM_PM has been taken into account
-- for the purpose of page and line counts.
else
ch := Getc (File);
-- If at end of file now, then immediately raise End_Error. Note
-- that we can never be positioned between a line mark and a page
-- mark, so if we are at the end of file, we cannot logically be
-- before the implicit page mark that is at the end of the file.
-- For the same reason, we do not need an explicit check for a
-- page mark. If there is a FF in the middle of a line, the file
-- is not in canonical format and we do not care about the page
-- numbers for files other than ones in canonical format.
if ch = EOF then
raise End_Error;
end if;
-- If not at end of file, then loop till we get to an LM or EOF.
-- The latter case happens only in non-canonical files where the
-- last line is not terminated by LM, but we don't want to blow
-- up for such files, so we assume an implicit LM in this case.
loop
exit when ch = LM or else ch = EOF;
ch := Getc (File);
end loop;
end if;
-- We have got past a line mark, now, for a regular file only,
-- see if a page mark immediately follows this line mark and
-- if so, skip past the page mark as well. We do not do this
-- for non-regular files, since it would cause an undesirable
-- wait for an additional character.
File.Col := 1;
File.Line := File.Line + 1;
if File.Before_LM_PM then
File.Page := File.Page + 1;
File.Line := 1;
File.Before_LM_PM := False;
elsif File.Is_Regular_File then
ch := Getc (File);
-- Page mark can be explicit, or implied at the end of the file
if (ch = PM or else ch = EOF)
and then File.Is_Regular_File
then
File.Page := File.Page + 1;
File.Line := 1;
else
Ungetc (ch, File);
end if;
end if;
end loop;
File.Before_Upper_Half_Character := False;
end Skip_Line;
procedure Skip_Line (Spacing : Positive_Count := 1) is
begin
Skip_Line (Current_In, Spacing);
end Skip_Line;
---------------
-- Skip_Page --
---------------
procedure Skip_Page (File : File_Type) is
ch : int;
begin
FIO.Check_Read_Status (AP (File));
-- If at page mark already, just skip it
if File.Before_LM_PM then
File.Before_LM := False;
File.Before_LM_PM := False;
File.Page := File.Page + 1;
File.Line := 1;
File.Col := 1;
return;
end if;
-- This is a bit tricky, if we are logically before an LM then
-- it is not an error if we are at an end of file now, since we
-- are not really at it.
if File.Before_LM then
File.Before_LM := False;
File.Before_LM_PM := False;
ch := Getc (File);
-- Otherwise we do raise End_Error if we are at the end of file now
else
ch := Getc (File);
if ch = EOF then
raise End_Error;
end if;
end if;
-- Now we can just rumble along to the next page mark, or to the
-- end of file, if that comes first. The latter case happens when
-- the page mark is implied at the end of file.
loop
exit when ch = EOF
or else (ch = PM and then File.Is_Regular_File);
ch := Getc (File);
end loop;
File.Page := File.Page + 1;
File.Line := 1;
File.Col := 1;
File.Before_Upper_Half_Character := False;
end Skip_Page;
procedure Skip_Page is
begin
Skip_Page (Current_In);
end Skip_Page;
--------------------
-- Standard_Error --
--------------------
function Standard_Error return File_Type is
begin
return Standard_Err;
end Standard_Error;
function Standard_Error return File_Access is
begin
return Standard_Err'Access;
end Standard_Error;
--------------------
-- Standard_Input --
--------------------
function Standard_Input return File_Type is
begin
return Standard_In;
end Standard_Input;
function Standard_Input return File_Access is
begin
return Standard_In'Access;
end Standard_Input;
---------------------
-- Standard_Output --
---------------------
function Standard_Output return File_Type is
begin
return Standard_Out;
end Standard_Output;
function Standard_Output return File_Access is
begin
return Standard_Out'Access;
end Standard_Output;
--------------------
-- Terminate_Line --
--------------------
procedure Terminate_Line (File : File_Type) is
begin
FIO.Check_File_Open (AP (File));
-- For file other than In_File, test for needing to terminate last line
if Mode (File) /= In_File then
-- If not at start of line definition need new line
if File.Col /= 1 then
New_Line (File);
-- For files other than standard error and standard output, we
-- make sure that an empty file has a single line feed, so that
-- it is properly formatted. We avoid this for the standard files
-- because it is too much of a nuisance to have these odd line
-- feeds when nothing has been written to the file.
-- We also avoid this for files opened in append mode, in
-- accordance with (RM A.8.2(10))
elsif (File /= Standard_Err and then File /= Standard_Out)
and then (File.Line = 1 and then File.Page = 1)
and then Mode (File) = Out_File
then
New_Line (File);
end if;
end if;
end Terminate_Line;
------------
-- Ungetc --
------------
procedure Ungetc (ch : int; File : File_Type) is
begin
if ch /= EOF then
if ungetc (ch, File.Stream) = EOF then
raise Device_Error;
end if;
end if;
end Ungetc;
-----------
-- Write --
-----------
-- This is the primitive Stream Write routine, used when a Text_IO file
-- is treated directly as a stream using Text_IO.Streams.Stream.
procedure Write
(File : in out Text_AFCB;
Item : Stream_Element_Array)
is
pragma Warnings (Off, File);
-- Because in this implementation we don't need IN OUT, we only read
function Has_Translated_Characters return Boolean;
-- return True if Item array contains a character which will be
-- translated under the text file mode. There is only one such
-- character under DOS based systems which is character 10.
text_translation_required : Boolean;
for text_translation_required'Size use Character'Size;
pragma Import (C, text_translation_required,
"__gnat_text_translation_required");
Siz : constant size_t := Item'Length;
-------------------------------
-- Has_Translated_Characters --
-------------------------------
function Has_Translated_Characters return Boolean is
begin
for K in Item'Range loop
if Item (K) = 10 then
return True;
end if;
end loop;
return False;
end Has_Translated_Characters;
Needs_Binary_Write : constant Boolean :=
text_translation_required and then Has_Translated_Characters;
-- Start of processing for Write
begin
if File.Mode = FCB.In_File then
raise Mode_Error;
end if;
-- Now we do the write. Since this is a text file, it is normally in
-- text mode, but stream data must be written in binary mode, so we
-- temporarily set binary mode for the write, resetting it after. This
-- is done only if needed (i.e. there is some characters in Item which
-- needs to be written using the binary mode).
-- These calls have no effect in a system (like Unix) where there is
-- no distinction between text and binary files.
-- Since the character translation is done at the time the buffer is
-- written (this is true under Windows) we first flush current buffer
-- with text mode if needed.
if Needs_Binary_Write then
if fflush (File.Stream) = -1 then
raise Device_Error;
end if;
set_binary_mode (fileno (File.Stream));
end if;
if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
raise Device_Error;
end if;
-- At this point we need to flush the buffer using the binary mode then
-- we reset to text mode.
if Needs_Binary_Write then
if fflush (File.Stream) = -1 then
raise Device_Error;
end if;
set_text_mode (fileno (File.Stream));
end if;
end Write;
begin
-- Initialize Standard Files
for J in WC_Encoding_Method loop
if WC_Encoding = WC_Encoding_Letters (J) then
Default_WCEM := J;
end if;
end loop;
Initialize_Standard_Files;
FIO.Chain_File (AP (Standard_In));
FIO.Chain_File (AP (Standard_Out));
FIO.Chain_File (AP (Standard_Err));
end Ada.Text_IO;
|