1.\" Copyright (c) 2004 Kungliga Tekniska H�gskolan 2.\" (Royal Institute of Technology, Stockholm, Sweden). 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 16.\" 3. Neither the name of the Institute nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" $Id: rtbl.3 22088 2007-11-25 14:10:15Z lha $ 32.\" 33.Dd June 26, 2004 34.Dt RTBL 3 35.Os HEIMDAL 36.Sh NAME 37.Nm rtbl_create , 38.Nm rtbl_destroy , 39.Nm rtbl_set_flags , 40.Nm rtbl_get_flags , 41.Nm rtbl_set_prefix , 42.Nm rtbl_set_separator , 43.Nm rtbl_set_column_prefix , 44.Nm rtbl_set_column_affix_by_id , 45.Nm rtbl_add_column , 46.Nm rtbl_add_column_by_id , 47.Nm rtbl_add_column_entry , 48.Nm rtbl_add_column_entry_by_id , 49.Nm rtbl_new_row , 50.Nm rtbl_format 51.Nd format data in simple tables 52.Sh LIBRARY 53The roken library (libroken, -lroken) 54.Sh SYNOPSIS 55.In rtbl.h 56.Ft int 57.Fn rtbl_add_column "rtbl_t table" "const char *column_name" "unsigned int flags" 58.Ft int 59.Fn rtbl_add_column_by_id "rtbl_t table" "unsigned int column_id" "const char *column_header" "unsigned int flags" 60.Ft int 61.Fn rtbl_add_column_entry "rtbl_t table" "const char *column_name" "const char *cell_entry" 62.Ft int 63.Fn rtbl_add_column_entry_by_id "rtbl_t table" "unsigned int column_id" "const char *cell_entry" 64.Ft rtbl_t 65.Fn rtbl_create "void" 66.Ft void 67.Fn rtbl_destroy "rtbl_t table" 68.Ft int 69.Fn rtbl_new_row "rtbl_t table" 70.Ft int 71.Fn rtbl_set_column_affix_by_id "rtbl_t table" "unsigned int column_id "const char *prefix" "const char *suffix" 72.Ft int 73.Fn rtbl_set_column_prefix "rtbl_t table" "const char *column_name" "const char *prefix" 74.Ft "unsigned int" 75.Fn rtbl_get_flags "rtbl_t table" 76.Ft void 77.Fn rtbl_set_flags "rtbl_t table" "unsigned int flags" 78.Ft int 79.Fn rtbl_set_prefix "rtbl_t table" "const char *prefix" 80.Ft int 81.Fn rtbl_set_separator "rtbl_t table" "const char *separator" 82.Ft int 83.Fn rtbl_format "rtbl_t table "FILE *file" 84.Sh DESCRIPTION 85This set of functions assemble a simple table consisting of rows and 86columns, allowing it to be printed with certain options. Typical use 87would be output from tools such as 88.Xr ls 1 89or 90.Xr netstat 1 , 91where you have a fixed number of columns, but don't know the column 92widthds before hand. 93.Pp 94A table is created with 95.Fn rtbl_create 96and destroyed with 97.Fn rtbl_destroy . 98.Pp 99Global flags on the table are set with 100.Fa rtbl_set_flags 101and retrieved with 102.Fa rtbl_get_flags . 103At present the only defined flag is 104.Dv RTBL_HEADER_STYLE_NONE 105which suppresses printing the header. 106.Pp 107Before adding data to the table, one or more columns need to be 108created. This would normally be done with 109.Fn rtbl_add_column_by_id , 110.Fa column_id 111is any number of your choice (it's used only to identify columns), 112.Fa column_header 113is the header to print at the top of the column, and 114.Fa flags 115are flags specific to this column. Currently the only defined flag is 116.Dv RTBL_ALIGN_RIGHT , 117aligning column entries to the right. Columns are printed in the order 118they are added. 119.Pp 120There's also a way to add columns by column name with 121.Fn rtbl_add_column , 122but this is less flexible (you need unique header names), and is 123considered deprecated. 124.Pp 125To add data to a column you use 126.Fn rtbl_add_column_entry_by_id , 127where the 128.Fa column_id 129is the same as when the column was added (adding data to a 130non-existent column is undefined), and 131.Fa cell_entry 132is whatever string you wish to include in that cell. It should not 133include newlines. 134For columns added with 135.Fn rtbl_add_column 136you must use 137.Fn rtbl_add_column_entry 138instead. 139.Pp 140.Fn rtbl_new_row 141fills all columns with blank entries until they all have the same 142number of rows. 143.Pp 144Each column can have a separate prefix and suffix, set with 145.Fa rtbl_set_column_affix_by_id ; 146.Fa rtbl_set_column_prefix 147allows setting the prefix only by column name. In addition to this, 148columns may be separated by a string set with 149.Fa rtbl_set_separator ( Ns 150by default columns are not seprated by anything). 151.Pp 152The finished table is printed to 153.Fa file 154with 155.Fa rtbl_format . 156.Sh EXAMPLES 157This program: 158.Bd -literal -offset xxxx 159#include <stdio.h> 160#include <rtbl.h> 161int 162main(int argc, char **argv) 163{ 164 rtbl_t table; 165 table = rtbl_create(); 166 rtbl_set_separator(table, " "); 167 rtbl_add_column_by_id(table, 0, "Column A", 0); 168 rtbl_add_column_by_id(table, 1, "Column B", RTBL_ALIGN_RIGHT); 169 rtbl_add_column_by_id(table, 2, "Column C", 0); 170 rtbl_add_column_entry_by_id(table, 0, "A-1"); 171 rtbl_add_column_entry_by_id(table, 0, "A-2"); 172 rtbl_add_column_entry_by_id(table, 0, "A-3"); 173 rtbl_add_column_entry_by_id(table, 1, "B-1"); 174 rtbl_add_column_entry_by_id(table, 2, "C-1"); 175 rtbl_add_column_entry_by_id(table, 2, "C-2"); 176 rtbl_add_column_entry_by_id(table, 1, "B-2"); 177 rtbl_add_column_entry_by_id(table, 1, "B-3"); 178 rtbl_add_column_entry_by_id(table, 2, "C-3"); 179 rtbl_add_column_entry_by_id(table, 0, "A-4"); 180 rtbl_new_row(table); 181 rtbl_add_column_entry_by_id(table, 1, "B-4"); 182 rtbl_new_row(table); 183 rtbl_add_column_entry_by_id(table, 2, "C-4"); 184 rtbl_new_row(table); 185 rtbl_format(table, stdout); 186 rtbl_destroy(table); 187 return 0; 188} 189.Ed 190.Pp 191will output the following: 192.Bd -literal -offset xxxx 193Column A Column B Column C 194A-1 B-1 C-1 195A-2 B-2 C-2 196A-3 B-3 C-3 197A-4 198 B-4 199 C-4 200.Ed 201.\" .Sh SEE ALSO 202