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
|
/* abcio.cpp
*
* Copyright (C) 1992-2011 Paul Boersma
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* pb 2002/03/07 GPL
* pb 2003/05/19 accept percent signs in getReal
* pb 2004/10/01 Melder_double instead of %.17g
* pb 2006/02/17 support for Intel-based Macs
* pb 2006/02/20 corrected bingeti3, bingeti3LE, binputi3, binputi3LE
* pb 2006/03/28 support for systems where a long is not 32 bits and a short is not 16 bits
* pb 2007/07/21 MelderReadString
* pb 2007/08/14 check for NULL pointer before Melder_isValidAscii
* pb 2009/03/18 modern enums
* fb 2010/02/26 UTF-16 via bin(get|put)utf16()
* pb 2010/03/09 more support for Unicode values above 0xFFFF
* pb 2010/12/23 corrected bingeti3 and bingeti3LE for 64-bit systems
* pb 2011/03/30 C++
*/
#include "melder.h"
#include "NUM.h"
#include <ctype.h>
#ifdef macintosh
#include <TargetConditionals.h>
#endif
#include "abcio.h"
/********** ASCII I/O **********/
#define WCHAR_MINUS_1 (sizeof (wchar_t) == 2 ? 0xFFFF : 0xFFFFFFFF)
static long getInteger (MelderReadText me) {
wchar buffer [41], c;
/*
* Look for the first numeric character.
*/
for (c = MelderReadText_getChar (me); c != '-' && ! isdigit (c) && c != '+'; c = MelderReadText_getChar (me)) {
if (c == 0)
Melder_throw ("Early end of text detected while looking for an integer (line ", MelderReadText_getLineNumber (me), ").");
if (c == '!') { // end-of-line comment?
while ((c = MelderReadText_getChar (me)) != '\n' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected in comment while looking for an integer (line ", MelderReadText_getLineNumber (me), ").");
}
}
if (c == '\"')
Melder_throw ("Found a string while looking for an integer in text (line ", MelderReadText_getLineNumber (me), ").");
if (c == '<')
Melder_throw ("Found an enumerated value while looking for an integer in text (line ", MelderReadText_getLineNumber (me), ").");
while (c != ' ' && c != '\n' && c != '\t' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected in comment (line ", MelderReadText_getLineNumber (me), ").");
c = MelderReadText_getChar (me);
}
}
int i = 0;
for (; i < 40; i ++) {
buffer [i] = c;
c = MelderReadText_getChar (me);
if (c == 0) { break; } // this may well be OK here
if (c == ' ' || c == '\n' || c == '\t' || c == '\r') break;
}
if (i >= 40)
Melder_throw ("Found strange text while looking for an integer in text (line ", MelderReadText_getLineNumber (me), ").");
buffer [i + 1] = '\0';
return wcstol (buffer, NULL, 10);
}
static unsigned long getUnsigned (MelderReadText me) {
wchar buffer [41], c;
for (c = MelderReadText_getChar (me); ! isdigit (c) && c != '+'; c = MelderReadText_getChar (me)) {
if (c == 0)
Melder_throw ("Early end of text detected while looking for an unsigned integer (line ", MelderReadText_getLineNumber (me), ").");
if (c == '!') { // end-of-line comment?
while ((c = MelderReadText_getChar (me)) != '\n' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected in comment while looking for an unsigned integer (line ", MelderReadText_getLineNumber (me), ").");
}
}
if (c == '\"')
Melder_throw ("Found a string while looking for an unsigned integer in text (line ", MelderReadText_getLineNumber (me), ").");
if (c == '<')
Melder_throw ("Found an enumerated value while looking for an unsigned integer in text (line ", MelderReadText_getLineNumber (me), ").");
if (c == '-')
Melder_throw ("Found a negative value while looking for an unsigned integer in text (line ", MelderReadText_getLineNumber (me), ").");
while (c != ' ' && c != '\n' && c != '\t' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected in comment (line ", MelderReadText_getLineNumber (me), ").");
c = MelderReadText_getChar (me);
}
}
int i = 0;
for (i = 0; i < 40; i ++) {
buffer [i] = c;
c = MelderReadText_getChar (me);
if (c == 0) { break; } // this may well be OK here
if (c == ' ' || c == '\n' || c == '\t' || c == '\r') break;
}
if (i >= 40)
Melder_throw ("Found strange text while searching for an unsigned integer in text (line ", MelderReadText_getLineNumber (me), ").");
buffer [i + 1] = '\0';
return wcstoul (buffer, NULL, 10);
}
static double getReal (MelderReadText me) {
int i;
wchar_t buffer [41], c, *slash;
do {
for (c = MelderReadText_getChar (me); c != '-' && ! isdigit (c) && c != '+'; c = MelderReadText_getChar (me)) {
if (c == 0)
Melder_throw ("Early end of text detected while looking for a real number (line ", MelderReadText_getLineNumber (me), ").");
if (c == '!') { // end-of-line comment?
while ((c = MelderReadText_getChar (me)) != '\n' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected in comment while looking for a real number (line ", MelderReadText_getLineNumber (me), ").");
}
}
if (c == '\"')
Melder_throw ("Found a string while looking for a real number in text (line ", MelderReadText_getLineNumber (me), ").");
if (c == '<')
Melder_throw ("Found an enumerated value while looking for a real number in text (line ", MelderReadText_getLineNumber (me), ").");
while (c != ' ' && c != '\n' && c != '\t' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected in comment while looking for a real number (line ", MelderReadText_getLineNumber (me), ").");
c = MelderReadText_getChar (me);
}
}
for (i = 0; i < 40; i ++) {
buffer [i] = c;
c = MelderReadText_getChar (me);
if (c == 0) { break; } // this may well be OK here
if (c == ' ' || c == '\n' || c == '\t' || c == '\r') break;
}
if (i >= 40)
Melder_throw ("Found strange text while searching for a real number in text (line ", MelderReadText_getLineNumber (me), ").");
} while (i == 0 && buffer [0] == '+'); // guard against single '+' symbols, which occur in complex numbers
buffer [i + 1] = '\0';
slash = wcschr (buffer, '/');
if (slash) {
double numerator, denominator;
*slash = '\0';
numerator = Melder_atof (buffer), denominator = Melder_atof (slash + 1);
if (numerator == HUGE_VAL || denominator == HUGE_VAL || denominator == 0.0)
return HUGE_VAL;
return numerator / denominator;
}
return Melder_atof (buffer);
}
static short getEnum (MelderReadText me, int (*getValue) (const wchar *)) {
wchar buffer [41], c;
for (c = MelderReadText_getChar (me); c != '<'; c = MelderReadText_getChar (me)) {
if (c == 0)
Melder_throw ("Early end of text detected while looking for an enumerated value (line ", MelderReadText_getLineNumber (me), ").");
if (c == '!') { /* End-of-line comment? */
while ((c = MelderReadText_getChar (me)) != '\n' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected in comment while looking for an enumerated value (line ", MelderReadText_getLineNumber (me), ").");
}
}
if (c == '-' || isdigit (c) || c == '+')
Melder_throw ("Found a number while looking for an enumerated value in text (line ", MelderReadText_getLineNumber (me), ").");
if (c == '\"')
Melder_throw ("Found a string while looking for an enumerated value in text (line ", MelderReadText_getLineNumber (me), ").");
while (c != ' ' && c != '\n' && c != '\t' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected in comment while looking for an enumerated value (line ", MelderReadText_getLineNumber (me), ").");
c = MelderReadText_getChar (me);
}
}
int i = 0;
for (; i < 40; i ++) {
c = MelderReadText_getChar (me); // read past first '<'
if (c == 0)
Melder_throw ("Early end of text detected while reading an enumerated value (line ", MelderReadText_getLineNumber (me), ").");
if (c == ' ' || c == '\n' || c == '\t' || c == '\r')
Melder_throw ("No matching '>' while reading an enumerated value (line ", MelderReadText_getLineNumber (me), ").");
if (c == '>')
break; // the expected closing bracket; not added to the buffer
buffer [i] = c;
}
if (i >= 40)
Melder_throw ("Found strange text while reading an enumerated value in text (line ", MelderReadText_getLineNumber (me), ").");
buffer [i] = '\0';
int value = getValue (buffer);
if (value < 0)
Melder_throw ("\"", buffer, "\" is not a value of the enumerated type.");
return value;
}
static wchar * getString (MelderReadText me) {
static MelderString buffer = { 0 };
MelderString_empty (& buffer);
for (wchar c = MelderReadText_getChar (me); c != '\"'; c = MelderReadText_getChar (me)) {
if (c == 0)
Melder_throw ("Early end of text detected while looking for a string (line ", MelderReadText_getLineNumber (me), ").");
if (c == '!') { // end-of-line comment?
while ((c = MelderReadText_getChar (me)) != '\n' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected in comment while looking for a string (line ", MelderReadText_getLineNumber (me), ").");
}
}
if (c == '-' || isdigit (c) || c == '+')
Melder_throw ("Found a number while looking for a string in text (line ", MelderReadText_getLineNumber (me), ").");
if (c == '<')
Melder_throw ("Found an enumerated value while looking for a string in text (line ", MelderReadText_getLineNumber (me), ").");
while (c != ' ' && c != '\n' && c != '\t' && c != '\r') {
if (c == 0)
Melder_throw ("Early end of text detected while looking for a string (line ", MelderReadText_getLineNumber (me), ").");
c = MelderReadText_getChar (me);
}
}
for (int i = 0; 1; i ++) {
wchar c = MelderReadText_getChar (me); // read past first '"'
if (c == 0)
Melder_throw ("Early end of text detected while reading a string (line ", MelderReadText_getLineNumber (me), ").");
if (c == '\"') {
wchar_t next = MelderReadText_getChar (me);
if (next == 0) { break; } // closing quote is last character in file: OK
if (next != '\"') {
if (next == ' ' || next == '\n' || next == '\t' || next == '\r') {
// closing quote is followed by whitespace: it is OK to skip this whitespace (no need to "ungetChar")
} else {
wchar_t kar2 [2] = { next, '\0' };
Melder_throw ("Character ", kar2, " following quote (line ", MelderReadText_getLineNumber (me), "). End of string or undoubled quote?");
}
break; // the expected closing double quote; not added to the buffer
} // else: add only one of the two quotes to the buffer
}
MelderString_appendCharacter (& buffer, c);
}
return buffer. string;
}
#undef false
#undef true
#include "enums_getText.h"
#include "abcio_enums.h"
#include "enums_getValue.h"
#include "abcio_enums.h"
int texgeti1 (MelderReadText text) {
try {
long externalValue = getInteger (text);
if (externalValue < -128 || externalValue > +127)
Melder_throw ("Value (", externalValue, ") out of range (-128 .. +127).");
return (int) externalValue;
} catch (MelderError) {
Melder_throw ("Signed small integer not read from text file.");
}
}
int texgeti2 (MelderReadText text) {
try {
long externalValue = getInteger (text);
if (externalValue < -32768 || externalValue > +32767)
Melder_throw ("Value (", externalValue, ") out of range (-32768 .. +32767).");
return (int) externalValue;
} catch (MelderError) {
Melder_throw ("Signed short integer not read from text file.");
}
}
long texgeti4 (MelderReadText text) {
try {
long externalValue = getInteger (text);
return externalValue;
} catch (MelderError) {
Melder_throw ("Signed integer not read from text file.");
}
}
unsigned int texgetu1 (MelderReadText text) {
try {
long externalValue = getUnsigned (text);
if (externalValue > 255)
Melder_throw ("Value (", externalValue, ") out of range (0 .. 255).");
return (unsigned int) externalValue;
} catch (MelderError) {
Melder_throw ("Unsigned small integer not read from text file.");
}
}
unsigned int texgetu2 (MelderReadText text) {
try {
long externalValue = getUnsigned (text);
if (externalValue > 65535)
Melder_throw ("Value (", externalValue, ") out of range (0 .. 65535).");
return (unsigned int) externalValue;
} catch (MelderError) {
Melder_throw ("Unsigned short integer not read from text file.");
}
}
unsigned long texgetu4 (MelderReadText text) {
try {
long externalValue = getUnsigned (text);
return externalValue;
} catch (MelderError) {
Melder_throw ("Unsigned integer not read from text file.");
}
}
double texgetr4 (MelderReadText text) { return getReal (text); }
double texgetr8 (MelderReadText text) { return getReal (text); }
double texgetr10 (MelderReadText text) { return getReal (text); }
fcomplex texgetc8 (MelderReadText text) { fcomplex z; z.re = getReal (text); z.im = getReal (text); return z; }
dcomplex texgetc16 (MelderReadText text) { dcomplex z; z.re = getReal (text); z.im = getReal (text); return z; }
short texgete1 (MelderReadText text, int (*getValue) (const wchar_t *)) { return getEnum (text, getValue); }
short texgete2 (MelderReadText text, int (*getValue) (const wchar_t *)) { return getEnum (text, getValue); }
short texgeteb (MelderReadText text) { return getEnum (text, kBoolean_getValue); }
short texgeteq (MelderReadText text) { return getEnum (text, kQuestion_getValue); }
short texgetex (MelderReadText text) { return getEnum (text, kExistence_getValue); }
char *texgets2 (MelderReadText text) { return Melder_wcsToUtf8 (getString (text)); }
char *texgets4 (MelderReadText text) { return Melder_wcsToUtf8 (getString (text)); }
wchar_t *texgetw2 (MelderReadText text) { return Melder_wcsdup (getString (text)); }
wchar_t *texgetw4 (MelderReadText text) { return Melder_wcsdup (getString (text)); }
void texindent (MelderFile file) { file -> indent += 4; }
void texexdent (MelderFile file) { file -> indent -= 4; }
void texresetindent (MelderFile file) { file -> indent = 0; }
void texputintro (MelderFile file, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
if (file -> verbose) {
MelderFile_write1 (file, L"\n");
for (int iindent = 1; iindent <= file -> indent; iindent ++) {
MelderFile_write1 (file, L" ");
}
MelderFile_write6 (file,
s1 && s1 [0] == 'd' && s1 [1] == '_' ? & s1 [2] : & s1 [0],
s2 && s2 [0] == 'd' && s2 [1] == '_' ? & s2 [2] : & s2 [0],
s3 && s3 [0] == 'd' && s3 [1] == '_' ? & s3 [2] : & s3 [0],
s4 && s4 [0] == 'd' && s4 [1] == '_' ? & s4 [2] : & s4 [0],
s5 && s5 [0] == 'd' && s5 [1] == '_' ? & s5 [2] : & s5 [0],
s6 && s6 [0] == 'd' && s6 [1] == '_' ? & s6 [2] : & s6 [0]);
}
file -> indent += 4;
}
#define PUTLEADER \
MelderFile_write1 (file, L"\n"); \
if (file -> verbose) { \
for (int iindent = 1; iindent <= file -> indent; iindent ++) { \
MelderFile_write1 (file, L" "); \
} \
MelderFile_write6 (file, \
s1 && s1 [0] == 'd' && s1 [1] == '_' ? & s1 [2] : & s1 [0], \
s2 && s2 [0] == 'd' && s2 [1] == '_' ? & s2 [2] : & s2 [0], \
s3 && s3 [0] == 'd' && s3 [1] == '_' ? & s3 [2] : & s3 [0], \
s4 && s4 [0] == 'd' && s4 [1] == '_' ? & s4 [2] : & s4 [0], \
s5 && s5 [0] == 'd' && s5 [1] == '_' ? & s5 [2] : & s5 [0], \
s6 && s6 [0] == 'd' && s6 [1] == '_' ? & s6 [2] : & s6 [0]); \
}
void texputi1 (MelderFile file, int i, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = " : NULL, Melder_integer (i), file -> verbose ? L" " : NULL);
}
void texputi2 (MelderFile file, int i, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = " : NULL, Melder_integer (i), file -> verbose ? L" " : NULL);
}
void texputi4 (MelderFile file, long i, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = " : NULL, Melder_integer (i), file -> verbose ? L" " : NULL);
}
void texputu1 (MelderFile file, unsigned int u, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = " : NULL, Melder_integer (u), file -> verbose ? L" " : NULL);
}
void texputu2 (MelderFile file, unsigned int u, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = " : NULL, Melder_integer (u), file -> verbose ? L" " : NULL);
}
void texputu4 (MelderFile file, unsigned long u, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = " : NULL, Melder_integer (u), file -> verbose ? L" " : NULL);
}
void texputr4 (MelderFile file, double x, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = " : NULL, Melder_single (x), file -> verbose ? L" " : NULL);
}
void texputr8 (MelderFile file, double x, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = " : NULL, Melder_double (x), file -> verbose ? L" " : NULL);
}
void texputc8 (MelderFile file, fcomplex z, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write5 (file, file -> verbose ? L" = " : NULL, Melder_single (z.re),
file -> verbose ? L" + " : L" ", Melder_single (z.im), file -> verbose ? L" i " : NULL);
}
void texputc16 (MelderFile file, dcomplex z, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write5 (file, file -> verbose ? L" = " : NULL, Melder_double (z.re),
file -> verbose ? L" + " : L" ", Melder_double (z.im), file -> verbose ? L" i " : NULL);
}
void texpute1 (MelderFile file, int i, const wchar_t * (*getText) (int), const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = <" : L"<", getText (i), file -> verbose ? L"> " : L">");
}
void texpute2 (MelderFile file, int i, const wchar_t * (*getText) (int), const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = <" : L"<", getText (i), file -> verbose ? L"> " : L">");
}
void texputeb (MelderFile file, bool i, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L" = " : NULL, i ? L"<true>" : L"<false>", file -> verbose ? L" " : NULL);
}
void texputeq (MelderFile file, bool i, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L"? " : NULL, i ? L"<yes>" : L"<no>", file -> verbose ? L" " : NULL);
}
void texputex (MelderFile file, bool i, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write3 (file, file -> verbose ? L"? " : NULL, i ? L"<exists>" : L"<absent>", file -> verbose ? L" " : NULL);
}
void texputs1 (MelderFile file, const char *s, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write1 (file, file -> verbose ? L" = \"" : L"\"");
if (s != NULL) {
char c;
while ((c = *s ++) != '\0') {
MelderFile_writeCharacter (file, c);
if (c == '\"') MelderFile_writeCharacter (file, c); // double any internal quotes
}
}
MelderFile_write1 (file, file -> verbose ? L"\" " : L"\"");
}
void texputs2 (MelderFile file, const char *s, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write1 (file, file -> verbose ? L" = \"" : L"\"");
if (s != NULL) {
char c;
while ((c = *s ++) != '\0') {
MelderFile_writeCharacter (file, c);
if (c == '\"') MelderFile_writeCharacter (file, c); // double any internal quotes
}
}
MelderFile_write1 (file, file -> verbose ? L"\" " : L"\"");
}
void texputs4 (MelderFile file, const char *s, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write1 (file, file -> verbose ? L" = \"" : L"\"");
if (s != NULL) {
char c;
while ((c = *s ++) != '\0') {
MelderFile_writeCharacter (file, c);
if (c == '\"') MelderFile_writeCharacter (file, c); // double any internal quotes
}
}
MelderFile_write1 (file, file -> verbose ? L"\" " : L"\"");
}
void texputw2 (MelderFile file, const wchar_t *s, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write1 (file, file -> verbose ? L" = \"" : L"\"");
if (s != NULL) {
wchar_t c;
while ((c = *s ++) != '\0') {
MelderFile_writeCharacter (file, c);
if (c == '\"') MelderFile_writeCharacter (file, c); // double any internal quotes
}
}
MelderFile_write1 (file, file -> verbose ? L"\" " : L"\"");
}
void texputw4 (MelderFile file, const wchar_t *s, const wchar_t *s1, const wchar_t *s2, const wchar_t *s3, const wchar_t *s4, const wchar_t *s5, const wchar_t *s6) {
PUTLEADER
MelderFile_write1 (file, file -> verbose ? L" = \"" : L"\"");
if (s != NULL) {
wchar_t c;
while ((c = *s ++) != '\0') {
MelderFile_writeCharacter (file, c);
if (c == '\"') MelderFile_writeCharacter (file, c); // double any internal quotes
}
}
MelderFile_write1 (file, file -> verbose ? L"\" " : L"\"");
}
/********** machine-independent binary I/O **********/
/* Optimizations for machines for which some of the formats are native. */
/* On which machines is "short" a two's complement Big-Endian (MSB-first) 2-byte word? */
#if defined (macintosh) && TARGET_RT_BIG_ENDIAN == 1
#define binario_shortBE2 (sizeof (short) == 2)
#define binario_shortLE2 0
#elif defined (_WIN32) || defined (macintosh) && TARGET_RT_LITTLE_ENDIAN == 1
#define binario_shortBE2 0
#define binario_shortLE2 (sizeof (short) == 2)
#else
#define binario_shortBE2 0
#define binario_shortLE2 0
#endif
/* On which machines is "long" a two's complement Big-Endian (MSB-first) 4-byte word? */
#if defined (macintosh) && TARGET_RT_BIG_ENDIAN == 1
#define binario_longBE4 (sizeof (long) == 4)
#define binario_longLE4 0
#elif defined (_WIN32) || defined (macintosh) && TARGET_RT_LITTLE_ENDIAN == 1
#define binario_longBE4 0
#define binario_longLE4 (sizeof (long) == 4)
#else
#define binario_longBE4 0
#define binario_longLE4 0
#endif
/* On which machines is "float" IEEE, four bytes, Most Significant Bit first? */
#if defined (macintosh) && TARGET_RT_BIG_ENDIAN == 1
#define binario_floatIEEE4msb (sizeof (float) == 4)
#define binario_floatIEEE4lsb 0
#elif defined (_WIN32) || defined (macintosh) && TARGET_RT_LITTLE_ENDIAN == 1
#define binario_floatIEEE4msb 0
#define binario_floatIEEE4lsb (sizeof (float) == 4)
#else
#define binario_floatIEEE4msb 0
#define binario_floatIEEE4lsb 0
#endif
/* On which machines is "double" IEEE, eight bytes, Most Significant Bit first? */
#if defined (macintosh) && TARGET_RT_BIG_ENDIAN == 1
#define binario_doubleIEEE8msb (sizeof (double) == 8)
#define binario_doubleIEEE8lsb 0
#elif defined (_WIN32) || defined (macintosh) && TARGET_RT_LITTLE_ENDIAN == 1
#define binario_doubleIEEE8msb 0
#define binario_doubleIEEE8lsb (sizeof (double) == 8)
#else
#define binario_doubleIEEE8msb 0
#define binario_doubleIEEE8lsb 0
#endif
/*
The routines bingetr4, bingetr8, binputr4, and binputr8,
were implemented by Paul Boersma from the descriptions of the IEEE floating-point formats,
as found in the MC68881/MC68882 User's Manual by Motorola (second edition, 1989).
The following copyright notice only refers to the code of bingetr10 and binputr10.
*/
/* Copyright (C) 1988-1991 Apple Computer, Inc.
* All rights reserved.
*
* Warranty Information
* Even though Apple has reviewed this software, Apple makes no warranty
* or representation, either express or implied, with respect to this
* software, its quality, accuracy, merchantability, or fitness for a
* particular purpose. As a result, this software is provided "as is,"
* and you, its user, are assuming the entire risk as to its quality
* and accuracy.
*
* This code may be used and freely distributed as long as it includes
* this copyright notice and the above warranty information.
*
* Machine-independent I/O routines for IEEE floating-point numbers.
*
* NaN's and infinities are converted to HUGE_VAL or HUGE, which
* happens to be infinity on IEEE machines. Unfortunately, it is
* impossible to preserve NaN's in a machine-independent way.
* Infinities are, however, preserved on IEEE machines.
*
* These routines have been tested on the following machines:
* Apple Macintosh, MPW 3.1 C compiler
* Apple Macintosh, THINK C compiler
* Silicon Graphics IRIS, MIPS compiler
* Cray X/MP and Y/MP
* Digital Equipment VAX
*
*
* Implemented by Malcolm Slaney and Ken Turkowski.
*
* Malcolm Slaney contributions during 1988-1990 include big- and little-
* endian file I/O, conversion to and from Motorola's extended 80-bit
* floating-point format, and conversions to and from IEEE single-
* precision floating-point format.
*
* In 1991, Ken Turkowski implemented the conversions to and from
* IEEE double-precision format, added more precision to the extended
* conversions, and accommodated conversions involving +/- infinity,
* NaN's, and denormalized numbers.
*/
// QUESTION: do the following work correctly if a long is 64 bits?
# define FloatToUnsigned(f) \
((unsigned long)(((long)((f) - 2147483648.0)) + 2147483647L + 1))
# define UnsignedToFloat(u) \
(((double)((long)((u) - 2147483647L - 1))) + 2147483648.0)
/****************************************************************
* Extended precision IEEE floating-point conversion routines.
****************************************************************/
/*
* C O N V E R T T O I E E E E X T E N D E D
*/
/*
* C O N V E R T F R O M I E E E E X T E N D E D
*/
/*************** End of Apple Computer intermezzo. ***************/
static void readError (FILE *f, const char *text) {
Melder_throw (feof (f) ? "Reached end of file" : "Error in file", " while trying to read ", text);
}
static void writeError (const char *text) {
Melder_throw ("Error in file while trying to write ", text);
}
unsigned int bingetu1 (FILE *f) {
try {
int externalValue = getc (f); // either -1 (EOF) or in the range 0..255
if (externalValue < 0) readError (f, "a byte.");
return (unsigned int) externalValue;
} catch (MelderError) {
Melder_throw ("Unsigned integer not read from 1 byte in binary file.");
}
}
void binputu1 (unsigned int u, FILE *f) {
try {
if (putc (u, f) < 0) writeError ("a byte.");
} catch (MelderError) {
Melder_throw ("Unsigned integer not written to 1 byte in binary file.");
}
}
int bingeti1 (FILE *f) {
try {
int externalValue = getc (f);
if (externalValue < 0) readError (f, "a byte.");
return (signed char) externalValue; // this converts e.g. 200 to -56
} catch (MelderError) {
Melder_throw ("Signed integer not read from 1 byte in binary file.");
}
}
void binputi1 (int u, FILE *f) {
try {
if (putc (u, f) < 0) writeError ("a byte.");
} catch (MelderError) {
Melder_throw ("Signed integer not written to 1 byte in binary file.");
}
}
int bingete1 (FILE *f, int min, int max, const wchar *type) {
try {
int externalValue = getc (f);
if (externalValue < 0) readError (f, "a byte.");
int result = (signed char) externalValue; // this converts e.g. 200 to -56, so the enumerated type is signed
if (result < min || result > max)
Melder_throw (result, " is not a value of enumerated type <", type, ">.");
return result;
} catch (MelderError) {
Melder_throw ("Enumerated type not read from 1 byte in binary file.");
}
}
void binpute1 (int value, FILE *f) {
try {
if (putc (value, f) < 0) writeError ("a byte.");
} catch (MelderError) {
Melder_throw ("Enumerated type not written to 1 byte in binary file.");
}
}
static int bitsInReadBuffer = 0;
static unsigned char readBuffer;
#define macro_bingetb(nbits) \
unsigned int bingetb##nbits (FILE *f) { \
unsigned char result; \
if (bitsInReadBuffer < nbits) { \
int externalValue = fgetc (f); \
if (externalValue < 0) readError (f, "a bit."); \
readBuffer = (unsigned char) externalValue; \
bitsInReadBuffer = 8; \
} \
result = readBuffer << (8 - bitsInReadBuffer); \
bitsInReadBuffer -= nbits; \
return result >> (8 - nbits); \
}
macro_bingetb (1)
macro_bingetb (2)
macro_bingetb (3)
macro_bingetb (4)
macro_bingetb (5)
macro_bingetb (6)
macro_bingetb (7)
void bingetb (FILE *f) { (void) f; bitsInReadBuffer = 0; }
int bingeti2 (FILE *f) {
try {
if (binario_shortBE2 && Melder_debug != 18) {
signed short s;
if (fread (& s, sizeof (signed short), 1, f) != 1) readError (f, "a signed short integer.");
return (int) s; // with sign extension if an int is 4 bytes
} else {
unsigned char bytes [2];
if (fread (bytes, sizeof (unsigned char), 2, f) != 2) readError (f, "two bytes.");
uint16_t externalValue = ((uint16_t) bytes [0] << 8) | (uint16_t) bytes [1];
return (int) (int16_t) externalValue; // with sign extension if an int is 4 bytes
}
} catch (MelderError) {
Melder_throw ("Signed integer not read from 2 bytes in binary file.");
}
}
unsigned int bingetu2 (FILE *f) {
try {
if (binario_shortBE2 && Melder_debug != 18) {
unsigned short s;
if (fread (& s, sizeof (unsigned short), 1, f) != 1) readError (f, "an unsigned short integer.");
return s; // without sign extension
} else {
unsigned char bytes [2];
if (fread (bytes, sizeof (unsigned char), 2, f) != 2) readError (f, "two bytes.");
uint16_t externalValue = ((uint16_t) bytes [0] << 8) | (uint16_t) bytes [1];
return (unsigned int) externalValue;
}
} catch (MelderError) {
Melder_throw ("Unsigned integer not read from 2 bytes in binary file.");
}
}
int bingete2 (FILE *f, int min, int max, const wchar_t *type) {
try {
short result;
if (binario_shortBE2 && Melder_debug != 18) {
if (fread (& result, sizeof (short), 1, f) != 1) readError (f, "a signed short integer.");
} else {
unsigned char bytes [2];
if (fread (bytes, sizeof (unsigned char), 2, f) != 2) readError (f, "two bytes.");
uint16_t externalValue = ((uint16_t) bytes [0] << 8) | (uint16_t) bytes [1];
result = (short) (int16_t) externalValue;
}
if (result < min || result > max)
Melder_throw (result, " is not a value of enumerated type \"", type, L"\".");
return (int) result;
} catch (MelderError) {
Melder_throw ("Enumerated value not read from 2 bytes in binary file.");
}
}
long bingeti3 (FILE *f) {
try {
unsigned char bytes [3];
if (fread (bytes, sizeof (unsigned char), 3, f) != 3) readError (f, "three bytes.");
uint32_t externalValue = ((uint32_t) bytes [0] << 16) | ((uint32_t) bytes [1] << 8) | (uint32_t) bytes [2];
if ((bytes [0] & 128) != 0) // is the 24-bit sign bit on?
externalValue |= 0xFF000000; // extend negative sign to 32 bits
return (long) (int32_t) externalValue; // first convert signedness, then perhaps extend sign to 64 bits!
} catch (MelderError) {
Melder_throw ("Signed long integer not read from 3 bytes in binary file.");
}
}
long bingeti4 (FILE *f) {
try {
if (binario_longBE4 && Melder_debug != 18) {
long l;
if (fread (& l, sizeof (long), 1, f) != 1) readError (f, "a signed long integer.");
return l;
} else {
unsigned char bytes [4];
if (fread (bytes, sizeof (unsigned char), 4, f) != 4) readError (f, "four bytes.");
return
((unsigned long) bytes [0] << 24) | ((unsigned long) bytes [1] << 16) |
((unsigned long) bytes [2] << 8) | (unsigned long) bytes [3]; // 32 or 64 bits
}
} catch (MelderError) {
Melder_throw ("Signed long integer not read from 4 bytes in binary file.");
}
}
unsigned long bingetu4 (FILE *f) {
try {
if (binario_longBE4 && Melder_debug != 18) {
unsigned long l;
if (fread (& l, sizeof (unsigned long), 1, f) != 1) readError (f, "an unsigned long integer.");
return l;
} else {
unsigned char bytes [4];
if (fread (bytes, sizeof (unsigned char), 4, f) != 4) readError (f, "four bytes.");
return
((unsigned long) bytes [0] << 24) | ((unsigned long) bytes [1] << 16) |
((unsigned long) bytes [2] << 8) | (unsigned long) bytes [3]; // 32 or 64 bits
}
} catch (MelderError) {
Melder_throw ("Unsigned long integer not read from 4 bytes in binary file.");
}
}
int bingeti2LE (FILE *f) {
try {
if (binario_shortLE2 && Melder_debug != 18) {
signed short s;
if (fread (& s, sizeof (signed short), 1, f) != 1) readError (f, "a signed short integer.");
return (int) s; // with sign extension if an int is 4 bytes
} else {
unsigned char bytes [2];
if (fread (bytes, sizeof (unsigned char), 2, f) != 2) readError (f, "two bytes.");
uint16_t externalValue = ((uint16_t) bytes [1] << 8) | (uint16_t) bytes [0];
return (int) (int16_t) externalValue; // with sign extension if an int is 4 bytes
}
} catch (MelderError) {
Melder_throw ("Signed integer not read from 2 bytes in binary file.");
}
}
unsigned int bingetu2LE (FILE *f) {
try {
if (binario_shortLE2 && Melder_debug != 18) {
unsigned short s;
if (fread (& s, sizeof (unsigned short), 1, f) != 1) readError (f, "an unsigned short integer.");
return s; // without sign extension
} else {
unsigned char bytes [2];
if (fread (bytes, sizeof (unsigned char), 2, f) != 2) readError (f, "two bytes.");
uint16_t externalValue = ((uint16_t) bytes [1] << 8) | (uint16_t) bytes [0];
return (unsigned int) externalValue;
}
} catch (MelderError) {
Melder_throw ("Unsigned integer not read from 2 bytes in binary file.");
}
}
long bingeti3LE (FILE *f) {
try {
unsigned char bytes [3];
if (fread (bytes, sizeof (unsigned char), 3, f) != 3) readError (f, "three bytes.");
uint32_t externalValue = ((uint32_t) bytes [2] << 16) | ((uint32_t) bytes [1] << 8) | (uint32_t) bytes [0];
if ((bytes [2] & 128) != 0) // is the 24-bit sign bit on?
externalValue |= 0xFF000000; // extend negative sign to 32 bits
return (long) (int32_t) externalValue; // first convert signedness, then perhaps extend sign to 64 bits!
} catch (MelderError) {
Melder_throw ("Signed long integer not read from 3 bytes in binary file.");
}
}
long bingeti4LE (FILE *f) {
try {
if (binario_longLE4 && Melder_debug != 18) {
long l;
if (fread (& l, sizeof (long), 1, f) != 1) readError (f, "a signed long integer.");
return l;
} else {
unsigned char bytes [4];
if (fread (bytes, sizeof (unsigned char), 4, f) != 4) readError (f, "four bytes.");
return
((unsigned long) bytes [3] << 24) | ((unsigned long) bytes [2] << 16) |
((unsigned long) bytes [1] << 8) | (unsigned long) bytes [0]; // 32 or 64 bits
}
} catch (MelderError) {
Melder_throw ("Signed long integer not read from 4 bytes in binary file.");
}
}
unsigned long bingetu4LE (FILE *f) {
try {
if (binario_longLE4 && Melder_debug != 18) {
unsigned long l;
if (fread (& l, sizeof (unsigned long), 1, f) != 1) readError (f, "an unsigned long integer.");
return l;
} else {
unsigned char bytes [4];
if (fread (bytes, sizeof (unsigned char), 4, f) != 4) readError (f, "four bytes.");
return
((unsigned long) bytes [3] << 24) | ((unsigned long) bytes [2] << 16) |
((unsigned long) bytes [1] << 8) | (unsigned long) bytes [0]; // 32 or 64 bits
}
} catch (MelderError) {
Melder_throw ("Unsigned long integer not read from 4 bytes in binary file.");
}
}
double bingetr4 (FILE *f) {
try {
if (binario_floatIEEE4msb && Melder_debug != 18) {
float x;
if (fread (& x, sizeof (float), 1, f) != 1) readError (f, "a single-precision floating-point number.");
return x;
} else {
unsigned char bytes [4];
if (fread (bytes, sizeof (unsigned char), 4, f) != 4) readError (f, "four bytes.");
long exponent = ((unsigned long) (bytes [0] & 0x7F) << 1) | ((unsigned long) (bytes [1] & 0x80) >> 7); // 32 or 64 bits
unsigned long mantissa = ((unsigned long) (bytes [1] & 0x7F) << 16) | ((unsigned long) bytes [2] << 8) | (unsigned long) bytes [3]; // 32 or 64 bits
double x;
if (exponent == 0)
if (mantissa == 0) x = 0.0;
else x = ldexp (UnsignedToFloat (mantissa), exponent - 149); // denormalized
else if (exponent == 0x00FF) // Infinity or Not-a-Number
x = HUGE_VAL;
else // finite
x = ldexp (UnsignedToFloat (mantissa | 0x00800000), exponent - 150);
return bytes [0] & 0x80 ? - x : x;
}
} catch (MelderError) {
Melder_throw ("Floating-point number not read from 4 bytes in binary file.");
}
}
double bingetr4LE (FILE *f) {
try {
if (binario_floatIEEE4lsb && Melder_debug != 18) {
float x;
if (fread (& x, sizeof (float), 1, f) != 1) readError (f, "a single-precision floating-point number.");
return x;
} else {
unsigned char bytes [4];
if (fread (bytes, sizeof (unsigned char), 4, f) != 4) readError (f, "four bytes.");
long exponent = ((unsigned long) (bytes [3] & 0x7F) << 1) | ((unsigned long) (bytes [2] & 0x80) >> 7); // 32 or 64 bits
unsigned long mantissa = ((unsigned long) (bytes [2] & 0x7F) << 16) | ((unsigned long) bytes [1] << 8) | (unsigned long) bytes [0]; // 32 or 64 bits
double x;
if (exponent == 0)
if (mantissa == 0) x = 0.0;
else x = ldexp (UnsignedToFloat (mantissa), exponent - 149); // denormalized
else if (exponent == 0x00FF) // Infinity or Not-a-Number. */
x = HUGE_VAL;
else // finite
x = ldexp (UnsignedToFloat (mantissa | 0x00800000), exponent - 150);
return bytes [3] & 0x80 ? - x : x;
}
} catch (MelderError) {
Melder_throw ("Floating-point number not read from 4 bytes in binary file.");
}
}
double bingetr8 (FILE *f) {
try {
if (binario_doubleIEEE8msb && Melder_debug != 18) {
double x;
if (fread (& x, sizeof (double), 1, f) != 1) readError (f, "a double-precision floating-point number.");
return x;
} else {
unsigned char bytes [8];
if (fread (bytes, sizeof (unsigned char), 8, f) != 8) readError (f, "eight bytes.");
Melder_assert (sizeof (long) >= 4);
long exponent = ((unsigned long) (bytes [0] & 0x7F) << 4) | ((unsigned long) (bytes [1] & 0xF0) >> 4);
unsigned long highMantissa = ((unsigned long) (bytes [1] & 0x0F) << 16) | ((unsigned long) bytes [2] << 8) | (unsigned long) bytes [3];
unsigned long lowMantissa = ((unsigned long) bytes [4] << 24) | ((unsigned long) bytes [5] << 16) | ((unsigned long) bytes [6] << 8) | (unsigned long) bytes [7];
double x;
if (exponent == 0)
if (highMantissa == 0 && lowMantissa == 0) x = 0.0;
else x = ldexp (UnsignedToFloat (highMantissa), exponent - 1042) +
ldexp (UnsignedToFloat (lowMantissa), exponent - 1074); // denormalized
else if (exponent == 0x07FF) // Infinity or Not-a-Number
x = HUGE_VAL;
else
x = ldexp (UnsignedToFloat (highMantissa | 0x00100000), exponent - 1043) +
ldexp (UnsignedToFloat (lowMantissa), exponent - 1075);
return bytes [0] & 0x80 ? - x : x;
}
} catch (MelderError) {
Melder_throw ("Floating-point number not read from 8 bytes in binary file.");
}
}
double bingetr10 (FILE *f) {
try {
unsigned char bytes [10];
if (fread (bytes, sizeof (unsigned char), 10, f) != 10) readError (f, "ten bytes.");
long exponent = ((bytes [0] & 0x7F) << 8) | bytes [1];
unsigned long highMantissa = ((unsigned long) bytes [2] << 24) | ((unsigned long) bytes [3] << 16) | ((unsigned long) bytes [4] << 8) | (unsigned long) bytes [5];
unsigned long lowMantissa = ((unsigned long) bytes [6] << 24) | ((unsigned long) bytes [7] << 16) | ((unsigned long) bytes [8] << 8) | (unsigned long) bytes [9];
double x;
if (exponent == 0 && highMantissa == 0 && lowMantissa == 0) x = 0;
else if (exponent == 0x7FFF) x = HUGE_VAL; /* Infinity or NaN */
else {
exponent -= 16383;
x = ldexp (UnsignedToFloat (highMantissa), exponent - 31);
x += ldexp (UnsignedToFloat (lowMantissa), exponent - 63);
}
return bytes [0] & 0x80 ? - x : x;
} catch (MelderError) {
Melder_throw ("Floating-point number not read from 10 bytes in binary file.");
}
}
static int bitsInWriteBuffer = 0;
static unsigned char writeBuffer = 0;
#define macro_binputb(nbits) \
void binputb##nbits (unsigned int value, FILE *f) { \
if (bitsInWriteBuffer + nbits > 8) { \
if (fputc (writeBuffer, f) < 0) writeError ("a bit."); \
bitsInWriteBuffer = 0; \
writeBuffer = 0; \
} \
writeBuffer |= (value << (8 - nbits)) >> bitsInWriteBuffer; \
bitsInWriteBuffer += nbits; \
}
macro_binputb (1)
macro_binputb (2)
macro_binputb (3)
macro_binputb (4)
macro_binputb (5)
macro_binputb (6)
macro_binputb (7)
void binputb (FILE *f) {
if (bitsInWriteBuffer == 0) return;
if (fputc (writeBuffer, f) < 0) writeError ("a bit."); // flush
bitsInWriteBuffer = 0;
writeBuffer = 0;
}
void binputi2 (int i, FILE *f) {
try {
if (binario_shortBE2 && Melder_debug != 18) {
short s = i;
if (fwrite (& s, sizeof (short), 1, f) != 1) writeError ("a signed short integer.");
} else {
char bytes [2];
bytes [0] = i >> 8;
bytes [1] = i;
if (fwrite (bytes, sizeof (char), 2, f) != 2) writeError ("two bytes.");
}
} catch (MelderError) {
Melder_throw ("Signed integer not written to 2 bytes in binary file.");
}
}
void binputu2 (unsigned int u, FILE *f) {
try {
if (binario_shortBE2 && Melder_debug != 18) {
unsigned short s = u;
if (fwrite (& s, sizeof (unsigned short), 1, f) != 1) writeError ("an unsigned short integer.");
} else {
char bytes [2];
bytes [0] = u >> 8;
bytes [1] = u;
if (fwrite (bytes, sizeof (char), 2, f) != 2) writeError ("two bytes.");
}
} catch (MelderError) {
Melder_throw ("Unsigned integer not written to 2 bytes in binary file.");
}
}
void binpute2 (int value, FILE *f) {
try {
if (binario_shortBE2 && Melder_debug != 18) {
short s = value;
if (fwrite (& s, sizeof (short), 1, f) != 1) writeError ("a signed short integer");
} else {
char bytes [2];
bytes [0] = value >> 8;
bytes [1] = value;
if (fwrite (bytes, sizeof (char), 2, f) != 2) writeError ("two bytes.");
}
} catch (MelderError) {
Melder_throw ("Enumerated value not written to 2 bytes in binary file.");
}
}
void binputi3 (long i, FILE *f) {
try {
char bytes [3];
bytes [0] = i >> 16;
bytes [1] = i >> 8;
bytes [2] = i;
if (fwrite (bytes, sizeof (char), 3, f) != 3) writeError ("three bytes");
} catch (MelderError) {
Melder_throw ("Signed integer not written to 3 bytes in binary file.");
}
}
void binputi4 (long i, FILE *f) {
try {
if (binario_longBE4 && Melder_debug != 18) {
if (fwrite (& i, sizeof (long), 1, f) != 1) writeError ("a signed long integer.");
} else {
char bytes [4];
bytes [0] = i >> 24;
bytes [1] = i >> 16;
bytes [2] = i >> 8;
bytes [3] = i;
if (fwrite (bytes, sizeof (char), 4, f) != 4) writeError ("four bytes.");
}
} catch (MelderError) {
Melder_throw ("Signed integer not written to 4 bytes in binary file.");
}
}
void binputu4 (unsigned long u, FILE *f) {
try {
if (binario_longBE4 && Melder_debug != 18) {
if (fwrite (& u, sizeof (unsigned long), 1, f) != 1) writeError ("an unsigned long integer.");
} else {
char bytes [4];
bytes [0] = u >> 24;
bytes [1] = u >> 16;
bytes [2] = u >> 8;
bytes [3] = u;
if (fwrite (bytes, sizeof (char), 4, f) != 4) writeError ("four bytes.");
}
} catch (MelderError) {
Melder_throw ("Unsigned integer not written to 4 bytes in binary file.");
}
}
void binputi2LE (int i, FILE *f) {
try {
if (binario_shortLE2 && Melder_debug != 18) {
short s = i;
if (fwrite (& s, sizeof (short), 1, f) != 1) writeError ("a signed short integer.");
} else {
char bytes [2];
bytes [1] = i >> 8;
bytes [0] = i;
if (fwrite (bytes, sizeof (char), 2, f) != 2) writeError ("two bytes.");
}
} catch (MelderError) {
Melder_throw ("Signed integer not written to 2 bytes in binary file.");
}
}
void binputu2LE (unsigned int u, FILE *f) {
try {
if (binario_shortLE2 && Melder_debug != 18) {
unsigned short s = u;
if (fwrite (& s, sizeof (unsigned short), 1, f) != 1) writeError ("an unsigned short integer.");
} else {
char bytes [2];
bytes [1] = u >> 8;
bytes [0] = u;
if (fwrite (bytes, sizeof (char), 2, f) != 2) writeError ("two bytes.");
}
} catch (MelderError) {
Melder_throw ("Unsigned integer not written to 2 bytes in binary file.");
}
}
void binputi3LE (long i, FILE *f) {
try {
char bytes [3];
bytes [2] = i >> 16;
bytes [1] = i >> 8;
bytes [0] = i;
if (fwrite (bytes, sizeof (char), 3, f) != 3) writeError ("three bytes");
} catch (MelderError) {
Melder_throw ("Signed integer not written to 3 bytes in binary file.");
}
}
void binputi4LE (long i, FILE *f) {
try {
if (binario_longLE4 && Melder_debug != 18) {
if (fwrite (& i, sizeof (long), 1, f) != 1) writeError ("a signed long integer.");
} else {
char bytes [4];
bytes [3] = i >> 24;
bytes [2] = i >> 16;
bytes [1] = i >> 8;
bytes [0] = i;
if (fwrite (bytes, sizeof (char), 4, f) != 4) writeError ("four bytes.");
}
} catch (MelderError) {
Melder_throw ("Signed integer not written to 4 bytes in binary file.");
}
}
void binputu4LE (unsigned long u, FILE *f) {
try {
if (binario_longLE4 && Melder_debug != 18) {
if (fwrite (& u, sizeof (unsigned long), 1, f) != 1) writeError ("an unsigned long integer.");
} else {
char bytes [4];
bytes [3] = u >> 24;
bytes [2] = u >> 16;
bytes [1] = u >> 8;
bytes [0] = u;
if (fwrite (bytes, sizeof (char), 4, f) != 4) writeError ("four bytes.");
}
} catch (MelderError) {
Melder_throw ("Unsigned integer not written to 4 bytes in binary file.");
}
}
void binputr4 (double x, FILE *f) {
try {
if (binario_floatIEEE4msb && Melder_debug != 18) {
float x4 = x;
if (fwrite (& x4, sizeof (float), 1, f) != 1) writeError ("a single-precision floating-point number.");
} else {
unsigned char bytes [4];
int sign, exponent;
double fMantissa, fsMantissa;
unsigned long mantissa;
if (x < 0.0) { sign = 0x0100; x *= -1; }
else sign = 0;
if (x == 0.0) { exponent = 0; mantissa = 0; }
else {
fMantissa = frexp (x, & exponent);
if ((exponent > 128) || ! (fMantissa < 1)) // Infinity or Not-a-Number
{ exponent = sign | 0x00FF; mantissa = 0; } // Infinity
else { /* Finite. */
exponent += 126; // add bias
if (exponent <= 0) { // denormalized
fMantissa = ldexp (fMantissa, exponent - 1);
exponent = 0;
}
exponent |= sign;
fMantissa = ldexp (fMantissa, 24);
fsMantissa = floor (fMantissa);
mantissa = FloatToUnsigned (fsMantissa) & 0x007FFFFF;
}
}
bytes [0] = exponent >> 1;
bytes [1] = (exponent << 7) | (mantissa >> 16);
bytes [2] = mantissa >> 8;
bytes [3] = mantissa;
if (fwrite (bytes, sizeof (unsigned char), 4, f) != 4) writeError ("four bytes.");
}
} catch (MelderError) {
Melder_throw ("Floating-point number not written to 4 bytes in binary file.");
}
}
void binputr4LE (double x, FILE *f) {
try {
if (binario_floatIEEE4lsb && Melder_debug != 18) {
float x4 = x;
if (fwrite (& x4, sizeof (float), 1, f) != 1) writeError ("a single-precision floating-point number.");
} else {
unsigned char bytes [4];
int sign, exponent;
double fMantissa, fsMantissa;
unsigned long mantissa;
if (x < 0.0) { sign = 0x0100; x *= -1; }
else sign = 0;
if (x == 0.0) { exponent = 0; mantissa = 0; }
else {
fMantissa = frexp (x, & exponent);
if ((exponent > 128) || ! (fMantissa < 1)) // Infinity or Not-a-Number
{ exponent = sign | 0x00FF; mantissa = 0; } // Infinity
else { /* Finite. */
exponent += 126; // add bias
if (exponent <= 0) { // denormalized
fMantissa = ldexp (fMantissa, exponent - 1);
exponent = 0;
}
exponent |= sign;
fMantissa = ldexp (fMantissa, 24);
fsMantissa = floor (fMantissa);
mantissa = FloatToUnsigned (fsMantissa) & 0x007FFFFF;
}
}
bytes [3] = exponent >> 1;
bytes [2] = (exponent << 7) | (mantissa >> 16);
bytes [1] = mantissa >> 8;
bytes [0] = mantissa;
if (fwrite (bytes, sizeof (unsigned char), 4, f) != 4) writeError ("four bytes.");
}
} catch (MelderError) {
Melder_throw ("Floating-point number not written to 4 bytes in binary file.");
}
}
void binputr8 (double x, FILE *f) {
try {
if (binario_doubleIEEE8msb && Melder_debug != 18) {
if (fwrite (& x, sizeof (double), 1, f) != 1) writeError ("a double-precision floating-point number.");
} else {
unsigned char bytes [8];
int sign, exponent;
double fMantissa, fsMantissa;
unsigned long highMantissa, lowMantissa;
if (x < 0.0) { sign = 0x0800; x *= -1; }
else sign = 0;
if (x == 0.0) { exponent = 0; highMantissa = 0; lowMantissa = 0; }
else {
fMantissa = frexp (x, & exponent);
if ((exponent > 1024) || ! (fMantissa < 1)) // Infinity or Not-a-Number
{ exponent = sign | 0x07FF; highMantissa = 0; lowMantissa = 0; } // Infinity
else { /* Finite. */
exponent += 1022; // add bias
if (exponent <= 0) { // denormalized
fMantissa = ldexp (fMantissa, exponent - 1);
exponent = 0;
}
exponent |= sign;
fMantissa = ldexp (fMantissa, 21);
fsMantissa = floor (fMantissa);
highMantissa = FloatToUnsigned (fsMantissa) & 0x000FFFFF;
fMantissa = ldexp (fMantissa - fsMantissa, 32);
fsMantissa = floor (fMantissa);
lowMantissa = FloatToUnsigned (fsMantissa);
}
}
bytes [0] = exponent >> 4;
bytes [1] = (exponent << 4) | (highMantissa >> 16);
bytes [2] = highMantissa >> 8;
bytes [3] = highMantissa;
bytes [4] = lowMantissa >> 24;
bytes [5] = lowMantissa >> 16;
bytes [6] = lowMantissa >> 8;
bytes [7] = lowMantissa;
if (fwrite (bytes, sizeof (unsigned char), 8, f) != 8) writeError ("eight bytes.");
}
} catch (MelderError) {
Melder_throw ("Floating-point number not written to 8 bytes in binary file.");
}
}
void binputr10 (double x, FILE *f) {
try {
unsigned char bytes [10];
int sign, exponent;
double fMantissa, fsMantissa;
unsigned long highMantissa, lowMantissa;
if (x < 0.0) { sign = 0x8000; x *= -1; }
else sign = 0;
if (x == 0.0) { exponent = 0; highMantissa = 0; lowMantissa = 0; }
else {
fMantissa = frexp (x, & exponent);
if ((exponent > 16384) || ! (fMantissa < 1)) // Infinity or Not-a-Number
{ exponent = sign | 0x7FFF; highMantissa = 0; lowMantissa = 0; } // Infinity
else { /* Finite */
exponent += 16382; /* Add bias. */
if (exponent < 0) { /* Denormalized. */
fMantissa = ldexp (fMantissa, exponent);
exponent = 0;
}
exponent |= sign;
fMantissa = ldexp (fMantissa, 32);
fsMantissa = floor (fMantissa);
highMantissa = FloatToUnsigned (fsMantissa);
fMantissa = ldexp (fMantissa - fsMantissa, 32);
fsMantissa = floor (fMantissa);
lowMantissa = FloatToUnsigned (fsMantissa);
}
}
bytes [0] = exponent >> 8;
bytes [1] = exponent;
bytes [2] = highMantissa >> 24;
bytes [3] = highMantissa >> 16;
bytes [4] = highMantissa >> 8;
bytes [5] = highMantissa;
bytes [6] = lowMantissa >> 24;
bytes [7] = lowMantissa >> 16;
bytes [8] = lowMantissa >> 8;
bytes [9] = lowMantissa;
if (fwrite (bytes, sizeof (unsigned char), 10, f) != 10) writeError ("ten bytes.");
} catch (MelderError) {
Melder_throw ("Floating-point number not written to 10 bytes in binary file.");
}
}
fcomplex bingetc8 (FILE *f) {
try {
fcomplex result;
result. re = bingetr4 (f);
result. im = bingetr4 (f);
return result;
} catch (MelderError) {
Melder_throw ("Complex number not read from 8 bytes in binary file.");
fcomplex result = { 0 };
return result;
}
}
dcomplex bingetc16 (FILE *f) {
try {
dcomplex result;
result. re = bingetr8 (f);
result. im = bingetr8 (f);
return result;
} catch (MelderError) {
Melder_throw ("Complex number not read from 16 bytes in binary file.");
dcomplex result = { 0 };
return result;
}
}
void binputc8 (fcomplex z, FILE *f) {
try {
binputr4 (z. re, f);
binputr4 (z. im, f);
} catch (MelderError) {
Melder_throw ("Complex number not written to 8 bytes in binary file.");
}
}
void binputc16 (dcomplex z, FILE *f) {
try {
binputr8 (z. re, f);
binputr8 (z. im, f);
} catch (MelderError) {
Melder_throw ("Complex number not written to 16 bytes in binary file.");
}
}
char * bingets1 (FILE *f) {
try {
unsigned int length = bingetu1 (f);
autostring8 result = Melder_malloc (char, length + 1);
if (fread (result.peek(), sizeof (char), length, f) != length)
Melder_throw (feof (f) ? "Reached end of file" : "Error in file", " while trying to read ", length, " one-byte characters.");
result [length] = 0; // trailing null byte
return result.transfer();
} catch (MelderError) {
Melder_throw ("Text not read from a binary file.");
}
}
char * bingets2 (FILE *f) {
try {
unsigned int length = bingetu2 (f);
autostring8 result = Melder_malloc (char, length + 1);
if (fread (result.peek(), sizeof (char), length, f) != length)
Melder_throw (feof (f) ? "Reached end of file" : "Error in file", " while trying to read ", length, " one-byte characters.");
result [length] = 0; // trailing null byte
return result.transfer();
} catch (MelderError) {
Melder_throw ("Text not read from a binary file.");
}
}
char * bingets4 (FILE *f) {
try {
unsigned long length = bingetu4 (f);
autostring8 result = Melder_malloc (char, length + 1);
if (fread (result.peek(), sizeof (char), length, f) != length)
Melder_throw (feof (f) ? "Reached end of file" : "Error in file", " while trying to read ", length, " one-byte characters.");
result [length] = 0; // trailing null byte
return result.transfer();
} catch (MelderError) {
Melder_throw ("Text not read from a binary file.");
}
}
wchar * bingetw1 (FILE *f) {
try {
autostring result = NULL;
unsigned short length = bingetu1 (f);
if (length == 0xFF) { // an escape for encoding
/*
* UTF-16
*/
length = bingetu1 (f);
result.reset (Melder_malloc (wchar, length + 1));
for (unsigned short i = 0; i < length; i ++) {
if (sizeof (wchar_t) == 2) {
result [i] = bingetu2 (f);
} else {
uint16_t kar = bingetu2 (f);
if ((kar & 0xF800) == 0xD800) {
if (kar > 0xDBFF)
Melder_throw ("Incorrect Unicode value (first surrogate member ", kar, ").");
uint16_t kar2 = bingetu2 (f);
if (kar2 < 0xDC00 || kar2 > 0xDFFF)
Melder_throw ("Incorrect Unicode value (second surrogate member ", kar2, ").");
result [i] = (((kar & 0x3FF) << 10) | (kar2 & 0x3FF)) + 0x10000;
} else {
result [i] = kar;
}
}
}
} else {
/*
* ASCII
*/
result.reset (Melder_malloc (wchar, length + 1));
for (unsigned short i = 0; i < length; i ++) {
result [i] = bingetu1 (f);
}
}
result [length] = L'\0';
return result.transfer();
} catch (MelderError) {
Melder_throw ("Text not read from a binary file.");
}
}
wchar * bingetw2 (FILE *f) {
try {
autostring result = NULL;
unsigned short length = bingetu2 (f);
if (length == 0xFFFF) { // an escape for encoding
/*
* UTF-16
*/
length = bingetu2 (f);
result.reset (Melder_malloc (wchar, length + 1));
for (unsigned short i = 0; i < length; i ++) {
if (sizeof (wchar_t) == 2) {
result [i] = bingetu2 (f);
} else {
unsigned short kar = bingetu2 (f);
if ((kar & 0xF800) == 0xD800) {
if (kar > 0xDBFF)
Melder_throw ("Incorrect Unicode value (first surrogate member ", kar, ").");
unsigned short kar2 = bingetu2 (f);
if (kar2 < 0xDC00 || kar2 > 0xDFFF)
Melder_throw ("Incorrect Unicode value (second surrogate member ", kar2, ").");
result [i] = (((kar & 0x3FF) << 10) | (kar2 & 0x3FF)) + 0x10000;
} else {
result [i] = kar;
}
}
}
} else {
/*
* ASCII
*/
result.reset (Melder_malloc (wchar, length + 1));
for (unsigned short i = 0; i < length; i ++) {
result [i] = bingetu1 (f);
}
}
result [length] = L'\0';
return result.transfer();
} catch (MelderError) {
Melder_throw ("Text not read from a binary file.");
}
}
wchar * bingetw4 (FILE *f) {
try {
autostring result = NULL;
unsigned long length = bingetu4 (f);
if (length == 0xFFFFFFFF) { // an escape for encoding
/*
* UTF-16
*/
length = bingetu4 (f);
result.reset (Melder_malloc (wchar, length + 1));
for (unsigned long i = 0; i < length; i ++) {
if (sizeof (wchar_t) == 2) {
result [i] = bingetu2 (f);
} else {
unsigned short kar = bingetu2 (f);
if ((kar & 0xF800) == 0xD800) {
if (kar > 0xDBFF)
Melder_throw ("Incorrect Unicode value (first surrogate member ", kar, ").");
unsigned short kar2 = bingetu2 (f);
if (kar2 < 0xDC00 || kar2 > 0xDFFF)
Melder_throw ("Incorrect Unicode value (second surrogate member ", kar2, ").");
result [i] = (((kar & 0x3FF) << 10) | (kar2 & 0x3FF)) + 0x10000;
} else {
result [i] = kar;
}
}
}
} else {
/*
* ASCII
*/
result.reset (Melder_malloc (wchar, length + 1));
for (unsigned long i = 0; i < length; i ++) {
result [i] = bingetu1 (f);
}
}
result [length] = L'\0';
return result.transfer();
} catch (MelderError) {
Melder_throw ("Text not read from a binary file.");
}
}
void binputs1 (const char *s, FILE *f) {
try {
if (s == NULL) {
binputu1 (0, f);
} else {
unsigned long length = strlen (s);
if (length > 255) {
Melder_warning ("Text of ", length, " characters truncated to 255 characters.");
length = 255;
}
binputu1 (length, f);
if (fwrite (s, sizeof (char), length, f) != length)
Melder_throw ("Error in file while trying to write ", length, " one-byte characters.");
}
} catch (MelderError) {
Melder_throw ("Text not written to a binary file.");
}
}
void binputs2 (const char *s, FILE *f) {
try {
if (s == NULL) {
binputu2 (0, f);
} else {
unsigned long length = strlen (s);
if (length > 65535) {
Melder_warning ("Text of ", length, " characters truncated to 65535 characters.");
length = 65535;
}
binputu2 (length, f);
if (fwrite (s, sizeof (char), length, f) != length)
Melder_throw ("Error in file while trying to write ", length, " one-byte characters.");
}
} catch (MelderError) {
Melder_throw ("Text not written to a binary file.");
}
}
void binputs4 (const char *s, FILE *f) {
try {
if (s == NULL) {
binputu4 (0, f);
} else {
unsigned long length = strlen (s);
binputu4 (length, f);
if (fwrite (s, sizeof (char), length, f) != length)
Melder_throw ("Error in file while trying to write ", length, " one-byte characters.");
}
} catch (MelderError) {
Melder_throw ("Text not written to a binary file.");
}
}
static inline void binpututf16 (wchar_t character, FILE *f) {
if (sizeof (wchar_t) == 2) { // wchar_t is UTF-16?
binputu2 (character, f);
} else { // wchar_t is UTF-32.
MelderUtf32 kar = character;
if (kar <= 0xFFFF) {
binputu2 (character, f);
} else if (kar <= 0x10FFFF) {
kar -= 0x10000;
binputu2 (0xD800 | (kar >> 10), f);
binputu2 (0xDC00 | (kar & 0x3FF), f);
} else {
Melder_fatal ("Impossible Unicode value.");
}
}
}
void binputw1 (const wchar_t *s, FILE *f) {
try {
if (s == NULL) {
binputu1 (0, f);
} else {
unsigned long length = wcslen (s);
if (length > 254) {
Melder_warning ("Text of ", length, " characters truncated to 254 characters.");
length = 254;
}
if (Melder_isValidAscii (s)) {
/*
* ASCII
*/
binputu1 (length, f);
for (unsigned long i = 0; i < length; i ++) {
binputu1 (s [i], f);
}
} else {
/*
* UTF-16
*/
binputu1 (0xFF, f); // an escape for multibyte encoding
binputu1 (length, f);
for (unsigned long i = 0; i < length; i ++) {
binpututf16 (s [i], f);
}
}
}
} catch (MelderError) {
Melder_throw ("Text not written to a binary file.");
}
}
void binputw2 (const wchar_t *s, FILE *f) {
try {
if (s == NULL) {
binputu2 (0, f);
} else {
unsigned long length = wcslen (s);
if (length > 65534) {
Melder_warning ("Text of ", length, " characters truncated to 65534 characters.");
length = 65534;
}
if (Melder_isValidAscii (s)) {
/*
* ASCII
*/
binputu2 (length, f);
for (unsigned long i = 0; i < length; i ++) {
binputu1 (s [i], f);
}
} else {
/*
* UTF-16
*/
binputu2 (0xFFFF, f); // an escape for multibyte encoding
binputu2 (length, f);
for (unsigned long i = 0; i < length; i ++) {
binpututf16 (s [i], f);
}
}
}
} catch (MelderError) {
Melder_throw ("Text not written to a binary file.");
}
}
void binputw4 (const wchar_t *s, FILE *f) {
try {
if (s == NULL) {
binputu4 (0, f);
} else {
unsigned long length = wcslen (s);
if (Melder_isValidAscii (s)) {
/*
* ASCII
*/
binputu4 (length, f);
for (unsigned long i = 0; i < length; i ++) {
binputu1 (s [i], f);
}
} else {
/*
* UTF-16
*/
binputu4 (0xFFFFFFFF, f); // an escape for multibyte encoding
binputu4 (length, f);
for (unsigned long i = 0; i < length; i ++) {
binpututf16 (s [i], f);
}
}
}
} catch (MelderError) {
Melder_throw ("Text not written to a binary file.");
}
}
/********** machine-independent cache I/O **********/
#define my me ->
#define START(x) char *ptr = (char *) & (x);
#define READ * ptr ++ = * f -> ptr ++;
#define WRITE * f -> ptr ++ = * ptr ++;
CACHE * memopen (size_t nbytes) {
CACHE *me;
if (nbytes < 1) return NULL;
me = Melder_malloc (CACHE, 1);
my base = Melder_malloc (unsigned char, nbytes);
my max = my base + nbytes;
my ptr = my base;
return me;
}
int memclose (CACHE *me) {
if (! me || ! my base) return EOF;
Melder_free (my base);
Melder_free (me);
return 0;
}
size_t memread (void *ptr, size_t size, size_t nmemb, CACHE *me) {
size_t nbytes = size * nmemb;
memcpy (ptr, my ptr, nbytes);
my ptr += nbytes;
return nmemb;
}
size_t memwrite (const void *ptr, size_t size, size_t nmemb, CACHE *me) {
size_t nbytes = size * nmemb;
Melder_assert (my ptr + nbytes <= my max);
memcpy (my ptr, ptr, nbytes);
my ptr += nbytes;
return nmemb;
}
void memprint1 (CACHE *me, const char *s1) {
(void) memwrite (s1, 1, strlen (s1), me);
}
void memprint2 (CACHE *me, const char *s1, const char *s2) {
(void) memwrite (s1, 1, strlen (s1), me);
(void) memwrite (s2, 1, strlen (s2), me);
}
void memprint3 (CACHE *me, const char *s1, const char *s2, const char *s3) {
(void) memwrite (s1, 1, strlen (s1), me);
(void) memwrite (s2, 1, strlen (s2), me);
(void) memwrite (s3, 1, strlen (s3), me);
}
void memprint4 (CACHE *me, const char *s1, const char *s2, const char *s3, const char *s4) {
(void) memwrite (s1, 1, strlen (s1), me);
(void) memwrite (s2, 1, strlen (s2), me);
(void) memwrite (s3, 1, strlen (s3), me);
(void) memwrite (s4, 1, strlen (s4), me);
}
void memprint5 (CACHE *me, const char *s1, const char *s2, const char *s3, const char *s4, const char *s5) {
(void) memwrite (s1, 1, strlen (s1), me);
(void) memwrite (s2, 1, strlen (s2), me);
(void) memwrite (s3, 1, strlen (s3), me);
(void) memwrite (s4, 1, strlen (s4), me);
(void) memwrite (s5, 1, strlen (s5), me);
}
unsigned int cacgetu1 (CACHE *me) { return * (unsigned char *) my ptr ++; }
void cacputu1 (unsigned int u, CACHE *me) { * (unsigned char *) my ptr ++ = u; }
int cacgeti1 (CACHE *me) { return * (signed char *) my ptr ++; }
void cacputi1 (int u, CACHE *me) { * (signed char *) my ptr ++ = u; }
int cacgete1 (CACHE *me, const wchar_t *type) {
int result = * (signed char *) my ptr ++;
if (result < 0)
Melder_throw ("(cacgete1:) ", result, " is not a value of enumerated type \"", type, "\".");
return result;
}
void cacpute1 (int value, CACHE *me) {
* (signed char *) my ptr ++ = value;
}
int memseek (CACHE *me, long offset, int whence) {
my ptr = whence == 0 ? my base + offset : my ptr + offset;
return 0;
}
long memtell (CACHE *me) { return my ptr - my base; }
void memrewind (CACHE *me) { my ptr = my base; }
#define macro_cacgetb(nbits) \
unsigned int cacgetb##nbits (CACHE *f) { \
unsigned char result; \
if (bitsInReadBuffer < nbits) { readBuffer = * f -> ptr ++; bitsInReadBuffer = 8; } \
result = readBuffer << (8 - bitsInReadBuffer); \
bitsInReadBuffer -= nbits; \
return result >> (8 - nbits); \
}
macro_cacgetb (1)
macro_cacgetb (2)
macro_cacgetb (3)
macro_cacgetb (4)
macro_cacgetb (5)
macro_cacgetb (6)
macro_cacgetb (7)
void cacgetb (CACHE *f) { (void) f; bitsInReadBuffer = 0; }
int cacgeti2 (CACHE *f) {
if (binario_shortBE2) {
short s;
START (s) READ READ
return s; /* With sign extension if an int is 4 bytes. */
} else {
unsigned char bytes [2];
START (bytes) READ READ
return (signed short) (((unsigned short) bytes [0] << 8) | (unsigned short) bytes [1]);
}
}
unsigned int cacgetu2 (CACHE *f) {
if (binario_shortBE2) {
unsigned short s;
START (s) READ READ
return s; /* Without sign extension. */
} else {
unsigned char bytes [2];
START (bytes) READ READ
return ((unsigned short) bytes [0] << 8) | (unsigned short) bytes [1];
}
}
int cacgete2 (CACHE *f, const wchar_t *type) {
signed short s;
if (binario_shortBE2) {
START (s) READ READ
} else {
unsigned char bytes [2];
START (bytes) READ READ
s = ((unsigned short) bytes [0] << 8) | (unsigned short) bytes [1];
}
if (s < 0)
Melder_throw ("(cacgete2:) ", s, " is not a value of enumerated type \"", type, "\".");
return s;
}
long cacgeti4 (CACHE *f) {
if (binario_longBE4) {
long l;
START (l) READ READ READ READ
return l;
} else {
unsigned char bytes [4];
START (bytes) READ READ READ READ
return
((unsigned long) bytes [0] << 24) |
((unsigned long) bytes [1] << 16) |
((unsigned long) bytes [2] << 8) |
(unsigned long) bytes [3];
}
}
unsigned long cacgetu4 (CACHE *f) {
if (binario_longBE4) {
unsigned long l;
START (l) READ READ READ READ
return l;
} else {
unsigned char bytes [4];
START (bytes) READ READ READ READ
return
((unsigned long) bytes [0] << 24) |
((unsigned long) bytes [1] << 16) |
((unsigned long) bytes [2] << 8) |
(unsigned long) bytes [3];
}
}
int cacgeti2LE (CACHE *f) {
unsigned char bytes [2];
START (bytes) READ READ
return (signed short) (((unsigned short) bytes [1] << 8) | (unsigned short) bytes [0]);
}
unsigned int cacgetu2LE (CACHE *f) {
unsigned char bytes [2];
START (bytes) READ READ
return ((unsigned short) bytes [1] << 8) | (unsigned short) bytes [0];
}
long cacgeti4LE (CACHE *f) {
unsigned char bytes [4];
START (bytes) READ READ READ READ
return
((unsigned long) bytes [3] << 24) | ((unsigned long) bytes [2] << 16) |
((unsigned long) bytes [1] << 8) | (unsigned long) bytes [0];
}
unsigned long cacgetu4LE (CACHE *f) {
unsigned char bytes [4];
START (bytes) READ READ READ READ
return
((unsigned long) bytes [3] << 24) | ((unsigned long) bytes [2] << 16) |
((unsigned long) bytes [1] << 8) | (unsigned long) bytes [0];
}
double cacgetr4 (CACHE *f) {
if (binario_floatIEEE4msb) {
float x;
START (x) READ READ READ READ
return x;
} else {
unsigned char bytes [4];
double x;
long exponent;
unsigned long mantissa;
START (bytes) READ READ READ READ
exponent = ((unsigned long) (bytes [0] & 0x7F) << 1) |
((unsigned long) (bytes [1] & 0x80) >> 7);
mantissa = ((unsigned long) (bytes [1] & 0x7F) << 16) |
((unsigned long) bytes [2] << 8) | (unsigned long) bytes [3];
if (exponent == 0)
if (mantissa == 0) x = 0.0;
else x = ldexp (UnsignedToFloat (mantissa), exponent - 149); /* Denormalized. */
else if (exponent == 0x00FF) /* Infinity or Not-a-Number. */
x = HUGE_VAL;
else /* Finite. */
x = ldexp (UnsignedToFloat (mantissa | 0x00800000), exponent - 150);
return bytes [0] & 0x80 ? - x : x;
}
}
double cacgetr8 (CACHE *f) {
if (binario_doubleIEEE8msb) {
double x;
START (x) READ READ READ READ READ READ READ READ
return x;
} else {
unsigned char bytes [8];
double x;
long exponent;
unsigned long highMantissa, lowMantissa;
START (bytes) READ READ READ READ READ READ READ READ
exponent = ((unsigned long) (bytes [0] & 0x7F) << 4) |
((unsigned long) (bytes [1] & 0xF0) >> 4);
highMantissa = ((unsigned long) (bytes [1] & 0x0F) << 16) |
((unsigned long) bytes [2] << 8) | (unsigned long) bytes [3];
lowMantissa = ((unsigned long) bytes [4] << 24) | ((unsigned long) bytes [5] << 16) |
((unsigned long) bytes [6] << 8) | (unsigned long) bytes [7];
if (exponent == 0)
if (highMantissa == 0 && lowMantissa == 0) x = 0.0;
else x = ldexp (UnsignedToFloat (highMantissa), exponent - 1042) +
ldexp (UnsignedToFloat (lowMantissa), exponent - 1074); /* Denormalized. */
else if (exponent == 0x07FF) /* Infinity of Not-a-Number. */
x = HUGE_VAL;
else
x = ldexp (UnsignedToFloat (highMantissa | 0x00100000), exponent - 1043) +
ldexp (UnsignedToFloat (lowMantissa), exponent - 1075);
return bytes [0] & 0x80 ? - x : x;
}
}
double cacgetr10 (CACHE *f) {
unsigned char bytes [10];
double x;
long exponent;
unsigned long highMantissa, lowMantissa;
START (bytes) READ READ READ READ READ READ READ READ READ READ
exponent = ((unsigned long) (bytes [0] & 0x7F) << 8) | bytes [1];
highMantissa = ((unsigned long) bytes [2] << 24) | ((unsigned long) bytes [3] << 16) |
((unsigned long) bytes [4] << 8) | (unsigned long) bytes [5];
lowMantissa = ((unsigned long) bytes [6] << 24) | ((unsigned long) bytes [7] << 16) |
((unsigned long) bytes [8] << 8) | (unsigned long) bytes [9];
if (exponent == 0 && highMantissa == 0 && lowMantissa == 0) x = 0;
else if (exponent == 0x7FFF) x = HUGE_VAL; /* Infinity or NaN */
else {
exponent -= 16383;
x = ldexp (UnsignedToFloat (highMantissa), exponent - 31);
x += ldexp (UnsignedToFloat (lowMantissa), exponent - 63);
}
return bytes [0] & 0x80 ? - x : x;
}
#define macro_cacputb(nbits) \
void cacputb##nbits (unsigned int value, CACHE *f) { \
if (bitsInWriteBuffer + nbits > 8) { * f -> ptr ++ = writeBuffer; bitsInWriteBuffer = 0; writeBuffer = 0; } \
writeBuffer |= (value << (8 - nbits)) >> bitsInWriteBuffer; \
bitsInWriteBuffer += nbits; \
}
macro_cacputb (1)
macro_cacputb (2)
macro_cacputb (3)
macro_cacputb (4)
macro_cacputb (5)
macro_cacputb (6)
macro_cacputb (7)
void cacputb (CACHE *f) {
if (bitsInWriteBuffer == 0) return;
cacputu1 (writeBuffer, f); /* Flush. */
bitsInWriteBuffer = 0;
writeBuffer = 0;
}
void cacputi2 (int i, CACHE *f) {
if (binario_shortBE2) {
short s = i;
START (s) WRITE WRITE
} else {
char bytes [2];
bytes [0] = i >> 8;
bytes [1] = i;
{ START (bytes) WRITE WRITE }
}
}
void cacputu2 (unsigned int u, CACHE *f) {
if (binario_shortBE2) {
unsigned short s = u;
START (s) WRITE WRITE
} else {
char bytes [2];
bytes [0] = u >> 8;
bytes [1] = u;
{ START (bytes) WRITE WRITE }
}
}
void cacpute2 (int value, CACHE *f) {
if (binario_shortBE2) {
signed short s = value;
START (s) WRITE WRITE
} else {
char bytes [2];
bytes [0] = value >> 8;
bytes [1] = value;
{ START (bytes) WRITE WRITE }
}
}
void cacputi4 (long i, CACHE *f) {
if (binario_longBE4) {
START (i) WRITE WRITE WRITE WRITE
} else {
char bytes [4];
bytes [0] = i >> 24;
bytes [1] = i >> 16;
bytes [2] = i >> 8;
bytes [3] = i;
{ START (bytes) WRITE WRITE WRITE WRITE }
}
}
void cacputu4 (unsigned long u, CACHE *f) {
if (binario_longBE4) {
START (u) WRITE WRITE WRITE WRITE
} else {
char bytes [4];
bytes [0] = u >> 24;
bytes [1] = u >> 16;
bytes [2] = u >> 8;
bytes [3] = u;
{ START (bytes) WRITE WRITE WRITE WRITE }
}
}
void cacputi2LE (int i, CACHE *f) {
char bytes [2];
bytes [1] = i >> 8;
bytes [0] = i;
{ START (bytes) WRITE WRITE }
}
void cacputu2LE (unsigned int u, CACHE *f) {
char bytes [2];
bytes [1] = u >> 8;
bytes [0] = u;
{ START (bytes) WRITE WRITE }
}
void cacputi4LE (long i, CACHE *f) {
char bytes [4];
bytes [3] = i >> 24;
bytes [2] = i >> 16;
bytes [1] = i >> 8;
bytes [0] = i;
{ START (bytes) WRITE WRITE WRITE WRITE }
}
void cacputu4LE (unsigned long u, CACHE *f) {
char bytes [4];
bytes [3] = u >> 24;
bytes [2] = u >> 16;
bytes [1] = u >> 8;
bytes [0] = u;
{ START (bytes) WRITE WRITE WRITE WRITE }
}
void cacputr4 (double x, CACHE *f) {
if (binario_floatIEEE4msb) {
float x4 = x;
START (x4) WRITE WRITE WRITE WRITE
} else {
unsigned char bytes [4];
int sign, exponent;
double fMantissa, fsMantissa;
unsigned long mantissa;
if (x < 0.0) { sign = 0x0100; x *= -1; }
else sign = 0;
if (x == 0.0) { exponent = 0; mantissa = 0; }
else {
fMantissa = frexp (x, & exponent);
if ((exponent > 128) || ! (fMantissa < 1)) /* Infinity or Not-a-Number. */
{ exponent = sign | 0x00FF; mantissa = 0; } /* Infinity. */
else { /* Finite. */
exponent += 126; /* Add bias. */
if (exponent <= 0) { /* Denormalized. */
fMantissa = ldexp (fMantissa, exponent - 1);
exponent = 0;
}
exponent |= sign;
fMantissa = ldexp (fMantissa, 24);
fsMantissa = floor (fMantissa);
mantissa = FloatToUnsigned (fsMantissa) & 0x007FFFFF;
}
}
bytes [0] = exponent >> 1;
bytes [1] = (exponent << 7) | (mantissa >> 16);
bytes [2] = mantissa >> 8;
bytes [3] = mantissa;
{ START (bytes) WRITE WRITE WRITE WRITE }
}
}
void cacputr8 (double x, CACHE *f) {
if (binario_doubleIEEE8msb) {
START (x) WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE
} else {
unsigned char bytes [8];
int sign, exponent;
double fMantissa, fsMantissa;
unsigned long highMantissa, lowMantissa;
if (x < 0.0) { sign = 0x0800; x *= -1; }
else sign = 0;
if (x == 0.0) { exponent = 0; highMantissa = 0; lowMantissa = 0; }
else {
fMantissa = frexp (x, & exponent);
if ((exponent > 1024) || ! (fMantissa < 1)) /* Infinity or Not-a-Number. */
{ exponent = sign | 0x07FF; highMantissa = 0; lowMantissa = 0; } /* Infinity. */
else { /* Finite. */
exponent += 1022; /* Add bias. */
if (exponent <= 0) { /* Denormalized. */
fMantissa = ldexp (fMantissa, exponent - 1);
exponent = 0;
}
exponent |= sign;
fMantissa = ldexp (fMantissa, 21);
fsMantissa = floor (fMantissa);
highMantissa = FloatToUnsigned (fsMantissa) & 0x000FFFFF;
fMantissa = ldexp (fMantissa - fsMantissa, 32);
fsMantissa = floor (fMantissa);
lowMantissa = FloatToUnsigned (fsMantissa);
}
}
bytes [0] = exponent >> 4;
bytes [1] = (exponent << 4) | (highMantissa >> 16);
bytes [2] = highMantissa >> 8;
bytes [3] = highMantissa;
bytes [4] = lowMantissa >> 24;
bytes [5] = lowMantissa >> 16;
bytes [6] = lowMantissa >> 8;
bytes [7] = lowMantissa;
{ START (bytes) WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE }
}
}
void cacputr10 (double x, CACHE *f) {
unsigned char bytes [10];
int sign, exponent;
double fMantissa, fsMantissa;
unsigned long highMantissa, lowMantissa;
if (x < 0.0) { sign = 0x8000; x *= -1; }
else sign = 0;
if (x == 0.0) { exponent = 0; highMantissa = 0; lowMantissa = 0; }
else {
fMantissa = frexp (x, & exponent);
if ((exponent > 16384) || ! (fMantissa < 1)) /* Infinity or Not-a-Number. */
{ exponent = sign | 0x7FFF; highMantissa = 0; lowMantissa = 0; } /* Infinity. */
else { /* Finite */
exponent += 16382; /* Add bias. */
if (exponent < 0) { /* Denormalized. */
fMantissa = ldexp (fMantissa, exponent);
exponent = 0;
}
exponent |= sign;
fMantissa = ldexp (fMantissa, 32);
fsMantissa = floor (fMantissa);
highMantissa = FloatToUnsigned (fsMantissa);
fMantissa = ldexp (fMantissa - fsMantissa, 32);
fsMantissa = floor (fMantissa);
lowMantissa = FloatToUnsigned (fsMantissa);
}
}
bytes [0] = exponent >> 8;
bytes [1] = exponent;
bytes [2] = highMantissa >> 24;
bytes [3] = highMantissa >> 16;
bytes [4] = highMantissa >> 8;
bytes [5] = highMantissa;
bytes [6] = lowMantissa >> 24;
bytes [7] = lowMantissa >> 16;
bytes [8] = lowMantissa >> 8;
bytes [9] = lowMantissa;
{ START (bytes) WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE }
}
fcomplex cacgetc8 (CACHE *f) {
fcomplex result;
result. re = cacgetr4 (f);
result. im = cacgetr4 (f);
return result;
}
dcomplex cacgetc16 (CACHE *f) {
dcomplex result;
result. re = cacgetr8 (f);
result. im = cacgetr8 (f);
return result;
}
void cacputc8 (fcomplex z, CACHE *f) {
cacputr4 (z. re, f);
cacputr4 (z. im, f);
}
void cacputc16 (dcomplex z, CACHE *f) {
cacputr8 (z. re, f);
cacputr8 (z. im, f);
}
char * cacgets1 (CACHE *f) {
try {
unsigned int length = (unsigned char) * f -> ptr ++;
autostring8 result = Melder_malloc (char, length + 1);
memread (result.peek(), 1, length, f);
result [length] = 0; // trailing null byte
return result.transfer();
} catch (MelderError) {
Melder_throw ("Cannot read string from cache.");
}
}
char * cacgets2 (CACHE *f) {
try {
unsigned int length = cacgetu2 (f);
autostring8 result = Melder_malloc (char, length + 1);
memread (result.peek(), 1, length, f);
result [length] = 0; // trailing null byte
return result.transfer();
} catch (MelderError) {
Melder_throw ("Cannot read string from cache.");
}
}
char * cacgets4 (CACHE *f) {
try {
unsigned long length = cacgetu4 (f);
autostring8 result = Melder_malloc (char, length + 1);
memread (result.peek(), 1, length, f);
result [length] = 0; // trailing null byte
return result.transfer();
} catch (MelderError) {
Melder_throw ("Cannot read string from cache.");
}
}
void cacputs1 (const char *s, CACHE *f) {
unsigned int length = s ? strlen (s) : 0; if (length > 255) length = 255;
* f -> ptr ++ = length; if (s) memwrite (s, 1, length, f);
}
void cacputs2 (const char *s, CACHE *f) {
unsigned int length = s ? strlen (s) : 0; if (length > 65535) length = 65535;
cacputu2 (length, f); if (s) memwrite (s, 1, length, f);
}
void cacputs4 (const char *s, CACHE *f) {
unsigned long length = s ? strlen (s) : 0;
cacputu4 (length, f); if (s) memwrite (s, 1, length, f);
}
/* End of file abcio.cpp */
|