1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
|
// vim:ft=pascal
unit YTools;
{===============================================================================
cYcnus.YTools 1.0.3 Beta for Delphi 4+
by licenser and Murphy
2000-2003 by cYcnus
visit www.cYcnus.de
licenser@cYcnus.de (Heinz N. Gies)
murphy@cYcnus.de (Kornelius Kalnbach)
this unit is published under the terms of the GPL
===============================================================================}
interface
uses
Windows, SysUtils, Classes, YTypes;
const
BackSpace = #8;
Tab = #9;
LF = #10; //Line Feed
CR = #13; //Carriage Return
Space = #32;
EOLChars = [CR, LF];
{$IFNDEF VER140}
sLineBreak = #13#10;
SwitchChars = ['/', '-'];
{$ENDIF}
EOL = sLineBreak;
MaxCard = High(Cardinal);
AllChars = [#0..#255];
Alphabetical = ['A'..'Z', 'a'..'z'];
DecimalChars = ['0'..'9'];
AlphaNumerical = Alphabetical + DecimalChars;
StrangeChars = [#0..#31, #127, #129, #141..#144, #157, #158];
HexadecimalChars = DecimalChars + ['A'..'F', 'a'..'f'];
OctalChars = ['0'..'7'];
BinaryChars = ['0', '1'];
QuoteChars = ['''', '"'];
WildCards = ['*', '?'];
FileNameEnemies = WildCards + ['\', '/', ':', '<', '>', '|'];
HexChar: array[THex] of Char = (
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
LowerHexChar: array[THex] of Char = (
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
BaseNChar: array[TBaseN] of Char = (
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
cYcnusOverlayColor = $050001;
faFindEveryFile = faReadOnly + faHidden + faSysFile + faArchive;
platWin9x = [VER_PLATFORM_WIN32s, VER_PLATFORM_WIN32_WINDOWS];
{ Debugging }
procedure ClearReport(const ReportName: string);
procedure Report(const ReportName, Text: string);
procedure ReportFmt(const ReportName, Fmt: string; const Args: array of const);
{ Params }
procedure GetParams(Strings: TStrings); overload;
function GetParams(const Separator: string = ' '): string; overload;
function ParamNum(const S: string): Integer;
function ParamPrefixNum(const Prefix: string): Integer;
function Param(const S: string): Boolean;
function ParamPrefix(const Prefix: string): Boolean;
function Switch(const Switch: string; const PrefixChars: TCharSet = SwitchChars;
IgnoreCase: Boolean = True): Boolean;
function GetParam(const Prefix: string = ''; const Default: string = ''): string;
{ Dirs & UserName}
function GetMyDir(FullPath: Boolean = False): string;
function WinDir: string;
function SysDir: string;
function UserName: string;
{ Strings & Chars}
function FirstChar(const S: string): Char;
function LastChar(const S: string): Char;
function CharPos(C: Char; const S: string; Offset: Integer = 1): Integer; overload;
function CharPos(C: TCharSet; const S: string; Offset: Integer = 1): Integer; overload;
function CharPosR(C: Char; const S: string; Offset: Integer = -1): Integer;
function PosEx(const SubStr, S: string; Offset: Integer = 1): Integer;
function PosExText(const SubStr, S: string; Offset: Integer = 1): Integer;
function PosExAnsiText(const SubStr, S: string; Offset: Integer = 1): Integer;
function UntilChar(const S: string; Brake: Char): string; overload;
function UntilChar(const S: string; Brake: TCharSet): string; overload;
function UntilLastChar(const S: string; Brake: Char;
IgnoreNoBrake: Boolean = True): string;
function FromChar(const S: string; Brake: Char): string; overload;
function FromChar(const S: string; Brake: TCharSet): string; overload;
function FromLastChar(const S: string; Brake: Char;
IgnoreNoBrake: Boolean = False): string;
function BetweenChars(const S: string; Start, Finish: Char;
Inclusive: Boolean = False): string;
function UntilStr(const S: string; Brake: string): string;
function FromStr(const S: string; Brake: string): string;
function StringWrap(const S: string; Width: Integer; const LineEnd: string = EOL): string;
{ Splitting & Combining }
function Split(const S, Separator: string; IgnoreMultiSep: Boolean = True;
MinCount: Integer = 0): TStrA; overload;
procedure Split(const S, Separator: string; Strings: TStrings;
IgnoreMultiSep: Boolean = True); overload;
function Split(const S: string; Separators: TCharSet;
IgnoreMultiSep: Boolean = True; MinCount: Integer = 0): TStrA; overload;
procedure TileStr(const S: string; BrakeStart: Integer; BrakeEnd: Integer;
out Left, Right: string);
function Join(Strings: TStrings; Separator: string = ' '): string; overload;
function Join(StrA: TStrA; Separator: string = ' '): string; overload;
function MulStr(const S: string; Count: Integer): string;
{ Strings ausrichten }
function AlignR(const S: string; Width: Integer; Filler: Char = ' '): string;
function MaxStr(const S: string; MaxLen: Integer): string;
{ Stringing }
function TrimAll(const S: string): string;
function ControlChar(C: Char): Boolean;
function FriendlyChar(C: Char): Char;
function FriendlyStr(const S: string): string; overload;
function FriendlyStr(a: TByteA): string; overload;
function Quote(const S: string; Quoter: Char = '"'): string;
function UnQuote(const S: string): string;
function DeQuote(const S: string): string;
function StrNumerus(const Value: Integer; const Singular, Plural: string;
const Zero: string = '0'): string;
function MakeStr(const Items: array of const; Separator: string = ''): string;
procedure ShowText(const Items: array of const; Separator: string = '');
{ Delete }
function DeleteChars(const S: string; C: Char): string; overload;
function DeleteChars(const S: string; C: TCharSet): string; overload;
function ExtractChars(const S: string; C: TCharSet): string;
{ Find }
function CharCount(const S: string; C: Char): Integer;
function CharIn(const S: string; C: Char): Boolean; overload;
function CharIn(const S: string; C: TCharSet): Boolean; overload;
function StrAtPos(const S: string; Pos: Integer; const Str: string): Boolean;
function StrAtBegin(const S, Str: string): Boolean;
function StrIn(const S, SubStr: string): Boolean; overload;
function StrIn(A: TStrA; const S: string): Boolean; overload;
function StrIn(SL: TStrings; const S: string): Boolean; overload;
function StrIndex(A: TStrA; const S: string): Integer; overload;
function StrIndex(SL: TStrings; const S: string): Integer; overload;
function TextAtPos(const S: string; Pos: Integer; const Text: string): Boolean;
function TextAtBegin(const S, Text: string): Boolean;
function TextIn(const S, Text: string): Boolean; overload;
function TextIn(A: TStrA; const Text: string): Boolean; overload;
function TextIn(SL: TStrings; const Text: string): Boolean; overload;
function TextIndex(A: TStrA; const Text: string): Integer; overload;
function TextIndex(SL: TStrings; const Text: string): Integer; overload;
{ Replace }
function ReplaceChars(const S: string; Old, New: Char): string; overload;
function ReplaceChars(const S: string; Old: TCharSet; New: Char): string; overload;
function Replace(const S, Old, New: string): string;
{ TStrings }
function SLOfFile(const FileName: string): TStringList;
function ContainsEmptyLines(SL: TStrings): Boolean;
procedure DeleteEmptyLines(SL: TStrings);
procedure DeleteCommentLines(SL: TStrings; const CommentSign: string = '//');
procedure WriteSL(Strings: TStrings; const Prefix: string = '';
const Suffix: string = '');
function FindLine(SL: TStrings; const S: string): Integer;
procedure QuickSortSL(SL: TStringList);
{ TStrA }
function IncStrA(StrA: TStrA): Integer;
{ TByteA }
function StrOfByteA(a: TByteA): string;
function ByteAOfStr(const S: string): TByteA;
function ByteAOfInt(i: Integer): TByteA;
function IntOfByteA(A: TByteA): Integer;
function ByteAOfHex(const Hex: string): TByteA;
function SameByteA(const A, B: TByteA): Boolean;
function Reverse(a: TByteA): TByteA;
function SaveByteA(Data: TByteA; const FileName: string; Overwrite: Boolean = True): Boolean;
function LoadByteA(const FileName: string): TByteA;
function Endian(i: Integer): Integer;
{ Files }
function SizeOfFile(const FileName: string): Integer;
function FileEx(const FileName: string; AllowFolders: Boolean = False): Boolean;
function LWPSolve(const Dir: string): string;
function LWPSlash(const Dir: string): string;
function ExtractDrive(const FileName: string): string;
function ExtractPath(const FileName: string): string;
function ExtractPrefix(const FileName: string): string;
function ExtractSuffix(const FileName: string): string;
function IsValidFileName(const FileName: string): Boolean;
function MakeValidFileName(FileName: string; const Default: string = 'File'): string;
{ Converting }
function IsValidInteger(const S: string): Boolean;
function IsValidCardinal(const S: string): Boolean;
function StrOfBool(flag: Boolean; const TrueStr: string = 'True';
const FalseStr: string = 'False'): string;
function StrOfInt(i: Integer): string;
function CardOfStr(const S: string): Cardinal;
function HexOrd(Hex: Char): THex;
function ByteOfHex(Hex: THexByteStr): Byte;
function DecOfHex(const Hex: string): string;
function HexOfByte(b: Byte): THexByteStr;
function HexOfCard(i: Cardinal): string; overload;
function HexOfCard(i: Cardinal; Digits: Integer): string; overload;
function PascalHexArray(a: TByteA; Name: string): string;
function HexOfByteA(a: TByteA; Blocks: Integer = 1;
const Splitter: string = ' '): string;
function BinOfByteA(a: TByteA; Blocks: Integer = 4;
const Splitter: string = ' '): string;
function CardOfHex(Hex: string): Cardinal;
function IntOfBin(Bin: string): Cardinal;
function BinOfIntFill(n: cardinal; MinCount: Integer = 8): string;
function BinOfInt(n: cardinal): string;
function BaseNOfInt(I: Cardinal; B: TBaseN): string;
function IntOfBaseN(V: string; B: TBaseN): Cardinal;
{ Ranges }
function KeepIn(i, Bottom, Top: Variant): Variant;
function InRange(Value, Bottom, Top: Variant): Boolean;
function InStrictRange(Value, Bottom, Top: Variant): Boolean;
function Min(const A, B: Integer): Integer; overload;
function Min(const A: TIntA): Integer; overload;
function Max(const A, B: Integer): Integer; overload;
function Max(const A: TIntA): Integer; overload;
const
RangesSeparator = ',';
RangeInnerSeparator = '-';
RangeInfinite = '*';
RangeSpecialChars = [RangesSeparator, RangeInnerSeparator, RangeInfinite];
function RangesOfStr(const S: string): TRanges;
function InRanges(Ranges: TRanges; TestValue: Cardinal): Boolean;
function Success(Res: Integer; ResultOnSuccess: Integer = ERROR_SUCCESS): Boolean;
function Failure(Res: Integer; ResultOnSuccess: Integer = ERROR_SUCCESS): Boolean;
function ExpandString(const S: string): string;
{ Files }
procedure DeleteFiles(const Mask: string; ScanSubDirs: Boolean = True;
Attributes: Integer = faFindEveryFile);
procedure FileNew(const FileName: string);
function DateTimeOfFileTime(const FileTime: TFileTime): TDateTime;
{ FileNames }
function GetFileNew(FileName: string; NoFloppyDrives: Boolean = True): string;
{ Finding Files }
function FindAll(Strings: TStrings; const Mask: string;
ScanSubDirs: Boolean = True; Attributes: Integer = faFindEveryFile;
FileReturn: TFileNameFunc = nil): Boolean;
function FindAllFirst(const Mask: string; ScanSubDirs: Boolean = True;
Attributes: Integer = faFindEveryFile): string;
function FullOSInfo: string;
function Win32PlatformStr: string;
function Win9x: Boolean;
function WinNT: Boolean;
function Win2000: Boolean;
function WinXP: Boolean;
var
MyDir: string = '';
LastSuccessRes: Integer = 0;
{ Backward compatibility }
{$IFNDEF VER130}
function SameText(const S1, S2: string): Boolean;
{$ENDIF}
implementation
{$IFNDEF VER140}
uses FileCtrl;
{$ENDIF}
{$IFNDEF VER130}
function SameText(const S1, S2: string): Boolean;
begin
Result := CompareText(S1, S2) = 0;
end;
{$ENDIF}
procedure Report(const ReportName, Text: string);
var
F: TextFile;
FileName: string;
begin
FileName := MyDir + ReportName + '.rep';
Assign(F, FileName);
try
if not FileExists(FileName) then
Rewrite(F)
else
Append(F);
WriteLn(F, Text);
finally
Close(F);
end;
end;
procedure ClearReport(const ReportName: string);
var
FileName: string;
begin
FileName := MyDir + ReportName + '.rep';
DeleteFile(FileName);
end;
procedure ReportFmt(const ReportName, Fmt: string; const Args: array of const);
begin
Report(ReportName, Format(Fmt, Args));
end;
procedure GetParams(Strings: TStrings);
var
P: PChar;
Param: string;
function GetParamStr(var P: PChar; var Param: string): Boolean;
var
Quoted: Boolean;
begin
Param := '';
repeat
while (P[0] <> #0) and (P[0] <= ' ') do
Inc(P);
Quoted := False;
while P[0] <> #0 do begin
if P[0] = '"' then begin
Quoted := not Quoted;
Inc(P);
Continue; end;
if (P[0] <= ' ') and not Quoted then
Break;
Param := Param + P[0];
Inc(P);
end;
until (Param <> '') or (P[0] = #0);
Result := Param <> '';
end;
begin
Strings.Clear;
P := GetCommandLine;
GetParamStr(P, Param);
while GetParamStr(P, Param) do
Strings.Add(Param);
end;
function GetParams(const Separator: string = ' '): string;
var
SL: TStringList;
begin
SL := TStringList.Create;
GetParams(SL);
Result := Join(SL, Separator);
SL.Free;
end;
function Switch(const Switch: string; const PrefixChars: TCharSet = SwitchChars;
IgnoreCase: Boolean = True): Boolean;
//= SysUtils.FindCmdLineSwitch
var
i: Integer;
s: string;
begin
Result := True;
for i := 1 to ParamCount do begin
s := ParamStr(i);
if (s <> '') and (s[1] in PrefixChars) then begin
//i know that always s <> '', but this is saver
s := Copy(s, 2, MaxInt);
if (s = Switch) or (IgnoreCase and (0=AnsiCompareText(s, Switch))) then
Exit;
end;
end;
Result := False;
end;
function ParamNum(const S: string): Integer;
begin
for Result := 1 to ParamCount do
if 0=AnsiCompareText(ParamStr(Result), S) then
Exit;
Result := 0;
end;
function ParamPrefixNum(const Prefix: string): Integer;
var
Len: Integer;
begin
Len := Length(Prefix);
for Result := 1 to ParamCount do
if 0=AnsiCompareText(Copy(ParamStr(Result), 1, Len), Prefix) then
Exit;
Result := 0;
end;
function Param(const S: string): Boolean;
begin
Result := ParamNum(S) > 0;
end;
function ParamPrefix(const Prefix: string): Boolean;
begin
Result := ParamPrefixNum(Prefix) > 0;
end;
function GetParam(const Prefix: string = ''; const Default: string = ''): string;
var
i: Integer;
begin
Result := Default;
if Prefix = '' then begin
Result := ParamStr(1);
Exit; end;
i := ParamPrefixNum(Prefix);
if i > 0 then
Result := Copy(ParamStr(i), Length(Prefix) + 1, MaxInt);
end;
function GetMyDir(FullPath: Boolean = False): string;
var
Buffer: array[0..260] of Char;
begin
Result := '';
SetString(Result, Buffer, GetModuleFileName(0, Buffer, SizeOf(Buffer)));
if FullPath then
Result := GetFileNew(Result);
Result := ExtractPath(Result);
end;
function WinDir: string;
var
Res: PChar;
begin
Result := '\';
GetMem(Res, MAX_PATH);
GetWindowsDirectory(Res, MAX_PATH);
Result := Res + '\';
FreeMem(Res, MAX_PATH);
end;
function SysDir: string;
var
Res: PChar;
begin
Result := '\';
GetMem(Res, MAX_PATH);
GetSystemDirectory(Res, MAX_PATH);
Result := Res + '\';
FreeMem(Res, MAX_PATH);
end;
function UserName: string;
var
Len: Cardinal;
Res: PChar;
begin
Result := '';
GetMem(Res, MAX_PATH);
Len := MAX_PATH;
GetUserName(Res, Len);
Result := Res;
FreeMem(Res, MAX_PATH);
end;
function FirstChar(const S: string): Char;
begin
if s = '' then
Result := #0
else
Result := s[1];
end;
function LastChar(const S: string): Char;
begin
if s = '' then
Result := #0
else
Result := s[Length(s)];
end;
function CharPos(C: Char; const S: string; Offset: Integer = 1): Integer;
var
MaxPosToSearch: Integer;
begin
Result := Offset;
MaxPosToSearch := Length(S);
while Result <= MaxPosToSearch do begin
if S[Result] = C then
Exit;
Inc(Result);
end;
Result := 0;
end;
function CharPos(C: TCharSet; const S: string; Offset: Integer = 1): Integer;
var
MaxPosToSearch: Integer;
begin
Result := Offset;
MaxPosToSearch := Length(S);
while Result <= MaxPosToSearch do begin
if S[Result] in C then
Exit;
Inc(Result);
end;
Result := 0;
end;
function CharPosR(C: Char; const S: string; Offset: Integer = -1): Integer;
begin
if Offset < 0 then
Result := Length(S) + 1 - Offset
else
Result := Offset;
if Result > Length(S) then
Result := Length(S);
while Result > 0 do begin
if S[Result] = C then
Exit;
Dec(Result);
end;
end;
function PosEx(const SubStr, S: string; Offset: Integer = 1): Integer;
var
MaxPosToSearch, LenSubStr, i: Integer;
begin
if SubStr = '' then begin
Result := 0;
Exit; end;
if Offset < 1 then
Result := 1
else
Result := Offset;
LenSubStr := Length(SubStr);
MaxPosToSearch := Length(S) - LenSubStr + 1;
while Result <= MaxPosToSearch do begin
if S[Result] = SubStr[1] then begin
i := 1;
while (i < LenSubStr)
and (S[Result + i] = SubStr[i + 1]) do
Inc(i);
if i = LenSubStr then
Exit;
end;
Inc(Result);
end;
Result := 0;
end;
function PosExText(const SubStr, S: string; Offset: Integer = 1): Integer;
var
MaxPosToSearch, LenSubStr, i: Integer;
function SameChar(a, b: Char): Boolean;
begin
Result := UpCase(a) = UpCase(b)
end;
begin
if SubStr = '' then begin
Result := 0;
Exit; end;
if Offset < 1 then
Result := 1
else
Result := Offset;
LenSubStr := Length(SubStr);
MaxPosToSearch := Length(S) - LenSubStr + 1;
while Result <= MaxPosToSearch do begin
if SameChar(S[Result], SubStr[1]) then begin
i := 1;
while (i < LenSubStr)
and (SameChar(S[Result + i], SubStr[i + 1])) do
Inc(i);
if i = LenSubStr then
Exit;
end;
Inc(Result);
end;
Result := 0;
end;
function PosExAnsiText(const SubStr, S: string; Offset: Integer = 1): Integer;
var
MaxPosToSearch, LenSubStr, i: Integer;
function SameChar(a, b: Char): Boolean;
begin
Result := CharLower(PChar(a)) = CharLower(PChar(b));
end;
begin
if SubStr = '' then begin
Result := 0;
Exit; end;
if Offset < 1 then
Result := 1
else
Result := Offset;
LenSubStr := Length(SubStr);
MaxPosToSearch := Length(S) - LenSubStr + 1;
while Result <= MaxPosToSearch do begin
if SameChar(S[Result], SubStr[1]) then begin
i := 1;
while (i < LenSubStr)
and (SameChar(S[Result + i], SubStr[i + 1])) do
Inc(i);
if i = LenSubStr then
Exit;
end;
Inc(Result);
end;
Result := 0;
end;
function UntilChar(const S: string; Brake: Char): string;
var
p: Integer;
begin
p := CharPos(Brake, S);
if p > 0 then
Result := Copy(S, 1, p - 1)
else
Result := S;
end;
function UntilChar(const S: string; Brake: TCharSet): string;
var
p: Integer;
begin
Result := '';
p := CharPos(Brake, S);
if p > 0 then
Result := Copy(S, 1, p - 1)
else
Result := S;
end;
function UntilLastChar(const S: string; Brake: Char;
IgnoreNoBrake: Boolean = True): string;
var
p: Integer;
begin
Result := '';
p := CharPosR(Brake, S);
if p > 0 then
Result := Copy(S, 1, p - 1)
else if IgnoreNoBrake then
Result := S;
end;
function FromChar(const S: string; Brake: Char): string;
var
p: Integer;
begin
Result := '';
p := CharPos(Brake, S);
if p > 0 then
Result := Copy(S, p + 1, Length(S) - p);
end;
function FromChar(const S: string; Brake: TCharSet): string;
var
p: Integer;
begin
Result := '';
p := CharPos(Brake, S);
if p > 0 then
Result := Copy(S, p + 1, Length(S) - p);
end;
function FromLastChar(const S: string; Brake: Char;
IgnoreNoBrake: Boolean = False): string;
var
p: Integer;
begin
Result := '';
p := CharPosR(Brake, S);
if p > 0 then
Result := Copy(S, p + 1, Length(S) - p)
else if IgnoreNoBrake then
Result := S;
end;
function BetweenChars(const S: string; Start, Finish: Char;
Inclusive: Boolean = False): string;
var
p, fin: Integer;
begin
Result := '';
p := CharPos(Start, S);
if p = 0 then
Exit;
fin := CharPos(Finish, S, p + 1);
if fin = 0 then
Exit;
if not Inclusive then begin
Inc(p);
Dec(fin);
end;
Result := Copy(S, p, fin - p + 1);
end;
function UntilStr(const S: string; Brake: string): string;
var
p: Integer;
begin
if Length(Brake) = 1 then begin
Result := UntilChar(S, Brake[1]);
Exit; end;
p := PosEx(Brake, S);
if p > 0 then
Result := Copy(S, 1, p - 1)
else
Result := S;
end;
function FromStr(const S: string; Brake: string): string;
var
p: Integer;
begin
if Length(Brake) = 1 then begin
Result := FromChar(S, Brake[1]);
Exit; end;
Result := '';
p := PosEx(Brake, s);
if p > 0 then begin
Inc(p, Length(Brake));
Result := Copy(S, p, Length(S) - p + 1);
end;
end;
function StringWrap(const S: string; Width: Integer; const LineEnd: string = EOL): string;
var
i: Integer;
begin
Result := '';
if (S = '') or (Width < 1) then
Exit;
i := 1;
while True do begin
Result := Result + Copy(S, i, Width);
Inc(i, Width);
if i <= Length(S) then
Result := Result + LineEnd
else
Exit;
end;
end;
function Split(const S, Separator: string; IgnoreMultiSep: Boolean = True;
MinCount: Integer = 0): TStrA;
var
p, fin, SepLen: Integer;
procedure Add(const S: string);
begin
if IgnoreMultiSep and (S = '') then
Exit;
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := S;
end;
begin
if S = '' then begin
if Length(Result) < MinCount then
SetLength(Result, MinCount);
Exit; end;
Result := nil;
SepLen := Length(Separator);
p := 1;
fin := PosEx(Separator, S);
while fin > 0 do begin
Add(Copy(S, p, fin - p));
p := fin + SepLen;
fin := PosEx(Separator, S, p);
end;
Add(Copy(S, p, Length(S) - p + 1));
if Length(Result) < MinCount then
SetLength(Result, MinCount);
end;
procedure Split(const S, Separator: string; Strings: TStrings;
IgnoreMultiSep: Boolean = True);
var
p, fin, SepLen: Integer;
procedure Add(const S: string);
begin
if IgnoreMultiSep and (S = '') then
Exit;
Strings.Add(S);
end;
begin
if S = '' then
Exit;
Strings.BeginUpdate;
SepLen := Length(Separator);
p := 1;
fin := PosEx(Separator, S);
while fin > 0 do begin
Add(Copy(S, p, fin - p));
p := fin + SepLen;
fin := PosEx(Separator, S, p);
end;
Add(Copy(S, p, Length(S) - p + 1));
Strings.EndUpdate;
end;
function Split(const S: string; Separators: TCharSet;
IgnoreMultiSep: Boolean = True; MinCount: Integer = 0): TStrA;
var
p, fin: Integer;
procedure Add(const S: string);
begin
if IgnoreMultiSep and (S = '') then
Exit;
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := S;
end;
begin
if S = '' then begin
if Length(Result) < MinCount then
SetLength(Result, MinCount);
Exit; end;
Result := nil;
p := 1;
fin := CharPos(Separators, S);
while fin > 0 do begin
Add(Copy(S, p, fin - p));
p := fin + 1;
fin := CharPos(Separators, S, p);
end;
Add(Copy(S, p, Length(S) - p + 1));
if Length(Result) < MinCount then
SetLength(Result, MinCount);
end;
procedure TileStr(const S: string; BrakeStart: Integer; BrakeEnd: Integer;
out Left, Right: string);
begin
Left := Copy(S, 1, BrakeStart-1);
Right := Copy(S, BrakeEnd + 1, MaxInt);
end;
function Join(Strings: TStrings; Separator: string = ' '): string;
var
i, imax: Integer;
begin
Result := '';
imax := Strings.Count-1;
for i := 0 to imax do begin
Result := Result + Strings[i];
if i < imax then
Result := Result + Separator;
end;
end;
function Join(StrA: TStrA; Separator: string = ' '): string; overload;
var
i: Integer;
begin
Result := '';
for i := 0 to High(StrA) do begin
Result := Result + StrA[i];
if i < High(StrA) then
Result := Result + Separator;
end;
end;
function MulStr(const S: string; Count: Integer): string;
var
P: PChar;
Len, i: Integer;
begin
Result := '';
if Count = 0 then
Exit;
Len := Length(S);
SetLength(Result, Len * Count);
P := Pointer(Result);
for i := 1 to Count do begin
Move(Pointer(S)^, P^, Len);
Inc(P, Len);
end;
end;
function AlignR(const S: string; Width: Integer; Filler: Char = ' '): string;
begin
Result := MulStr(Filler, Width - Length(S)) + S;
end;
function MaxStr(const S: string; MaxLen: Integer): string;
var
Len: Integer;
begin
Len := Length(S);
if Len <= MaxLen then begin
Result := S;
Exit end;
Result := Copy(S, 1, MaxLen - 3) + '...';
end;
function TrimAll(const S: string): string;
var
i: Integer;
begin
for i := 1 to Length(S) do
if S[i] > #32 then
Result := Result + S[i];
end;
function ControlChar(C: Char): Boolean;
begin
Result := C in StrangeChars;
end;
function FriendlyChar(C: Char): Char;
begin
case C of
#0: Result := '.';
#1..#31: Result := '?';
#255: Result := '#';
else
Result := C;
end;
end;
function FriendlyStr(const S: string): string;
var
i: Integer;
begin
SetLength(Result, Length(S));
for i := 1 to Length(S) do
Result[i] := FriendlyChar(S[i]);
end;
function FriendlyStr(a: TByteA): string;
var
i: Integer;
begin
SetLength(Result, Length(a));
for i := 0 to High(a) do
Result[i + 1] := FriendlyChar(Char(a[i]));
end;
function Quote(const S: string; Quoter: Char = '"'): string;
begin
Result := S;
if FirstChar(S) <> Quoter then
Result := Quoter + Result;
if LastChar(S) <> Quoter then
Result := Result + Quoter;
end;
function DeQuote(const S: string): string;
begin
Result := '';
if Length(S) > 2 then
Result := Copy(S, 2, Length(S) - 2);
end;
function UnQuote(const S: string): string;
var
Start, Len: Integer;
begin
Start := 1;
Len := Length(S);
if (S <> '') and (S[1] in ([#0..#32] + QuoteChars)) then begin
if (LastChar(S) = S[1]) then
Dec(Len);
Inc(Start);
end;
Result := Copy(S, Start, Len - Start + 1);
end;
function StrNumerus(const Value: Integer; const Singular, Plural: string;
const Zero: string = '0'): string;
begin
if Abs(Value) = 1 then
Result := IntToStr(Value) + ' ' + Singular
else if Value = 0 then
Result := Zero + ' ' + Plural
else
Result := IntToStr(Value) + ' ' + Plural;
end;
function MakeStr(const Items: array of const; Separator: string = ''): string;
const
BoolStrings: array[Boolean] of string = ('False', 'True');
var
i: Integer;
function StrOfP(P: Pointer): string;
begin
if P = nil then
Result := '[nil]'
else
Result := '[' + IntToStr(Cardinal(P)) + ']';
end;
procedure Add(const S: string);
begin
Result := Result + s + Separator;
end;
begin
Result := '';
for i := 0 to High(Items) do
with Items[i] do
case VType of
vtString: Add(VString^);
vtInteger: Add(IntToStr(VInteger));
vtBoolean: Add(BoolStrings[VBoolean]);
vtChar: Add(VChar);
vtPChar: Add(VPChar);
vtExtended: Add(FloatToStr(VExtended^));
vtObject: if VObject is TComponent then
Add(TComponent(VObject).Name)
else
Add(VObject.ClassName);
vtClass: Add(VClass.ClassName);
vtAnsiString: Add(string(VAnsiString));
vtCurrency: Add(CurrToStr(VCurrency^));
vtInt64: Add(IntToStr(VInt64^));
vtVariant: Add(string(VVariant^));
vtWideChar: Add(VWideChar);
vtPWideChar: Add(VPWideChar);
vtInterface: Add(StrOfP(VInterface));
vtPointer: Add(StrOfP(VPointer));
vtWideString: Add(WideString(VWideString));
end;
if Result <> '' then
SetLength(result, Length(Result) - Length(Separator));
end;
procedure ShowText(const Items: array of const; Separator: string = '');
var
Text: string;
begin
Text := MakeStr(Items, Separator);
MessageBox(0, PChar(Text), 'Info', MB_OK and MB_APPLMODAL);
end;
function DeleteChars(const S: string; C: Char): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(S) do
if S[i] <> C then
Result := Result + S[i];
end;
function DeleteChars(const S: string; C: TCharSet): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(S) do
if not (S[i] in C) then
Result := Result + S[i];
end;
function ExtractChars(const S: string; C: TCharSet): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(S) do
if S[i] in C then
Result := Result + S[i];
end;
function CharCount(const S: string; C: Char): Integer;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(S) do
if S[i] = C then
Inc(Result);
end;
function StrAtPos(const S: string; Pos: Integer; const Str: string): Boolean;
begin
Result := (Str <> '') and (Str = Copy(S, Pos, Length(Str)));
end;
function TextAtPos(const S: string; Pos: Integer; const Text: string): Boolean;
begin
Result := (Text <> '') and SameText(Text, Copy(S, Pos, Length(Text)));
end;
function StrAtBegin(const S, Str: string): Boolean;
begin
Result := StrAtPos(S, 1, Str);
end;
function TextAtBegin(const S, Text: string): Boolean;
begin
Result := TextAtPos(S, 1, Text);
end;
function CharIn(const S: string; C: Char): Boolean;
var
i: Integer;
begin
Result := True;
for i := 1 to Length(S) do
if S[i] = C then Exit;
Result := False;
end;
function CharIn(const S: string; C: TCharSet): Boolean;
var
i: Integer;
begin
Result := False;
for i := 1 to Length(S) do begin
Result := S[i] in C;
if Result then
Exit;
end;
end;
function StrIn(const S, SubStr: string): Boolean;
begin
Result := PosEx(SubStr, S) > 0;
end;
function StrIn(SL: TStrings; const S: string): Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to SL.Count-1 do begin
Result := (S = SL[i]);
if Result then
Exit;
end;
end;
function StrIn(A: TStrA; const S: string): Boolean;
var
i: Integer;
begin
Result := False;
for i := Low(A) to High(A) do begin
Result := (S = A[i]);
if Result then
Exit;
end;
end;
function TextIn(const S, Text: string): Boolean;
begin
Result := PosExText(Text, S) > 0;
end;
function TextIn(SL: TStrings; const Text: string): Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to SL.Count-1 do begin
Result := SameText(Text, SL[i]);
if Result then
Exit;
end;
end;
function TextIn(A: TStrA; const Text: string): Boolean;
var
i: Integer;
begin
Result := False;
for i := Low(A) to High(A) do begin
Result := SameText(Text, A[i]);
if Result then
Exit;
end;
end;
function StrIndex(SL: TStrings; const S: string): Integer;
begin
for Result := 0 to SL.Count-1 do
if S = SL[Result] then
Exit;
Result := -1;
end;
function StrIndex(A: TStrA; const S: string): Integer;
begin
for Result := Low(A) to High(A) do
if S = A[Result] then
Exit;
Result := -1;
end;
function TextIndex(SL: TStrings; const Text: string): Integer;
begin
for Result := 0 to SL.Count-1 do
if SameText(Text, SL[Result]) then
Exit;
Result := -1;
end;
function TextIndex(A: TStrA; const Text: string): Integer;
begin
for Result := Low(A) to High(A) do
if SameText(Text, A[Result]) then
Exit;
Result := -1;
end;
function ReplaceChars(const S: string; Old, New: Char): string;
var
i: Integer;
begin
Result := S;
for i := 1 to Length(Result) do
if Result[i] = Old then
Result[i] := New;
end;
function ReplaceChars(const S: string; Old: TCharSet; New: Char): string;
var
i: Integer;
begin
Result := S;
for i := 1 to Length(Result) do
if Result[i] in Old then
Result[i] := New;
end;
function Replace(const S, Old, New: string): string;
var
oldp, ps: Integer;
begin
ps := 1;
Result := '';
while True do begin
oldp := ps;
ps := PosEx(Old, S, oldp);
if ps = 0 then begin
Result := Result + Copy(S, oldp, Length(S) - oldp + 1);
Exit; end;
Result := Result + Copy(S, oldp, ps - oldp) + New;
Inc(ps, Length(Old));
end;
end;
function SLOfFile(const FileName: string): TStringList;
begin
Result := TStringList.Create;
if FileExists(FileName) then
Result.LoadFromFile(FileName);
end;
function ContainsEmptyLines(SL: TStrings): Boolean;
begin
Result := StrIn(SL, '');
end;
procedure DeleteEmptyLines(SL: TStrings);
var
i: Integer;
begin
i := 0;
while i < SL.Count do begin
if SL[i] = '' then
SL.Delete(i)
else
Inc(i);
end;
end;
procedure DeleteCommentLines(SL: TStrings; const CommentSign: string = '//');
var
i: Integer;
begin
i := 0;
while i < SL.Count do begin
if (SL[i] = '') or (StrAtBegin(TrimLeft(SL[i]), CommentSign)) then
SL.Delete(i)
else
Inc(i);
end;
end;
function FindLine(SL: TStrings; const S: string): Integer;
begin
for Result := 0 to SL.Count-1 do
if TextAtBegin(SL[Result], S) then
Exit;
Result := -1;
end;
procedure QuickSortSL(SL: TStringList);
procedure Sort(l, r: Integer);
var
i,j: Integer;
z,x: string;
begin
i := l;
j := r;
x := SL[(j + i) div 2];
repeat
while SL[i] < x do Inc(i);
while SL[j] > x do Dec(j);
if i <= j then begin
z := SL[i];
SL[i] := SL[j];
SL[j] := z;
Inc(i); Dec(j);
end;
until i > j;
if j > l then Sort(l, j);
if i < r then Sort(i, r);
end;
begin
if SL.Count > 0 then
Sort(0, SL.Count-1);
end;
function IncStrA(StrA: TStrA): Integer;
begin
SetLength(StrA, Length(StrA) + 1);
Result := High(StrA);
end;
function StrOfByteA(a: TByteA): string;
begin
Result := string(Copy(a, 0, Length(a)));
end;
function ByteAOfStr(const S: string): TByteA;
begin
Result := TByteA(Copy(S, 1, Length(s)));
end;
function ByteAOfInt(i: Integer): TByteA;
begin
SetLength(Result, SizeOf(Integer));
Move(i, Pointer(Result)^, SizeOf(Integer));
end;
function IntOfByteA(A: TByteA): Integer;
begin
Result := 0;
Move(Pointer(A)^, Result, Min(Length(A), SizeOf(Integer)));
end;
function ByteAOfHex(const Hex: string): TByteA;
var
i: Integer;
h: string;
begin
h := ExtractChars(Hex, HexadecimalChars);
SetLength(Result, Length(h) div 2);
for i := 0 to High(Result) do
Result[i] := ByteOfHex(Copy(h, (i shl 1) + 1, 2));
end;
function SizeOfFile(const FileName: string): Integer;
var
F: file;
begin
AssignFile(F, FileName);
{$I-}Reset(F, 1);{$I+}
if IOResult = 0 then begin
Result := FileSize(F);
CloseFile(F);
end else
Result := 0;
end;
function FileEx(const FileName: string; AllowFolders: Boolean = False): Boolean;
var
FindData: TWin32FindData;
begin
if FileName = '' then begin
Result := False;
Exit; end;
Result := (AllowFolders and DirectoryExists(FileName)) or
(FindFirstFile(PChar(FileName), FindData) <> INVALID_HANDLE_VALUE);
Result := Result and not CharIn(FileName, WildCards);
Result := Result and (AllowFolders
or ((FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0));
end;
function LWPSolve(const Dir: string): string;
begin
if (Dir <> '') and (Dir[Length(Dir)] = '\') then begin
Result := Copy(Dir, 1, Length(Dir) - 1);
end else
Result := Dir;
end;
function LWPSlash(const Dir: string): string;
begin
if (Dir <> '') and (Dir[Length(Dir)] = '\') then begin
Result := Copy(Dir, 1, Length(Dir));
end else
Result := Dir + '\';
end;
function ExtractDrive(const FileName: string): string;
begin
Result := '';
if (Length(FileName) >= 2) and (FileName[2] = ':') then
Result := UpperCase(FileName[1] + ':\');
end;
function ExtractPath(const FileName: string): string;
var
p: Integer;
begin
p := CharPosR('\', FileName);
if P > 0 then
Result := Copy(FileName, 1, p)
else
Result := FileName;
end;
function ExtractPrefix(const FileName: string): string;
begin
Result := UntilLastChar(ExtractFileName(FileName), '.');
end;
function ExtractSuffix(const FileName: string): string;
begin
Result := FromLastChar(ExtractFileName(FileName), '.');
end;
function SameByteA(const A, B: TByteA): Boolean;
begin
Result := (A = B) or ((Length(A) = Length(B)) and CompareMem(A, B, Length(A)));
end;
function Reverse(A: TByteA): TByteA;
var
i: Integer;
begin
SetLength(Result, Length(A));
for i := 0 to High(A) do
Result[High(Result) - i] := A[i];
end;
function Endian(i: Integer): Integer;
type
EndianArray = packed array[0..3] of Byte;
var
a, b: EndianArray;
begin
a := EndianArray(i);
b[0] := a[3];
b[1] := a[2];
b[2] := a[1];
b[3] := a[0];
Result := Integer(b);
end;
function SaveByteA(Data: TByteA; const FileName: string;
Overwrite: Boolean = True): Boolean;
var
F: file;
begin
if FileExists(FileName) and not Overwrite then begin
Result := False;
Exit end;
AssignFile(F, FileName);
{$I-}Rewrite(F, 1);{$I+}
if IOResult = 0 then begin
if Length(Data) > 0 then
BlockWrite(F, Data[0], Length(Data));
CloseFile(F);
Result := True;
end else
Result := False;
end;
function LoadByteA(const FileName: string): TByteA;
var
F: file;
begin
AssignFile(F, FileName);
{$I-}Reset(F, 1);{$I+}
if IOResult = 0 then begin
SetLength(Result, FileSize(F));
if Length(Result) > 0 then
BlockRead(F, Result[0], FileSize(F));
CloseFile(F);
end else
SetLength(Result, 0);
end;
function IsValidFileName(const FileName: string): Boolean;
begin
Result := (FileName <> '') and not CharIn(FileName, FileNameEnemies)
and CharIn(Trim(FileName), AllChars - ['.']);
end;
function MakeValidFileName(FileName: string; const Default: string = 'File'): string;
begin
if FileName = '' then
FileName := Default;
if CharIn(FileName, FileNameEnemies) then
Result := ReplaceChars(FileName, FileNameEnemies, '_')
else if not CharIn(Trim(FileName), AllChars - ['.']) then
Result := Default
else
Result := FileName;
end;
function IsValidInteger(const S: string): Boolean;
{const
LowInt = '2147483648';
HighInt = '2147483647';
var
len, RealLen, i, o: Integer;
c: Char;
begin
Result := False;
if S = '' then
Exit;
len := Length(S);
o := 1;
if S[1] = '-' then begin
if len = 1 then
Exit;
Inc(o);
while (o <= len) and (S[o] = '0') do
Inc(o);
if o > len then
Exit;
if o < len then begin
RealLen := len - o + 1;
if RealLen > Length(LowInt) then
Exit
else if RealLen = Length(LowInt) then begin
for i := 1 to Length(LowInt) do begin
c := S[i + o - 1];
if (c < '0') or (c > LowInt[i]) then
Exit;
if c in ['0'..Char((Byte(LowInt[i])-1))] then
Break;
end;
Inc(o, i);
end;
end;
end else begin
while (o <= len) and (S[o] = '0') do
Inc(o);
if o <= len then begin
RealLen := len - o + 1;
if RealLen > Length(HighInt) then
Exit
else if RealLen = Length(HighInt) then begin
for i := 1 to Length(HighInt) do begin
c := S[i + o - 1];
if (c < '0') or (c > HighInt[i]) then
Exit;
if c in ['0'..Char((Byte(HighInt[i])-1))] then
Break;
end;
Inc(o, i);
end;
end;
end;
for i := o to len do
if not (S[i] in ['0'..'9']) then
Exit;
Result := True; }
var
i: Int64;
begin
i := StrToInt64Def(S, High(Int64));
Result := (i >= Low(Integer)) and (i <= High(Integer));
end;
function IsValidCardinal(const S: string): Boolean;
{const
HighCard = '4294967295';
var
len, RealLen, i, o: Integer;
begin
Result := False;
if S = '' then
Exit;
len := Length(S);
o := 1;
while (o <= len) and (S[o] = '0') do
Inc(o);
if o <= len then begin
RealLen := len - o + 1;
if RealLen > Length(HighCard) then
Exit
else if RealLen = Length(HighCard) then begin
for i := 1 to Length(HighCard) do begin
if S[i + o - 1] > HighCard[i] then
Exit;
if S[i + o - 1] in ['0'..Char((Byte(HighCard[i])-1))] then
Break;
end;
Inc(o, i);
end;
end;
for i := o to len do
if not (S[i] in ['0'..'9']) then
Exit;
Result := True; }
var
i: Int64;
begin
i := StrToInt64Def(S, -1);
Result := (i >= 0) and (i <= High(Cardinal));
end;
function StrOfBool(flag: Boolean; const TrueStr: string = 'True';
const FalseStr: string = 'False'): string;
begin
if Flag then
Result := TrueStr
else
Result := FalseStr;
end;
function StrOfInt(i: Integer): string;
begin
{ if i = 0 then begin
Result := '0';
Exit end;
while i > 0 do begin
Result := Char(Byte('0') + (i mod 10)) + Result;
i := i div 10;
end;}
Result := IntToStr(i);
end;
function CardOfStr(const S: string): Cardinal;
var
Res: Int64;
begin
Res := StrToInt64Def(S, -1);
if Res > High(Cardinal) then
Res := High(Cardinal)
else if Res < 0 then
Res := 0;
Result := Cardinal(Res);
end;
function HexOrd(Hex: Char): THex;
begin
case Hex of
'0'..'9':
Result := Byte(Hex) - 48;
'A'..'F':
Result := Byte(Hex) - 55;
'a'..'f':
Result := Byte(Hex) - 87;
else
Result := 0;
end;
end;
function ByteOfHex(Hex: THexByteStr): Byte;
begin
Result := (HexOrd(Hex[1]) shl 4) + HexOrd(Hex[2]);
end;
function DecOfHex(const Hex: string): string;
begin
Result := IntToStr(CardOfHex(Hex));
end;
function HexOfByte(b: Byte): THexByteStr;
begin
Result := HexChar[(b and $F0) shr 4]
+ HexChar[ b and $0F ];
end;
{function HexOfCard2(c: Cardinal): string;
var
Data: array[0..(1 shl 4) - 1] of Char;
i: Integer;
begin
for i := 0 to (1 shl 4) - 1 do
if i < 10 then
Data[i] := Char(Ord('0') + i)
else
Data[i] := Char(Ord('A') + i - 10);
Result := Data[(c and (((1 shl (1 shl 2)) - 1) shl (7 shl 2))) shr (7 shl 2)]
+ Data[(c and (((1 shl (1 shl 2)) - 1) shl (6 shl 2))) shr (6 shl 2)]
+ Data[(c and (((1 shl (1 shl 2)) - 1) shl (5 shl 2))) shr (5 shl 2)]
+ Data[(c and (((1 shl (1 shl 2)) - 1) shl (4 shl 2))) shr (4 shl 2)]
+ Data[(c and (((1 shl (1 shl 2)) - 1) shl (3 shl 2))) shr (3 shl 2)]
+ Data[(c and (((1 shl (1 shl 2)) - 1) shl (2 shl 2))) shr (2 shl 2)]
+ Data[(c and (((1 shl (1 shl 2)) - 1) shl (1 shl 2))) shr (1 shl 2)]
+ Data[(c and (((1 shl (1 shl 2)) - 1) shl (0 shl 2))) shr (0 shl 2)];
end; }
function HexOfCard(i: Cardinal): string;
var
a: Cardinal;
begin
Result := '';
while i > 0 do begin
a := i and $F;
Result := HexChar[a] + Result;
i := i shr 4;
end;
end;
function HexOfCard(i: Cardinal; Digits: Integer): string;
var
a: Cardinal;
begin
Result := '';
while i > 0 do begin
a := i and $F;
Result := HexChar[a] + Result;
i := i shr 4;
end;
Result := MulStr('0', Digits - Length(Result)) + Result;
end;
function PascalHexArray(a: TByteA; Name: string): string;
var
i, len: Integer;
begin
Result := 'const' + EOL +
' ' + Name + ': array[0..' + IntToStr(High(a)) + '] of Byte = (';
len := Length(a);
for i := 0 to len-1 do begin
if (i mod 19) = 0 then
Result := Result + EOL + ' ' + ' ';
Result := Result + '$' + HexOfByte(a[i]);
if i < len-1 then
Result := Result + ',';
end;
Result := Result + EOL + ' );';
end;
function HexOfByteA(a: TByteA; Blocks: Integer = 1;
const Splitter: string = ' '): string;
var
i: Integer;
begin
Result := '';
if Blocks > 0 then
for i := 0 to High(a) do begin
Result := Result + HexOfByte(a[i]);
if i < High(a) then
if ((i+1) mod Blocks) = 0 then
Result := Result + Splitter;
end
else
for i := 0 to High(a) do
Result := Result + HexOfByte(a[i]);
end;
function BinOfByteA(a: TByteA; Blocks: Integer = 4;
const Splitter: string = ' '): string;
var
i, max: Integer;
Bit: Boolean;
begin
Result := '';
if Blocks > 0 then begin
max := 8 * (High(a)) + 7;
for i := 0 to max do begin
Bit := 7-(i mod 8) in TBitSet(a[i div 8]);
Result := Result + Char(Byte('0') + Byte(Bit));
if i < max then
if ((i+1) mod Blocks) = 0 then
Result := Result + Splitter;
end;
end else
for i := 0 to High(a) do
Result := Result + Char(Byte('0') + a[i] shr (i and 8));
end;
function CardOfHex(Hex: string): Cardinal;
var
i: Integer;
begin
Result := 0;
Hex := Copy(ExtractChars(Hex, HexadecimalChars), 1, 8);
for i := 1 to Length(Hex) do
if Hex[i] <> '0' then
Inc(Result, HexOrd(Hex[i]) shl ((Length(Hex) - i) shl 2));
end;
function IntOfBin(Bin: string): Cardinal;
var
i: Integer;
begin
Result := 0;
Bin := Copy(ExtractChars(Bin, BinaryChars), 1, 32);
for i := Length(Bin) downto 1 do
if Bin[i] = '1' then
Inc(Result, 1 shl (Length(Bin) - i));
end;
function BinOfInt(n: Cardinal): string;
var
a: Integer;
begin
if n = 0 then begin
Result := '0';
exit; end;
Result := '';
while n > 0 do begin
a := n and 1;
Result := Char(a + Byte('0')) + Result;
n := n shr 1;
end;
end;
function BinOfIntFill(n: Cardinal; MinCount: Integer = 8): string;
var
a: Integer;
begin
if n = 0 then begin
Result := MulStr('0', MinCount);
Exit; end;
Result := '';
while n > 0 do begin
a := n and 1;
Result := Char(a + Byte('0')) + Result;
n := n shr 1;
end;
Result := MulStr('0', MinCount - Length(Result)) + Result;
end;
function BaseNOfInt(I: Cardinal; B: TBaseN): string;
var
a: Integer;
begin
if (B < 2) or (i = 0) then begin
Result := '0';
Exit; end;
Result := '';
while i > 0 do begin
a := i mod B;
Result := BaseNChar[a] + Result;
i := i div B;
end;
end;
function IntOfBaseN(V: string; B: TBaseN): Cardinal;
var
i: Integer;
F: Cardinal;
c: Byte;
begin
Result := 0;
V := TrimAll(V);
F := 1;
for i := Length(V) downto 1 do begin
c := Byte(UpCase(V[i]));
case Char(c) of
'0'..'9': c := c - 48;
'A'..'Z': c := c - 55;
end;
if c < B then
Result := Result + Byte(c) * F;
F := F * B;
end;
end;
function KeepIn(i, Bottom, Top: Variant): Variant;
begin
Result := i;
if Result > Top then
Result := Top
else if Result < Bottom then
Result := Bottom;
end;
function InRange(Value, Bottom, Top: Variant): Boolean;
begin
Result := (Value >= Bottom) and (Value <= Top);
end;
function InStrictRange(Value, Bottom, Top: Variant): Boolean;
begin
Result := (Value > Bottom) and (Value < Top);
end;
function Min(const A, B: Integer): Integer;
begin
if A < B then
Result := A
else
Result := B;
end;
function Min(const A: TIntA): Integer;
var
i: Integer;
begin
Result := 0;
if Length(A) = 0 then
Exit;
Result := A[0];
for i := 1 to High(A) do
if A[i] < Result then
Result := A[i];
end;
function Max(const A, B: Integer): Integer;
begin
if A > B then
Result := A
else
Result := B;
end;
function Max(const A: TIntA): Integer;
var
i: Integer;
begin
Result := 0;
if Length(A) = 0 then
Exit;
Result := A[0];
for i := 1 to High(A) do
if A[i] > Result then
Result := A[i];
end;
function RangesOfStr(const S: string): TRanges;
var
SL: TStringList;
r, b, t: string;
i, p: Integer;
function TryStrToCard(const S: string; out Value: Cardinal): Boolean;
var
E: Integer;
begin
Val(S, Value, E);
Result := E = 0;
end;
begin
Result := nil;
SL := TStringList.Create;
try
Split(S, RangesSeparator, SL);
SetLength(Result, SL.Count);
for i := 0 to SL.Count-1 do begin
r := SL[i];
with Result[i] do begin
p := CharPos(RangeInnerSeparator, r);
Simple := p = 0; // no '-' found
if Simple then begin
if r = RangeInfinite then begin // * --> *-*
Simple := False;
Bottom := Low(Bottom);
Top := High(Top);
end else if not TryStrToCard(r, Value) then
Break;
end else begin
TileStr(r, p, p, b, t);
if b = RangeInfinite then
Bottom := Low(Bottom)
else if not TryStrToCard(b, Bottom) then
Break;
if t = RangeInfinite then
Top := High(Top)
else if not TryStrToCard(t, Top) then
Break;
if Bottom > Top then begin
p := Bottom; Bottom := Top; Top := p;
end;
end;
end;
end;
if i <> SL.Count then
Result := nil;
finally
SL.Free;
end;
end;
function InRanges(Ranges: TRanges; TestValue: Cardinal): Boolean;
var
i: Integer;
begin
Result := True;
for i := 0 to High(Ranges) do
with Ranges[i] do
if Simple then begin
if TestValue = Value then
Exit;
end else begin
if InRange(TestValue, Bottom, Top) then
Exit;
end;
Result := False;
end;
procedure WriteSL(Strings: TStrings; const Prefix: string = '';
const Suffix: string = '');
var
i: Integer;
begin
for i := 0 to Strings.Count-1 do
WriteLn(Prefix + Strings[i] + Suffix);
end;
function Success(Res: Integer; ResultOnSuccess: Integer = ERROR_SUCCESS): Boolean;
begin
Result := (Res = ResultOnSuccess);
LastSuccessRes := Res;
end;
function Failure(Res: Integer; ResultOnSuccess: Integer = ERROR_SUCCESS): Boolean;
begin
Result := not Success(Res, ResultOnSuccess);
end;
function ExpandString(const S: string): string;
var
Len: Integer;
P, Res: PChar;
begin
Result := '';
P := PChar(S);
Len := ExpandEnvironmentStrings(P, nil, 0);
if Len = 0 then
Exit;
GetMem(Res, Len);
ExpandEnvironmentStrings(P, Res, Len);
Result := Res;
FreeMem(Res, Len);
end;
function FindAll(Strings: TStrings; const Mask: string;
ScanSubDirs: Boolean = True; Attributes: Integer = faFindEveryFile;
FileReturn: TFileNameFunc = nil): Boolean;
var
Path, FileName: string;
procedure ScanDir(const Path, FileName: string);
var
PSR: TSearchRec;
Res: Integer;
procedure Add(const S: string);
begin
if S <> '' then
Strings.Add(S);
end;
begin
Res := FindFirst(Path + FileName, Attributes, PSR);
while Success(Res, 0) do begin
if Assigned(FileReturn) then
Add(FileReturn(Path + PSR.Name))
else
Add(Path + PSR.Name);
Res := FindNext(PSR);
end;
FindClose(PSR);
if not ScanSubDirs then
Exit;
Res := FindFirst(Path + '*', faDirectory, PSR);
while Success(Res, 0) do begin
if (PSR.Attr and faDirectory > 0)
and (PSR.Name <> '.') and (PSR.Name <> '..') then
ScanDir(Path + PSR.Name + '\', FileName);
Res := FindNext(PSR);
end;
FindClose(PSR);
end;
begin
Strings.Clear;
Path := ExtractPath(Mask);
FileName := ExtractFileName(Mask);
ScanDir(Path, FileName);
Result := Strings.Count > 0;
end;
function FindAllFirst(const Mask: string; ScanSubDirs: Boolean = True;
Attributes: Integer = faFindEveryFile): string;
var
Path, FileName: string;
function ScanDir(const Path, FileName: string): Boolean;
var
PSR: TSearchRec;
Res: Integer;
begin
Result := False;
if Success(FindFirst(Path + FileName, Attributes, PSR), 0) then begin
FindAllFirst := Path + PSR.Name;
Result := True;
FindClose(PSR);
Exit; end;
if not ScanSubDirs then
Exit;
Res := FindFirst(Path + '*', faDirectory, PSR);
while not Result and Success(Res, 0) do begin
if (PSR.Attr and faDirectory > 0)
and (PSR.Name <> '.') and (PSR.Name <> '..') then
Result := ScanDir(Path + PSR.Name + '\', FileName);
Res := FindNext(PSR);
end;
FindClose(PSR);
end;
begin
Result := '';
Path := ExtractPath(Mask);
FileName := ExtractFileName(Mask);
ScanDir(Path, FileName);
end;
procedure DeleteFiles(const Mask: string; ScanSubDirs: Boolean = True;
Attributes: Integer = faFindEveryFile);
var
Path, FileName: string;
procedure ScanDir(const Path, FileName: string);
var
PSR: TSearchRec;
Res: Integer;
procedure TryDeleteFile(const FileName: string);
begin
try
DeleteFile(Path + PSR.Name);
except
end;
end;
begin
Res := FindFirst(Path + FileName, Attributes, PSR);
while Success(Res, 0) do begin
TryDeleteFile(Path + PSR.Name);
Res := FindNext(PSR);
end;
FindClose(PSR);
if not ScanSubDirs then
Exit;
Res := FindFirst(Path + '*', faDirectory, PSR);
while Success(Res, 0) do begin
if (PSR.Attr and faDirectory > 0)
and (PSR.Name <> '.') and (PSR.Name <> '..') then begin
ScanDir(Path + PSR.Name + '\', FileName);
TryDeleteFile(Path + PSR.Name);
end;
Res := FindNext(PSR);
end;
FindClose(PSR);
end;
begin
Path := ExtractPath(Mask);
FileName := ExtractFileName(Mask);
ScanDir(Path, FileName);
end;
function GetFileNew(FileName: string; NoFloppyDrives: Boolean = True): string;
var
Drive: string;
pf, pd, Len: Integer;
PSR: TSearchRec;
begin
Result := '';
FileName := Trim(FileName);
if Length(FileName) < 2 then
Exit;
Drive := ExtractDrive(FileName);
if not DirectoryExists(Drive) then
Exit;
if NoFloppyDrives and (Drive[1] in ['A', 'B']) then
Exit;
Len := Length(FileName);
Result := Drive;
pf := Length(Drive) + 1;
while pf <= Len do begin
if FileName[pf] = '\' then begin
Result := Result + '\';
Inc(pf);
Continue; end;
pd := CharPos('\', FileName, pf);
if pd = 0 then begin
if 0=FindFirst(Result + Copy(FileName, pf, MaxInt), faFindEveryFile, PSR) then begin
Result := Result + PSR.Name;
Break; end else begin
FindClose(PSR);
if 0=FindFirst(Result + Copy(FileName, pf, MaxInt), faDirectory, PSR) then
Result := Result + PSR.Name + '\'
else
Result := '';
FindClose(PSR);
if Result = '' then
Break;
end;
end;
if 0=FindFirst(Result + Copy(FileName, pf, pd - pf), faDirectory, PSR) then
Result := Result + PSR.Name + '\'
else
Result := '';
FindClose(PSR);
if Result = '' then
Break;
pf := pd + 1;
end;
if (Result <> '') and not FileEx(Result, True) then
Result := '';
end;
function DateTimeOfFileTime(const FileTime: TFileTime): TDateTime;
var
LocalFileTime: TFileTime;
Res: Integer;
begin
Result := 0;
FileTimeToLocalFileTime(FileTime, LocalFileTime);
if not FileTimeToDosDateTime(LocalFileTime, LongRec(Res).Hi,
LongRec(Res).Lo) then
Res := -1;
if (Res = -1) or (Res = 0) then
Exit;
try
Result := FileDateToDateTime(Res);
except
end;
end;
procedure FileNew(const FileName: string);
var
Handle: Integer;
begin
Handle := FileCreate(FileName);
FileClose(Handle);
end;
function Win32PlatformStr: string;
const
PlatformStrings: array[VER_PLATFORM_WIN32s..VER_PLATFORM_WIN32_NT] of string =
('VER_PLATFORM_WIN32s', 'VER_PLATFORM_WIN32_WINDOWS', 'VER_PLATFORM_WIN32_NT');
begin
Result := PlatformStrings[Win32Platform];
end;
function FullOSInfo: string;
begin
Result := Format(
'Platform: %s' + EOL +
'Version: %d.%d Build %d' + EOL +
'CSD: %s',
[
Win32PlatformStr,
Win32MajorVersion, Win32MinorVersion, Win32BuildNumber,
Win32CSDVersion
]
);
end;
function Win9x: Boolean;
begin
Result := Win32Platform = VER_PLATFORM_WIN32_WINDOWS;
end;
function WinNT: Boolean;
begin
Result := Win32Platform = VER_PLATFORM_WIN32_NT;
end;
function Win2000: Boolean;
begin
Result := (Win32Platform = VER_PLATFORM_WIN32_NT)
and (Win32MajorVersion = 4);
end;
function WinXP: Boolean;
begin
Result := Win32MajorVersion >= 5;
end;
initialization
MyDir := GetMyDir;
end.
unit FifoStream;
interface
uses Classes, windows, Dialogs;
const
DefaultChunksize = 32768; // 32kb per chunk as default.
type
PMemChunk = ^TMemChunk;
TMemChunk = record
Filled: Longword;
Read: Longword;
Data: pointer;
end;
TFifo = class
private
FBuffers: TList;
FChunksize: Longword;
FCritSect: TRTLCriticalSection;
FIsWinNT: boolean;
FBytesInFifo: LongWord;
protected
function GetBytesInFifo: LongWord;
public
constructor Create;
destructor Destroy; override;
procedure Write(Data: pointer; Size: LongWord);
procedure Read(Buff: pointer; var ReqSize: LongWord);
procedure PeekData(Buff: pointer; var ReqSize: LongWord);
published
property BytesInFifo: LongWord read FBytesInFifo;
end;
implementation
constructor TFifo.Create;
begin
inherited;
FBuffers := TList.Create;
// set default chunksize...
FChunksize := DefaultChunksize;
InitializeCriticalSection(FCritSect);
end;
destructor TFifo.Destroy;
var
I: Integer;
begin
EnterCriticalSection(FCritSect);
for I := 0 to FBuffers.count - 1 do
begin
FreeMem(PMemChunk(Fbuffers[I]).Data);
Dispose(PMemChunk(Fbuffers[I]));
end;
FBuffers.Clear;
FBuffers.Free;
LeaveCriticalSection(FCritSect);
DeleteCriticalSection(FCritSect);
inherited;
end;
function TFifo.GetBytesInFifo: LongWord;
begin
Result := 0;
if FBuffers.Count = 0 then
begin
exit;
end
else
begin
if FBuffers.Count > 1 then
Inc(Result, (FBuffers.Count - 1) * FChunkSize);
Inc(Result, PMemChunk(FBuffers[Fbuffers.Count - 1]).Filled);
Dec(Result, PMemChunk(FBuffers[0]).Read);
end;
end;
procedure TFifo.Write(Data: pointer; Size: LongWord);
var
Privpointer: pointer;
PrivSize: LongWord;
Chunk: PMemChunk;
PosInChunk: pointer;
begin
if LongWord(Data) = 0 then
begin
// null pointer? somebody is trying to fool us, get out...
Exit;
end;
EnterCriticalSection(FCritSect);
PrivPointer := Data;
PrivSize := 0;
// are already buffers there?
if FBuffers.count > 0 then
begin
// is the last one of them not completely filled?
if PMemChunk(FBuffers[FBuffers.count - 1]).filled < FChunksize then
// not completely filled, so fill up the buffer.
begin
Chunk := PMemChunk(FBuffers[FBuffers.count - 1]);
// fetch chunkdata.
PosInChunk := Chunk.Data;
// move to current fill pos...
Inc(LongWord(PosInChunk), Chunk.Filled);
// can we fill the chunk completely?
if Size > FChunksize - Chunk.Filled then
begin
// yes we can.
Move(PrivPointer^, PosInChunk^, FChunksize - Chunk.Filled);
Inc(PrivSize, FChunksize - Chunk.Filled);
Inc(LongWord(PrivPointer), FChunksize - Chunk.Filled);
Chunk.Filled := FChunkSize;
end
else
// we have to less data for filling the chunk completely,
// just put everything in.
begin
Move(PrivPointer^, PosInChunk^, Size);
Inc(PrivSize, Size);
Inc(Chunk.Filled, Size);
end;
end;
end;
// as long as we have remaining stuff put it into new chunks.
while (PrivSize < Size) do
begin
new(Chunk);
GetMem(Chunk.Data, FChunksize);
Chunk.Read := 0;
// can we fill an entire chunk with the remaining data?
if Privsize + FChunksize < Size then
begin
// yes we can, so put the stuff in.
Move(Privpointer^, Chunk.Data^, FChunksize);
Inc(LongWord(PrivPointer), FChunksize);
Inc(PrivSize, FChunksize);
Chunk.Filled := FChunksize;
end
else // we have to less data to fill the entire chunk, just put the remaining stuff in.
begin
Move(Privpointer^, Chunk.Data^, Size - Privsize);
Chunk.Filled := Size - Privsize;
Inc(PrivSize, Size - Privsize);
end;
Fbuffers.Add(Chunk);
end;
if Size <> Privsize then
Showmessage('miscalculation in TFifo.write');
FBytesInFifo := GetBytesInFifo;
LeaveCriticalSection(FCritSect);
end;
procedure TFifo.Read(Buff: pointer; var ReqSize: LongWord);
var
PrivSize: Integer;
Privpos: pointer;
Chunk: PMemChunk;
ChunkPos: pointer;
begin
if LongWord(Buff) = 0 then
begin
// null pointer? somebody is trying to fool us, get out...
Exit;
end;
EnterCriticalSection(FCritSect);
PrivSize := 0;
Privpos := Buff;
while FBuffers.Count > 0 do
begin
Chunk := PMemChunk(FBuffers[0]);
ChunkPos := Chunk.data;
Inc(LongWord(ChunkPos), Chunk.Read);
// does the remaining part of the chunk fit into the buffer?
if PrivSize + (Chunk.Filled - Chunk.read) < ReqSize then
begin // yep, it fits
Move(ChunkPos^, Privpos^, Chunk.Filled - Chunk.read);
Inc(PrivSize, Chunk.Filled - Chunk.read);
FreeMem(Chunk.Data);
Dispose(Chunk);
FBuffers.Delete(0);
end
else // remaining part didn't fit, get as much as we can and increment the
// read attribute.
begin
Move(ChunkPos^, Privpos^, ReqSize - PrivSize);
Inc(Chunk.read, ReqSize - PrivSize);
Inc(PrivSize, ReqSize - PrivSize);
// as we filled the buffer, we'll have to break here.
break;
end;
end;
FBytesInFifo := GetBytesInFifo;
LeaveCriticalSection(FCritSect);
ReqSize := PrivSize;
end;
// read Data from Stream without removing it from the Stream...
procedure TFifo.PeekData(Buff: pointer; var ReqSize: LongWord);
var
PrivSize: Integer;
Privpos: pointer;
Chunk: PMemChunk;
ChunkPos: pointer;
ChunkNr: Integer;
begin
if LongWord(Buff) = 0 then
begin
// null pointer? somebody is trying to fool us, get out...
Exit;
end;
EnterCriticalSection(FCritSect);
PrivSize := 0;
Privpos := Buff;
ChunkNr := 0;
while FBuffers.Count > ChunkNr do
begin
Chunk := PMemChunk(FBuffers[ChunkNr]);
ChunkPos := Chunk.data;
Inc(LongWord(ChunkPos), Chunk.Read);
// does the remaining part of the chunk fit into the buffer?
if PrivSize + (Chunk.Filled - Chunk.read) < ReqSize then
begin // yep, it fits
Move(ChunkPos^, Privpos^, Chunk.Filled - Chunk.read);
Inc(PrivSize, Chunk.Filled - Chunk.read);
Inc(ChunkNr);
end
else // remaining part didn't fit, get as much as we can and increment the
// read attribute.
begin
Move(ChunkPos^, Privpos^, ReqSize - PrivSize);
Inc(PrivSize, ReqSize - PrivSize);
// as we filled the buffer, we'll have to break here.
break;
end;
end;
LeaveCriticalSection(FCritSect);
ReqSize := PrivSize;
end;
end.
|