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