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
|
/*! \file basestrm.cpp
\brief The various streams that ASC offers, like file and memory streams.
*/
/*
This file is part of Advanced Strategic Command; http://www.asc-hq.de
Copyright (C) 1994-2005 Martin Bickel and Marc Schellenberger
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; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <ctype.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <list>
#include "global.h"
#ifdef HAVE_LIMITS
#include <limits>
#endif
#include <sys/stat.h>
#include "global.h"
#include "basestrm.h"
#ifdef _DOS_
#include "dos/fileio.h"
#else
#ifdef _WIN32_
#include "win32/fileio.h"
#else
#ifdef _UNIX_
#include "unix/fileio.h"
#endif
#endif
#endif
//#include sdlheader
#include <SDL_endian.h>
#include "messaginghub.h"
const int maxSearchDirNum = 30;
int searchDirNum = 0;
char* ascDirectory[maxSearchDirNum] = { NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL };
#pragma pack(1)
struct trleheader {
unsigned short int id;
unsigned short int size;
char rle;
unsigned short int x;
unsigned short int y;
};
#pragma pack()
#define bzip_xor_byte 'M'
const char* containermagic = "NCBM";
const char* LZ_SIGNATURE = "MBLZW16";
const char* RLE_SIGNATURE = "MBRLE1";
const char* BZIP_SIGNATURE = "MBZLB2X!";
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////// Watchpointer
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CharBuf :: CharBuf ( void )
{
size = 0;
buf = NULL;
}
CharBuf :: CharBuf ( int _size )
{
size = _size;
buf = new char[ size ];
}
void CharBuf :: resize ( int newsize )
{
char* nb = new char[newsize];
for ( int i = 0; i < size; i++ )
nb[i] = buf[i];
delete[] buf;
buf = nb;
}
CharBuf :: ~CharBuf()
{
if ( buf )
delete[] buf;
buf = NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////// Exceptions
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
toutofmem::toutofmem ( int m )
{
required = m;
}
tfileerror::tfileerror ( const ASCString& fileName )
{
_filename = fileName ;
}
/*
tfileerror::~tfileerror()
{
if ( filename ) {
delete filename;
filename = NULL;
}
}
*/
tinvalidmode :: tinvalidmode ( const ASCString& _fileName, tnstream::IOMode org_mode, tnstream::IOMode requested_mode )
: tfileerror ( _fileName )
{
orgmode = org_mode;
requestmode = requested_mode;
}
treadafterend :: treadafterend ( const ASCString& _fileName )
: tfileerror ( _fileName )
{
}
tinternalerror::tinternalerror ( const char* filename, int l )
{
linenum = l;
sourcefilename = filename;
}
tinvalidversion :: tinvalidversion ( const ASCString& _fileName, int ex, int fnd )
: tfileerror ( _fileName ), expected ( ex ), found ( fnd )
{
}
ASCString tinvalidversion :: getMessage() const
{
ASCString s;
if ( expected < found )
s.format( "File/module %s has invalid version.\nExpected file version %d\nFound file version %d\nThe file/module is newer than your application\nPlease install the latest version of ASC from www.asc-hq.org", getFileName().c_str(), expected, found );
else
s.format ( "File/module %s has invalid version.\nExpected file version %d\nFound file version %d\nThis is a bug, please report it!", getFileName().c_str(), expected, found );
return s;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////// Streams
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
tnstream :: tnstream ( void )
: devicename ( "-abstract tnstream-" ) {}
void tnstream::seek ( int pos )
{
throw tfileerror ( "Seeking not supported for stream " + getDeviceName() );
}
void tnstream::readrlepict( void** pnter, bool allocated, int* size)
{
trleheader hd;
int w;
char* q;
hd.id = readWord();
hd.size = readWord();
hd.rle = readChar();
hd.x = readWord();
hd.y = readWord();
if (hd.id == 16973) {
if (!allocated)
*pnter = new char [ hd.size + sizeof(hd) ];
memcpy( *pnter, &hd, sizeof(hd));
q = (char*) (*pnter) + sizeof(hd);
readdata( q, hd.size); // endian ok ?
*size = hd.size + sizeof(hd);
}
else {
w = (hd.id + 1) * (hd.size + 1) + 4 ;
if (!allocated)
*pnter = new char [ w ];
memcpy ( *pnter, &hd, sizeof ( hd ));
q = (char*) (*pnter) + sizeof(hd);
readdata ( q, w - sizeof(hd) ); // endian ok ?
*size = w;
}
}
void tnstream :: writerlepict ( const void* buf )
{
writeImage( buf, true );
}
void tnstream :: writeImage ( const void* buf, bool compress )
{
if ( compress ) {
char* tempbuf = new char [ 0xffff ];
if ( tempbuf ) {
int size = compressrle ( buf, tempbuf );
trleheader* hd = (trleheader*) tempbuf;
writeWord( hd->id );
writeWord( hd->size );
writeChar( hd->rle );
writeWord( hd->x );
writeWord( hd->y );
writedata ( hd+1, size - sizeof(*hd) );
delete[] tempbuf;
} else
compress = false;
}
if ( !compress ) {
Uint16* pw = (Uint16*) buf;
writeWord(pw[0] );
writeWord(pw[1] );
writedata ( pw+2, ( pw[0] + 1 ) * ( pw[1] + 1 ) );
}
}
ASCString tnstream::getDeviceName ( void )
{
return devicename;
}
ASCString tnstream::getLocation ( void )
{
return devicename;
}
int tnstream::readInt ( void )
{
int i;
readdata2 ( i );
return SDL_SwapLE32( i );
}
int tnstream::readWord ( void )
{
Uint16 w;
readdata2 ( w );
return SDL_SwapLE16( w );
}
char tnstream::readChar ( void )
{
char c;
readdata2 ( c );
return c;
}
float SwapFloat( float f )
{
union
{
float f;
unsigned char b[4];
} dat1, dat2;
dat1.f = f;
dat2.b[0] = dat1.b[3];
dat2.b[1] = dat1.b[2];
dat2.b[2] = dat1.b[1];
dat2.b[3] = dat1.b[0];
return dat2.f;
}
float tnstream::readFloat ( void )
{
float c;
readdata2 ( c );
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
c = SwapFloat(c);
#endif
return c;
}
#if SIZE_T_not_identical_to_INT
void tnstream::writeInt ( size_t i )
{
#ifdef HAVE_LIMITS
// assert( i <= numeric_limits<int>::max());
#endif
writeInt( int(i) );
}
#endif
void tnstream::writeInt ( unsigned int i )
{
i = SDL_SwapLE32(i);
writedata2 ( i );
}
void tnstream::writeInt ( bool b )
{
int i = b;
i = SDL_SwapLE32(i);
writedata2 ( i );
}
void tnstream::writeInt ( int i )
{
i = SDL_SwapLE32(i);
writedata2 ( i );
}
void tnstream::writeWord ( int w )
{
Uint16 w2 = SDL_SwapLE16( Uint16(w) );
writedata2 ( w2 );
}
void tnstream::writeChar ( char c )
{
writedata2 ( c );
}
void tnstream::writeFloat ( float f )
{
writedata2 ( f );
}
void tnstream::readpchar(char** pc, int maxlength )
{
int actpos2 = 0;
int maxav = 100000;
if ( maxlength )
if ( maxav > maxlength )
maxav = maxlength;
CharBuf charbuf ( maxav );
maxav--;
char* pch2 = charbuf.buf;
int loop = 0;
do {
actpos2++;
if ( loop )
pch2++;
loop++;
readdata( pch2, 1 ); // endian ok !
} while (*pch2 != 0 && actpos2 < maxav ); /* enddo */
if ( actpos2 >= maxav ) {
pch2[1] = 0;
actpos2++;
if ( pch2[0] ) {
char temp;
do {
readdata( &temp, 1 ); // endian ok !
} while ( temp ); /* enddo */
}
}
pch2 = new char [ actpos2 ];
memcpy ( pch2, charbuf.buf, actpos2 );
*pc = pch2;
}
void tnstream::readpnchar(char** pc, int maxlength )
{
int actpos2 = 0;
int maxav = 10000;
if ( maxlength )
if ( maxav > maxlength )
maxav = maxlength;
CharBuf charbuf ( maxav );
maxav--;
char* pch2 = charbuf.buf;
int ende = 0;
do {
char bt;
int red = readdata( &bt, 1, 0 );
if ( red < 1 ) {
ende = 2;
*pch2 = 0;
} else
if ( bt == '\n' || bt == 0 ) {
*pch2 = 0;
ende = 1;
} else
if ( bt != '\r' ) {
*pch2 = bt;
pch2++;
actpos2++;
}
} while ( !ende && actpos2 < maxav ); /* enddo */
if ( !ende ) {
if ( actpos2 >= maxav ) {
*pch2 = 0;
if ( pch2[0] ) {
char temp;
do {
int red = readdata( &temp, 1, 0 );
if ( red < 1 ) {
temp = 0;
} else
if ( temp == '\n' )
temp = 0;
} while ( temp ); /* enddo */
}
}
}
if ( ende == 2 ) {
if ( !actpos2 )
*pc = NULL;
else
*pc = strdup ( charbuf.buf );
} else
*pc = strdup ( charbuf.buf );
}
bool tnstream::readTextString ( ASCString& s, bool includeCR )
{
s = "";
char c;
int red;
int end = 0;
do {
red = readdata( &c, 1, 0 ); // endian ok !
if ( red < 1 ) {
end = 2;
} else
if ( (c == '\n' && !includeCR) || c == 0 ) {
end = 1;
} else
if ( c != '\r' )
s += c;
} while ( red && !end );
if ( end == 2)
return false;
else
return true;
}
ASCString tnstream::readString ( bool includeCR )
{
ASCString s;
bool data = readTextString ( s, includeCR );
if ( !data && s.empty() )
throw treadafterend ( getLocation() );
return s;
}
void tnstream::writeString(const string& pc, bool binary )
{
if ( binary )
writepchar ( pc.c_str() );
else
writedata ( pc.data(), pc.length() );
}
void tnstream::writepchar(const char* pc)
{
if ( pc ) {
const char *pch1 = pc;
int loop = 0;
do {
if ( loop )
pch1++;
writedata( pch1, 1 );
loop++;
} while ( *pch1 > 0 ); /* enddo */
} else {
char pch1 = 0;
writedata ( &pch1, 1 );
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MemoryStreamCopy :: MemoryStreamCopy ( pnstream stream )
{
buf = NULL;
int bufused = 0;
int memreserved = 0;
int blocksize = 500000;
int red;
do {
if ( bufused + blocksize > memreserved ) {
int newsize = memreserved + blocksize;
void* newbuf = malloc (newsize);
if ( buf ) {
memcpy ( newbuf, buf, bufused );
free ( buf );
}
buf = newbuf;
memreserved = newsize;
}
char* cp = (char*) buf;
red = stream->readdata ( cp + bufused, blocksize, 0 ); // endian ok !
bufused += red;
} while ( red == blocksize );
size = bufused;
pos = 0;
devicename = stream->getDeviceName();
}
ASCString MemoryStreamCopy::getLocation()
{
return devicename + " (memory bufferd)";
}
MemoryStreamCopy :: ~MemoryStreamCopy ( )
{
if ( buf )
free ( buf );
}
void MemoryStreamCopy :: writedata ( const void* buf, int size )
{
throw tinvalidmode ( getDeviceName(), reading, writing );
}
int MemoryStreamCopy :: readdata ( void* buffer, int _size, bool excpt )
{
char* cp = (char*) buf;
if ( pos + _size > size ) {
if ( excpt )
throw treadafterend ( getDeviceName() );
else {
int tr = size-pos;
memcpy ( buffer, cp+pos, tr );
pos += tr;
return tr;
}
} else {
memcpy ( buffer, cp+pos, _size );
pos += _size;
return _size;
}
}
void MemoryStreamCopy :: seek ( int newpos )
{
if ( newpos > size || newpos < 0 )
throw treadafterend ( getDeviceName() );
pos = newpos;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _SDL_
static int stream_seek( struct SDL_RWops *context, int offset, int whence)
{
MemoryStreamCopy* stream = (MemoryStreamCopy*) context->hidden.unknown.data1;
if ( whence == SEEK_SET )
stream->seek ( offset );
else
if ( whence == SEEK_CUR )
stream->seek ( offset + stream->getPosition() );
else
if ( whence == SEEK_END )
stream->seek ( offset + stream->getSize() );
return stream->getPosition();
}
static int stream_read(SDL_RWops *context, void *ptr, int size, int maxnum)
{
MemoryStreamCopy* stream = (MemoryStreamCopy*) context->hidden.unknown.data1;
size_t nread = stream->readdata ( ptr, size * maxnum, 0 );
return(nread / size);
}
static int stream_close(SDL_RWops *context)
{
if ( context ) {
if ( context->hidden.unknown.data1 ) {
MemoryStreamCopy* stream = (MemoryStreamCopy*) context->hidden.unknown.data1;
delete stream;
}
SDL_FreeRW(context);
}
return(0);
}
SDL_RWops *SDL_RWFromStream( pnstream stream )
{
MemoryStreamCopy* msb = new MemoryStreamCopy ( stream );
SDL_RWops *rwops;
rwops = SDL_AllocRW();
if ( rwops != NULL ) {
rwops->seek = stream_seek;
rwops->read = stream_read;
rwops->write = NULL;
rwops->close = stream_close;
rwops->hidden.unknown.data1 = msb;
}
return(rwops);
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
tncontainerstream :: tncontainerstream ( const char* containerfilename, ContainerIndexer* indexer, int dirLevel )
: tn_file_buf_stream ( containerfilename, reading ), index(NULL)
{
num = 0;
char magic[4];
readdata ( &magic, 4 ); // endian ok !
if ( strncmp ( magic, containermagic, 4 ) == 0) {
int pos = readInt();
seek ( pos );
num = readInt();
index = new tcontainerindex[num];
for ( int i = 0; i < num; i++ ) {
bool __loadName = readInt();
index[i].start = readInt();
index[i].end = readInt();
if ( __loadName ) {
readpchar ( &index[i].name );
#if CASE_SENSITIVE_FILE_NAMES == 1
// quick hack to be able to use existing CON files.
char *c = index[i].name;
while ( *c ) {
*c = tolower( *c );
c++;
}
#endif
indexer->addfile ( index[i].name, this, dirLevel );
} else
index[i].name = NULL;
}
}
actfile = NULL;
containerfilepos = 0;
}
int tncontainerstream :: getcontainerfilesize ( const char* name )
{
int i = 0;
while ( i < num && stricmp ( index[i].name, name ) )
i++;
if ( i >= num )
return -1;
else
return index[i].end - index[i].start + 1;
}
void tncontainerstream :: opencontainerfile ( const char* name )
{
if ( actfile ) {
ASCString err = ASCString("two files simultaneously: ") + actfile->name + " and " + name;
throw tfileerror ( err );
}
containerfilepos = 0;
int i = 0;
while ( i < num && stricmp ( index[i].name, name ) )
i++;
if ( i >= num )
throw tfileerror ( name );
actfile = &index[i];
containerfilepos = 0;
seek ( actfile->start );
}
int tncontainerstream :: readcontainerdata ( void* buf, int size, bool excpt )
{
if ( actfile->start + containerfilepos + size > actfile->end+1 ) {
if ( excpt )
throw treadafterend ( actfile->name );
else {
int got = readdata ( buf, (actfile->end+1 - actfile->start) - containerfilepos , excpt );
containerfilepos+=got;
return got;
}
}
readdata ( buf, size );
containerfilepos+=size;
return size;
}
void tncontainerstream :: closecontainerfile ( void )
{
actfile = NULL;
}
char* tncontainerstream :: getfirstname ( void )
{
actname = 0;
return getnextname();
}
char* tncontainerstream :: getnextname ( void )
{
if ( actname < num )
return index[actname++].name;
else
return NULL;
}
tncontainerstream :: ~tncontainerstream ()
{
for ( int i = 0; i < num; i++ )
if ( index[i].name )
delete[] index[i].name;
delete[] index;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ContainerCollector : public ContainerIndexer {
public:
struct FileIndex {
ASCString name;
pncontainerstream container;
int directoryLevel;
};
protected:
dynamic_array<FileIndex> index[256]; // not very efficient, but who cares :-)
dynamic_array<pncontainerstream> container;
int containernum;
struct {
int alpha;
int index;
} namesearch; // next entry to return
public:
ContainerCollector ( void );
void init ( const char* wildcard );
void addfile ( const char* filename, const pncontainerstream stream, int directoryLevel );
// pncontainerstream getfile ( const char* filename );
FileIndex* getfile ( const ASCString& filename );
FileIndex* getfirstname ( void );
FileIndex* getnextname ( void );
ASCString listContainer();
virtual ~ContainerCollector();
};
ContainerCollector containercollector;
char* constructFileName( char* buf, int directoryLevel, const char* path, const char* filename )
{
if ( buf ) {
const char* filename2 = filename;
buf[0] = 0;
// filenames beginning with / or ~/ have an absolute path ; ignore variable path for them
if ( ! (filename && (filename[0] == pathdelimitter || (filename[0]=='~' && filename[1] == pathdelimitter)) )) {
if ( path )
strcpy ( buf, path);
else
if ( directoryLevel >= 0 && ascDirectory[ directoryLevel ] )
strcpy ( buf, ascDirectory[ directoryLevel ]);
}
appendbackslash ( buf );
if ( filename && strchr ( filename, pathdelimitter )) {
char name2[ maxFileStringSize ];
// filename contains directories
strcpy ( name2, filename );
int i = strlen ( name2 )-1;
while ( name2[i] != pathdelimitter )
i--;
name2[i+1] = 0;
filename2 = &filename[i+1];
// filename2 is now the pure filename without directory
// name2 is the directory
if ( buf[0] && name2[0]==pathdelimitter )
strcpy ( buf, name2+1);
else
strcpy ( buf, name2);
}
if ( buf[0] == '~' && buf[1] == pathdelimitter ) {
char* home = getenv ( "HOME" );
if ( home ) {
char temp[ maxFileStringSize ];
strcpy ( temp, buf );
strcpy ( buf, home );
appendbackslash ( buf );
strcat ( buf, &temp[2]);
}
}
appendbackslash ( buf );
if ( filename2 )
strcat ( buf, filename2 );
}
return buf;
}
bool isPathRelative( const ASCString& path )
{
if ( path.length() < 2 )
return true;
if ( path[0] == '~' && path[1] == pathdelimitter )
return false;
#ifdef WIN32
if ( path[1] == ':' && path[2] == pathdelimitter )
return false;
#endif
if ( path[0] == pathdelimitter )
return false;
return true;
}
ASCString constructFileName( int directoryLevel, const ASCString& path, ASCString filename )
{
ASCString result;
// filenames beginning with / or ~/ have an absolute path ; ignore variable path for them
if ( isPathRelative( filename )) {
if ( !path.empty() )
result += path;
else
if ( directoryLevel >= 0 && ascDirectory[ directoryLevel ] )
result += ascDirectory[ directoryLevel ];
}
appendbackslash ( result );
if ( !filename.empty() && filename.find( pathdelimitter )!= ASCString::npos ) {
ASCString dir = filename;
dir.erase( dir.rfind( pathdelimitter ) + 1);
filename.erase( 0, filename.find( pathdelimitter ) + 1 );
if ( dir.find( pathdelimitter ) == 0 )
dir.erase( 0, 1 );
result = dir;
}
if ( result.length() > 2 && result[0] == '~' && result[1] == pathdelimitter ) {
char* home = getenv ( "HOME" );
if ( home ) {
ASCString temp = result;
result = home;
appendbackslash ( result );
result += temp.substr( 2 );
}
}
appendbackslash ( result );
result += filename;
return result;
}
struct FileLocation {
int directoryLevel;
pncontainerstream container;
int found;
};
void locateFile ( const ASCString& filename, FileLocation* loc )
{
loc->found = 0;
ContainerCollector::FileIndex* idx = containercollector.getfile ( filename );
int maxnum;
if ( idx ) {
maxnum = idx->directoryLevel+1;
loc->directoryLevel = idx->directoryLevel;
loc->found = 1;
loc->container = idx->container;
} else {
maxnum = searchDirNum;
loc->container = NULL;
loc->directoryLevel = -1;
}
if ( maxnum ) {
int localfound = 0;
for ( int i = 0; i < maxnum && !localfound; i++ ) {
char buf[2000];
FILE* fp = fopen ( constructFileName ( buf, i, NULL, filename.c_str()), "r" );
if ( fp ) {
localfound = loc->found = 1;
fclose ( fp );
loc->container = NULL;
loc->directoryLevel = i;
}
}
} else {
char buf[2000];
FILE* fp = fopen ( constructFileName ( buf, -1, ".", filename.c_str()), "r" );
if ( fp ) {
loc->found = 1;
fclose ( fp );
loc->container = NULL;
loc->directoryLevel = -2;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ASCString listContainer()
{
return containercollector.listContainer();
}
ContainerCollector :: ContainerCollector ( void )
{
containernum = 0;
}
void ContainerCollector :: init ( const char* wildcard )
{
for ( int i = 0; i < searchDirNum; i++ ) {
DIR *dirp;
struct ASC_direct *direntp;
char buf[ maxFileStringSize ];
char buf2[ maxFileStringSize ];
char buf3 [ maxFileStringSize ];
dirp = opendir( extractPath ( buf2, constructFileName ( buf, i, NULL, wildcard )));
extractFileName ( buf3, buf );
if( dirp != NULL ) {
for(;;) {
direntp = readdir( dirp );
if ( direntp == NULL ) {
break;
}
if ( patimat ( buf3, direntp->d_name )) {
container[containernum++] = new tncontainerstream( constructFileName ( buf, i, buf2, direntp->d_name), this, i);
if ( MessagingHub::Instance().getVerbosity() >= 2 )
printf("container %s mounted\n", buf );
}
}
closedir( dirp );
}
}
}
void ContainerCollector :: addfile ( const char* filename, const pncontainerstream stream, int directoryLevel )
{
int found = 0;
FileIndex* cci = NULL;
int i1 = toupper ( filename[0] );
for ( int i = 0; i <= index[i1].getlength(); i++ )
if ( index[i1][i].name.compare_ci ( filename ) == 0 ) {
if ( index[i1][i].directoryLevel <= directoryLevel )
return;
else {
cci = &(index[i1][i]);
found = 1;
}
}
if ( !found )
cci = &( index[i1][ index[i1].getlength()+1 ] );
cci->name = filename;
cci->container = stream;
cci->directoryLevel = directoryLevel;
}
ContainerCollector::FileIndex* ContainerCollector :: getfile ( const ASCString& filename )
{
int i1 = toupper ( filename[0] );
for ( int i = 0; i <= index[i1].getlength(); i++ )
if ( index[i1][i].name.compare_ci ( filename) == 0 )
return &index[i1][i];
return NULL;
}
ContainerCollector::FileIndex* ContainerCollector :: getfirstname ( void )
{
namesearch.alpha = 0;
namesearch.index = 0;
return getnextname();
}
ContainerCollector::FileIndex* ContainerCollector :: getnextname ( void )
{
while ( index[namesearch.alpha].getlength() < namesearch.index) {
if ( namesearch.alpha == 255 )
return NULL;
namesearch.alpha++;
namesearch.index = 0;
} /* endwhile */
return &index[namesearch.alpha][namesearch.index++];
}
ASCString ContainerCollector :: listContainer()
{
ASCString s;
for ( int i = 0; i < containernum; i++ )
s += container[i]->getLocation() + "\n";
return s;
}
ContainerCollector :: ~ContainerCollector()
{
int i;
for (i = 0; i < containernum; i++ )
delete container[i];
containernum = 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
libbzip_compression :: libbzip_compression ( p_compressor_stream_interface strm )
{
bzs.bzalloc = NULL;
bzs.bzfree = NULL;
bzs.opaque = NULL;
BZ2_bzCompressInit ( &bzs, 5, 0, 0 );
outputbufsize = 100000;
outputbuf = new char [ outputbufsize ];
stream = strm;
}
void libbzip_compression :: writedata ( const void* buf, int size )
{
char* cbuf = (char*) buf;
bzs.next_in = cbuf ;
bzs.avail_in = size ;
bzs.total_in_lo32 = 0 ;
bzs.total_in_hi32 = 0 ;
while ( bzs.total_in_lo32 < size ) {
bzs.next_out = outputbuf;
bzs.avail_out = outputbufsize;
bzs.total_out_lo32 = 0;
bzs.total_out_hi32 = 0;
int res = BZ2_bzCompress ( &bzs, BZ_RUN );
if ( res < 0 )
throw tcompressionerror ( "MBZLB2 compression :: writedata", res );
for ( int i = 0; i < bzs.total_out_lo32; i++ )
outputbuf[i] ^= bzip_xor_byte;
stream->writecmpdata ( outputbuf, bzs.total_out_lo32 );
}
}
void libbzip_compression :: close_compression ( void )
{
int res;
do {
bzs.next_in = outputbuf;
bzs.avail_in = 0;
bzs.next_out = outputbuf;
bzs.avail_out = outputbufsize;
bzs.total_out_lo32 = 0;
bzs.total_out_hi32 = 0;
res = BZ2_bzCompress ( &bzs, BZ_FINISH );
if ( res < 0 )
throw tcompressionerror ( "MBZLB2 compression :: closecompression", res );
for ( int i = 0; i < bzs.total_out_lo32; i++ )
outputbuf[i] ^= bzip_xor_byte;
stream->writecmpdata ( outputbuf, bzs.total_out_lo32 );
} while ( res != BZ_STREAM_END );
BZ2_bzCompressEnd ( &bzs );
}
libbzip_compression :: ~libbzip_compression ( )
{
if ( outputbuf ) {
delete[] outputbuf;
outputbuf = NULL;
}
}
libbzip_decompression :: libbzip_decompression ( p_compressor_stream_interface strm )
{
bzs.bzalloc = NULL;
bzs.bzfree = NULL;
bzs.opaque = NULL;
BZ2_bzDecompressInit ( &bzs, 0, 0 );
inputbufsize = 100000;
inputbuf = new char [ inputbufsize ];
inputbufused = 0;
inputbufread = 0;
stream = strm;
}
int libbzip_decompression :: readdata ( void* buf, int size, bool excpt )
{
int decompressed = 0;
char* cbuf = (char*) buf;
bzs.next_in = cbuf ;
bzs.avail_in = size ;
bzs.total_in_lo32 = 0 ;
bzs.total_in_hi32 = 0 ;
int abrt = 0;
while ( decompressed < size && !abrt ) {
if ( inputbufread >= inputbufused ) {
inputbufused = stream->readcmpdata ( inputbuf, inputbufsize, 0 );
if ( !inputbufused && excpt )
throw tcompressionerror ( "Decompressor :: out of data", 0 );
for ( int i = 0; i < inputbufused; i++ )
inputbuf[i] ^= bzip_xor_byte;
inputbufread = 0;
}
bzs.next_in = inputbuf + inputbufread;
bzs.avail_in = inputbufused - inputbufread;
bzs.total_in_lo32 = 0;
bzs.total_in_hi32 = 0;
bzs.next_out = cbuf + decompressed;
bzs.avail_out = size - decompressed;
bzs.total_out_lo32 = 0;
bzs.total_out_hi32 = 0;
int res = BZ2_bzDecompress ( &bzs );
decompressed += bzs.total_out_lo32;
inputbufread += bzs.total_in_lo32;
if ( decompressed < size ) {
if ( res == BZ_STREAM_END ) {
if ( excpt )
throw treadafterend ( "BZ_decompress_stream" );
abrt = 1;
} else {
if ( res != BZ_OK ) {
if ( excpt ) {
if ( res == BZ_MEM_ERROR )
throw toutofmem ( -1 );
else
throw tcompressionerror ( "MBZLB2 decompression :: readdata", res );
}
abrt = 1;
}
}
}
}
return decompressed;
}
libbzip_decompression :: ~libbzip_decompression ( )
{
if ( inputbuf ) {
BZ2_bzDecompressEnd ( &bzs );
delete[] inputbuf;
inputbuf = NULL;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
t_compressor_2ndbuf_filter :: t_compressor_2ndbuf_filter ( t_compressor_stream_interface* strm )
{
stream = strm;
}
void t_compressor_2ndbuf_filter :: writecmpdata ( const void* buf, int size )
{
stream->writecmpdata ( buf, size );
}
int t_compressor_2ndbuf_filter :: readcmpdata ( void* buf, int size, bool excpt )
{
int got = 0;
char* pc = (char*) buf;
while ( size && _queue.size() ) {
*pc = _queue.front();
_queue.pop();
pc++;
size--;
got++;
}
if ( size )
got += stream->readcmpdata ( pc, size, excpt );
return got;
}
void t_compressor_2ndbuf_filter :: insert_data_into_queue ( const void* buf, int size )
{
char* pc = (char*) buf;
for (int i = 0; i < size; i++) {
_queue.push ( *pc );
pc++;
}
}
*/
tanycompression :: tanycompression ( int md )
{
mmd = md;
bzip_compress = NULL;
bzip_decompress = NULL;
}
void tanycompression :: init ( void )
{
if ( mmd == 1 ) {
char buf[10];
int maxlen = strlen ( BZIP_SIGNATURE ) + 1;
int bufdatanum = readcmpdata ( buf, maxlen, 0 );
int siglen = 0;
if ( bufdatanum == maxlen ) {
if ( strncmp ( &buf[1], LZ_SIGNATURE, 9 ) == 0 && !buf[0]) {
status = 110;
siglen = 0;
} else
if ( strncmp ( &buf[1], RLE_SIGNATURE, 9 ) == 0 && !buf[0]) {
status = 111;
siglen = 0;
} else
if ( strncmp ( buf, BZIP_SIGNATURE, 9 ) == 0 ) {
status = 112;
siglen = strlen ( BZIP_SIGNATURE ) + 1;
bzip_decompress = new libbzip_decompression ( this );
} else
status= 109;
} else
status = 109;
for ( int i = siglen; i < bufdatanum; i++ )
_queue.push ( buf[i] );
} else {
status = 201;
bzip_compress = new libbzip_compression ( this );
writecmpdata ( BZIP_SIGNATURE, strlen ( BZIP_SIGNATURE ) + 1 );
}
}
int tanycompression :: readdata ( void* rbuf, int size, bool excpt )
{
int red = 0;
if ( size ) {
switch ( status ) {
case 109: red += readlzwdata ( rbuf, size, excpt );
break;
case 110:
case 111: red += tlzwstreamcompression :: readdata ( rbuf, size, excpt );
break;
case 112: red += bzip_decompress -> readdata ( rbuf, size, excpt );
break;
} /* endswitch */
}
return red;
}
void tanycompression :: writedata ( const void* buf, int size )
{
bzip_compress -> writedata ( buf, size );
// writecmpdata ( buf, size );
}
int tanycompression :: readlzwdata ( void* buf, int size, bool excpt )
{
if ( _queue.size() ) {
int got = 0;
char* pc = (char*) buf;
while ( size && _queue.size() ) {
*pc = _queue.front();
_queue.pop();
pc++;
size--;
got++;
} /* endwhile */
if ( size )
got += readcmpdata ( pc, size, excpt );
return got;
} else
return readcmpdata ( buf, size, excpt );
}
void tanycompression :: writelzwdata ( const void* buf, int size )
{
writecmpdata ( buf, size );
}
void tanycompression :: close_compression ( void )
{
if ( bzip_compress ) {
bzip_compress->close_compression ( );
delete bzip_compress;
bzip_compress = NULL;
}
}
tanycompression :: ~tanycompression ( )
{
if ( bzip_decompress ) {
delete bzip_decompress;
bzip_decompress = NULL;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
void tn_lzw_bufstream :: writedata ( const void* buf, int size )
{
tlzwstreamcompression :: writedata ( buf, size );
}
int tn_lzw_bufstream :: readdata ( void* buf, int size, bool excpt )
{
return tlzwstreamcompression :: readdata ( buf, size, excpt );
}
int tn_lzw_bufstream :: readlzwdata ( void* buf, int size, bool excpt )
{
return tnbufstream :: readdata ( buf, size, excpt );
}
void tn_lzw_bufstream :: writelzwdata ( const void* buf, int size )
{
tnbufstream :: writedata ( buf, size );
}
tn_lzw_bufstream :: ~tn_lzw_bufstream ()
{
tlzwstreamcompression :: close();
tnbufstream :: close();
}
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void tn_lzw_file_buf_stream :: writedata ( const void* buf, int size )
{
tanycompression :: writedata ( buf, size );
}
int tn_lzw_file_buf_stream :: readdata ( void* buf, int size, bool excpt )
{
return tanycompression :: readdata ( buf, size, excpt );
}
int tn_lzw_file_buf_stream:: readcmpdata ( void* buf, int size, bool excpt )
{
return tn_file_buf_stream :: readdata ( buf, size, excpt );
}
void tn_lzw_file_buf_stream :: writecmpdata ( const void* buf, int size )
{
tn_file_buf_stream :: writedata ( buf, size );
}
tn_lzw_file_buf_stream :: ~tn_lzw_file_buf_stream()
{
close_compression ();
tn_file_buf_stream :: close();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
tn_c_lzw_filestream :: tn_c_lzw_filestream ( const ASCString& name, IOMode mode ) : tanycompression ( mode )
{
#ifdef logfiles
FILE* fp = fopen ( "files.lst", "at" );
fprintf ( fp, "%s\n", name );
fclose ( fp );
#endif
strm = NULL;
inp = 0;
containerstream = NULL;
FileLocation fl;
if ( mode == tnstream::reading ) {
locateFile ( name, &fl );
if ( !fl.found )
throw tfileerror ( name );
} else {
fl.directoryLevel = 0;
fl.container = NULL;
}
if ( fl.container == NULL ) {
char string[2000];
ASCString fileNameComplete = constructFileName ( string, fl.directoryLevel, NULL, name.c_str());
strm = new tn_file_buf_stream ( fileNameComplete, mode );
inp = 1;
devicename = fileNameComplete;
location = fileNameComplete;
} else {
containerstream = fl.container;
if ( containerstream ) {
containerstream->opencontainerfile ( name.c_str() );
devicename = name;
location = name + " located inside " + containerstream->getDeviceName();
inp = 2;
} else
throw tfileerror ( name );
}
fname = name;
tanycompression :: init ( );
}
ASCString tn_c_lzw_filestream::getLocation()
{
return location;
}
int tn_c_lzw_filestream :: getSize ( void )
{
if ( inp == 2 )
return containerstream->getSize();
else
return strm->getSize();
}
void tn_c_lzw_filestream :: writecmpdata ( const void* buf, int size )
{
if ( inp == 2 )
throw tinvalidmode ( fname, tnstream::reading, tnstream::writing );
else
strm->writedata ( buf, size );
}
int tn_c_lzw_filestream :: readcmpdata ( void* buf, int size, bool excpt )
{
if ( inp == 2 )
return containerstream->readcontainerdata ( buf, size, excpt );
else
return strm->readdata ( buf, size, excpt );
};
void tn_c_lzw_filestream :: writedata ( const void* buf, int size )
{
tanycompression :: writedata ( buf, size );
}
int tn_c_lzw_filestream :: readdata ( void* buf, int size, bool excpt )
{
if ( tanycompression :: mode == readingdirect && !tempbuf.size() )
if ( inp == 2 )
return containerstream->readcontainerdata ( buf, size, excpt );
else
return strm->readdata ( buf, size, excpt );
else
return tanycompression :: readdata ( buf, size, excpt );
};
time_t tn_c_lzw_filestream :: get_time ( void )
{
if ( inp == 2 )
return containerstream->get_time();
else
return strm->get_time();
}
tn_c_lzw_filestream :: ~tn_c_lzw_filestream()
{
close_compression ();
close();
if ( inp == 1 ) {
delete strm;
strm = NULL;
} else
containerstream->closecontainerfile();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int compressrle ( const void* p, void* q)
{
trleheader* sourcehead = (trleheader*) p;
if ( sourcehead->id == 16973 ) {
memcpy ( q, p, sourcehead->size + sizeof ( trleheader ) );
return sourcehead->size + sizeof ( trleheader );
}
char* s = (char*) p;
char* d = (char*) q;
trleheader* header = (trleheader*) q;
Uint16 x,y;
int size;
{
Uint16* pw = (Uint16*) s;
x = pw[0];
y = pw[1];
header->x = x;
header->y = y;
x++;
y++;
size = x * y;
header->id = 16973;
}
{
int bts[256];
memset ( bts, 0, sizeof ( bts ));
for ( int i = 0; i < size ;i++ )
bts[int(s[i+4])]++;
int min = 70000;
for ( int i = 0; i < 256; i++ )
if ( bts[i] < min ) {
min = bts[i];
header->rle = i;
}
}
s+=4;
d+=sizeof ( trleheader );
{
char* startpos = d;
int xp;
for (int j = 0; j < y ; j++ ) {
xp = 0;
unsigned char num;
unsigned char actbyte ;
do {
num = 1;
actbyte = *s;
while ( xp+num < x && num < 255 && s[num] == actbyte )
num++;
if ( num > 2 || actbyte == header->rle ) {
*(d++) = header->rle;
*(d++) = num;
*(d++) = actbyte;
} else {
*(d++) = actbyte;
if ( num > 1 )
*(d++) = actbyte;
}
s += num;
xp += num;
} while ( xp < x );
} /* endfor */
header->size = d - startpos;
}
return header->size + sizeof ( trleheader );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool patimat (const char *pat, const char *str, bool forceCaseInsensitivity )
{
switch (*pat)
{
case '\0':
return !*str;
case '*' :
return patimat(pat+1, str, forceCaseInsensitivity) || (*str && patimat(pat, str+1, forceCaseInsensitivity));
case '?' :
return *str && patimat(pat+1, str+1, forceCaseInsensitivity);
default :
if ( forceCaseInsensitivity ||
#if CASE_SENSITIVE_FILE_NAMES == 0
true
#else
false
#endif
)
return (toupper(*pat) == toupper(*str)) && patimat(pat+1, str+1, forceCaseInsensitivity);
else
return (*pat == *str) && patimat(pat+1, str+1, forceCaseInsensitivity );
}
}
bool patimat (const ASCString& pat, const ASCString& str, bool forceCaseInsensitivity)
{
return patimat( pat.c_str(), str.c_str(), forceCaseInsensitivity );
}
tfindfile :: tfindfile ( ASCString name, SearchPosition searchPosition, SearchTypes searchTypes )
{
convertPathDelimitters ( name );
if ( searchPosition == DefaultDir )
searchPosition = AllDirs;
if ( searchDirNum == 0 )
searchPosition = CurrentDir;
found = 0;
act = 0;
if ( name.empty() )
return;
ASCString directory[maxSearchDirNum];
int dirNum;
ASCString wildcard;
int ppos = name.rfind ( pathdelimitterstring );
if ( ppos != name.npos ) {
// name contains a directory entry
// checking if absolute or relative path
bool absolute = false;
if ( name[0] == pathdelimitter )
absolute = true;
if ( has_drive_letters && name.length() > 3 && name.find ( ":\\", 1 ) != name.npos )
absolute = true;
if ( absolute || searchPosition == CurrentDir ) {
directory[0].assign ( name, 0, ppos );
dirNum = 1;
} else {
ASCString strippedPath;
strippedPath.assign ( name, 0, ppos );
if ( name.find ( ASCString(".") + pathdelimitterstring ) == 0 )
strippedPath.erase( 0, 2);
int upDir = 0;
while ( name.find ( ASCString("..") + pathdelimitterstring ) == 0 ) {
upDir++;
strippedPath.erase ( 0, 3 );
}
int dirsToProcess;
if ( searchPosition == AllDirs ) {
dirsToProcess = searchDirNum;
} else
dirsToProcess = 1;
dirNum = 0;
for ( int i = 0; i < dirsToProcess; i++ ) {
ASCString dir = ascDirectory[i];
// removing the trailing pathdelimitterstring
dir.erase ( dir.length() -1 );
for ( int j = 0; j < upDir; j++ ) {
int pos = dir.rfind ( pathdelimitterstring );
if ( pos > 0 && pos == dir.npos )
dir.erase ( pos );
}
// append the trailing pathdelimitterstring again
dir += pathdelimitterstring;
directory[dirNum++] = dir + strippedPath;
}
if ( !dirNum ) {
directory[0] = ".";
dirNum = 1;
}
}
wildcard.assign ( name, ppos+1, name.npos );
} else {
if ( searchDirNum ) {
for (int i = 0; i < searchDirNum; i++ )
directory[i] = ascDirectory[i];
dirNum = searchDirNum;
} else {
directory[0] = ".";
dirNum = 1;
}
wildcard = name;
}
if ( searchTypes == All || searchTypes == OutsideContainer )
for ( int i = 0; i < dirNum; i++ ) {
DIR *dirp;
struct ASC_direct *direntp;
dirp = opendir( directory[i].c_str() );
if( dirp != NULL ) {
for(;;) {
direntp = readdir( dirp );
if ( direntp == NULL )
break;
if ( patimat ( wildcard.c_str(), direntp->d_name )) {
int localfound = 0;
for ( int j = 0; j < found; j++ )
if ( strcmpi ( fileInfo[j].name.c_str(), direntp->d_name ) == 0 )
localfound++;
if ( !localfound ) {
FileInfo fi;
fi.name = direntp->d_name;
fi.directoryLevel = i ;
fi.isInContainer = false ;
fi.location = directory[i];
char buf[1000];
ASCString fullName = constructFileName( buf, i, NULL, direntp->d_name );
struct stat statbuf;
stat( fullName.c_str(), &statbuf);
fi.size = statbuf.st_size ;
fi.date = statbuf.st_mtime;
fileInfo.push_back ( fi );
found++;
}
}
}
closedir( dirp );
}
}
if ( searchTypes == All || searchTypes == InsideContainer ) {
const ContainerCollector::FileIndex* c = containercollector.getfirstname();
while ( c ) {
if ( patimat ( name.c_str(), c->name ) ) {
int f = 0;
for ( int i = 0; i < found; i++ )
if ( stricmp ( c->name.c_str(), fileInfo[i].name.c_str() ) == 0 ) {
if ( fileInfo[i].directoryLevel <= c->directoryLevel )
f = 1;
else {
FileInfo& fi = fileInfo[i];
fi.name = c->name ;
fi.isInContainer = true;
fi.directoryLevel = c->directoryLevel;
fi.location = c->container->getDeviceName();
fi.size = c->container->getstreamsize();
fi.date = c->container->get_time();
f = 1;
}
}
if ( !f ) {
FileInfo fi;
fi.name = c->name ;
fi.directoryLevel = c->directoryLevel ;
fi.isInContainer = true ;
fi.location = c->container->getDeviceName() ;
fi.size = c->container->getstreamsize() ;
fi.date = c->container->get_time() ;
fileInfo.push_back ( fi );
found++;
}
}
c = containercollector.getnextname();
}
}
}
ASCString tfindfile :: getnextname ( int* loc, bool* inContainer, ASCString* location )
{
if ( act < found ) {
if ( loc )
*loc = fileInfo[act].directoryLevel;
if ( inContainer )
*inContainer = fileInfo[act].isInContainer;
if ( location )
*location = fileInfo[act].location;
/*
if ( directoryLevel[act] >= 0 && this->location[act] != ascDirectory[directoryLevel[act]] ) {
ASCString s = this->location[act];
appendbackslash ( s );
s += names[act++];
return s;
} else */
return fileInfo[act++].name;
} else {
if ( loc )
*loc = -1;
return "";
}
}
bool tfindfile :: getnextname ( FileInfo& fi )
{
if ( act < found ) {
fi = fileInfo[act++];
return true;
} else
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// tncontainerstream* containerstream = NULL;
int checkforvaliddirectory ( char* dir )
{
int stat = 0;
DIR *dirp;
struct ASC_direct *direntp;
/* char temp[200];
int l = strlen(dir) - 1;
for (int i = 0; i < l; i++) {
temp[i] = dir[i];
}
temp[i] = 0;
*/
dirp = opendir( dir );
if( dirp != NULL ) {
for(;;) {
direntp = readdir( dirp );
if ( direntp == NULL )
break;
if ( strcmp ( direntp -> d_name, ".") == 0 )
stat = 1;
}
closedir( dirp );
}
return stat;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
tmemorystreambuf :: tmemorystreambuf ( void )
{
initialized = false;
used = 0;
allocated = 0;
buf = 0;
memset ( dummy, 0, sizeof ( dummy ));
}
tmemorystreambuf :: ~tmemorystreambuf ( void )
{
if ( buf ) {
delete[] buf;
buf = NULL;
}
}
void tmemorystreambuf :: writetostream ( pnstream stream )
{
if ( stream ) {
stream->writeInt ( initialized );
stream->writeInt ( used );
stream->writeInt ( allocated );
for ( int i = 0; i < 10; i++ )
stream->writeInt ( dummy[i] );
if ( used > 0 )
stream->writedata ( buf, used );
}
}
void tmemorystreambuf :: readfromstream ( pnstream stream )
{
if ( stream ) {
initialized = stream->readInt();
used = stream->readInt();
allocated = stream->readInt();
for ( int i = 0; i< 10; i++ )
dummy[i] = stream->readInt();
if ( buf ) {
delete[] buf;
buf = NULL;
}
if ( used > 0 || allocated > 0 ) {
allocated = max(allocated,used);
buf = new char[allocated];
stream->readdata ( buf, used );
}
}
}
tmemorystream :: tmemorystream ( tmemorystreambuf* lbuf, IOMode lmode )
{
blocksize = 1024;
buf = lbuf;
_mode = lmode;
if ( !buf )
throw tfileerror ( "memorystream" );
if ( _mode == reading ) {
zeiger = buf->buf;
actmempos = 0;
} else
if ( _mode == writing ) { // neuen Puffer anlegen
if ( buf->buf ) {
delete[] buf->buf;
buf->buf = NULL;
}
buf->buf = new char[blocksize];
buf->allocated = blocksize;
buf->used = 0;
zeiger = buf->buf;
actmempos = 0;
}
if ( _mode == appending ) {
zeiger = buf->buf;
actmempos = buf->used;
_mode = writing;
}
}
void tmemorystream :: writedata ( const void* nbuf, int size )
{
if ( _mode != writing )
throw tinvalidmode ( "memorystream", _mode, writing );
if ( buf->used + size > buf->allocated ) {
int newsize = ((buf->used + size + blocksize - 1) / blocksize);
newsize *= blocksize;
char* tmp = new char[newsize];
memcpy ( tmp, buf->buf, buf->used );
delete[] buf->buf;
buf->buf = tmp;
buf->allocated = newsize;
}
memcpy ( &buf->buf[buf->used], nbuf, size );
buf->used += size;
}
int tmemorystream :: readdata ( void* nbuf, int size, bool excpt )
{
if (_mode != reading )
throw tinvalidmode ( "memorystream", _mode, reading );
if ( actmempos + size > buf->used ) {
if ( excpt )
throw treadafterend ( "memory stream" );
else
size = buf->used-actmempos;
}
memcpy ( nbuf, &buf->buf[actmempos], size );
actmempos += size;
return size;
}
int tmemorystream :: dataavail ( void )
{
if ( _mode == writing )
return 1;
else
return actmempos < buf->used;
}
ASCString getnextfilenumname ( const ASCString& first, const ASCString& suffix, int num )
{
ASCString name;
if ( num < 0 )
num = 0;
do {
name = first;
while ( name.length() - first.length() + ASCString::toString(num).length() < 3 )
name += "0";
name += ASCString::toString(num) + "." + suffix;
tfindfile ff ( name );
ASCString c = ff.getnextname();
if ( c.empty() )
return name;
num++;
} while ( true );
return "";
}
bool exist ( const ASCString& s )
{
tfindfile ff ( s );
return !ff.getnextname().empty();
}
#include "oldlzw.cpp"
void tnbufstream :: writebuffer( void ) {
}
void opencontainer ( const char* wildcard )
{
if ( !searchDirNum )
addSearchPath(".");
containercollector.init ( wildcard );
}
time_t get_filetime ( const char* fileName )
{
FileLocation fl;
locateFile ( fileName, &fl );
if ( fl.found ) {
if ( fl.container )
return fl.container->get_time();
else {
struct stat stbuf;
char buf[ maxFileStringSize ];
if ( !stat ( constructFileName ( buf, fl.directoryLevel, NULL, fileName), &stbuf) )
return ( stbuf.st_mtime);
else
return -1;
}
} else
return -1;
}
int filesize( const char *name)
{
struct stat buf;
if ( !stat (name, &buf))
return (buf.st_size);
else
return -1;
}
bool directoryExist ( const ASCString& path )
{
bool existence = false;
DIR *dirp = opendir( path.c_str() );
if( dirp ) {
if ( readdir( dirp ) )
existence = true;
else
existence = false;
closedir( dirp );
}
return existence;
}
void addSearchPath ( const ASCString& path )
{
if ( !path.empty() ) {
ASCString s = constructFileName ( -3, path, "" );
if ( directoryExist( s.c_str() )) {
bool found = false;
for ( int i = 0; i < searchDirNum; i++ )
if ( s == ascDirectory[i] )
found = true;
if ( !found )
ascDirectory[ searchDirNum++ ] = strdup ( s.c_str() );
}
}
}
int getSearchPathNum()
{
return searchDirNum;
}
ASCString getSearchPath ( int i )
{
if ( i < searchDirNum )
return ascDirectory[i];
else
return "";
}
char* extractPath ( char* buf, const char* filename )
{
if ( buf && filename ) {
if ( strchr ( filename, pathdelimitter )) {
strcpy ( buf, filename );
int i = strlen ( buf )-1;
while ( buf[i] != pathdelimitter )
i--;
buf[i+1] = 0;
} else
strcpy ( buf, "./" );
}
return buf;
}
char* extractFileName ( char* buf, const char* filename )
{
if ( buf && filename ) {
if ( strchr ( filename, pathdelimitter )) {
int i = strlen ( filename )-1;
while ( filename[i] != pathdelimitter )
i--;
strcpy ( buf, filename +i+1);
} else
strcpy ( buf, filename );
}
return buf;
}
ASCString extractFileName ( const ASCString& filename )
{
char buf[10000];
return extractFileName( buf, filename.c_str() );
}
ASCString extractFileName_withoutSuffix ( const ASCString& filename )
{
char buf[10000];
extractFileName( buf, filename.c_str() );
char* c = strchr ( buf, '.' );
if ( c )
*c = 0;
return ASCString(buf);
}
void appendbackslash ( char* string )
{
if ( strlen ( string ) && string[strlen ( string ) -1] != pathdelimitter )
strcat ( string, pathdelimitterstring );
}
void appendbackslash ( ASCString& string )
{
if ( !string.empty() && string[ string.length() -1] != pathdelimitter )
string += pathdelimitterstring ;
}
int createDirectory ( const char* name )
{
#ifdef _UNIX_
char *nname;
int i;
if (name == NULL || (nname=strdup(name)) == NULL)
return -1;
i = strlen(nname);
/* leave one '/' */
while (i>1 && nname[i-1] == '/')
nname[--i] = '\0';
i = mkdir ( nname, 0700 );
free(nname);
return i;
#else
return mkdir ( name );
#endif
}
ASCString FileName::suffix ( )
{
size_type slash = find_last_of ( pathdelimitterstring );
size_type point = find_last_of ( "." );
if ( point == npos )
return "";
else
if ( slash == npos || slash < point )
return substr(point+1);
else
return "";
}
void convertPathDelimitters ( ASCString& path )
{
int pos;
while ( (pos = path.find ( foreignPathDelimitterString )) != path.npos )
path.replace ( pos, 1, pathdelimitterstring );
}
|