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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<meta name="generator" content="AsciiDoc 8.6.7" />
<title>tpl User Guide</title>
<style type="text/css">
/* Shared CSS for AsciiDoc xhtml11 and html5 backends */
/* Default font. */
body {
font-family: Georgia,serif;
}
/* Title font. */
h1, h2, h3, h4, h5, h6,
div.title, caption.title,
thead, p.table.header,
#toctitle,
#author, #revnumber, #revdate, #revremark,
#footer {
font-family: Arial,Helvetica,sans-serif;
}
body {
margin: 1em 5% 1em 5%;
}
a {
color: blue;
text-decoration: underline;
}
a:visited {
color: fuchsia;
}
em {
font-style: italic;
color: navy;
}
strong {
font-weight: bold;
color: #083194;
}
h1, h2, h3, h4, h5, h6 {
color: #527bbd;
margin-top: 1.2em;
margin-bottom: 0.5em;
line-height: 1.3;
}
h1, h2, h3 {
border-bottom: 2px solid silver;
}
h2 {
padding-top: 0.5em;
}
h3 {
float: left;
}
h3 + * {
clear: left;
}
h5 {
font-size: 1.0em;
}
div.sectionbody {
margin-left: 0;
}
hr {
border: 1px solid silver;
}
p {
margin-top: 0.5em;
margin-bottom: 0.5em;
}
ul, ol, li > p {
margin-top: 0;
}
ul > li { color: #aaa; }
ul > li > * { color: black; }
pre {
padding: 0;
margin: 0;
}
#author {
color: #527bbd;
font-weight: bold;
font-size: 1.1em;
}
#email {
}
#revnumber, #revdate, #revremark {
}
#footer {
font-size: small;
border-top: 2px solid silver;
padding-top: 0.5em;
margin-top: 4.0em;
}
#footer-text {
float: left;
padding-bottom: 0.5em;
}
#footer-badges {
float: right;
padding-bottom: 0.5em;
}
#preamble {
margin-top: 1.5em;
margin-bottom: 1.5em;
}
div.imageblock, div.exampleblock, div.verseblock,
div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
div.admonitionblock {
margin-top: 1.0em;
margin-bottom: 1.5em;
}
div.admonitionblock {
margin-top: 2.0em;
margin-bottom: 2.0em;
margin-right: 10%;
color: #606060;
}
div.content { /* Block element content. */
padding: 0;
}
/* Block element titles. */
div.title, caption.title {
color: #527bbd;
font-weight: bold;
text-align: left;
margin-top: 1.0em;
margin-bottom: 0.5em;
}
div.title + * {
margin-top: 0;
}
td div.title:first-child {
margin-top: 0.0em;
}
div.content div.title:first-child {
margin-top: 0.0em;
}
div.content + div.title {
margin-top: 0.0em;
}
div.sidebarblock > div.content {
background: #ffffee;
border: 1px solid #dddddd;
border-left: 4px solid #f0f0f0;
padding: 0.5em;
}
div.listingblock > div.content {
border: 1px solid #dddddd;
border-left: 5px solid #f0f0f0;
background: #f8f8f8;
padding: 0.5em;
}
div.quoteblock, div.verseblock {
padding-left: 1.0em;
margin-left: 1.0em;
margin-right: 10%;
border-left: 5px solid #f0f0f0;
color: #888;
}
div.quoteblock > div.attribution {
padding-top: 0.5em;
text-align: right;
}
div.verseblock > pre.content {
font-family: inherit;
font-size: inherit;
}
div.verseblock > div.attribution {
padding-top: 0.75em;
text-align: left;
}
/* DEPRECATED: Pre version 8.2.7 verse style literal block. */
div.verseblock + div.attribution {
text-align: left;
}
div.admonitionblock .icon {
vertical-align: top;
font-size: 1.1em;
font-weight: bold;
text-decoration: underline;
color: #527bbd;
padding-right: 0.5em;
}
div.admonitionblock td.content {
padding-left: 0.5em;
border-left: 3px solid #dddddd;
}
div.exampleblock > div.content {
border-left: 3px solid #dddddd;
padding-left: 0.5em;
}
div.imageblock div.content { padding-left: 0; }
span.image img { border-style: none; }
a.image:visited { color: white; }
dl {
margin-top: 0.8em;
margin-bottom: 0.8em;
}
dt {
margin-top: 0.5em;
margin-bottom: 0;
font-style: normal;
color: navy;
}
dd > *:first-child {
margin-top: 0.1em;
}
ul, ol {
list-style-position: outside;
}
ol.arabic {
list-style-type: decimal;
}
ol.loweralpha {
list-style-type: lower-alpha;
}
ol.upperalpha {
list-style-type: upper-alpha;
}
ol.lowerroman {
list-style-type: lower-roman;
}
ol.upperroman {
list-style-type: upper-roman;
}
div.compact ul, div.compact ol,
div.compact p, div.compact p,
div.compact div, div.compact div {
margin-top: 0.1em;
margin-bottom: 0.1em;
}
tfoot {
font-weight: bold;
}
td > div.verse {
white-space: pre;
}
div.hdlist {
margin-top: 0.8em;
margin-bottom: 0.8em;
}
div.hdlist tr {
padding-bottom: 15px;
}
dt.hdlist1.strong, td.hdlist1.strong {
font-weight: bold;
}
td.hdlist1 {
vertical-align: top;
font-style: normal;
padding-right: 0.8em;
color: navy;
}
td.hdlist2 {
vertical-align: top;
}
div.hdlist.compact tr {
margin: 0;
padding-bottom: 0;
}
.comment {
background: yellow;
}
.footnote, .footnoteref {
font-size: 0.8em;
}
span.footnote, span.footnoteref {
vertical-align: super;
}
#footnotes {
margin: 20px 0 20px 0;
padding: 7px 0 0 0;
}
#footnotes div.footnote {
margin: 0 0 5px 0;
}
#footnotes hr {
border: none;
border-top: 1px solid silver;
height: 1px;
text-align: left;
margin-left: 0;
width: 20%;
min-width: 100px;
}
div.colist td {
padding-right: 0.5em;
padding-bottom: 0.3em;
vertical-align: top;
}
div.colist td img {
margin-top: 0.3em;
}
@media print {
#footer-badges { display: none; }
}
#toc {
margin-bottom: 2.5em;
}
#toctitle {
color: #527bbd;
font-size: 1.1em;
font-weight: bold;
margin-top: 1.0em;
margin-bottom: 0.1em;
}
div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
margin-top: 0;
margin-bottom: 0;
}
div.toclevel2 {
margin-left: 2em;
font-size: 0.9em;
}
div.toclevel3 {
margin-left: 4em;
font-size: 0.9em;
}
div.toclevel4 {
margin-left: 6em;
font-size: 0.9em;
}
span.aqua { color: aqua; }
span.black { color: black; }
span.blue { color: blue; }
span.fuchsia { color: fuchsia; }
span.gray { color: gray; }
span.green { color: green; }
span.lime { color: lime; }
span.maroon { color: maroon; }
span.navy { color: navy; }
span.olive { color: olive; }
span.purple { color: purple; }
span.red { color: red; }
span.silver { color: silver; }
span.teal { color: teal; }
span.white { color: white; }
span.yellow { color: yellow; }
span.aqua-background { background: aqua; }
span.black-background { background: black; }
span.blue-background { background: blue; }
span.fuchsia-background { background: fuchsia; }
span.gray-background { background: gray; }
span.green-background { background: green; }
span.lime-background { background: lime; }
span.maroon-background { background: maroon; }
span.navy-background { background: navy; }
span.olive-background { background: olive; }
span.purple-background { background: purple; }
span.red-background { background: red; }
span.silver-background { background: silver; }
span.teal-background { background: teal; }
span.white-background { background: white; }
span.yellow-background { background: yellow; }
span.big { font-size: 2em; }
span.small { font-size: 0.6em; }
span.underline { text-decoration: underline; }
span.overline { text-decoration: overline; }
span.line-through { text-decoration: line-through; }
div.unbreakable { page-break-inside: avoid; }
/*
* xhtml11 specific
*
* */
tt {
font-family: "Courier New", Courier, monospace;
font-size: inherit;
color: navy;
}
div.tableblock {
margin-top: 1.0em;
margin-bottom: 1.5em;
}
div.tableblock > table {
border: 3px solid #527bbd;
}
thead, p.table.header {
font-weight: bold;
color: #527bbd;
}
p.table {
margin-top: 0;
}
/* Because the table frame attribute is overriden by CSS in most browsers. */
div.tableblock > table[frame="void"] {
border-style: none;
}
div.tableblock > table[frame="hsides"] {
border-left-style: none;
border-right-style: none;
}
div.tableblock > table[frame="vsides"] {
border-top-style: none;
border-bottom-style: none;
}
/*
* html5 specific
*
* */
.monospaced {
font-family: "Courier New", Courier, monospace;
font-size: inherit;
color: navy;
}
table.tableblock {
margin-top: 1.0em;
margin-bottom: 1.5em;
}
thead, p.tableblock.header {
font-weight: bold;
color: #527bbd;
}
p.tableblock {
margin-top: 0;
}
table.tableblock {
border-width: 3px;
border-spacing: 0px;
border-style: solid;
border-color: #527bbd;
border-collapse: collapse;
}
th.tableblock, td.tableblock {
border-width: 1px;
padding: 4px;
border-style: solid;
border-color: #527bbd;
}
table.tableblock.frame-topbot {
border-left-style: hidden;
border-right-style: hidden;
}
table.tableblock.frame-sides {
border-top-style: hidden;
border-bottom-style: hidden;
}
table.tableblock.frame-none {
border-style: hidden;
}
th.tableblock.halign-left, td.tableblock.halign-left {
text-align: left;
}
th.tableblock.halign-center, td.tableblock.halign-center {
text-align: center;
}
th.tableblock.halign-right, td.tableblock.halign-right {
text-align: right;
}
th.tableblock.valign-top, td.tableblock.valign-top {
vertical-align: top;
}
th.tableblock.valign-middle, td.tableblock.valign-middle {
vertical-align: middle;
}
th.tableblock.valign-bottom, td.tableblock.valign-bottom {
vertical-align: bottom;
}
/*
* manpage specific
*
* */
body.manpage h1 {
padding-top: 0.5em;
padding-bottom: 0.5em;
border-top: 2px solid silver;
border-bottom: 2px solid silver;
}
body.manpage h2 {
border-style: none;
}
body.manpage div.sectionbody {
margin-left: 3em;
}
@media print {
body.manpage div#toc { display: none; }
}
@media screen {
body {
max-width: 50em; /* approximately 80 characters wide */
margin-left: 16em;
}
#toc {
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 13em;
padding: 0.5em;
padding-bottom: 1.5em;
margin: 0;
overflow: auto;
border-right: 3px solid #f8f8f8;
background-color: white;
}
#toc .toclevel1 {
margin-top: 0.5em;
}
#toc .toclevel2 {
margin-top: 0.25em;
display: list-item;
color: #aaaaaa;
}
#toctitle {
margin-top: 0.5em;
}
}
</style>
<script type="text/javascript">
/*<+'])');
// Function that scans the DOM tree for header elements (the DOM2
// nodeIterator API would be a better technique but not supported by all
// browsers).
var iterate = function (el) {
for (var i = el.firstChild; i != null; i = i.nextSibling) {
if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
var mo = re.exec(i.tagName);
if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
}
iterate(i);
}
}
}
iterate(el);
return result;
}
var toc = document.getElementById("toc");
if (!toc) {
return;
}
// Delete existing TOC entries in case we're reloading the TOC.
var tocEntriesToRemove = [];
var i;
for (i = 0; i < toc.childNodes.length; i++) {
var entry = toc.childNodes[i];
if (entry.nodeName.toLowerCase() == 'div'
&& entry.getAttribute("class")
&& entry.getAttribute("class").match(/^toclevel/))
tocEntriesToRemove.push(entry);
}
for (i = 0; i < tocEntriesToRemove.length; i++) {
toc.removeChild(tocEntriesToRemove[i]);
}
// Rebuild TOC entries.
var entries = tocEntries(document.getElementById("content"), toclevels);
for (var i = 0; i < entries.length; ++i) {
var entry = entries[i];
if (entry.element.id == "")
entry.element.id = "_toc_" + i;
var a = document.createElement("a");
a.href = "#" + entry.element.id;
a.appendChild(document.createTextNode(entry.text));
var div = document.createElement("div");
div.appendChild(a);
div.className = "toclevel" + entry.toclevel;
toc.appendChild(div);
}
if (entries.length == 0)
toc.parentNode.removeChild(toc);
},
/////////////////////////////////////////////////////////////////////
// Footnotes generator
/////////////////////////////////////////////////////////////////////
/* Based on footnote generation code from:
* http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
*/
footnotes: function () {
// Delete existing footnote entries in case we're reloading the footnodes.
var i;
var noteholder = document.getElementById("footnotes");
if (!noteholder) {
return;
}
var entriesToRemove = [];
for (i = 0; i < noteholder.childNodes.length; i++) {
var entry = noteholder.childNodes[i];
if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote")
entriesToRemove.push(entry);
}
for (i = 0; i < entriesToRemove.length; i++) {
noteholder.removeChild(entriesToRemove[i]);
}
// Rebuild footnote entries.
var cont = document.getElementById("content");
var spans = cont.getElementsByTagName("span");
var refs = {};
var n = 0;
for (i=0; i<spans.length; i++) {
if (spans[i].className == "footnote") {
n++;
var note = spans[i].getAttribute("data-note");
if (!note) {
// Use [\s\S] in place of . so multi-line matches work.
// Because JavaScript has no s (dotall) regex flag.
note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
spans[i].innerHTML =
"[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
"' title='View footnote' class='footnote'>" + n + "</a>]";
spans[i].setAttribute("data-note", note);
}
noteholder.innerHTML +=
"<div class='footnote' id='_footnote_" + n + "'>" +
"<a href='#_footnoteref_" + n + "' title='Return to text'>" +
n + "</a>. " + note + "</div>";
var id =spans[i].getAttribute("id");
if (id != null) refs["#"+id] = n;
}
}
if (n == 0)
noteholder.parentNode.removeChild(noteholder);
else {
// Process footnoterefs.
for (i=0; i<spans.length; i++) {
if (spans[i].className == "footnoteref") {
var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
href = href.match(/#.*/)[0]; // Because IE return full URL.
n = refs[href];
spans[i].innerHTML =
"[<a href='#_footnote_" + n +
"' title='View footnote' class='footnote'>" + n + "</a>]";
}
}
}
},
install: function(toclevels) {
var timerId;
function reinstall() {
asciidoc.footnotes();
if (toclevels) {
asciidoc.toc(toclevels);
}
}
function reinstallAndRemoveTimer() {
clearInterval(timerId);
reinstall();
}
timerId = setInterval(reinstall, 500);
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false);
else
window.onload = reinstallAndRemoveTimer;
}
}
asciidoc.install(2);
/*]]>*/
</script>
</head>
<body class="article">
<div id="header">
<h1>tpl User Guide</h1>
<span id="author">Troy D. Hanson</span><br />
<span id="email"><tt><<a href="mailto:tdh@tkhanson.net">tdh@tkhanson.net</a>></tt></span><br />
<span id="revnumber">version 1.5,</span>
<span id="revdate">February 2010</span>
<div id="toc">
<div id="toctitle">Table of Contents</div>
<noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>To download tpl, follow this link back to the
<a href="https://github.com/troydhanson/tpl">GitHub project page</a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_overview">Overview</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_serialization_in_c">Serialization in C</h3>
<div class="paragraph"><p>Tpl is a library for serializing C data. The data is stored in its natural
binary form. The API is small and tries to stay "out of the way".
Tpl can serialize many C data types, including structures.</p></div>
</div>
<div class="sect2">
<h3 id="_uses_for_tpl">Uses for tpl</h3>
<div class="paragraph"><p>Tpl makes a convenient file format. For example, suppose a program needs to
store a list of user names and ids. This can be expressed using the format
string <tt>A(si)</tt>. If the program needs two such lists (say, one for regular
users and one for administrators) this could be expressed as <tt>A(si)A(si)</tt>. It
is easy to read and write this kind of structured data using tpl.</p></div>
<div class="paragraph"><p>Tpl can also be used as an IPC message format. It handles byte order issues
and deframing individual messages off of a stream automatically.</p></div>
</div>
<div class="sect2">
<h3 id="_expressing_type">Expressing type</h3>
<div class="paragraph"><p>The "data type" of a tpl is explicitly stated as a format string. There is
never any ambiguity about the type of data stored in a tpl. Some examples:</p></div>
<div class="ulist"><ul>
<li>
<p>
<tt>A(is)</tt> is a variable-length array of integer-string pairs
</p>
</li>
<li>
<p>
<tt>A(is)A(is)</tt> are two such arrays, completely independent of one another
</p>
</li>
<li>
<p>
<tt>S(ci)</tt> is a structure containing a char and integer
</p>
</li>
<li>
<p>
<tt>S(ci)#</tt> is a fixed-length array of the latter structure
</p>
</li>
<li>
<p>
<tt>A(A(i))</tt> is a nested array, that is, an array of integer arrays
</p>
</li>
</ul></div>
</div>
<div class="sect2">
<h3 id="_the_tpl_image">The tpl image</h3>
<div class="paragraph"><p>A tpl image is the serialized form of a tpl, stored in a memory buffer or file,
or written to a file descriptor.</p></div>
<div class="sect3">
<h4 id="_what_8217_s_in_a_tpl_image">What’s in a tpl image?</h4>
<div class="paragraph"><p>There is no need to understand the internal structure of the tpl image. But for the
curious, the image is a strictly defined binary buffer having two sections,
a header and the data. The header encodes the length of the image, its
format string, endian order and other flags. The data section contains the
packed data.</p></div>
</div>
<div class="sect3">
<h4 id="_no_framing_needed">No framing needed</h4>
<div class="paragraph"><p>A property of the tpl image is that consecutive images can be written to a stream
without requiring any delimiter between them. The reader making use of
<tt>tpl_gather</tt> (or <tt>tpl_load</tt> in <tt>TPL_FD</tt> mode) will obtain exactly one tpl image at
a time. Therefore tpl images can be used as an IPC message format without any
higher-level framing protocol.</p></div>
</div>
<div class="sect3">
<h4 id="_data_portability">Data portability</h4>
<div class="paragraph"><p>A tpl image generated on one kind of CPU will generally be portable to other
CPU types when tpl is used properly. This may be a surprise considering that
tpl is a binary format. But tpl has been carefully designed to make this work.
Each <a href="#types">format character</a> has an associated explicitly-sized type. For
integer and floating point types, whose "endian" or byte-order convention varies
from one CPU to another, tpl automatically and transparently corrects the
endian order (if needed) during the unpacking process. Floating point numbers
present their own <a href="#trouble_with_double">special difficulties</a>. <em>No guarantees
are made with regard to floating point portability.</em> That said, because many
modern CPU’s use IEEE 754 floating point representation, data is likely to be
portable among them.</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_xml_and_perl">XML and Perl</h3>
<div class="paragraph"><p><em>Note: The <tt>tplxml</tt> utility and the Perl module are currently unsupported in tpl 1.5.</em></p></div>
<div class="sect3">
<h4 id="_xml">XML</h4>
<div class="paragraph"><p>While a tpl image is a binary entity, you can view any tpl image in XML format
using the included <tt>tplxml</tt> utility, located in the <tt>lang/perl</tt> directory.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>tplxml file.tpl > file.xml
tplxml file.xml > file.tpl</tt></pre>
</div></div>
<div class="paragraph"><p>The utility is bidirectional, as shown. The file extension is not important;
<tt>tplxml</tt> inspects its input to see if it’s tpl or XML. You can also pipe data
into it instead of giving it a filename. The <tt>tplxml</tt> utility is slow. Its
purpose is two-fold: debugging (manual inspection of the data in a tpl), and
interoperability with XML-based programs. The resulting XML is often ten times
the size of the original binary tpl image.</p></div>
</div>
<div class="sect3">
<h4 id="_perl">Perl</h4>
<div class="paragraph"><p>There is a Perl module in <tt>lang/perl/Tpl.pm</tt>. The <a href="perl.html">Perl API</a>
is convenient for writing Perl scripts that interoperate with C programs, and
need to pass structured data back and forth. It is written in pure Perl.</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_platforms">Platforms</h3>
<div class="paragraph"><p>The tpl software was developed for POSIX systems and has been tested on 32- and 64-bit
platforms including:</p></div>
<div class="ulist"><ul>
<li>
<p>
Linux
</p>
</li>
<li>
<p>
Solaris
</p>
</li>
<li>
<p>
Mac OS X
</p>
</li>
<li>
<p>
OpenBSD
</p>
</li>
<li>
<p>
Windows using Visual Studio 2008 or 2010, or Cygwin or MinGW
</p>
</li>
</ul></div>
</div>
<div class="sect2">
<h3 id="_bsd_licensed">BSD licensed</h3>
<div class="paragraph"><p>This software is made available under the
<a href="license.html">revised BSD license</a>.
It is free and open source.</p></div>
</div>
<div class="sect2">
<h3 id="_download">Download</h3>
<div class="paragraph"><p>You can clone tpl, or get a zipfile, from the
<a href="https://github.com/troydhanson/tpl">GitHub project page</a>.</p></div>
</div>
<div class="sect2">
<h3 id="_getting_help">Getting help</h3>
<div class="paragraph"><p>Please ask on Github if you need help. You can email the author at
Troy D. Hanson <<a href="mailto:tdh@tkhanson.net">tdh@tkhanson.net</a>>, but I am often behind on email by weeks or
months. Sorry!</p></div>
</div>
<div class="sect2">
<h3 id="_contributing">Contributing</h3>
<div class="paragraph"><p>If you add a new feature or fix something in tpl or in the extras, please
make a pull request on Github. For anything other than a trivial change, include
a unit test and documentation if you possibly can. (And don’t be discouraged if
it takes weeks or even months for me to merge it. Sorry, my life is busy!) Thanks!</p></div>
</div>
<div class="sect2">
<h3 id="_news">News</h3>
<div class="paragraph"><p>The author has a blog for <a href="http://troydhanson.wordpress.com/">software updates</a>
<span class="image">
<img src="rss.png" alt="(RSS)" />
</span>. You can also follow @troydhanson on Twitter for updates.</p></div>
<div class="sect3">
<h4 id="_other_software">Other software</h4>
<div class="paragraph"><p>Other open-source software by the author is listed at <a href="http://tkhanson.net">http://tkhanson.net</a>.</p></div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_build_and_install">Build and install</h2>
<div class="sectionbody">
<div class="paragraph"><p>Tpl has no dependencies on libraries other than the system C library. You
can simply copy the tpl source into your project, so you have no dependencies.
Alternatively, you can build tpl as a library and link it to your program.</p></div>
<div class="sect2">
<h3 id="_as_source">As source</h3>
<div class="paragraph"><p>The simplest way to use tpl is to copy the source files <tt>tpl.h</tt> and <tt>tpl.c</tt>
(from the <tt>src/</tt> directory) right into your project, and build them with the
rest of your source files. No special compiler flags are required.</p></div>
</div>
<div class="sect2">
<h3 id="_as_a_library">As a library</h3>
<div class="paragraph"><p>Alternatively, to build tpl as a library, from the top-level directory, run:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>./configure
make
make install</tt></pre>
</div></div>
<div class="paragraph"><p>(Note that, if <tt>configure</tt> is missing, generate it by running <tt>bootstrap</tt>.)</p></div>
<div class="paragraph"><p>This installs a static library <tt>libtpl.a</tt> and a shared library (e.g.,
<tt>libtpl.so</tt>), if your system supports them, in standard places. The installation
directory can be customized using <tt>./configure --prefix=/some/directory</tt>. Run
<tt>configure --help</tt> for further options.</p></div>
<div class="sect3">
<h4 id="_test_suite">Test suite</h4>
<div class="paragraph"><p>You can compile and run the built-in test suite by running:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>cd tests/
make</tt></pre>
</div></div>
</div>
</div>
<div class="sect2">
<h3 id="_on_windows">On Windows</h3>
<div class="sect3">
<h4 id="_compile_in">Compile-in</h4>
<div class="paragraph"><p>Tpl can be used directly (instead of as a DLL) by compiling the tpl sources
right into your program. To do this, add <tt>tpl.c</tt>, <tt>tpl.h</tt>, <tt>win/mman.h</tt> and
<tt>win/mmap.c</tt> to your program’s source and header files and add the preprocessor
definition <tt>TPL_NOLIB</tt>.</p></div>
</div>
<div class="sect3">
<h4 id="_dll">DLL</h4>
<div class="paragraph"><p>If you like, you can build the DLL yourself using VS2008 or VS2010 (the
free Express Edition is sufficient) and perhaps newer versions, though this has
not been tested by the author. (As of 2013 we need to restore the solution file
to make this easy, that’s currently missing).</p></div>
</div>
<div class="sect3">
<h4 id="_mingw_cygwin">MinGW/Cygwin</h4>
<div class="paragraph"><p>You can build it in the traditional Unix method under Cygwin/MinGW using the
"configure; make; make install" approach. If the "configure" script is not
present in the repository you cloned, generate it by running "bootstrap".</p></div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_api_concepts">API concepts</h2>
<div class="sectionbody">
<div class="paragraph"><p>To use tpl, you need to know the order in which to call the API functions, and
the background concepts of format string, arrays and index numbers.</p></div>
<div class="sect2">
<h3 id="_order_of_functions">Order of functions</h3>
<div class="paragraph"><p>Creating a tpl is always the first step, and freeing it is the last step. In
between, you either pack and dump the tpl (if you’re serializing data) or you
load a tpl image and unpack it (if you’re deserializing data).</p></div>
<div class="tableblock">
<table rules="none"
width="50%"
frame="border"
cellspacing="0" cellpadding="4">
<caption class="title">Table 1. Order of usage</caption>
<col width="9%" />
<col width="45%" />
<col width="45%" />
<thead>
<tr>
<th align="center" valign="top">Step </th>
<th align="center" valign="top"> If you’re serializing…</th>
<th align="center" valign="top"> If you’re deserializing…</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" valign="top"><p class="table">1.</p></td>
<td align="center" valign="top"><p class="table"><tt>tpl_map()</tt></p></td>
<td align="center" valign="top"><p class="table"><tt>tpl_map()</tt></p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table">2.</p></td>
<td align="center" valign="top"><p class="table"><tt>tpl_pack()</tt></p></td>
<td align="center" valign="top"><p class="table"><tt>tpl_load()</tt></p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table">3.</p></td>
<td align="center" valign="top"><p class="table"><tt>tpl_dump()</tt></p></td>
<td align="center" valign="top"><p class="table"><tt>tpl_unpack()</tt></p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table">4.</p></td>
<td align="center" valign="top"><p class="table"><tt>tpl_free()</tt></p></td>
<td align="center" valign="top"><p class="table"><tt>tpl_free()</tt></p></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="sect2">
<h3 id="format">Format string</h3>
<div class="paragraph"><p>When a tpl is created using <tt>tpl_map()</tt>, its data type is expressed as a format
string. Each character in the format string has an associated argument of a
specific type. For example, this is how a format string and its arguments are
passed in to <tt>tpl_map</tt>:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>tpl_node *tn;
char c;
int i[10];
tn = tpl_map("ci#", &c, i, 10); /* ci# is our format string */</tt></pre>
</div></div>
<div class="tableblock" id="types">
<table rules="none"
width="90%"
frame="border"
cellspacing="0" cellpadding="4">
<caption class="title">Table 2. Supported format characters</caption>
<col width="11%" />
<col width="44%" />
<col width="44%" />
<thead>
<tr>
<th align="center" valign="top">Type </th>
<th align="left" valign="top"> Description </th>
<th align="left" valign="top"> Required argument type</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" valign="top"><p class="table"><tt>j</tt></p></td>
<td align="left" valign="top"><p class="table">16-bit signed int</p></td>
<td align="left" valign="top"><p class="table">int16_t* or equivalent</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>v</tt></p></td>
<td align="left" valign="top"><p class="table">16-bit unsigned int</p></td>
<td align="left" valign="top"><p class="table">uint16_t* or equivalent</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>i</tt></p></td>
<td align="left" valign="top"><p class="table">32-bit signed int</p></td>
<td align="left" valign="top"><p class="table">int32_t* or equivalent</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>u</tt></p></td>
<td align="left" valign="top"><p class="table">32-bit unsigned int</p></td>
<td align="left" valign="top"><p class="table">uint32_t* or equivalent</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>I</tt></p></td>
<td align="left" valign="top"><p class="table">64-bit signed int</p></td>
<td align="left" valign="top"><p class="table">int64_t* or equivalent</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>U</tt></p></td>
<td align="left" valign="top"><p class="table">64-bit unsigned int</p></td>
<td align="left" valign="top"><p class="table">uint64_t* or equivalent</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>c</tt></p></td>
<td align="left" valign="top"><p class="table">character (byte)</p></td>
<td align="left" valign="top"><p class="table">char*</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>s</tt></p></td>
<td align="left" valign="top"><p class="table">string</p></td>
<td align="left" valign="top"><p class="table">char**</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>f</tt></p></td>
<td align="left" valign="top"><p class="table">64-bit double precision float</p></td>
<td align="left" valign="top"><p class="table">double* (varies by platform)</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>#</tt></p></td>
<td align="left" valign="top"><p class="table">array length; modifies preceding <tt>iujvIUcsf</tt> or <tt>S(...)</tt></p></td>
<td align="left" valign="top"><p class="table">int</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>B</tt></p></td>
<td align="left" valign="top"><p class="table">binary buffer (arbitrary-length)</p></td>
<td align="left" valign="top"><p class="table">tpl_bin*</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>S</tt></p></td>
<td align="left" valign="top"><p class="table">structure (…)</p></td>
<td align="left" valign="top"><p class="table">struct *</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>$</tt></p></td>
<td align="left" valign="top"><p class="table">nested structure (…)</p></td>
<td align="left" valign="top"><p class="table">none</p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table"><tt>A</tt></p></td>
<td align="left" valign="top"><p class="table">array (…)</p></td>
<td align="left" valign="top"><p class="table">none</p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect3">
<h4 id="_explicit_sizes">Explicit sizes</h4>
<div class="paragraph"><p>The sizes of data types such as <tt>long</tt> and <tt>double</tt> vary by platform. This must
be kept in mind because most tpl format characters require a pointer argument to
a specific-sized type, listed above. You can use explicit-sized types such as
<tt>int32_t</tt> (defined in <tt>inttypes.h</tt>) in your program if you find this helpful.</p></div>
<div class="sect4">
<h5 id="trouble_with_double">The trouble with double</h5>
<div class="paragraph"><p>Unfortunately there are no standard explicit-sized floating-point types-- no
<tt>float64_t</tt>, for example. If you plan to serialize <tt>double</tt> on your platform
using tpl’s <tt>f</tt> format character, first be sure that your <tt>double</tt> is 64 bits.
Second, if you plan to deserialize it on a different kind of CPU, be sure that
both CPU’s use the same floating-point representation such as IEEE 754.</p></div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="arrays">Arrays</h3>
<div class="paragraph"><p>Arrays come in two kinds: <strong>fixed-length</strong> and <strong>variable-length</strong> arrays.
Intuitively, they can be thought of like conventional C arrays and linked lists.
In general, use fixed-length arrays if possible, and variable-length arrays
if necessary. The variable-length arrays support more complex data types, and
give or receive the elements to your program one by one.</p></div>
<div class="sect3">
<h4 id="_fixed_length_vs_variable_length_arrays">Fixed-length vs. Variable-length arrays</h4>
<div class="dlist"><dl>
<dt class="hdlist1">
Notation
</dt>
<dd>
<p>
Fixed-length arrays are denoted like <tt>i#</tt> (a simple type followed by one or
more <tt>#</tt> signs), but variable-length arrays are denoted like <tt>A(i)</tt>.
</p>
</dd>
<dt class="hdlist1">
Element handling
</dt>
<dd>
<p>
All the elements of a fixed-length array are packed or unpacked at once. But
the elements of a variable-length array are packed or unpacked one by one.
</p>
</dd>
<dt class="hdlist1">
Array length
</dt>
<dd>
<p>
The number of elements in a fixed-length array is specified before use--
before any data is packed. But variable-length arrays do not have a fixed
element count. They can have any number of elements packed into them. When
unpacking a variable-length array, they are unpacked one by one until they
are exhausted.
</p>
</dd>
<dt class="hdlist1">
Element types
</dt>
<dd>
<p>
Elements of fixed-length arrays can be the integer, byte, double, string
types or structures. (This excludes format characters <tt>BA</tt>). Fixed-length
arrays can also be multi-dimensional like <tt>i##</tt>. Variable-length arrays can
have simple or complex elements-- for example, an array of ints <tt>A(i)</tt>, an
array of int/double pairs <tt>A(if)</tt>, or even nested arrays like <tt>A(A(if))</tt>.
</p>
</dd>
</dl></div>
<div class="paragraph"><p>Before explaining all the concepts, it’s illustrative to see how both kinds of
arrays are used. Let’s pack the integers 0 through 9 both ways.</p></div>
<div class="listingblock" id="fixed_pack">
<div class="title">Packing 0-9 as a fixed-length array</div>
<div class="content">
<pre><tt>#include "tpl.h"
int main() {
tpl_node *tn;
int x[] = {0,1,2,3,4,5,6,7,8,9};
tn = tpl_map("i#", x, 10);
tpl_pack(tn,0); /* pack all 10 elements at once */
tpl_dump(tn, TPL_FILE, "/tmp/fixed.tpl");
tpl_free(tn);
}</tt></pre>
</div></div>
<div class="paragraph"><p>Note that the length of the fixed-length array (10) was passed as an argument to
<tt>tpl_map()</tt>. The corresponding unpacking <a href="#fixed_unpack">example</a> is listed
further below. Now let’s see how we would pack 0-9 as a variable-length array:</p></div>
<div class="listingblock">
<div class="title">Packing 0-9 as a variable-length array</div>
<div class="content">
<pre><tt>#include "tpl.h"
int main() {
tpl_node *tn;
int x;
tn = tpl_map("A(i)", &x);
for(x = 0; x < 10; x++) tpl_pack(tn,1); /* pack one element at a time */
tpl_dump(tn, TPL_FILE, "/tmp/variable.tpl");
tpl_free(tn);
}</tt></pre>
</div></div>
<div class="paragraph"><p>Notice how we called <tt>tpl_pack</tt> in a loop, once for each element 0-9. Again,
there is a corresponding unpacking <a href="#var_unpack">example</a> shown later in the
guide. You might also notice that this time, we passed 1 as the final argument
to tpl_pack. This is an index number designating which variable-length array
we’re packing. In this case, there is only one.</p></div>
</div>
<div class="sect3">
<h4 id="index">Index numbers</h4>
<div class="paragraph"><p>Index numbers identify a particular variable-length array in the format string.
Each <tt>A(...)</tt> in a format string has its own index number. The index numbers
are assigned left-to-right starting from 1. Examples:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>A(i) /* index number 1 */
A(i)A(i) /* index numbers 1 and 2 */
A(A(i)) /* index numbers 1 and 2 (order is independent of nesting) */</tt></pre>
</div></div>
<div class="sect4">
<h5 id="_special_index_number_0">Special index number 0</h5>
<div class="paragraph"><p>The special index number 0 designates all the format characters that are not
inside an <tt>A(...)</tt>. Examples of what index 0 does (and does not) designate:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>S(ius) /* index 0 designates the whole thing */
iA(c)u /* index 0 designates the i and the u */
c#A(i)S(ci) /* index 0 designates the c# and the S(ci) */</tt></pre>
</div></div>
<div class="paragraph"><p>An index number is passed to <tt>tpl_pack</tt> and <tt>tpl_unpack</tt> to specify which
variable-length array (or non-array, in the case of index number 0) to act upon.</p></div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_integers">Integers</h3>
<div class="paragraph"><p>The array examples <a href="#fixed_pack">above</a> demonstrated how integers could be
packed. We’ll show some further examples here of unpacking integers and dealing
with multi-dimensional arrays. The same program could be used to demonstrate
working with byte, 16-bit shorts, 32-bit or 64-bit signed and unsigned integers
with only a change to the data type and the format character.</p></div>
<div class="listingblock" id="fixed_unpack">
<div class="title">Unpacking 0-9 from a fixed-length array</div>
<div class="content">
<pre><tt>#include "tpl.h"
int main() {
tpl_node *tn;
int x[10];
tn = tpl_map("i#", x, 10);
tpl_load(tn, TPL_FILE, "/tmp/fixed.tpl");
tpl_unpack(tn,0); /* unpack all 10 elements at once */
tpl_free(tn);
/* now do something with x[0]...x[9].. (not shown */
}</tt></pre>
</div></div>
<div class="paragraph"><p>For completeness, let’s also see how to unpack a variable-length integer array.</p></div>
<div class="listingblock" id="var_unpack">
<div class="title">Unpacking 0-9 from a variable-length array</div>
<div class="content">
<pre><tt>#include "tpl.h"
int main() {
tpl_node *tn;
int x;
tn = tpl_map("A(i)", &x);
tpl_load(tn, TPL_FILE, "/tmp/variable.tpl");
while (tpl_unpack(tn,1) > 0) printf("%d\n",x); /* unpack one by one */
tpl_free(tn);
}</tt></pre>
</div></div>
<div class="sect3">
<h4 id="multidim_int">Multi-dimensional arrays</h4>
<div class="paragraph"><p>A multi-dimensional matrix of integers can be packed and unpacked the same way
as any fixed-length array.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>int xy[XDIM][YDIM];
...
tn = tpl_map("i##", xy, XDIM, YDIM);
tpl_pack(tn, 0);</tt></pre>
</div></div>
<div class="paragraph"><p>This single call to <tt>tpl_pack</tt> packs the entire matrix.</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_strings">Strings</h3>
<div class="paragraph"><p>Tpl can serialize C strings. A different format is used for <tt>char*</tt> vs. <tt>char[ ]</tt>
as described below. Let’s look at <tt>char*</tt> first:</p></div>
<div class="listingblock">
<div class="title">Packing a string</div>
<div class="content">
<pre><tt> #include "tpl.h"
int main() {
tpl_node *tn;
char *s = "hello, world!";
tn = tpl_map("s", &s);
tpl_pack(tn,0); /* copies "hello, world!" into the tpl */
tpl_dump(tn,TPL_FILE,"string.tpl");
tpl_free(tn);
}</tt></pre>
</div></div>
<div class="paragraph"><p>The <tt>char*</tt> must point to a null-terminated string or be a <tt>NULL</tt> pointer.</p></div>
<div class="paragraph"><p>When deserializing (unpacking) a C string, space for it will be allocated
automatically, but you are responsible for freeing it (unless it is <tt>NULL</tt>):</p></div>
<div class="listingblock">
<div class="title">Unpacking a string</div>
<div class="content">
<pre><tt> #include "tpl.h"
int main() {
tpl_node *tn;
char *s;
tn = tpl_map("s", &s);
tpl_load(tn,TPL_FILE,"string.tpl");
tpl_unpack(tn,0); /* allocates space, points s to "hello, world!" */
printf("unpacked %s\n", s);
free(s); /* our responsibility to free s */
tpl_free(tn);
}</tt></pre>
</div></div>
<div class="sect3">
<h4 id="_char_vs_char">char* vs char[ ]</h4>
<div class="paragraph"><p>The <tt>s</tt> format character is only for use with <tt>char*</tt> types. In the example
above, <tt>s</tt> is a <tt>char*</tt>. If it had been a <tt>char s[14]</tt>, we would use the format
characters <tt>c#</tt> to pack or unpack it, as a fixed-length character array. (This
unpacks the characters "in-place", instead of into a dynamically allocated
buffer). Also, a fixed-length buffer described by <tt>c#</tt> need not be
null-terminated.</p></div>
</div>
<div class="sect3">
<h4 id="_arrays_of_strings">Arrays of strings</h4>
<div class="paragraph"><p>You can use fixed- or variable-length arrays of strings in tpl. An example of
packing a fixed-length two-dimensional array of strings is shown here.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>char *labels[2][3] = { {"one", "two", "three"},
{"eins", "zwei", "drei" } };
tpl_node *tn;
tn = tpl_map("s##", labels, 2, 3);
tpl_pack(tn,0);
tpl_dump(tn,TPL_FILE,filename);
tpl_free(tn);</tt></pre>
</div></div>
<div class="paragraph"><p>Later, when unpacking these strings, the programmer must remember to free them
one by one, after they are no longer needed.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>char *olabels[2][3];
int i,j;</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>tn = tpl_map("s##", olabels, 2, 3);
tpl_load(tn,TPL_FILE,filename);
tpl_unpack(tn,0);
tpl_free(tn);</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>for(i=0;i<2;i++) {
for(j=0;j<3;j++) {
printf("%s\n", olabels[i][j]);
free(olabels[i][j]);
}
}</tt></pre>
</div></div>
</div>
</div>
<div class="sect2">
<h3 id="_binary_buffers">Binary buffers</h3>
<div class="paragraph"><p>Packing an arbitrary-length binary buffer (tpl format character <tt>B</tt>) makes use
of the <tt>tpl_bin</tt> structure. You must declare this structure and populate it
with the address and length of the binary buffer to be packed.</p></div>
<div class="listingblock">
<div class="title">Packing a binary buffer</div>
<div class="content">
<pre><tt> #include "tpl.h"
#include <sys/time.h>
int main() {
tpl_node *tn;
tpl_bin tb;
/* we'll use a timeval as our guinea pig */
struct timeval tv;
gettimeofday(&tv,NULL);
tn = tpl_map( "B", &tb );
tb.sz = sizeof(struct timeval); /* size of buffer to pack */
tb.addr = &tv; /* address of buffer to pack */
tpl_pack( tn, 0 );
tpl_dump(tn, TPL_FILE, "bin.tpl");
tpl_free(tn);
}</tt></pre>
</div></div>
<div class="paragraph"><p>When you unpack a binary buffer, tpl will automatically allocate it, and will
populate your <tt>tpl_bin</tt> structure with its address and length. You are
responsible for eventually freeing the buffer.</p></div>
<div class="listingblock">
<div class="title">Unpacking a binary buffer</div>
<div class="content">
<pre><tt> #include "tpl.h"
int main() {
tpl_node *tn;
tpl_bin tb;
tn = tpl_map( "B", &tb );
tpl_load( tn, TPL_FILE, "bin.tpl" );
tpl_unpack( tn, 0 );
tpl_free(tn);
printf("binary buffer of length %d at address %p\n", tb.sz, tb.addr);
free(tb.addr); /* our responsibility to free it */
}</tt></pre>
</div></div>
</div>
<div class="sect2">
<h3 id="_structures">Structures</h3>
<div class="paragraph"><p>You can use tpl to pack and unpack structures, and arrays of structures.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>struct ci {
char c;
int i;
};
struct ci s = {'a', 1};</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>tn = tpl_map("S(ci)", &s); /* pass structure address */
tpl_pack(tn, 0);
tpl_dump(tn, TPL_FILE, "struct.tpl");
tpl_free(tn);</tt></pre>
</div></div>
<div class="paragraph"><p>As shown, omit the individual arguments for the format characters inside the
parenthesis. The exception is for fixed-length arrays; when <tt>S(...)</tt> contains a
<tt>#</tt> character, its length argument is required: <tt>tpl_map("S(f#i)", &s, 10);</tt></p></div>
<div class="paragraph"><p>When using the <tt>S(...)</tt> format, the only characters allowed inside the
parentheses are <tt>iujvcsfIU#$()</tt>.</p></div>
<div class="sect3">
<h4 id="_structure_arrays">Structure arrays</h4>
<div class="paragraph"><p>Arrays of structures are the same as simple arrays. Fixed- or variable- length
arrays are supported.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>struct ci sa[100], one;</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>tn = tpl_map("S(ci)#", sa, 100); /* fixed-length array of 100 structures */
tn = tpl_map("A(S(ci))", &one); /* variable-length array (one at a time)*/</tt></pre>
</div></div>
<div class="paragraph"><p>The differences between fixed- and variable-length arrays are explained in the
<a href="#arrays">Arrays</a> section.</p></div>
</div>
<div class="sect3">
<h4 id="_nested_structures">Nested structures</h4>
<div class="paragraph"><p>When dealing with nested structures, the outermost structure uses the <tt>S</tt> format
character, and the inner nested structures use the <tt>$</tt> format. Only the
<em>outermost</em> structure’s address is given to <tt>tpl_map</tt>.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>struct inner_t {
char a;
}</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>struct outer_t {
char b;
struct inner_t i;
}</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>tpl_node *tn;
struct outer_t outer = {'b', {'a'}};</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>tn = tpl_map("S(c$(c))", &outer);</tt></pre>
</div></div>
<div class="paragraph"><p>Structures can nest to any level. Currently tpl does not support fixed-length
array suffixes on inner structures. However the outermost structure can have a
length suffix even if it contains some nested structures.</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_linked_lists">Linked lists</h3>
<div class="paragraph"><p>While tpl has no specific data type for a linked list, the technique for
packing them is illustrated here. First describe your list element as a
format string and then surround it with <tt>A(...)</tt> to describe it as
variable-length array. Then, using a temporary variable, iterate over each
list element, copying it to the temporary variable and packing it.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>struct element {
char c;
int i;
struct element *next;
}</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>struct element *list, *i, tmp;
tpl_node *tn;</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>/* add some elements to list.. (not shown)*/</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>tn = tpl_map("A(S(ci))", &tmp);
for(i = list; i != NULL; i=i->next) {
tmp = *i;
tpl_pack(tn, 1);
}
tpl_dump(tn,TPL_FILE,"list.tpl");
tpl_free(tn);</tt></pre>
</div></div>
<div class="paragraph"><p>Unpacking is similar. The <tt>for</tt> loop is just replaced with:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>while( tpl_unpack(tn,1) > 0) {
struct element *newelt = malloc(sizeof(struct element));
*newelt = tmp;
add_to_list(list, newelt);
}</tt></pre>
</div></div>
<div class="paragraph"><p>As you can see, tpl does not reinstate the whole list at once-- just one
element at a time. You need to link the elements manually. A future release of
tpl may support <em>pointer swizzling</em> to make this easier.</p></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_api">API</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="tpl_map">tpl_map</h3>
<div class="paragraph"><p>The only way to create a tpl is to call <tt>tpl_map()</tt>. The first argument is the
<a href="#format">format string</a>. This is followed by a list of arguments as required by
the particular characters in the format string. E.g,</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>tpl_node *tn;
int i;
tn = tpl_map( "A(i)", &i );</tt></pre>
</div></div>
<div class="paragraph"><p>The function creates a mapping between the items in the format string and the C
program variables whose addresses are given. Later, the C variables will be read
or written as the tpl is packed or unpacked.</p></div>
<div class="paragraph"><p>This function returns a <tt>tpl_node*</tt> on success, or <tt>NULL</tt> on failure.</p></div>
</div>
<div class="sect2">
<h3 id="tpl_pack">tpl_pack</h3>
<div class="paragraph"><p>The function <tt>tpl_pack()</tt> packs data into a tpl. The arguments to
<tt>tpl_pack()</tt> are a <tt>tpl_node*</tt> and an <a href="#index">index number</a>.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>tn = tpl_map("A(i)A(c)", &i, &c);
for(i=0; i<10; i++) tpl_pack(tn, 1); /* pack 0-9 into index 1 */
for(c='a'; c<='z'; c++) tpl_pack(tn, 2); /* pack a-z into index 2 */</tt></pre>
</div></div>
<div class="sidebarblock">
<div class="content">
<div class="title">Data is copied when packed</div>
<div class="paragraph"><p>Every call to <tt>tpl_pack()</tt> immediately <em>copies</em> the data being packed. Thus
the program is free to immediately overwrite or re-use the packed variables.</p></div>
</div></div>
<div class="sect3">
<h4 id="_index_number_0">Index number 0</h4>
<div class="paragraph"><p>It is necessary to pack index number 0 only if the format string contains
characters that are not inside an <tt>A(...)</tt>, such as the <tt>i</tt> in the format string
<tt>iA(c)</tt>.</p></div>
</div>
<div class="sect3">
<h4 id="_variable_length_arrays">Variable-length arrays</h4>
<div class="sect4">
<h5 id="_adding_elements_to_an_array">Adding elements to an array</h5>
<div class="paragraph"><p>To add elements to a variable-length array, call <tt>tpl_pack()</tt> repeatedly. Each
call adds another element to the array.</p></div>
</div>
<div class="sect4">
<h5 id="_zero_length_arrays_are_ok">Zero-length arrays are ok</h5>
<div class="paragraph"><p>It’s perfectly acceptable to pack nothing into a variable-length array,
resulting in a zero-length array.</p></div>
</div>
<div class="sect4">
<h5 id="nested_pack">Packing nested arrays</h5>
<div class="paragraph"><p>In a format string containing a nested, variable-length array, such as
<tt>A(A(s))</tt>, the inner, child array should be packed prior to the parent array.</p></div>
<div class="paragraph"><p>When you pack a parent array, a "snapshot" of the current child array is placed
into the parent’s new element. Packing a parent array also empties the child
array. This way, you can pack new data into the child, then pack the parent
again. This creates distinct parent elements which each contain distinct child
arrays.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">When dealing with nested arrays like <tt>A(A(i))</tt>, <em>pack</em> them from the "inside
out" (child first), but <em>unpack</em> them from the "outside in" (parent first).</td>
</tr></table>
</div>
<div class="paragraph"><p>The example below creates a tpl having the format string <tt>A(A(c))</tt>.</p></div>
<div class="listingblock">
<div class="title">Packing nested arrays</div>
<div class="content">
<pre><tt>#include "tpl.h"
int main() {
char c;
tpl_node *tn;
tn = tpl_map("A(A(c))", &c);
for(c='a'; c<'c'; c++) tpl_pack(tn,2); /* pack child (twice) */
tpl_pack(tn, 1); /* pack parent */
for(c='1'; c<'4'; c++) tpl_pack(tn,2); /* pack child (three times) */
tpl_pack(tn, 1); /* pack parent */
tpl_dump(tn, TPL_FILE, "test40.tpl");
tpl_free(tn);
}</tt></pre>
</div></div>
<div class="paragraph"><p>This creates a nested array in which the parent has two elements: the first
element is the two-element nested array <em>a</em>, <em>b</em>; and the second element is
the three-element nested array <em>1</em>, <em>2</em>, <em>3</em>.
The <a href="#nested_unpack">nested unpacking example</a> shows how this tpl is unpacked.</p></div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="tpl_dump">tpl_dump</h3>
<div class="paragraph"><p>After packing a tpl, <tt>tpl_dump()</tt> is used to write the tpl image to a file,
memory buffer or file descriptor. The corresponding modes are shown below. A
final mode is for querying the output size without actually performing the dump.</p></div>
<div class="tableblock">
<table rules="none"
width="80%"
frame="border"
cellspacing="0" cellpadding="4">
<col width="30%" />
<col width="70%" />
<thead>
<tr>
<th align="center" valign="top">Write to… </th>
<th align="left" valign="top">Usage</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" valign="top"><p class="table">file</p></td>
<td align="left" valign="top"><p class="table"><tt>tpl_dump(tn, TPL_FILE, "file.tpl" );</tt></p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table">file descriptor</p></td>
<td align="left" valign="top"><p class="table"><tt>tpl_dump(tn, TPL_FD, 2);</tt></p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table">memory</p></td>
<td align="left" valign="top"><p class="table"><tt>tpl_dump(tn, TPL_MEM, &addr, &len );</tt></p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table">caller’s memory</p></td>
<td align="left" valign="top"><p class="table"><tt>tpl_dump(tn, TPL_MEM|TPL_PREALLOCD, buf, sizeof(buf));</tt></p></td>
</tr>
<tr>
<td align="center" valign="top"><p class="table">just get size</p></td>
<td align="left" valign="top"><p class="table"><tt>tpl_dump(tn, TPL_GETSIZE, &sz);</tt></p></td>
</tr>
</tbody>
</table>
</div>
<div class="paragraph"><p>The first argument is the <tt>tpl_node*</tt> and the second is one of these constants:</p></div>
<div class="dlist"><dl>
<dt class="hdlist1">
<tt>TPL_FILE</tt>
</dt>
<dd>
<p>
Writes the tpl to a file whose name is given in the following argument.
The file is created with permissions 664 (<tt>rw-rw-r--</tt>) unless further
restricted by the process <tt>umask</tt>.
</p>
</dd>
<dt class="hdlist1">
<tt>TPL_FD</tt>
</dt>
<dd>
<p>
Writes the tpl to the file descriptor given in the following argument.
The descriptor can be either blocking or non-blocking, but will busy-loop
if non-blocking and the contents cannot be written immediately.
</p>
</dd>
<dt class="hdlist1">
<tt>TPL_MEM</tt>
</dt>
<dd>
<p>
Writes the tpl to a memory buffer. The following two arguments must be a
<tt>void\*\*</tt> and a <tt>size_t*</tt>. The function will allocate a buffer and store
its address and length into these locations. The caller is responsible to
<tt>free()</tt> the buffer when done using it.
</p>
</dd>
<dt class="hdlist1">
<tt>TPL_MEM|TPL_PREALLOCD</tt>
</dt>
<dd>
<p>
Writes the tpl to a memory buffer that the caller has already allocated or
declared. The following two arguments must be a <tt>void*</tt> and a <tt>size_t</tt>
specifying the buffer address and size respectively. (If the buffer is of
insufficient size to receive the tpl dump, the function will return -1).
This mode can be useful in conjunction with <tt>tpl_load</tt> in <tt>TPL_EXCESS_OK</tt>
mode, as shown <a href="#excess_ok">here.</a>
</p>
</dd>
<dt class="hdlist1">
<tt>TPL_GETSIZE</tt>
</dt>
<dd>
<p>
This special mode does not actually dump the tpl. Instead it places the size
that the dump <em>would</em> require into the <tt>size_t</tt> pointed to by the
following argument.
</p>
</dd>
</dl></div>
<div class="paragraph"><p>The return value is 0 on success, or -1 on error.</p></div>
<div class="paragraph"><p>The <tt>tpl_dump()</tt> function does not free the tpl. Use <tt>tpl_free()</tt> to release
the tpl’s resources when done.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
<div class="title">Back-to-back tpl images require no delimiter</div>If you want to store a series of tpl images, or transmit sequential tpl images
over a socket (perhaps as messages to another program), you can simply dump them
sequentially without needing to add any delimiter for the individual tpl images.
Tpl images are internally delimited, so <tt>tpl_load</tt> will read just one at a time
even if multiple images are contiguous.</td>
</tr></table>
</div>
</div>
<div class="sect2">
<h3 id="tpl_load">tpl_load</h3>
<div class="paragraph"><p>This API function reads a previously-dumped tpl image from a file, memory
buffer or file descriptor, and prepares it for subsequent unpacking. The format
string specified in the preceding call to <tt>tpl_map()</tt> will be cross-checked
for equality with the format string stored in the tpl image.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>tn = tpl_map( "A(i)", &i );
tpl_load( tn, TPL_FILE, "demo.tpl" );</tt></pre>
</div></div>
<div class="paragraph"><p>The first argument to <tt>tpl_load()</tt> is the <tt>tpl_node*</tt>. The second argument is
one of the constants:</p></div>
<div class="dlist"><dl>
<dt class="hdlist1">
<tt>TPL_FILE</tt>
</dt>
<dd>
<p>
Loads the tpl from the file named in the following argument. It is also
possible to bitwise-OR this flag with <tt>TPL_EXCESS_OK</tt> as explained below.
</p>
</dd>
<dt class="hdlist1">
<tt>TPL_MEM</tt>
</dt>
<dd>
<p>
Loads the tpl from a memory buffer. The following two arguments must be a
<tt>void*</tt> and a <tt>size_t</tt>, specifying the buffer address and size,
respectively. The caller must not free the memory buffer until after
freeing the tpl with <tt>tpl_free()</tt>. (If the caller wishes to hand over
responsibility for freeing the memory buffer, so that it’s automatically
freed along with the tpl when <tt>tpl_free()</tt> is called, the constant
<tt>TPL_UFREE</tt> may be bitwise-OR’d with <tt>TPL_MEM</tt> to achieve this).
Furthermore, <tt>TPL_MEM</tt> may be bitwise-OR’d with <tt>TPL_EXCESS_OK</tt>, explained
below.
</p>
</dd>
<dt class="hdlist1">
<tt>TPL_FD</tt>
</dt>
<dd>
<p>
Loads the tpl from the file descriptor given in the following argument.
The descriptor is read until one complete tpl image is loaded; no bytes
past the end of the tpl image will be read. The descriptor can be either
blocking or non-blocking, but will busy-loop if non-blocking and the
contents cannot be read immediately.
</p>
</dd>
</dl></div>
<div class="paragraph"><p>During loading, the tpl image will be extensively checked for internal validity.</p></div>
<div class="paragraph"><p>This function returns 0 on success or -1 on error.</p></div>
<div class="sect3">
<h4 id="excess_ok"><tt>TPL_EXCESS_OK</tt></h4>
<div class="paragraph"><p>When reading a tpl image from a file or memory (but not from a file descriptor)
the size of the file or memory buffer must exactly equal that of the tpl image
stored therein. In other words, no excess trailing data beyond the tpl image is
permitted. The bit flag <tt>TPL_EXCESS_OK</tt> can be OR’d with <tt>TPL_MEM</tt> or <tt>TPL_FILE</tt>
to relax this requirement.</p></div>
<div class="paragraph"><p>A situation where this flag can be useful is in conjunction with <tt>tpl_dump</tt> in
the <tt>TPL_MEM|TPL_PREALLOCD</tt> mode. In this example, the program does not concern
itself with the actual tpl size as long as <tt>LEN</tt> is sufficiently large.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>char buf[LEN]; /* will store and read tpl images here */
...
tpl_dump(tn, TPL_MEM|TPL_PREALLOCD, buf, LEN);
...
tpl_load(tn, TPL_MEM|TPL_EXCESS_OK, buf, LEN);</tt></pre>
</div></div>
</div>
</div>
<div class="sect2">
<h3 id="tpl_unpack">tpl_unpack</h3>
<div class="paragraph"><p>The <tt>tpl_unpack()</tt> function unpacks data from the tpl. When data is unpacked,
it is copied to the C program variables originally specified in <tt>tpl_map()</tt>.
The first argument to <tt>tpl_unpack</tt> is the <tt>tpl_node*</tt> for the tpl and the
second argument is an <a href="#index">index number</a>.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>tn = tpl_map( "A(i)A(c)", &i, &c );
tpl_load( tn, TPL_FILE, "nested.tpl" );
while (tpl_unpack( tn, 1) > 0) printf("i is %d\n", i); /* unpack index 1 */
while (tpl_unpack( tn, 2) > 0) printf("c is %c\n", c); /* unpack index 2 */</tt></pre>
</div></div>
<div class="sect3">
<h4 id="_index_number_0_2">Index number 0</h4>
<div class="paragraph"><p>It is necessary to unpack index number 0 only if the format string contains
characters that are not inside an <tt>A(...)</tt>, such as the <tt>i</tt> in the format string
<tt>iA(c)</tt>.</p></div>
</div>
<div class="sect3">
<h4 id="_variable_length_arrays_2">Variable-length arrays</h4>
<div class="sect4">
<h5 id="_unpacking_elements_from_an_array">Unpacking elements from an array</h5>
<div class="paragraph"><p>For variable-length arrays, each call to <tt>tpl_unpack()</tt> unpacks another element.
The return value can be used to tell when you’re done: if it’s positive, an
element was unpacked; if it’s 0, nothing was unpacked because there are no more
elements. A negative retun value indicates an error (e.g. invalid index number).
In this document, we usually unpack variable-length arrays using a <tt>while</tt> loop:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>while( tpl_unpack( tn, 1 ) > 0 ) {
/* got another element */
}</tt></pre>
</div></div>
</div>
<div class="sect4">
<h5 id="_array_length">Array length</h5>
<div class="paragraph"><p>When unpacking a variable-length array, it may be convenient to know ahead of
time how many elements will need to be unpacked. You can use <tt>tpl_Alen()</tt> to
get this number.</p></div>
</div>
<div class="sect4">
<h5 id="nested_unpack">Unpacking nested arrays</h5>
<div class="paragraph"><p>In a format string containing a nested variable-length array such as <tt>A(A(s))</tt>,
unpack the outer, parent array before unpacking the child array.</p></div>
<div class="paragraph"><p>When you unpack a parent array, it prepares the child array for unpacking.
After unpacking the elements of the child array, the program can repeat the
process by unpacking another parent element, then the child elements, and so on.
The example below unpacks a tpl having the format string <tt>A(A(c))</tt>.</p></div>
<div class="listingblock">
<div class="title">Unpacking nested arrays</div>
<div class="content">
<pre><tt>#include "tpl.h"
#include <stdio.h>
int main() {
char c;
tpl_node *tn;
tn = tpl_map("A(A(c))", &c);
tpl_load(tn, TPL_FILE, "test40.tpl");
while (tpl_unpack(tn,1) > 0) {
while (tpl_unpack(tn,2) > 0) printf("%c ",c);
printf("\n");
}
tpl_free(tn);
}</tt></pre>
</div></div>
<div class="paragraph"><p>The file <tt>test40.tpl</tt> is from the <a href="#nested_pack">nested packing example</a>. When
run, this program prints:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>a b
1 2 3</tt></pre>
</div></div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="tpl_free">tpl_free</h3>
<div class="paragraph"><p>The final step for any tpl is to release it using <tt>tpl_free()</tt>. Its only
argument is the the <tt>tpl_node*</tt> to free.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>tpl_free( tn );</tt></pre>
</div></div>
<div class="paragraph"><p>This function does not return a value (it is <tt>void</tt>).</p></div>
</div>
<div class="sect2">
<h3 id="tpl_alen">tpl_Alen</h3>
<div class="paragraph"><p>This function takes a <tt>tpl_node*</tt> and an index number and returns an <tt>int</tt>
specifying the number of elements in the variable-length array.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>num_elements = tpl_Alen(tn, index);</tt></pre>
</div></div>
<div class="paragraph"><p>This is mainly useful for programs that unpack data and need to know ahead of
time the number of elements that will need to be unpacked. (It returns the
current number of elements; it will decrease as elements are unpacked).</p></div>
</div>
<div class="sect2">
<h3 id="tpl_peek">tpl_peek</h3>
<div class="paragraph"><p>This function peeks into a file or a memory buffer containing a tpl image and
and returns a copy of its format string. It can also peek at the lengths of
any fixed-length arrays in the format string, or it can also peek into the data
stored in the tpl.</p></div>
<div class="sect3">
<h4 id="_format_peek">Format peek</h4>
<div class="paragraph"><p>The format string can be obtained
like this:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>fmt = tpl_peek(TPL_FILE, "file.tpl");
fmt = tpl_peek(TPL_MEM, addr, sz);</tt></pre>
</div></div>
<div class="paragraph"><p>On success, a copy of the format string is returned. The caller must eventually
free it. On error, such as a non-existent file, or an invalid tpl image, it
returns <tt>NULL</tt>.</p></div>
</div>
<div class="sect3">
<h4 id="_array_length_peek">Array length peek</h4>
<div class="paragraph"><p>The lengths of all fixed-length arrays in the format string can be queried using
the <tt>TPL_FXLENS</tt> mode. It provides the number of such fixed-length arrays and
their lengths. If the former is non-zero, the caller must free the latter array
when finished. The format string itself must also be freed.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>uint32_t num_fxlens, *fxlens, j;
fmt = tpl_peek(TPL_FILE|TPL_FXLENS, filename, &num_fxlens, &fxlens);
if (fmt) {
printf("format %s, num_fxlens %u\n", fmt, num_fxlens);
for(j=0; j<num_fxlens; j++) printf("fxlens[%u] %u\n", j, fxlens[j]);
if (num_fxlens > 0) free(fxlens);
free(fmt);
}</tt></pre>
</div></div>
<div class="paragraph"><p>The <tt>TPL_FXLENS</tt> mode is mutually exclusive with <tt>TPL_DATAPEEK</tt>.</p></div>
</div>
<div class="sect3">
<h4 id="_data_peek">Data peek</h4>
<div class="paragraph"><p>To peek into the data, additional arguments are used. This is a quick
alternative to mapping, loading and unpacking the tpl, but peeking is limited
to the data in index 0. In other words, no peeking into <tt>A(...)</tt> types.
Suppose the tpl image in <tt>file.tpl</tt> has the format string <tt>siA(i)</tt>. Then the
index 0 format characters are <tt>si</tt>. This is how to peek at their content:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>char *s;
int i;
fmt = tpl_peek(TPL_FILE | TPL_DATAPEEK, "file.tpl", "si", &s, &i);</tt></pre>
</div></div>
<div class="paragraph"><p>Now <tt>s</tt>, <tt>i</tt>, and <tt>fmt</tt> have been populated with data. The caller must
eventually free <tt>fmt</tt> and <tt>s</tt> because they are allocated strings.
Of course, it works with <tt>TPL_MEM</tt> as well as <tt>TPL_FILE</tt>. Notice that
<tt>TPL_DATAPEEK</tt> was OR’d with the mode. You can also specify <em>any leading
portion</em> of the index 0 format if you don’t want to peek at the whole thing:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>fmt = tpl_peek(TPL_FILE | TPL_DATAPEEK, "file.tpl", "s", &s);</tt></pre>
</div></div>
<div class="paragraph"><p>The <tt>TPL_DATAPEEK</tt> mode is mutually exclusive with <tt>TPL_FXLENS</tt>.</p></div>
<div class="sect4">
<h5 id="_structure_peek">Structure peek</h5>
<div class="paragraph"><p>Lastly you can peek into <tt>S(...)</tt> structures in index 0, but omit the
surrounding <tt>S(...)</tt> in the format, and specify an argument to receive
each structure member individually. You can specify any leading portion
of the structure format. For example if <tt>struct.tpl</tt> has the format string
<tt>S(si)</tt>, you can peek at its data in these ways:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>fmt = tpl_peek(TPL_FILE | TPL_DATAPEEK, "struct.tpl", "s", &s);
fmt = tpl_peek(TPL_FILE | TPL_DATAPEEK, "struct.tpl", "si", &s, &i);</tt></pre>
</div></div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="tpl_jot">tpl_jot</h3>
<div class="paragraph"><p>This is a quick shortcut for generating a tpl. It can be used instead of the
usual "map, pack, dump, and free" lifecycle. With <tt>tpl_jot</tt> all those steps are
handled for you. It only works for simple formats-- namely, those without
<tt>A(...)</tt> in their format string. Here is how it is used:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>char *hello = "hello", *world = "world";
tpl_jot( TPL_FILE, "file.tpl", "ss", &hello, &world);</tt></pre>
</div></div>
<div class="paragraph"><p>It supports the three standard modes, <tt>TPL_FILE</tt>, <tt>TPL_FD</tt> and <tt>TPL_MEM</tt>.
It returns -1 on failure (such as a bad format string or error writing the
file) or 0 on success.</p></div>
</div>
<div class="sect2">
<h3 id="hooks">tpl_hook</h3>
<div class="paragraph"><p>Most users will just leave these hooks at their default values. You can change
these hook values if you want to modify tpl’s internal memory management and
error reporting behavior.</p></div>
<div class="paragraph"><p>A global structure called <tt>tpl_hook</tt> encapsulates the hooks. A program can
reconfigure any hook by specifying an alternative function whose prototype
matches the default. For example:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>#include "tpl.h"
extern tpl_hook_t tpl_hook;</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>int main() {
tpl_hook.oops = printf;
...
}</tt></pre>
</div></div>
<div class="tableblock">
<table rules="none"
width="90%"
frame="border"
cellspacing="0" cellpadding="4">
<caption class="title">Table 3. Configurable hooks</caption>
<col width="33%" />
<col width="33%" />
<col width="33%" />
<thead>
<tr>
<th align="left" valign="top">Hook </th>
<th align="left" valign="top">Description </th>
<th align="left" valign="top"> Default</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p class="table"><tt>tpl_hook.oops</tt></p></td>
<td align="left" valign="top"><p class="table">log error messages</p></td>
<td align="left" valign="top"><p class="table"><tt>tpl_oops</tt></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><tt>tpl_hook.malloc</tt></p></td>
<td align="left" valign="top"><p class="table">allocate memory</p></td>
<td align="left" valign="top"><p class="table"><tt>malloc</tt></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><tt>tpl_hook.realloc</tt></p></td>
<td align="left" valign="top"><p class="table">reallocate memory</p></td>
<td align="left" valign="top"><p class="table"><tt>realloc</tt></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><tt>tpl_hook.free</tt></p></td>
<td align="left" valign="top"><p class="table">free memory</p></td>
<td align="left" valign="top"><p class="table"><tt>free</tt></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><tt>tpl_hook.fatal</tt></p></td>
<td align="left" valign="top"><p class="table">log fatal message and exit</p></td>
<td align="left" valign="top"><p class="table"><tt>tpl_fatal</tt></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><tt>tpl_hook.gather_max</tt></p></td>
<td align="left" valign="top"><p class="table">tpl_gather max image size</p></td>
<td align="left" valign="top"><p class="table"><tt>0 (unlimited)</tt></p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect3">
<h4 id="_the_oops_hook">The oops hook</h4>
<div class="paragraph"><p>The <tt>oops</tt> has the same prototype as <tt>printf</tt>. The built-in default oops
handling function writes the error message to <tt>stderr</tt>.</p></div>
</div>
<div class="sect3">
<h4 id="_the_fatal_hook">The fatal hook</h4>
<div class="paragraph"><p>The fatal hook is invoked when a tpl function cannot continue because of an out-
of-memory condition or some other usage violation or inconsistency. It has this
prototype:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>void fatal_fcn(char *fmt, ...);</tt></pre>
</div></div>
<div class="paragraph"><p>The <tt>fatal</tt> hook must not return. It must either exit, <em>or</em> if the program needs
to handle the failure and keep executing, <tt>setjmp</tt> and <tt>longjmp</tt> can be used.
The default behavior is to <tt>exit(-1)</tt>.</p></div>
<div class="listingblock">
<div class="title">Using longjmp in a fatal error handler</div>
<div class="content">
<pre><tt>#include <setjmp.h>
#include <stdio.h>
#include <stdarg.h>
#include "tpl.h"
jmp_buf env;
extern tpl_hook_t tpl_hook;
void catch_fatal(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
longjmp(env,-1); /* return to setjmp point */
}
int main() {
int err;
tpl_node *tn;
tpl_hook.fatal = catch_fatal; /* install fatal handler */
err = setjmp(env); /* on error, control will return here */
if (err) {
printf("caught error!\n");
return -1;
}
tn = tpl_map("@"); /* generate a fatal error */
printf("program ending, without error\n");
return 0;
}</tt></pre>
</div></div>
<div class="paragraph"><p>This example is included in <tt>tests/test123.c</tt>. When run, this program prints:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>unsupported option @
failed to parse @
caught error!</tt></pre>
</div></div>
</div>
</div>
<div class="sect2">
<h3 id="_tpl_gather">tpl_gather</h3>
<div class="sidebarblock">
<div class="content">
<div class="title">Most programs don’t need this</div>
<div class="paragraph"><p>Normally, <tt>tpl_load()</tt> is used to read a tpl image having an expected format
string. A more generic operation is to acquire a tpl image whose format string is
unknown. E.g., a generic message-receiving function might gather tpl images of
varying format and route them to their final destination. This is the purpose of
<tt>tpl_gather</tt>. It produces a memory buffer containing one tpl image. If there
are multiple contiguous images in the input, it gathers exactly one image at a
time.</p></div>
</div></div>
<div class="paragraph"><p>The prototype for this function is:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>int tpl_gather( int mode, ...);</tt></pre>
</div></div>
<div class="paragraph"><p>The <tt>mode</tt> argument is one of three constants listed below, which must be
followed by the mode-specific required arguments:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>TPL_GATHER_BLOCKING, int fd, void **img, size_t *sz
TPL_GATHER_NONBLOCKING, int fd, tpl_gather_t **gs, tpl_gather_cb *cb, void *data
TPL_GATHER_MEM, void *addr, size_t sz, tpl_gather_t **gs, tpl_gather_cb *cb, void *data</tt></pre>
</div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Note</div>
</td>
<td class="content">
<div class="title"><tt>tpl_hook.gather_max</tt></div>All modes honor <tt>tpl_hook.gather_max</tt>, specifying the maximum byte size for a
tpl image to be gathered (the default is unlimited, signified by 0). If a source
attempts to send a tpl image larger than this maximum, whatever partial image
has been read will be discarded, and no further reading will take place; in this
case <tt>tpl_gather</tt> will return a negative (error) value to inform the caller that
it should stop gathering from this source, and close the originating file
descriptor if there is one. (The whole idea is to prevent untrusted sources from
sending extremely large tpl images which would consume too much memory.)</td>
</tr></table>
</div>
<div class="sect3">
<h4 id="_tt_tpl_gather_blocking_tt"><tt>TPL_GATHER_BLOCKING</tt></h4>
<div class="paragraph"><p>In this mode, <tt>tpl_gather</tt> blocks while reading file descriptor <tt>fd</tt> until one
complete tpl image is read. No bytes past the end of the tpl image will be read.
The address of the buffer containing the image is returned in <tt>img</tt> and its size
is placed in <tt>sz</tt>. The caller is responsible for eventually freeing the buffer.
The function returns 1 on success, 0 on end-of-file, or a negative number on
error.</p></div>
</div>
<div class="sect3">
<h4 id="_tt_tpl_gather_nonblocking_tt"><tt>TPL_GATHER_NONBLOCKING</tt></h4>
<div class="paragraph"><p>This mode is for non-blocking, event-driven programs that implement their
own file descriptor readability testing using <tt>select()</tt> or the like. In this
mode, tpl images are gathered in chunks as data becomes readable. Whenever a
full tpl image has been gathered, it invokes a caller-specified callback to do
something with the image. The arguments are the file descriptor <tt>fd</tt> which the
caller has determined to be readable and which must be in non-blocking mode, a
pointer to a file-descriptor-specific handle which the caller has declared
(explained below); a callback to invoke when a tpl image has been read; and an
opaque pointer that will passed to the callback.</p></div>
<div class="paragraph"><p>For each file descriptor on which <tt>tpl_gather</tt> will be used, the caller must
declare a <tt>tpl_gather_t*</tt> and initialize it to <tt>NULL</tt>. Thereafter it will be
used internally by <tt>tpl_gather</tt> whenever data is readable on the descriptor.</p></div>
<div class="paragraph"><p>The callback will only be invoked whenever <tt>tpl_gather()</tt> has accumulated one
complete tpl image. It must have this prototype:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>int (tpl_gather_cb)(void *img, size_t sz, void *data);</tt></pre>
</div></div>
<div class="paragraph"><p>The callback can do anything with the tpl image but it must not free it. It can
be copied if it needs to survive past the callback’s return. The callback should
return 0 under normal circumstances, or a negative number to abort; that is,
returning a negative number causes <tt>tpl_gather</tt> itself to discard any remaining
full or partial tpl images that have been read, and to return a negative number
(-4 in particular) to signal its caller to close the file descriptor.</p></div>
<div class="paragraph"><p>The return value of <tt>tpl_gather()</tt> is negative if an error occured or 0 if a
normal EOF was encountered-- both cases require that the caller close the file
descriptor (and stop monitoring it for readability, obviously). If the return
value is positive, the function succeeded in gathering whatever data was
currently readable, which may have been a partial tpl image, or one or more
complete images.</p></div>
<div class="sect4">
<h5 id="_typical_usage">Typical Usage</h5>
<div class="paragraph"><p>The program will have established a file descriptor in non-blocking mode and
be monitoring it for readability, using <tt>select()</tt>. Whenever it’s readable, the
program calls <tt>tpl_gather()</tt>. In skeletal terms:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>tpl_gather_t *gt=NULL;
int rc;</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>void fd_is_readable(int fd) {
rc = tpl_gather( TPL_GATHER_NONBLOCKING, fd, &gt, callback, NULL );
if (rc <= 0) {
close(fd); /* got eof or fatal */
stop_watching_fd(fd);
}
}</tt></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>int callback( void *img, size_t sz, void *data ) {
printf("got a tpl image\n"); /* do something with img. do not free it. */
return 0; /* normal (no error) */
}</tt></pre>
</div></div>
</div>
</div>
<div class="sect3">
<h4 id="_tt_tpl_gather_mem_tt"><tt>TPL_GATHER_MEM</tt></h4>
<div class="paragraph"><p>This mode is identical to <tt>TPL_GATHER_NONBLOCKING</tt> except that it gathers from a
memory buffer instead of from a file descriptor. In other words, if some other
layer of code-- say, a decryption function (that is decrypting fixed-size
blocks) produces tpl fragments one-by-one, this mode can be used to reconstitute
the tpl images and invoke the callback for each one. Its parameters are the same
as for the <tt>TPL_GATHER_NONBLOCKING</tt> mode except that instead of a file
descriptor, it takes a buffer address and size. The return values are also the
same as for <tt>TPL_GATHER_NONBLOCKING</tt> noting of course there is no file
descriptor to close on a non-positive return value.</p></div>
</div>
</div>
</div>
</div>
</div>
<div id="footnotes"><hr /></div>
<div id="footer">
<div id="footer-text">
Version 1.5<br />
Last updated 2013-07-18 23:22:51 EDT
</div>
</div>
</body>
</html>
|