1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
|
/**********************************************************************
* $Id: avc_bin.c,v 1.29 2006/08/17 18:56:42 dmorissette Exp $
*
* Name: avc_bin.c
* Project: Arc/Info vector coverage (AVC) BIN->E00 conversion library
* Language: ANSI C
* Purpose: Binary files access functions.
* Author: Daniel Morissette, dmorissette@dmsolutions.ca
*
**********************************************************************
* Copyright (c) 1999-2005, Daniel Morissette
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**********************************************************************
*
* $Log: avc_bin.c,v $
* Revision 1.29 2006/08/17 18:56:42 dmorissette
* Support for reading standalone info tables (just tables, no coverage
* data) by pointing AVCE00ReadOpen() to the info directory (bug 1549).
*
* Revision 1.28 2006/06/14 16:31:28 daniel
* Added support for AVCCoverPC2 type (bug 1491)
*
* Revision 1.27 2005/06/03 03:49:58 daniel
* Update email address, website url, and copyright dates
*
* Revision 1.26 2004/02/28 06:35:49 warmerda
* Fixed AVCBinReadObject() index support to use 'x' or 'X' for index
* depending on the case of the original name.
* Fixed so that PC Arc/Info coverages with the extra 256 byte header work
* properly when using indexes to read them.
* http://bugzilla.remotesensing.org/show_bug.cgi?id=493
*
* Revision 1.25 2004/02/11 05:49:44 daniel
* Added support for deleted flag in arc.dir (bug 2332)
*
* Revision 1.24 2002/08/27 15:26:06 daniel
* Removed C++ style comments for IRIX compiler (GDAL bug 192)
*
* Revision 1.23 2002/04/16 20:04:24 daniel
* Use record size while reading ARC, PAL, CNT to skip junk bytes. (bug940)
*
* Revision 1.22 2002/03/18 19:03:37 daniel
* Fixed AVCBinReadObject() for PAL objects (bug 848)
*
* Revision 1.21 2002/02/14 22:54:13 warmerda
* added polygon and table support for random reading
*
* Revision 1.20 2002/02/13 20:35:24 warmerda
* added AVCBinReadObject
*
* Revision 1.19 2001/11/25 22:01:23 daniel
* Fixed order of args to AVCRawBinFSeek() in _AVCBinReadNextTableRec()
*
* Revision 1.18 2000/10/16 16:16:20 daniel
* Accept TXT files in AVCCoverWeird that use both PC or V7 TXT structure
*
* Revision 1.17 2000/09/26 20:21:04 daniel
* Added AVCCoverPC write
*
* Revision 1.16 2000/09/22 19:45:20 daniel
* Switch to MIT-style license
*
* Revision 1.15 2000/09/20 15:09:34 daniel
* Check for DAT/NIT fnames sometimes truncated to 8 chars in weird coverages
*
* Revision 1.14 2000/06/05 21:38:53 daniel
* Handle precision field > 1000 in cover file header as meaning double prec.
*
* Revision 1.13 2000/05/29 15:31:30 daniel
* Added Japanese DBCS support
*
* Revision 1.12 2000/02/14 17:22:36 daniel
* Check file signature (9993 or 9994) when reading header.
*
* Revision 1.11 2000/02/02 04:24:52 daniel
* Support double precision "weird" coverages
*
* Revision 1.10 2000/01/10 02:54:10 daniel
* Added read support for "weird" coverages
*
* Revision 1.9 2000/01/07 07:11:51 daniel
* Added support for reading PC Coverage TXT files
*
* Revision 1.8 1999/12/24 07:38:10 daniel
* Added missing DBFClose()
*
* Revision 1.7 1999/12/24 07:18:34 daniel
* Added PC Arc/Info coverages support
*
* Revision 1.6 1999/08/23 18:17:16 daniel
* Modified AVCBinReadListTables() to return INFO fnames for DeleteCoverage()
*
* Revision 1.5 1999/05/11 01:49:08 daniel
* Simple changes required by addition of coverage write support
*
* Revision 1.4 1999/03/03 18:42:53 daniel
* Fixed problem with INFO table headers (arc.dir) that sometimes contain an
* invalid number of records.
*
* Revision 1.3 1999/02/25 17:01:53 daniel
* Added support for 16 bit integers in INFO tables (type=50, size=2)
*
* Revision 1.2 1999/02/25 03:41:28 daniel
* Added TXT, TX6/TX7, RXP and RPL support
*
* Revision 1.1 1999/01/29 16:28:52 daniel
* Initial revision
*
**********************************************************************/
#include "avc.h"
#include <ctype.h> /* for isspace() */
/*=====================================================================
* Prototypes for some static functions
*====================================================================*/
static AVCBinFile *_AVCBinReadOpenTable(const char *pszInfoPath,
const char *pszTableName,
AVCCoverType eCoverType,
AVCDBCSInfo *psDBCSInfo);
static AVCBinFile *_AVCBinReadOpenDBFTable(const char *pszInfoPath,
const char *pszTableName);
static AVCBinFile *_AVCBinReadOpenPrj(const char *pszPath,const char *pszName);
static int _AVCBinReadNextTableRec(AVCRawBinFile *psFile, int nFields,
AVCFieldInfo *pasDef, AVCField *pasFields,
int nRecordSize);
static int _AVCBinReadNextDBFTableRec(DBFHandle hDBFFile, int *piRecordIndex,
int nFields, AVCFieldInfo *pasDef,
AVCField *pasFields);
/*=====================================================================
* Stuff related to reading the binary coverage files
*====================================================================*/
/**********************************************************************
* AVCBinReadOpen()
*
* Open a coverage file for reading, read the file header if applicable,
* and initialize a temp. storage structure to be ready to read objects
* from the file.
*
* pszPath is the coverage (or info directory) path, terminated by
* a '/' or a '\\'
* pszName is the name of the file to open relative to this directory.
*
* Note: For most file types except tables, passing pszPath="" and
* including the coverage path as part of pszName instead would work.
*
* Returns a valid AVCBinFile handle, or NULL if the file could
* not be opened.
*
* AVCBinClose() will eventually have to be called to release the
* resources used by the AVCBinFile structure.
**********************************************************************/
AVCBinFile *AVCBinReadOpen(const char *pszPath, const char *pszName,
AVCCoverType eCoverType, AVCFileType eFileType,
AVCDBCSInfo *psDBCSInfo)
{
AVCBinFile *psFile;
/*-----------------------------------------------------------------
* The case of INFO tables is a bit more complicated...
* pass the control to a separate function.
*----------------------------------------------------------------*/
if (eFileType == AVCFileTABLE)
{
if (eCoverType == AVCCoverPC || eCoverType == AVCCoverPC2)
return _AVCBinReadOpenDBFTable(pszPath, pszName);
else
return _AVCBinReadOpenTable(pszPath, pszName,
eCoverType, psDBCSInfo);
}
/*-----------------------------------------------------------------
* PRJ files are text files... we won't use the AVCRawBin*()
* functions for them...
*----------------------------------------------------------------*/
if (eFileType == AVCFilePRJ)
{
return _AVCBinReadOpenPrj(pszPath, pszName);
}
/*-----------------------------------------------------------------
* All other file types share a very similar opening method.
*----------------------------------------------------------------*/
psFile = (AVCBinFile*)CPLCalloc(1, sizeof(AVCBinFile));
psFile->eFileType = eFileType;
psFile->eCoverType = eCoverType;
psFile->pszFilename = (char*)CPLMalloc((strlen(pszPath)+strlen(pszName)+1)*
sizeof(char));
sprintf(psFile->pszFilename, "%s%s", pszPath, pszName);
AVCAdjustCaseSensitiveFilename(psFile->pszFilename);
psFile->psRawBinFile = AVCRawBinOpen(psFile->pszFilename, "r",
AVC_COVER_BYTE_ORDER(eCoverType),
psDBCSInfo);
if (psFile->psRawBinFile == NULL)
{
/* Failed to open file... just return NULL since an error message
* has already been issued by AVCRawBinOpen()
*/
CPLFree(psFile->pszFilename);
CPLFree(psFile);
return NULL;
}
/*-----------------------------------------------------------------
* Read the header, and set the precision field if applicable
*----------------------------------------------------------------*/
if (AVCBinReadRewind(psFile) != 0)
{
CPLFree(psFile->pszFilename);
CPLFree(psFile);
return NULL;
}
/*-----------------------------------------------------------------
* Allocate a temp. structure to use to read objects from the file
* (Using Calloc() will automatically initialize the struct contents
* to NULL... this is very important for ARCs and PALs)
*----------------------------------------------------------------*/
if (psFile->eFileType == AVCFileARC)
{
psFile->cur.psArc = (AVCArc*)CPLCalloc(1, sizeof(AVCArc));
}
else if (psFile->eFileType == AVCFilePAL ||
psFile->eFileType == AVCFileRPL )
{
psFile->cur.psPal = (AVCPal*)CPLCalloc(1, sizeof(AVCPal));
}
else if (psFile->eFileType == AVCFileCNT)
{
psFile->cur.psCnt = (AVCCnt*)CPLCalloc(1, sizeof(AVCCnt));
}
else if (psFile->eFileType == AVCFileLAB)
{
psFile->cur.psLab = (AVCLab*)CPLCalloc(1, sizeof(AVCLab));
}
else if (psFile->eFileType == AVCFileTOL)
{
psFile->cur.psTol = (AVCTol*)CPLCalloc(1, sizeof(AVCTol));
}
else if (psFile->eFileType == AVCFileTXT ||
psFile->eFileType == AVCFileTX6)
{
psFile->cur.psTxt = (AVCTxt*)CPLCalloc(1, sizeof(AVCTxt));
}
else if (psFile->eFileType == AVCFileRXP)
{
psFile->cur.psRxp = (AVCRxp*)CPLCalloc(1, sizeof(AVCRxp));
}
else
{
CPLError(CE_Failure, CPLE_IllegalArg,
"%s: Unsupported file type or corrupted file.",
psFile->pszFilename);
CPLFree(psFile->pszFilename);
CPLFree(psFile);
psFile = NULL;
}
return psFile;
}
/**********************************************************************
* AVCBinReadClose()
*
* Close a coverage file, and release all memory (object strcut., buffers,
* etc.) associated with this file.
**********************************************************************/
void AVCBinReadClose(AVCBinFile *psFile)
{
AVCRawBinClose(psFile->psRawBinFile);
psFile->psRawBinFile = NULL;
CPLFree(psFile->pszFilename);
psFile->pszFilename = NULL;
if (psFile->hDBFFile)
DBFClose(psFile->hDBFFile);
if( psFile->psIndexFile != NULL )
AVCRawBinClose( psFile->psIndexFile );
if (psFile->eFileType == AVCFileARC)
{
if (psFile->cur.psArc)
CPLFree(psFile->cur.psArc->pasVertices);
CPLFree(psFile->cur.psArc);
}
else if (psFile->eFileType == AVCFilePAL ||
psFile->eFileType == AVCFileRPL )
{
if (psFile->cur.psPal)
CPLFree(psFile->cur.psPal->pasArcs);
CPLFree(psFile->cur.psPal);
}
else if (psFile->eFileType == AVCFileCNT)
{
if (psFile->cur.psCnt)
CPLFree(psFile->cur.psCnt->panLabelIds);
CPLFree(psFile->cur.psCnt);
}
else if (psFile->eFileType == AVCFileLAB)
{
CPLFree(psFile->cur.psLab);
}
else if (psFile->eFileType == AVCFileTOL)
{
CPLFree(psFile->cur.psTol);
}
else if (psFile->eFileType == AVCFilePRJ)
{
CSLDestroy(psFile->cur.papszPrj);
}
else if (psFile->eFileType == AVCFileTXT ||
psFile->eFileType == AVCFileTX6)
{
if (psFile->cur.psTxt)
{
CPLFree(psFile->cur.psTxt->pasVertices);
CPLFree(psFile->cur.psTxt->pszText);
}
CPLFree(psFile->cur.psTxt);
}
else if (psFile->eFileType == AVCFileRXP)
{
CPLFree(psFile->cur.psRxp);
}
else if (psFile->eFileType == AVCFileTABLE)
{
_AVCDestroyTableFields(psFile->hdr.psTableDef, psFile->cur.pasFields);
_AVCDestroyTableDef(psFile->hdr.psTableDef);
}
else
{
CPLError(CE_Failure, CPLE_IllegalArg,
"Unsupported file type or invalid file handle!");
}
CPLFree(psFile);
}
/**********************************************************************
* _AVCBinReadHeader()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadRewind() instead)
*
* Read the first 100 bytes header of the file and fill the AVCHeader
* structure.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadHeader(AVCRawBinFile *psFile, AVCBinHeader *psHeader,
AVCCoverType eCoverType)
{
int nStatus = 0;
/*-----------------------------------------------------------------
* For AVCCoverPC coverages (files without hte .adf extension),
* there is a first 256 bytes header that we just skip and that
* precedes the 100 bytes header block.
*
* In AVCCoverV7, we only have the 100 bytes header.
*----------------------------------------------------------------*/
if (eCoverType == AVCCoverPC)
AVCRawBinFSeek(psFile, 256, SEEK_SET);
else
AVCRawBinFSeek(psFile, 0, SEEK_SET);
psHeader->nSignature = AVCRawBinReadInt32(psFile);
if (AVCRawBinEOF(psFile))
nStatus = -1;
psHeader->nPrecision = AVCRawBinReadInt32(psFile);
psHeader->nRecordSize= AVCRawBinReadInt32(psFile);
/* Jump to 24th byte in header */
AVCRawBinFSeek(psFile, 12, SEEK_CUR);
psHeader->nLength = AVCRawBinReadInt32(psFile);
/*-----------------------------------------------------------------
* File length, in words (16 bits)... pass the info to the RawBinFile
* to prevent it from trying to read junk bytes at the end of files...
* this problem happens specially with PC Arc/Info files.
*----------------------------------------------------------------*/
if (eCoverType == AVCCoverPC)
AVCRawBinSetFileDataSize(psFile, psHeader->nLength*2 + 256);
else
AVCRawBinSetFileDataSize(psFile, psHeader->nLength*2 );
/* Move the pointer at the end of the 100 bytes header
*/
AVCRawBinFSeek(psFile, 72, SEEK_CUR);
return nStatus;
}
/**********************************************************************
* AVCBinReadRewind()
*
* Rewind the read pointer, and read/skip the header if necessary so
* that we are ready to read the data objects from the file after
* this call.
*
* Returns 0 on success, -1 on error, and -2 if file has an invalid
* signature and is possibly corrupted.
**********************************************************************/
int AVCBinReadRewind(AVCBinFile *psFile)
{
AVCBinHeader sHeader;
int nStatus=0;
/*-----------------------------------------------------------------
* For AVCCoverPC coverages, there is a first 256 bytes header
* that we just skip and that precedes the 100 bytes header block.
*
* In AVCCoverV7, AVCCoverPC2 and AVCCoverWeird, we only find the
* 100 bytes header.
*
* Note: it is the call to _AVCBinReadHeader() that takes care
* of skipping the first 256 bytes header if necessary.
*----------------------------------------------------------------*/
AVCRawBinFSeek(psFile->psRawBinFile, 0, SEEK_SET);
if ( psFile->eFileType == AVCFileARC ||
psFile->eFileType == AVCFilePAL ||
psFile->eFileType == AVCFileRPL ||
psFile->eFileType == AVCFileCNT ||
psFile->eFileType == AVCFileLAB ||
psFile->eFileType == AVCFileTXT ||
psFile->eFileType == AVCFileTX6 )
{
nStatus = _AVCBinReadHeader(psFile->psRawBinFile, &sHeader,
psFile->eCoverType);
/* Store the precision information inside the file handle.
*
* Of course, there had to be an exception...
* At least PAL and TXT files in PC Arc/Info coverages sometimes
* have a negative precision flag even if they contain single
* precision data... why is that???? A PC Arc bug?
*
* 2000-06-05: Found a double-precision PAL file with a signature
* of 1011 (should have been -11). So we'll assume
* that signature > 1000 also means double precision.
*/
if ((sHeader.nPrecision < 0 || sHeader.nPrecision > 1000) &&
psFile->eCoverType != AVCCoverPC)
psFile->nPrecision = AVC_DOUBLE_PREC;
else
psFile->nPrecision = AVC_SINGLE_PREC;
/* Validate the signature value... this will allow us to detect
* corrupted files or files that do not belong in the coverage.
*/
if (sHeader.nSignature != 9993 && sHeader.nSignature != 9994)
{
CPLError(CE_Warning, CPLE_AssertionFailed,
"%s appears to have an invalid file header.",
psFile->pszFilename);
return -2;
}
/* In Weird coverages, TXT files can be stored in the PC or the V7
* format. Look at the 'precision' field in the header to tell which
* type we have.
* Weird TXT in PC format: nPrecision = 16
* Weird TXT in V7 format: nPrecision = +/-67
* Use AVCFileTXT for PC type, and AVCFileTX6 for V7 type.
*/
if (psFile->eCoverType == AVCCoverWeird &&
psFile->eFileType == AVCFileTXT && ABS(sHeader.nPrecision) == 67)
{
/* TXT file will be processed as V7 TXT/TX6/TX7 */
psFile->eFileType = AVCFileTX6;
}
}
else if (psFile->eFileType == AVCFileTOL)
{
/*-------------------------------------------------------------
* For some reason, the tolerance files do not follow the
* general rules!
* Single precision "tol.adf" have no header
* Double precision "par.adf" have the usual 100 bytes header,
* but the 3rd field, which usually defines the precision has
* a positive value, even if the file is double precision!
*
* Also, we have a problem with PC Arc/Info TOL files since they
* do not contain the first 256 bytes header either... so we will
* just assume that double precision TOL files cannot exist in
* PC Arc/Info coverages... this should be OK.
*------------------------------------------------------------*/
int nSignature = 0;
nSignature = AVCRawBinReadInt32(psFile->psRawBinFile);
if (nSignature == 9993)
{
/* We have a double precision par.adf... read the 100 bytes
* header and set the precision information inside the file
* handle.
*/
nStatus = _AVCBinReadHeader(psFile->psRawBinFile, &sHeader,
psFile->eCoverType);
psFile->nPrecision = AVC_DOUBLE_PREC;
}
else
{
/* It's a single precision tol.adf ... just set the
* precision field.
*/
AVCRawBinFSeek(psFile->psRawBinFile, 0, SEEK_SET);
psFile->nPrecision = AVC_SINGLE_PREC;
}
}
return nStatus;
}
/**********************************************************************
* AVCBinReadObject()
*
* Read the object with a particular index. For fixed length record
* files we seek directly to the object. For variable files we try to
* get the offset from the corresponding index file.
*
* NOTE: Currently only implemented for ARC, PAL and TABLE files.
*
* Returns the read object on success or NULL on error.
**********************************************************************/
void *AVCBinReadObject(AVCBinFile *psFile, int iObjIndex )
{
int bIndexed = FALSE;
int nObjectOffset, nRecordSize=0, nRecordStart = 0, nLen;
char *pszExt = NULL;
if( iObjIndex < 0 )
return NULL;
/*-----------------------------------------------------------------
* Determine some information from based on the coverage type.
*----------------------------------------------------------------*/
nLen = strlen(psFile->pszFilename);
if( psFile->eFileType == AVCFileARC &&
((nLen>=3 && EQUALN((pszExt=psFile->pszFilename+nLen-3), "arc", 3)) ||
(nLen>=7 && EQUALN((pszExt=psFile->pszFilename+nLen-7),"arc.adf",7))))
{
bIndexed = TRUE;
}
else if( psFile->eFileType == AVCFilePAL &&
((nLen>=3 && EQUALN((pszExt=psFile->pszFilename+nLen-3), "pal", 3)) ||
(nLen>=7 && EQUALN((pszExt=psFile->pszFilename+nLen-7),"pal.adf",7))))
{
bIndexed = TRUE;
}
else if( psFile->eFileType == AVCFileTABLE )
{
bIndexed = FALSE;
nRecordSize = psFile->hdr.psTableDef->nRecSize;
nRecordStart = 0;
}
else
return NULL;
/*-----------------------------------------------------------------
* Ensure the index file is opened if an index file is required.
*----------------------------------------------------------------*/
if( bIndexed && psFile->psIndexFile == NULL )
{
char chOrig;
if( pszExt == NULL )
return NULL;
chOrig = pszExt[2];
if( chOrig > 'A' && chOrig < 'Z' )
pszExt[2] = 'X';
else
pszExt[2] = 'x';
psFile->psIndexFile =
AVCRawBinOpen( psFile->pszFilename, "rb",
psFile->psRawBinFile->eByteOrder,
psFile->psRawBinFile->psDBCSInfo);
pszExt[2] = chOrig;
if( psFile->psIndexFile == NULL )
return NULL;
}
/*-----------------------------------------------------------------
* Establish the offset to read the object from.
*----------------------------------------------------------------*/
if( bIndexed )
{
int nIndexOffset;
if (psFile->eCoverType == AVCCoverPC)
nIndexOffset = 356 + (iObjIndex-1)*8;
else
nIndexOffset = 100 + (iObjIndex-1)*8;
AVCRawBinFSeek( psFile->psIndexFile, nIndexOffset, SEEK_SET );
if( AVCRawBinEOF( psFile->psIndexFile ) )
return NULL;
nObjectOffset = AVCRawBinReadInt32( psFile->psIndexFile );
nObjectOffset *= 2;
if (psFile->eCoverType == AVCCoverPC)
nObjectOffset += 256;
}
else
nObjectOffset = nRecordStart + nRecordSize * (iObjIndex-1);
/*-----------------------------------------------------------------
* Seek to the start of the object in the data file.
*----------------------------------------------------------------*/
AVCRawBinFSeek( psFile->psRawBinFile, nObjectOffset, SEEK_SET );
if( AVCRawBinEOF( psFile->psRawBinFile ) )
return NULL;
/*-----------------------------------------------------------------
* Read and return the object.
*----------------------------------------------------------------*/
return AVCBinReadNextObject( psFile );
}
/**********************************************************************
* AVCBinReadNextObject()
*
* Read the next structure from the file. This function is just a generic
* cover on top of the AVCBinReadNextArc/Lab/Pal/Cnt() functions.
*
* Returns a (void*) to a static structure with the contents of the object
* that was read. The contents of the structure will be valid only until
* the next call.
* If you use the returned value, then make sure that you cast it to
* the right type for the current file! (AVCArc, AVCPal, AVCCnt, ...)
*
* Returns NULL if an error happened or if EOF was reached.
**********************************************************************/
void *AVCBinReadNextObject(AVCBinFile *psFile)
{
void *psObj = NULL;
switch(psFile->eFileType)
{
case AVCFileARC:
psObj = (void*)AVCBinReadNextArc(psFile);
break;
case AVCFilePAL:
case AVCFileRPL:
psObj = (void*)AVCBinReadNextPal(psFile);
break;
case AVCFileCNT:
psObj = (void*)AVCBinReadNextCnt(psFile);
break;
case AVCFileLAB:
psObj = (void*)AVCBinReadNextLab(psFile);
break;
case AVCFileTOL:
psObj = (void*)AVCBinReadNextTol(psFile);
break;
case AVCFileTXT:
case AVCFileTX6:
psObj = (void*)AVCBinReadNextTxt(psFile);
break;
case AVCFileRXP:
psObj = (void*)AVCBinReadNextRxp(psFile);
break;
case AVCFileTABLE:
psObj = (void*)AVCBinReadNextTableRec(psFile);
break;
default:
CPLError(CE_Failure, CPLE_IllegalArg,
"AVCBinReadNextObject(): Unsupported file type!");
}
return psObj;
}
/**********************************************************************
* AVCBinReadNextTableRec()
*
* Reads the next record from an attribute table.
*
* Returns a pointer to an array of static AVCField structure whose
* contents will be valid only until the next call,
* or NULL if an error happened or if EOF was reached.
**********************************************************************/
AVCField *AVCBinReadNextTableRec(AVCBinFile *psFile)
{
if (psFile->eCoverType != AVCCoverPC &&
psFile->eCoverType != AVCCoverPC2 &&
psFile->eFileType == AVCFileTABLE &&
psFile->hdr.psTableDef->numRecords > 0 &&
! AVCRawBinEOF(psFile->psRawBinFile) &&
_AVCBinReadNextTableRec(psFile->psRawBinFile,
psFile->hdr.psTableDef->numFields,
psFile->hdr.psTableDef->pasFieldDef,
psFile->cur.pasFields,
psFile->hdr.psTableDef->nRecSize) == 0 )
{
return psFile->cur.pasFields;
}
else if ((psFile->eCoverType == AVCCoverPC ||
psFile->eCoverType == AVCCoverPC2 ) &&
psFile->eFileType == AVCFileTABLE &&
psFile->hdr.psTableDef->numRecords > 0 &&
_AVCBinReadNextDBFTableRec(psFile->hDBFFile,
&(psFile->nCurDBFRecord),
psFile->hdr.psTableDef->numFields,
psFile->hdr.psTableDef->pasFieldDef,
psFile->cur.pasFields) == 0)
{
return psFile->cur.pasFields;
}
return NULL;
}
/*=====================================================================
* ARC
*====================================================================*/
/**********************************************************************
* _AVCBinReadNextArc()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextArc() instead)
*
* Read the next Arc structure from the file.
*
* The contents of the psArc structure is assumed to be valid, and the
* psArc->pasVertices buffer may be reallocated or free()'d if it is not
* NULL.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextArc(AVCRawBinFile *psFile, AVCArc *psArc,
int nPrecision)
{
int i, numVertices;
int nRecordSize, nStartPos, nBytesRead;
psArc->nArcId = AVCRawBinReadInt32(psFile);
if (AVCRawBinEOF(psFile))
return -1;
nRecordSize = AVCRawBinReadInt32(psFile) * 2;
nStartPos = psFile->nCurPos+psFile->nOffset;
psArc->nUserId = AVCRawBinReadInt32(psFile);
psArc->nFNode = AVCRawBinReadInt32(psFile);
psArc->nTNode = AVCRawBinReadInt32(psFile);
psArc->nLPoly = AVCRawBinReadInt32(psFile);
psArc->nRPoly = AVCRawBinReadInt32(psFile);
numVertices = AVCRawBinReadInt32(psFile);
/* Realloc the vertices array only if it needs to grow...
* do not realloc to a smaller size.
* Note that for simplicity reasons, we always store the vertices as
* double values in memory, even for single precision coverages.
*/
if (psArc->pasVertices == NULL || numVertices > psArc->numVertices)
psArc->pasVertices = (AVCVertex*)CPLRealloc(psArc->pasVertices,
numVertices*sizeof(AVCVertex));
psArc->numVertices = numVertices;
if (nPrecision == AVC_SINGLE_PREC)
{
for(i=0; i<numVertices; i++)
{
psArc->pasVertices[i].x = AVCRawBinReadFloat(psFile);
psArc->pasVertices[i].y = AVCRawBinReadFloat(psFile);
}
}
else
{
for(i=0; i<numVertices; i++)
{
psArc->pasVertices[i].x = AVCRawBinReadDouble(psFile);
psArc->pasVertices[i].y = AVCRawBinReadDouble(psFile);
}
}
/*-----------------------------------------------------------------
* Record size may be larger than number of vertices. Skip up to
* start of next object.
*----------------------------------------------------------------*/
nBytesRead = (psFile->nCurPos + psFile->nOffset) - nStartPos;
if ( nBytesRead < nRecordSize)
AVCRawBinFSeek(psFile, nRecordSize - nBytesRead, SEEK_CUR);
return 0;
}
/**********************************************************************
* AVCBinReadNextArc()
*
* Read the next Arc structure from the file.
*
* Returns a pointer to a static AVCArc structure whose contents will be
* valid only until the next call or NULL if an error happened or if EOF
* was reached.
**********************************************************************/
AVCArc *AVCBinReadNextArc(AVCBinFile *psFile)
{
if (psFile->eFileType != AVCFileARC ||
AVCRawBinEOF(psFile->psRawBinFile) ||
_AVCBinReadNextArc(psFile->psRawBinFile, psFile->cur.psArc,
psFile->nPrecision) !=0)
{
return NULL;
}
return psFile->cur.psArc;
}
/*=====================================================================
* PAL
*====================================================================*/
/**********************************************************************
* _AVCBinReadNextPal()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextPal() instead)
*
* Read the next PAL (Polygon Arc List) structure from the file.
*
* The contents of the psPal structure is assumed to be valid, and the
* psPal->paVertices buffer may be reallocated or free()'d if it is not
* NULL.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextPal(AVCRawBinFile *psFile, AVCPal *psPal,
int nPrecision)
{
int i, numArcs;
int nRecordSize, nStartPos, nBytesRead;
psPal->nPolyId = AVCRawBinReadInt32(psFile);
nRecordSize = AVCRawBinReadInt32(psFile) * 2;
nStartPos = psFile->nCurPos+psFile->nOffset;
if (AVCRawBinEOF(psFile))
return -1;
if (nPrecision == AVC_SINGLE_PREC)
{
psPal->sMin.x = AVCRawBinReadFloat(psFile);
psPal->sMin.y = AVCRawBinReadFloat(psFile);
psPal->sMax.x = AVCRawBinReadFloat(psFile);
psPal->sMax.y = AVCRawBinReadFloat(psFile);
}
else
{
psPal->sMin.x = AVCRawBinReadDouble(psFile);
psPal->sMin.y = AVCRawBinReadDouble(psFile);
psPal->sMax.x = AVCRawBinReadDouble(psFile);
psPal->sMax.y = AVCRawBinReadDouble(psFile);
}
numArcs = AVCRawBinReadInt32(psFile);
/* Realloc the arc list array only if it needs to grow...
* do not realloc to a smaller size.
*/
if (psPal->pasArcs == NULL || numArcs > psPal->numArcs)
psPal->pasArcs = (AVCPalArc*)CPLRealloc(psPal->pasArcs,
numArcs*sizeof(AVCPalArc));
psPal->numArcs = numArcs;
for(i=0; i<numArcs; i++)
{
psPal->pasArcs[i].nArcId = AVCRawBinReadInt32(psFile);
psPal->pasArcs[i].nFNode = AVCRawBinReadInt32(psFile);
psPal->pasArcs[i].nAdjPoly = AVCRawBinReadInt32(psFile);
}
/*-----------------------------------------------------------------
* Record size may be larger than number of vertices. Skip up to
* start of next object.
*----------------------------------------------------------------*/
nBytesRead = (psFile->nCurPos + psFile->nOffset) - nStartPos;
if ( nBytesRead < nRecordSize)
AVCRawBinFSeek(psFile, nRecordSize - nBytesRead, SEEK_CUR);
return 0;
}
/**********************************************************************
* AVCBinReadNextPal()
*
* Read the next PAL structure from the file.
*
* Returns a pointer to a static AVCPal structure whose contents will be
* valid only until the next call or NULL if an error happened or if EOF
* was reached.
**********************************************************************/
AVCPal *AVCBinReadNextPal(AVCBinFile *psFile)
{
if ((psFile->eFileType!=AVCFilePAL && psFile->eFileType!=AVCFileRPL) ||
AVCRawBinEOF(psFile->psRawBinFile) ||
_AVCBinReadNextPal(psFile->psRawBinFile, psFile->cur.psPal,
psFile->nPrecision) !=0)
{
return NULL;
}
return psFile->cur.psPal;
}
/*=====================================================================
* CNT
*====================================================================*/
/**********************************************************************
* _AVCBinReadNextCnt()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextCnt() instead)
*
* Read the next CNT (Polygon Centroid) structure from the file.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextCnt(AVCRawBinFile *psFile, AVCCnt *psCnt,
int nPrecision)
{
int i, numLabels;
int nRecordSize, nStartPos, nBytesRead;
psCnt->nPolyId = AVCRawBinReadInt32(psFile);
nRecordSize = AVCRawBinReadInt32(psFile) * 2;
nStartPos = psFile->nCurPos+psFile->nOffset;
if (AVCRawBinEOF(psFile))
return -1;
if (nPrecision == AVC_SINGLE_PREC)
{
psCnt->sCoord.x = AVCRawBinReadFloat(psFile);
psCnt->sCoord.y = AVCRawBinReadFloat(psFile);
}
else
{
psCnt->sCoord.x = AVCRawBinReadDouble(psFile);
psCnt->sCoord.y = AVCRawBinReadDouble(psFile);
}
numLabels = AVCRawBinReadInt32(psFile);
/* Realloc the LabelIds array only if it needs to grow...
* do not realloc to a smaller size.
*/
if (psCnt->panLabelIds == NULL || numLabels > psCnt->numLabels)
psCnt->panLabelIds = (GInt32 *)CPLRealloc(psCnt->panLabelIds,
numLabels*sizeof(GInt32));
psCnt->numLabels = numLabels;
for(i=0; i<numLabels; i++)
{
psCnt->panLabelIds[i] = AVCRawBinReadInt32(psFile);
}
/*-----------------------------------------------------------------
* Record size may be larger than number of vertices. Skip up to
* start of next object.
*----------------------------------------------------------------*/
nBytesRead = (psFile->nCurPos + psFile->nOffset) - nStartPos;
if ( nBytesRead < nRecordSize)
AVCRawBinFSeek(psFile, nRecordSize - nBytesRead, SEEK_CUR);
return 0;
}
/**********************************************************************
* AVCBinReadNextCnt()
*
* Read the next CNT structure from the file.
*
* Returns a pointer to a static AVCCnt structure whose contents will be
* valid only until the next call or NULL if an error happened or if EOF
* was reached.
**********************************************************************/
AVCCnt *AVCBinReadNextCnt(AVCBinFile *psFile)
{
if (psFile->eFileType != AVCFileCNT ||
AVCRawBinEOF(psFile->psRawBinFile) ||
_AVCBinReadNextCnt(psFile->psRawBinFile, psFile->cur.psCnt,
psFile->nPrecision) !=0)
{
return NULL;
}
return psFile->cur.psCnt;
}
/*=====================================================================
* LAB
*====================================================================*/
/**********************************************************************
* _AVCBinReadNextLab()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextLab() instead)
*
* Read the next LAB (Centroid Label) structure from the file.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextLab(AVCRawBinFile *psFile, AVCLab *psLab,
int nPrecision)
{
psLab->nValue = AVCRawBinReadInt32(psFile);
psLab->nPolyId = AVCRawBinReadInt32(psFile);
if (AVCRawBinEOF(psFile))
return -1;
if (nPrecision == AVC_SINGLE_PREC)
{
psLab->sCoord1.x = AVCRawBinReadFloat(psFile);
psLab->sCoord1.y = AVCRawBinReadFloat(psFile);
psLab->sCoord2.x = AVCRawBinReadFloat(psFile);
psLab->sCoord2.y = AVCRawBinReadFloat(psFile);
psLab->sCoord3.x = AVCRawBinReadFloat(psFile);
psLab->sCoord3.y = AVCRawBinReadFloat(psFile);
}
else
{
psLab->sCoord1.x = AVCRawBinReadDouble(psFile);
psLab->sCoord1.y = AVCRawBinReadDouble(psFile);
psLab->sCoord2.x = AVCRawBinReadDouble(psFile);
psLab->sCoord2.y = AVCRawBinReadDouble(psFile);
psLab->sCoord3.x = AVCRawBinReadDouble(psFile);
psLab->sCoord3.y = AVCRawBinReadDouble(psFile);
}
return 0;
}
/**********************************************************************
* AVCBinReadNextLab()
*
* Read the next LAB structure from the file.
*
* Returns a pointer to a static AVCLab structure whose contents will be
* valid only until the next call or NULL if an error happened or if EOF
* was reached.
**********************************************************************/
AVCLab *AVCBinReadNextLab(AVCBinFile *psFile)
{
if (psFile->eFileType != AVCFileLAB ||
AVCRawBinEOF(psFile->psRawBinFile) ||
_AVCBinReadNextLab(psFile->psRawBinFile, psFile->cur.psLab,
psFile->nPrecision) !=0)
{
return NULL;
}
return psFile->cur.psLab;
}
/*=====================================================================
* TOL
*====================================================================*/
/**********************************************************************
* _AVCBinReadNextTol()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextTol() instead)
*
* Read the next TOL (tolerance) structure from the file.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextTol(AVCRawBinFile *psFile, AVCTol *psTol,
int nPrecision)
{
psTol->nIndex = AVCRawBinReadInt32(psFile);
psTol->nFlag = AVCRawBinReadInt32(psFile);
if (AVCRawBinEOF(psFile))
return -1;
if (nPrecision == AVC_SINGLE_PREC)
{
psTol->dValue = AVCRawBinReadFloat(psFile);
}
else
{
psTol->dValue = AVCRawBinReadDouble(psFile);
}
return 0;
}
/**********************************************************************
* AVCBinReadNextTol()
*
* Read the next TOL structure from the file.
*
* Returns a pointer to a static AVCTol structure whose contents will be
* valid only until the next call or NULL if an error happened or if EOF
* was reached.
**********************************************************************/
AVCTol *AVCBinReadNextTol(AVCBinFile *psFile)
{
if (psFile->eFileType != AVCFileTOL ||
AVCRawBinEOF(psFile->psRawBinFile) ||
_AVCBinReadNextTol(psFile->psRawBinFile, psFile->cur.psTol,
psFile->nPrecision) !=0)
{
return NULL;
}
return psFile->cur.psTol;
}
/*=====================================================================
* PRJ
*====================================================================*/
/**********************************************************************
* _AVCBinReadOpenPrj()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadOpen() with type AVCFilePRJ instead)
*
* Open a PRJ file.
*
* This call will actually read the whole PRJ file in memory since PRJ
* files are small text files.
**********************************************************************/
AVCBinFile *_AVCBinReadOpenPrj(const char *pszPath, const char *pszName)
{
AVCBinFile *psFile;
char *pszFname, **papszPrj;
/*-----------------------------------------------------------------
* Load the PRJ file contents into a stringlist.
*----------------------------------------------------------------*/
pszFname = (char*)CPLMalloc((strlen(pszPath)+strlen(pszName)+1)*
sizeof(char));
sprintf(pszFname, "%s%s", pszPath, pszName);
papszPrj = CSLLoad(pszFname);
CPLFree(pszFname);
if (papszPrj == NULL)
{
/* Failed to open file... just return NULL since an error message
* has already been issued by CSLLoad()
*/
return NULL;
}
/*-----------------------------------------------------------------
* Alloc and init the AVCBinFile handle.
*----------------------------------------------------------------*/
psFile = (AVCBinFile*)CPLCalloc(1, sizeof(AVCBinFile));
psFile->eFileType = AVCFilePRJ;
psFile->psRawBinFile = NULL;
psFile->cur.papszPrj = papszPrj;
psFile->pszFilename = NULL;
return psFile;
}
/**********************************************************************
* AVCBinReadPrj()
*
* Return the contents of the previously opened PRJ (projection) file.
*
* PRJ files are simple text files with variable length lines, so we
* don't use the AVCRawBin*() functions for this case.
*
* Returns a reference to a static stringlist with the whole file
* contents, or NULL in case of error.
*
* The returned stringlist should NOT be freed by the caller.
**********************************************************************/
char **AVCBinReadNextPrj(AVCBinFile *psFile)
{
/*-----------------------------------------------------------------
* The file should have already been loaded by AVCBinFileOpen(),
* so there is not much to do here!
*----------------------------------------------------------------*/
return psFile->cur.papszPrj;
}
/*=====================================================================
* TXT/TX6/TX7
*====================================================================*/
/**********************************************************************
* _AVCBinReadNextTxt()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextTxt() instead)
*
* Read the next TXT/TX6/TX7 (Annotation) structure from the file.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextTxt(AVCRawBinFile *psFile, AVCTxt *psTxt,
int nPrecision)
{
int i, numVerticesBefore, numVertices, numCharsToRead, nRecordSize;
int numBytesRead;
numVerticesBefore = ABS(psTxt->numVerticesLine) +
ABS(psTxt->numVerticesArrow);
psTxt->nTxtId = AVCRawBinReadInt32(psFile);
if (AVCRawBinEOF(psFile))
return -1;
nRecordSize = 8 + 2*AVCRawBinReadInt32(psFile);
psTxt->nUserId = AVCRawBinReadInt32(psFile);
psTxt->nLevel = AVCRawBinReadInt32(psFile);
psTxt->f_1e2 = AVCRawBinReadFloat(psFile);
psTxt->nSymbol = AVCRawBinReadInt32(psFile);
psTxt->numVerticesLine = AVCRawBinReadInt32(psFile);
psTxt->n28 = AVCRawBinReadInt32(psFile);
psTxt->numChars = AVCRawBinReadInt32(psFile);
psTxt->numVerticesArrow = AVCRawBinReadInt32(psFile);
for(i=0; i<20; i++)
{
psTxt->anJust1[i] = AVCRawBinReadInt16(psFile);
}
for(i=0; i<20; i++)
{
psTxt->anJust2[i] = AVCRawBinReadInt16(psFile);
}
if (nPrecision == AVC_SINGLE_PREC)
{
psTxt->dHeight = AVCRawBinReadFloat(psFile);
psTxt->dV2 = AVCRawBinReadFloat(psFile);
psTxt->dV3 = AVCRawBinReadFloat(psFile);
}
else
{
psTxt->dHeight = AVCRawBinReadDouble(psFile);
psTxt->dV2 = AVCRawBinReadDouble(psFile);
psTxt->dV3 = AVCRawBinReadDouble(psFile);
}
numCharsToRead = ((int)(psTxt->numChars + 3)/4)*4;
if (psTxt->pszText == NULL ||
((int)(strlen(psTxt->pszText)+3)/4)*4 < numCharsToRead )
{
psTxt->pszText = (char*)CPLRealloc(psTxt->pszText,
(numCharsToRead+1)*sizeof(char));
}
AVCRawBinReadString(psFile, numCharsToRead, psTxt->pszText);
psTxt->pszText[psTxt->numChars] = '\0';
/* Realloc the vertices array only if it needs to grow...
* do not realloc to a smaller size.
*/
numVertices = ABS(psTxt->numVerticesLine) + ABS(psTxt->numVerticesArrow);
if (psTxt->pasVertices == NULL || numVertices > numVerticesBefore)
psTxt->pasVertices = (AVCVertex*)CPLRealloc(psTxt->pasVertices,
numVertices*sizeof(AVCVertex));
if (nPrecision == AVC_SINGLE_PREC)
{
for(i=0; i<numVertices; i++)
{
psTxt->pasVertices[i].x = AVCRawBinReadFloat(psFile);
psTxt->pasVertices[i].y = AVCRawBinReadFloat(psFile);
}
}
else
{
for(i=0; i<numVertices; i++)
{
psTxt->pasVertices[i].x = AVCRawBinReadDouble(psFile);
psTxt->pasVertices[i].y = AVCRawBinReadDouble(psFile);
}
}
/* In V7 Coverages, we always have 8 bytes of junk at end of record.
* In Weird coverages, these 8 bytes are sometimes present, and
* sometimes not!!! (Probably another AI "random feature"! ;-)
* So we use the record size to establish if there is any junk to skip
*/
if (nPrecision == AVC_SINGLE_PREC)
numBytesRead = 132 + numCharsToRead + numVertices * 2 * 4;
else
numBytesRead = 144 + numCharsToRead + numVertices * 2 * 8;
if (numBytesRead < nRecordSize)
AVCRawBinFSeek(psFile, nRecordSize - numBytesRead, SEEK_CUR);
return 0;
}
/**********************************************************************
* _AVCBinReadNextPCCoverageTxt()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextTxt() instead)
*
* Read the next TXT (Annotation) structure from a PC Coverage file.
* Note that it is assumed that PC Coverage files are always single
* precision.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextPCCoverageTxt(AVCRawBinFile *psFile, AVCTxt *psTxt,
int nPrecision)
{
int i, numVerticesBefore, numVertices, numCharsToRead, nRecordSize;
numVerticesBefore = ABS(psTxt->numVerticesLine) +
ABS(psTxt->numVerticesArrow);
psTxt->nTxtId = AVCRawBinReadInt32(psFile);
if (AVCRawBinEOF(psFile))
return -1;
nRecordSize = 8 + 2*AVCRawBinReadInt32(psFile);
psTxt->nUserId = 0;
psTxt->nLevel = AVCRawBinReadInt32(psFile);
psTxt->numVerticesLine = AVCRawBinReadInt32(psFile);
/* We are not expecting more than 4 vertices */
psTxt->numVerticesLine = MIN(psTxt->numVerticesLine, 4);
psTxt->numVerticesArrow = 0;
/* Realloc the vertices array only if it needs to grow...
* do not realloc to a smaller size.
*
* Note that because of the way V7 binary TXT files work, the rest of the
* lib expects to receive duplicate coords for the first vertex, so
* we have to include an additional vertex for that.
*/
psTxt->numVerticesLine += 1;
numVertices = ABS(psTxt->numVerticesLine) + ABS(psTxt->numVerticesArrow);
if (psTxt->pasVertices == NULL || numVertices > numVerticesBefore)
psTxt->pasVertices = (AVCVertex*)CPLRealloc(psTxt->pasVertices,
numVertices*sizeof(AVCVertex));
for(i=1; i<numVertices; i++)
{
if (nPrecision == AVC_SINGLE_PREC)
{
psTxt->pasVertices[i].x = AVCRawBinReadFloat(psFile);
psTxt->pasVertices[i].y = AVCRawBinReadFloat(psFile);
}
else
{
psTxt->pasVertices[i].x = AVCRawBinReadDouble(psFile);
psTxt->pasVertices[i].y = AVCRawBinReadDouble(psFile);
}
}
/* Duplicate the first vertex because that's the way the other binary TXT
* files work and that's what the lib expects to generate the E00.
*/
psTxt->pasVertices[0].x = psTxt->pasVertices[1].x;
psTxt->pasVertices[0].y = psTxt->pasVertices[1].y;
/* Skip the other floats (vertices) that are unused */
if (nPrecision == AVC_SINGLE_PREC)
AVCRawBinFSeek(psFile, 4*(15-2*(numVertices-1)) , SEEK_CUR);
else
AVCRawBinFSeek(psFile, 8*(15-2*(numVertices-1)) , SEEK_CUR);
if (nPrecision == AVC_SINGLE_PREC)
{
psTxt->dHeight = AVCRawBinReadFloat(psFile);
}
else
{
psTxt->dHeight = AVCRawBinReadDouble(psFile);
}
psTxt->f_1e2 = AVCRawBinReadFloat(psFile);
psTxt->nSymbol = AVCRawBinReadInt32(psFile);
psTxt->numChars = AVCRawBinReadInt32(psFile);
/* In some cases, we may need to skip additional spaces after the
* text string... more than should be required to simply align with
* a 4 bytes boundary... include that in numCharsToRead
*/
if (nPrecision == AVC_SINGLE_PREC)
{
numCharsToRead = nRecordSize - (28 + 16*4);
}
else
{
numCharsToRead = nRecordSize - (28 + 16*8);
}
/* Do a quick check in case file is corrupt! */
psTxt->numChars = MIN(psTxt->numChars, numCharsToRead);
if (psTxt->pszText == NULL ||
((int)(strlen(psTxt->pszText)+3)/4)*4 < numCharsToRead )
{
psTxt->pszText = (char*)CPLRealloc(psTxt->pszText,
(numCharsToRead+5)*sizeof(char));
}
AVCRawBinReadString(psFile, numCharsToRead, psTxt->pszText);
psTxt->pszText[psTxt->numChars] = '\0';
/* Set unused members to default values...
*/
psTxt->dV2 = 0.0;
psTxt->dV3 = 0.0;
psTxt->n28 = 0;
for(i=0; i<20; i++)
{
psTxt->anJust1[i] = 0;
psTxt->anJust2[i] = 0;
}
return 0;
}
/**********************************************************************
* AVCBinReadNextTxt()
*
* Read the next TXT/TX6/TX7 structure from the file.
*
* Returns a pointer to a static AVCTxt structure whose contents will be
* valid only until the next call or NULL if an error happened or if EOF
* was reached.
**********************************************************************/
AVCTxt *AVCBinReadNextTxt(AVCBinFile *psFile)
{
int nStatus = 0;
if ((psFile->eFileType != AVCFileTXT && psFile->eFileType != AVCFileTX6) ||
AVCRawBinEOF(psFile->psRawBinFile) )
{
return NULL;
}
/* AVCCoverPC have a different TXT format than AVCCoverV7
*
* Note: Some Weird coverages use the PC TXT structure, and some use the
* V7 structure. We distinguish them using the header's precision
* field in AVCBinReadRewind().
*/
if (psFile->eFileType == AVCFileTXT &&
(psFile->eCoverType == AVCCoverPC ||
psFile->eCoverType == AVCCoverWeird) )
{
/* TXT file in PC Coverages (and some Weird Coverages)
*/
nStatus = _AVCBinReadNextPCCoverageTxt(psFile->psRawBinFile,
psFile->cur.psTxt,
psFile->nPrecision);
}
else
{
/* TXT in V7 Coverages (and some Weird Coverages), and TX6/TX7 in
* all coverage types
*/
nStatus = _AVCBinReadNextTxt(psFile->psRawBinFile, psFile->cur.psTxt,
psFile->nPrecision);
}
if (nStatus != 0)
{
return NULL;
}
return psFile->cur.psTxt;
}
/*=====================================================================
* RXP
*====================================================================*/
/**********************************************************************
* _AVCBinReadNextRxp()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextRxp() instead)
*
* Read the next RXP (Region something...) structure from the file.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextRxp(AVCRawBinFile *psFile, AVCRxp *psRxp,
int nPrecision)
{
psRxp->n1 = AVCRawBinReadInt32(psFile);
if (AVCRawBinEOF(psFile))
return -1;
psRxp->n2 = AVCRawBinReadInt32(psFile);
return 0;
}
/**********************************************************************
* AVCBinReadNextRxp()
*
* Read the next RXP structure from the file.
*
* Returns a pointer to a static AVCRxp structure whose contents will be
* valid only until the next call or NULL if an error happened or if EOF
* was reached.
**********************************************************************/
AVCRxp *AVCBinReadNextRxp(AVCBinFile *psFile)
{
if (psFile->eFileType != AVCFileRXP ||
AVCRawBinEOF(psFile->psRawBinFile) ||
_AVCBinReadNextRxp(psFile->psRawBinFile, psFile->cur.psRxp,
psFile->nPrecision) !=0)
{
return NULL;
}
return psFile->cur.psRxp;
}
/*=====================================================================
* NATIVE (V7.x) TABLEs
*
* Note: Also applies to AVCCoverWeird
*====================================================================*/
/**********************************************************************
* _AVCBinReadNextArcDir()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadOpen() with type AVCFileTABLE instead)
*
* Read the next record from an arc.dir (or "arcdr9") file.
*
* Note that arc.dir files have no header... they start with the
* first record immediately.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextArcDir(AVCRawBinFile *psFile, AVCTableDef *psArcDir)
{
int i;
/* Arc/Info Table name
*/
AVCRawBinReadString(psFile, 32, psArcDir->szTableName);
psArcDir->szTableName[32] = '\0';
if (AVCRawBinEOF(psFile))
return -1;
/* "ARC####" basename for .DAT and .NIT files
*/
AVCRawBinReadString(psFile, 8, psArcDir->szInfoFile);
psArcDir->szInfoFile[7] = '\0';
for (i=6; i>0 && psArcDir->szInfoFile[i]==' '; i--)
psArcDir->szInfoFile[i] = '\0';
psArcDir->numFields = AVCRawBinReadInt16(psFile);
psArcDir->nRecSize = AVCRawBinReadInt16(psFile);
AVCRawBinFSeek(psFile, 18, SEEK_CUR); /* Skip 18 bytes */
psArcDir->bDeletedFlag = AVCRawBinReadInt16(psFile);
psArcDir->numRecords = AVCRawBinReadInt32(psFile);
AVCRawBinFSeek(psFile, 10, SEEK_CUR); /* Skip 10 bytes */
AVCRawBinReadBytes(psFile, 2, psArcDir->szExternal);
psArcDir->szExternal[2] = '\0';
AVCRawBinFSeek(psFile, 300, SEEK_CUR); /* Skip the remaining 300 bytes */
return 0;
}
/**********************************************************************
* _AVCBinReadNextNit()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadOpen() with type AVCFileTABLE instead)
*
* Read the next record from an arc####.nit file.
*
* Note that arc####.nit files have no header... they start with the
* first record immediately.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextArcNit(AVCRawBinFile *psFile, AVCFieldInfo *psField)
{
AVCRawBinReadString(psFile, 16, psField->szName);
psField->szName[16] = '\0';
if (AVCRawBinEOF(psFile))
return -1;
psField->nSize = AVCRawBinReadInt16(psFile);
psField->v2 = AVCRawBinReadInt16(psFile); /* Always -1 ? */
psField->nOffset = AVCRawBinReadInt16(psFile);
psField->v4 = AVCRawBinReadInt16(psFile); /* Always 4 ? */
psField->v5 = AVCRawBinReadInt16(psFile); /* Always -1 ? */
psField->nFmtWidth = AVCRawBinReadInt16(psFile);
psField->nFmtPrec = AVCRawBinReadInt16(psFile);
psField->nType1 = AVCRawBinReadInt16(psFile);
psField->nType2 = AVCRawBinReadInt16(psFile); /* Always 0 ? */
psField->v10 = AVCRawBinReadInt16(psFile); /* Always -1 ? */
psField->v11 = AVCRawBinReadInt16(psFile); /* Always -1 ? */
psField->v12 = AVCRawBinReadInt16(psFile); /* Always -1 ? */
psField->v13 = AVCRawBinReadInt16(psFile); /* Always -1 ? */
AVCRawBinReadString(psFile, 16, psField->szAltName); /* Always Blank ? */
psField->szAltName[16] = '\0';
AVCRawBinFSeek(psFile, 56, SEEK_CUR); /* Skip 56 bytes */
psField->nIndex = AVCRawBinReadInt16(psFile);
AVCRawBinFSeek(psFile, 28, SEEK_CUR); /* Skip the remaining 28 bytes */
return 0;
}
/**********************************************************************
* _AVCBinReadGetInfoFilename()
*
* Look for the DAT or NIT files for a given table... returns TRUE if
* they exist, or FALSE otherwise.
*
* If pszRetFnmae/pszRetNitFile != NULL then the filename with full path
* will be copied to the specified buffer.
**********************************************************************/
GBool _AVCBinReadGetInfoFilename(const char *pszInfoPath,
const char *pszBasename,
const char *pszDatOrNit,
AVCCoverType eCoverType,
char *pszRetFname)
{
GBool bFilesExist = FALSE;
char *pszBuf = NULL;
VSIStatBuf sStatBuf;
if (pszRetFname)
pszBuf = pszRetFname;
else
pszBuf = (char*)CPLMalloc((strlen(pszInfoPath)+strlen(pszBasename)+10)*
sizeof(char));
if (eCoverType == AVCCoverWeird)
{
sprintf(pszBuf, "%s%s%s", pszInfoPath, pszBasename, pszDatOrNit);
}
else
{
sprintf(pszBuf, "%s%s.%s", pszInfoPath, pszBasename, pszDatOrNit);
}
AVCAdjustCaseSensitiveFilename(pszBuf);
if (VSIStat(pszBuf, &sStatBuf) == 0)
bFilesExist = TRUE;
if (eCoverType == AVCCoverWeird && !bFilesExist)
{
/* In some cases, the filename can be truncated to 8 chars
* and we end up with "ARC000DA"... check that possibility.
*/
pszBuf[strlen(pszBuf)-1] = '\0';
AVCAdjustCaseSensitiveFilename(pszBuf);
if (VSIStat(pszBuf, &sStatBuf) == 0)
bFilesExist = TRUE;
}
if (pszRetFname == NULL)
CPLFree(pszBuf);
return bFilesExist;
}
/**********************************************************************
* _AVCBinReadInfoFilesExist()
*
* Look for the DAT and NIT files for a given table... returns TRUE if
* they exist, or FALSE otherwise.
*
* If pszRetDatFile/pszRetNitFile != NULL then the .DAT and .NIT filename
* without the info path will be copied to the specified buffers.
**********************************************************************/
GBool _AVCBinReadInfoFileExists(const char *pszInfoPath,
const char *pszBasename,
AVCCoverType eCoverType)
{
return (_AVCBinReadGetInfoFilename(pszInfoPath, pszBasename,
"dat", eCoverType, NULL) == TRUE &&
_AVCBinReadGetInfoFilename(pszInfoPath, pszBasename,
"nit", eCoverType, NULL) == TRUE);
}
/**********************************************************************
* AVCBinReadListTables()
*
* Scan the arc.dir file and return stringlist with one entry for the
* Arc/Info name of each table that belongs to the specified coverage.
* Pass pszCoverName = NULL to get the list of all tables.
*
* ppapszArcDatFiles if not NULL will be set to point to a stringlist
* with the corresponding "ARC????" info file basenames corresponding
* to each table found.
*
* Note that arc.dir files have no header... they start with the
* first record immediately.
*
* In AVCCoverWeird, the file is called "arcdr9"
*
* Returns a stringlist that should be deallocated by the caller
* with CSLDestroy(), or NULL on error.
**********************************************************************/
char **AVCBinReadListTables(const char *pszInfoPath, const char *pszCoverName,
char ***ppapszArcDatFiles, AVCCoverType eCoverType,
AVCDBCSInfo *psDBCSInfo)
{
char **papszList = NULL;
char *pszFname;
char szNameToFind[33] = "";
int nLen;
AVCRawBinFile *hFile;
AVCTableDef sEntry;
if (ppapszArcDatFiles)
*ppapszArcDatFiles = NULL;
/*-----------------------------------------------------------------
* For AVCCoverV7Tables type we do not look for tables for a specific
* coverage, we return all tables from the info dir.
*----------------------------------------------------------------*/
if (eCoverType == AVCCoverV7Tables)
pszCoverName = NULL;
/*-----------------------------------------------------------------
* All tables that belong to a given coverage have their name starting
* with the coverage name (in uppercase letters), followed by a 3
* letters extension.
*----------------------------------------------------------------*/
if (pszCoverName != NULL)
sprintf(szNameToFind, "%-.28s.", pszCoverName);
nLen = strlen(szNameToFind);
/*-----------------------------------------------------------------
* Open the arc.dir and add all entries that match the criteria
* to our list.
* In AVCCoverWeird, the file is called "arcdr9"
*----------------------------------------------------------------*/
pszFname = (char*)CPLMalloc((strlen(pszInfoPath)+9)*sizeof(char));
if (eCoverType == AVCCoverWeird)
sprintf(pszFname, "%sarcdr9", pszInfoPath);
else
sprintf(pszFname, "%sarc.dir", pszInfoPath);
AVCAdjustCaseSensitiveFilename(pszFname);
hFile = AVCRawBinOpen(pszFname, "r", AVC_COVER_BYTE_ORDER(eCoverType),
psDBCSInfo);
if (hFile)
{
while (!AVCRawBinEOF(hFile) &&
_AVCBinReadNextArcDir(hFile, &sEntry) == 0)
{
if (/* sEntry.numRecords > 0 && (DO NOT skip empty tables) */
!sEntry.bDeletedFlag &&
(pszCoverName == NULL ||
EQUALN(szNameToFind, sEntry.szTableName, nLen)) &&
_AVCBinReadInfoFileExists(pszInfoPath,
sEntry.szInfoFile,
eCoverType) )
{
papszList = CSLAddString(papszList, sEntry.szTableName);
if (ppapszArcDatFiles)
*ppapszArcDatFiles = CSLAddString(*ppapszArcDatFiles,
sEntry.szInfoFile);
}
}
AVCRawBinClose(hFile);
}
CPLFree(pszFname);
return papszList;
}
/**********************************************************************
* _AVCBinReadOpenTable()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadOpen() with type AVCFileTABLE instead)
*
* Open a INFO table, read the header file (.NIT), and finally open
* the associated data file to be ready to read records from it.
*
* Returns a valid AVCBinFile handle, or NULL if the file could
* not be opened.
*
* _AVCBinReadCloseTable() will eventually have to be called to release the
* resources used by the AVCBinFile structure.
**********************************************************************/
AVCBinFile *_AVCBinReadOpenTable(const char *pszInfoPath,
const char *pszTableName,
AVCCoverType eCoverType,
AVCDBCSInfo *psDBCSInfo)
{
AVCBinFile *psFile;
AVCRawBinFile *hFile;
AVCTableDef sTableDef;
AVCFieldInfo *pasFieldDef;
char *pszFname;
GBool bFound;
int i;
/* Alloc a buffer big enough for the longest possible filename...
*/
pszFname = (char*)CPLMalloc((strlen(pszInfoPath)+81)*sizeof(char));
/*-----------------------------------------------------------------
* Fetch info about this table from the "arc.dir"
*----------------------------------------------------------------*/
if (eCoverType == AVCCoverWeird)
sprintf(pszFname, "%sarcdr9", pszInfoPath);
else
sprintf(pszFname, "%sarc.dir", pszInfoPath);
AVCAdjustCaseSensitiveFilename(pszFname);
hFile = AVCRawBinOpen(pszFname, "r", AVC_COVER_BYTE_ORDER(eCoverType),
psDBCSInfo);
bFound = FALSE;
if (hFile)
{
while(!bFound && _AVCBinReadNextArcDir(hFile, &sTableDef) == 0)
{
if (!sTableDef.bDeletedFlag &&
EQUALN(sTableDef.szTableName, pszTableName,
strlen(pszTableName)) &&
_AVCBinReadInfoFileExists(pszInfoPath,
sTableDef.szInfoFile,
eCoverType))
{
bFound = TRUE;
}
}
AVCRawBinClose(hFile);
}
/* Hummm... quite likely that this table does not exist!
*/
if (!bFound)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open table %s", pszTableName);
CPLFree(pszFname);
return NULL;
}
/*-----------------------------------------------------------------
* Establish the location of the data file... depends on the
* szExternal[] field.
*----------------------------------------------------------------*/
if (EQUAL(sTableDef.szExternal, "XX"))
{
/*-------------------------------------------------------------
* The data file is located outside of the INFO directory.
* Read the path to the data file from the arc####.dat file
*------------------------------------------------------------*/
_AVCBinReadGetInfoFilename(pszInfoPath, sTableDef.szInfoFile,
"dat", eCoverType, pszFname);
AVCAdjustCaseSensitiveFilename(pszFname);
hFile = AVCRawBinOpen(pszFname, "r", AVC_COVER_BYTE_ORDER(eCoverType),
psDBCSInfo);
if (hFile)
{
/* Read the relative file path, and remove trailing spaces.
*/
AVCRawBinReadBytes(hFile, 80, sTableDef.szDataFile);
sTableDef.szDataFile[80] = '\0';
for(i = strlen(sTableDef.szDataFile)-1;
isspace(sTableDef.szDataFile[i]);
i--)
{
sTableDef.szDataFile[i] = '\0';
}
AVCRawBinClose(hFile);
}
else
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open file %s", pszFname);
CPLFree(pszFname);
return NULL;
}
}
else
{
/*-------------------------------------------------------------
* The data file IS the arc####.dat file
* Note: sTableDef.szDataFile must be relative to info directory
*------------------------------------------------------------*/
_AVCBinReadGetInfoFilename(pszInfoPath, sTableDef.szInfoFile,
"dat", eCoverType, pszFname);
strcpy(sTableDef.szDataFile, pszFname+strlen(pszInfoPath));
}
/*-----------------------------------------------------------------
* Read the table field definitions from the "arc####.nit" file.
*----------------------------------------------------------------*/
_AVCBinReadGetInfoFilename(pszInfoPath, sTableDef.szInfoFile,
"nit", eCoverType, pszFname);
AVCAdjustCaseSensitiveFilename(pszFname);
hFile = AVCRawBinOpen(pszFname, "r", AVC_COVER_BYTE_ORDER(eCoverType),
psDBCSInfo);
if (hFile)
{
int iField;
pasFieldDef = (AVCFieldInfo*)CPLCalloc(sTableDef.numFields,
sizeof(AVCFieldInfo));
/*-------------------------------------------------------------
* There must be at least sTableDef.numFields valid entries
* in the .NIT file...
*
* Note that we ignore any deleted field entries (entries with
* index=-1)... I don't see any use for these deleted fields...
* and I don't understand why Arc/Info includes them in their
* E00 table headers...
*------------------------------------------------------------*/
for(i=0, iField=0; iField<sTableDef.numFields; i++)
{
if (_AVCBinReadNextArcNit(hFile, &(pasFieldDef[iField])) != 0)
{
/* Problems.... is the NIT file corrupt???
*/
AVCRawBinClose(hFile);
CPLFree(pszFname);
CPLFree(pasFieldDef);
CPLError(CE_Failure, CPLE_FileIO,
"Failed reading table field info for table %s "
"File may be corrupt?", pszTableName);
return NULL;
}
/*---------------------------------------------------------
* Check if the field has been deleted (nIndex == -1).
* We just ignore deleted fields
*--------------------------------------------------------*/
if (pasFieldDef[iField].nIndex > 0)
iField++;
}
AVCRawBinClose(hFile);
}
else
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open file %s", pszFname);
CPLFree(pszFname);
return NULL;
}
/*-----------------------------------------------------------------
* Open the data file... ready to read records from it.
* If the header says that table has 0 records, then we don't
* try to open the file... but we don't consider that as an error.
*----------------------------------------------------------------*/
if (sTableDef.numRecords > 0 &&
AVCFileExists(pszInfoPath, sTableDef.szDataFile))
{
VSIStatBuf sStatBuf;
sprintf(pszFname, "%s%s", pszInfoPath, sTableDef.szDataFile);
AVCAdjustCaseSensitiveFilename(pszFname);
hFile = AVCRawBinOpen(pszFname, "r", AVC_COVER_BYTE_ORDER(eCoverType),
psDBCSInfo);
/* OOPS... data file does not exist!
*/
if (hFile == NULL)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open file %s", pszFname);
CPLFree(pszFname);
return NULL;
}
/*-------------------------------------------------------------
* In some cases, the number of records field for a table in the
* arc.dir does not correspond to the real number of records
* in the data file. In this kind of situation, the number of
* records returned by Arc/Info in an E00 file will be based
* on the real data file size, and not on the value from the arc.dir.
*
* Fetch the data file size, and correct the number of record
* field in the table header if necessary.
*------------------------------------------------------------*/
if ( VSIStat(pszFname, &sStatBuf) != -1 &&
sTableDef.nRecSize > 0 &&
sStatBuf.st_size/sTableDef.nRecSize != sTableDef.numRecords)
{
sTableDef.numRecords = sStatBuf.st_size/sTableDef.nRecSize;
}
}
else
{
hFile = NULL;
sTableDef.numRecords = 0;
}
/*-----------------------------------------------------------------
* Alloc. and init. the AVCBinFile structure.
*----------------------------------------------------------------*/
psFile = (AVCBinFile*)CPLCalloc(1, sizeof(AVCBinFile));
psFile->psRawBinFile = hFile;
psFile->eCoverType = AVCCoverV7;
psFile->eFileType = AVCFileTABLE;
psFile->pszFilename = pszFname;
psFile->hdr.psTableDef = (AVCTableDef*)CPLMalloc(sizeof(AVCTableDef));
*(psFile->hdr.psTableDef) = sTableDef;
psFile->hdr.psTableDef->pasFieldDef = pasFieldDef;
/* We can't really tell the precision from a Table header...
* just set an arbitrary value... it probably won't be used anyways!
*/
psFile->nPrecision = AVC_SINGLE_PREC;
/*-----------------------------------------------------------------
* Allocate temp. structures to use to read records from the file
* And allocate buffers for those fields that are stored as strings.
*----------------------------------------------------------------*/
psFile->cur.pasFields = (AVCField*)CPLCalloc(sTableDef.numFields,
sizeof(AVCField));
for(i=0; i<sTableDef.numFields; i++)
{
if (pasFieldDef[i].nType1*10 == AVC_FT_DATE ||
pasFieldDef[i].nType1*10 == AVC_FT_CHAR ||
pasFieldDef[i].nType1*10 == AVC_FT_FIXINT ||
pasFieldDef[i].nType1*10 == AVC_FT_FIXNUM )
{
psFile->cur.pasFields[i].pszStr =
(char*)CPLCalloc(pasFieldDef[i].nSize+1, sizeof(char));
}
}
return psFile;
}
/**********************************************************************
* _AVCBinReadNextTableRec()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextTableRec() instead)
*
* Reads the next record from an attribute table and fills the
* pasFields[] array.
*
* Note that it is assumed that the pasFields[] array has been properly
* initialized, re the allocation of buffers for fields strored as
* strings.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextTableRec(AVCRawBinFile *psFile, int nFields,
AVCFieldInfo *pasDef, AVCField *pasFields,
int nRecordSize)
{
int i, nType, nBytesRead=0;
if (psFile == NULL)
return -1;
for(i=0; i<nFields; i++)
{
if (AVCRawBinEOF(psFile))
return -1;
nType = pasDef[i].nType1*10;
if (nType == AVC_FT_DATE || nType == AVC_FT_CHAR ||
nType == AVC_FT_FIXINT || nType == AVC_FT_FIXNUM)
{
/*---------------------------------------------------------
* Values stored as strings
*--------------------------------------------------------*/
AVCRawBinReadString(psFile, pasDef[i].nSize, pasFields[i].pszStr);
pasFields[i].pszStr[pasDef[i].nSize] = '\0';
}
else if (nType == AVC_FT_BININT && pasDef[i].nSize == 4)
{
/*---------------------------------------------------------
* 32 bit binary integers
*--------------------------------------------------------*/
pasFields[i].nInt32 = AVCRawBinReadInt32(psFile);
}
else if (nType == AVC_FT_BININT && pasDef[i].nSize == 2)
{
/*---------------------------------------------------------
* 16 bit binary integers
*--------------------------------------------------------*/
pasFields[i].nInt16 = AVCRawBinReadInt16(psFile);
}
else if (nType == AVC_FT_BINFLOAT && pasDef[i].nSize == 4)
{
/*---------------------------------------------------------
* Single precision floats
*--------------------------------------------------------*/
pasFields[i].fFloat = AVCRawBinReadFloat(psFile);
}
else if (nType == AVC_FT_BINFLOAT && pasDef[i].nSize == 8)
{
/*---------------------------------------------------------
* Double precision floats
*--------------------------------------------------------*/
pasFields[i].dDouble = AVCRawBinReadDouble(psFile);
}
else
{
/*---------------------------------------------------------
* Hummm... unsupported field type...
*--------------------------------------------------------*/
CPLError(CE_Failure, CPLE_NotSupported,
"Unsupported field type: (type=%d, size=%d)",
nType, pasDef[i].nSize);
return -1;
}
nBytesRead += pasDef[i].nSize;
}
/*-----------------------------------------------------------------
* Record size is rounded to a multiple of 2 bytes.
* Check the number of bytes read, and move the read pointer if
* necessary.
*----------------------------------------------------------------*/
if (nBytesRead < nRecordSize)
AVCRawBinFSeek(psFile, nRecordSize - nBytesRead, SEEK_CUR);
return 0;
}
/*=====================================================================
* PC Arc/Info DBF TABLEs
*====================================================================*/
void _AVCBinReadRepairDBFFieldName(char *pszFieldName);
/**********************************************************************
* _AVCBinReadOpenDBFTable()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadOpen() with type AVCCoverPC/AVCFileTABLE instead)
*
* Open the DBF table, reads the header information and inits the
* AVCBinFile handle to be ready to read records from it.
*
* Returns a valid AVCBinFile handle, or NULL if the file could
* not be opened.
*
* _AVCBinReadCloseDBFTable() will eventually have to be called to release the
* resources used by the AVCBinFile structure.
**********************************************************************/
AVCBinFile *_AVCBinReadOpenDBFTable(const char *pszDBFFilename,
const char *pszArcInfoTableName)
{
AVCBinFile *psFile;
DBFHandle hDBFFile = NULL;
int iField;
AVCTableDef *psTableDef;
AVCFieldInfo *pasFieldDef;
/*-----------------------------------------------------------------
* Try to open the DBF file
*----------------------------------------------------------------*/
if ( (hDBFFile = DBFOpen(pszDBFFilename, "rb")) == NULL)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open table %s", pszDBFFilename);
return NULL;
}
/*-----------------------------------------------------------------
* Alloc. and init. the AVCBinFile structure.
*----------------------------------------------------------------*/
psFile = (AVCBinFile*)CPLCalloc(1, sizeof(AVCBinFile));
psFile->hDBFFile = hDBFFile;
psFile->eCoverType = AVCCoverPC;
psFile->eFileType = AVCFileTABLE;
psFile->pszFilename = CPLStrdup(pszDBFFilename);
psFile->hdr.psTableDef = NULL;
/* nCurDBFRecord is used to keep track of the 0-based index of the
* last record we read from the DBF file... this is to emulate
* sequential access which is assumed by the rest of the lib.
* Since the first record (record 0) has not been read yet, then
* we init the index at -1.
*/
psFile->nCurDBFRecord = -1;
/* We can't really tell the precision from a Table header...
* just set an arbitrary value... it probably won't be used anyways!
*/
psFile->nPrecision = AVC_SINGLE_PREC;
/*-----------------------------------------------------------------
* Build TableDef from the info in the DBF header
*----------------------------------------------------------------*/
/* Use calloc() to init some unused struct members */
psTableDef = (AVCTableDef*)CPLCalloc(1, sizeof(AVCTableDef));
psFile->hdr.psTableDef = psTableDef;
sprintf(psTableDef->szTableName, "%-32.32s", pszArcInfoTableName);
psTableDef->numFields = DBFGetFieldCount(hDBFFile);
/* We'll compute nRecSize value when we read fields info later */
psTableDef->nRecSize = 0;
psTableDef->numRecords = DBFGetRecordCount(hDBFFile);
/* All DBF tables are considered External */
strcpy(psTableDef->szExternal, "XX");
/*-----------------------------------------------------------------
* Build Field definitions
*----------------------------------------------------------------*/
pasFieldDef = (AVCFieldInfo*)CPLCalloc(psTableDef->numFields,
sizeof(AVCFieldInfo));
psTableDef->pasFieldDef = pasFieldDef;
for(iField=0; iField< psTableDef->numFields; iField++)
{
int nWidth, nDecimals;
DBFFieldType eDBFType;
char cNativeType;
/*-------------------------------------------------------------
* Fetch DBF Field info and convert to Arc/Info type...
* Note that since DBF fields names are limited to 10 chars,
* we do not have to worry about field name length in the process.
*------------------------------------------------------------*/
eDBFType = DBFGetFieldInfo(hDBFFile, iField,
pasFieldDef[iField].szName,
&nWidth, &nDecimals);
cNativeType = DBFGetNativeFieldType(hDBFFile, iField);
pasFieldDef[iField].nFmtWidth = (GInt16)nWidth;
pasFieldDef[iField].nFmtPrec = (GInt16)nDecimals;
/* nIndex is the 1-based field index that we see in the E00 header */
pasFieldDef[iField].nIndex = iField+1;
if (cNativeType == 'F' || (cNativeType == 'N' && nDecimals > 0) )
{
/*---------------------------------------------------------
* BINARY FLOAT
*--------------------------------------------------------*/
pasFieldDef[iField].nType1 = AVC_FT_BINFLOAT/10;
pasFieldDef[iField].nSize = 4;
pasFieldDef[iField].nFmtWidth = 12; /* PC Arc/Info ignores the */
pasFieldDef[iField].nFmtPrec = 3; /* DBF width/precision */
}
else if (cNativeType == 'N')
{
/*---------------------------------------------------------
* BINARY INTEGER
*--------------------------------------------------------*/
pasFieldDef[iField].nType1 = AVC_FT_BININT/10;
pasFieldDef[iField].nSize = 4;
pasFieldDef[iField].nFmtWidth = 5; /* PC Arc/Info ignores the */
pasFieldDef[iField].nFmtPrec = -1; /* DBF width/precision */
/*---------------------------------------------------------
* Some special integer fields need to have their names
* repaired because DBF does not support special characters.
*--------------------------------------------------------*/
_AVCBinReadRepairDBFFieldName(pasFieldDef[iField].szName);
}
else if (cNativeType == 'D')
{
/*---------------------------------------------------------
* DATE - Actually handled as a string internally
*--------------------------------------------------------*/
pasFieldDef[iField].nType1 = AVC_FT_DATE/10;
pasFieldDef[iField].nSize = nWidth;
pasFieldDef[iField].nFmtPrec = -1;
}
else /* (cNativeType == 'C' || cNativeType == 'L') */
{
/*---------------------------------------------------------
* CHAR STRINGS ... and all unknown types also handled as strings
*--------------------------------------------------------*/
pasFieldDef[iField].nType1 = AVC_FT_CHAR/10;
pasFieldDef[iField].nSize = nWidth;
pasFieldDef[iField].nFmtPrec = -1;
}
/*---------------------------------------------------------
* Keep track of position of field in record... first one always
* starts at offset=1
*--------------------------------------------------------*/
if (iField == 0)
pasFieldDef[iField].nOffset = 1;
else
pasFieldDef[iField].nOffset = (pasFieldDef[iField-1].nOffset +
pasFieldDef[iField-1].nSize );
/*---------------------------------------------------------
* Set default values for all other unused members in the struct
*--------------------------------------------------------*/
pasFieldDef[iField].v2 = -1; /* Always -1 ? */
pasFieldDef[iField].v4 = 4; /* Always 4 ? */
pasFieldDef[iField].v5 = -1; /* Always -1 ? */
pasFieldDef[iField].nType2 = 0; /* Always 0 ? */
pasFieldDef[iField].v10 = -1; /* Always -1 ? */
pasFieldDef[iField].v11 = -1; /* Always -1 ? */
pasFieldDef[iField].v12 = -1; /* Always -1 ? */
pasFieldDef[iField].v13 = -1; /* Always -1 ? */
}
/*-----------------------------------------------------------------
* Compute record size...
* Record size has to be rounded to a multiple of 2 bytes.
*----------------------------------------------------------------*/
if (psTableDef->numFields > 0)
{
psTableDef->nRecSize = (pasFieldDef[psTableDef->numFields-1].nOffset-1+
pasFieldDef[psTableDef->numFields-1].nSize);
psTableDef->nRecSize = ((psTableDef->nRecSize+1)/2)*2;
}
else
psTableDef->nRecSize = 0;
/*-----------------------------------------------------------------
* Allocate temp. structures to use to read records from the file
* And allocate buffers for those fields that are stored as strings.
*----------------------------------------------------------------*/
psFile->cur.pasFields = (AVCField*)CPLCalloc(psTableDef->numFields,
sizeof(AVCField));
for(iField=0; iField<psTableDef->numFields; iField++)
{
if (pasFieldDef[iField].nType1*10 == AVC_FT_DATE ||
pasFieldDef[iField].nType1*10 == AVC_FT_CHAR ||
pasFieldDef[iField].nType1*10 == AVC_FT_FIXINT ||
pasFieldDef[iField].nType1*10 == AVC_FT_FIXNUM )
{
psFile->cur.pasFields[iField].pszStr =
(char*)CPLCalloc(pasFieldDef[iField].nSize+1, sizeof(char));
}
}
return psFile;
}
/**********************************************************************
* _AVCBinReadNextDBFTableRec()
*
* (This function is for internal library use... external calls should
* go to AVCBinReadNextTableRec() instead)
*
* Reads the next record from a AVCCoverPC DBF attribute table and fills the
* pasFields[] array.
*
* Note that it is assumed that the pasFields[] array has been properly
* initialized, re the allocation of buffers for fields stored as
* strings.
*
* Returns 0 on success or -1 on error.
**********************************************************************/
int _AVCBinReadNextDBFTableRec(DBFHandle hDBFFile, int *piRecordIndex,
int nFields, AVCFieldInfo *pasDef,
AVCField *pasFields)
{
int i, nType;
/*-----------------------------------------------------------------
* Increment current record index.
* We use nCurDBFRecord to keep track of the 0-based index of the
* last record we read from the DBF file... this is to emulate
* sequential access which is assumed by the rest of the lib.
*----------------------------------------------------------------*/
if (hDBFFile == NULL || piRecordIndex == NULL ||
pasDef == NULL || pasFields == NULL)
return -1;
(*piRecordIndex)++;
if (*piRecordIndex >= DBFGetRecordCount(hDBFFile))
return -1; /* Reached EOF */
/*-----------------------------------------------------------------
* Read/convert each field based on type
*----------------------------------------------------------------*/
for(i=0; i<nFields; i++)
{
nType = pasDef[i].nType1*10;
if (nType == AVC_FT_DATE || nType == AVC_FT_CHAR ||
nType == AVC_FT_FIXINT || nType == AVC_FT_FIXNUM)
{
/*---------------------------------------------------------
* Values stored as strings
*--------------------------------------------------------*/
const char *pszValue;
pszValue = DBFReadStringAttribute(hDBFFile,
*piRecordIndex, i);
strncpy(pasFields[i].pszStr, pszValue, pasDef[i].nSize);
pasFields[i].pszStr[pasDef[i].nSize] = '\0';
}
else if (nType == AVC_FT_BININT && pasDef[i].nSize == 4)
{
/*---------------------------------------------------------
* 32 bit binary integers
*--------------------------------------------------------*/
pasFields[i].nInt32 = DBFReadIntegerAttribute(hDBFFile,
*piRecordIndex, i);
}
else if (nType == AVC_FT_BININT && pasDef[i].nSize == 2)
{
/*---------------------------------------------------------
* 16 bit binary integers
*--------------------------------------------------------*/
pasFields[i].nInt16 = (GInt16)DBFReadIntegerAttribute(hDBFFile,
*piRecordIndex,
i);
}
else if (nType == AVC_FT_BINFLOAT && pasDef[i].nSize == 4)
{
/*---------------------------------------------------------
* Single precision floats
*--------------------------------------------------------*/
pasFields[i].fFloat = (float)DBFReadDoubleAttribute(hDBFFile,
*piRecordIndex,
i);
}
else if (nType == AVC_FT_BINFLOAT && pasDef[i].nSize == 8)
{
/*---------------------------------------------------------
* Double precision floats
*--------------------------------------------------------*/
pasFields[i].dDouble = DBFReadDoubleAttribute(hDBFFile,
*piRecordIndex,
i);
}
else
{
/*---------------------------------------------------------
* Hummm... unsupported field type...
*--------------------------------------------------------*/
CPLError(CE_Failure, CPLE_NotSupported,
"Unsupported field type: (type=%d, size=%d)",
nType, pasDef[i].nSize);
return -1;
}
}
return 0;
}
/**********************************************************************
* _AVCBinReadRepairDBFFieldName()
*
* Attempt to repair some special integer field names that usually
* carry special chars such as '#' or '-' but that are lost because of
* DBF limitations and are replaced by '_'.
*
**********************************************************************/
void _AVCBinReadRepairDBFFieldName(char *pszFieldName)
{
char *pszTmp;
if ((pszTmp = strrchr(pszFieldName, '_')) == NULL)
return; /* No special char to process */
/*-----------------------------------------------------------------
* Replace '_' at end of field name by a '#', as in:
* COVER# , FNODE#, TNODE#, LPOLY#, RPOLY#
*
* and replace names that end with "_ID" with "-ID" as in COVER-ID
*----------------------------------------------------------------*/
if (EQUAL(pszTmp, "_"))
*pszTmp = '#';
else if (EQUAL(pszTmp, "_ID"))
*pszTmp = '-';
}
|