1 .\" $NetBSD: rtbl.3,v 1.3 2014/04/24 13:45:34 pettai Exp $
3 .\" Copyright (c) 2004 Kungliga Tekniska Högskolan
4 .\" (Royal Institute of Technology, Stockholm, Sweden).
5 .\" All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
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.
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.
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
44 .Nm rtbl_set_separator ,
45 .Nm rtbl_set_column_prefix ,
46 .Nm rtbl_set_column_affix_by_id ,
48 .Nm rtbl_add_column_by_id ,
49 .Nm rtbl_add_column_entry ,
50 .Nm rtbl_add_column_entry_by_id ,
53 .Nd format data in simple tables
55 The roken library (libroken, -lroken)
59 .Fn rtbl_add_column "rtbl_t table" "const char *column_name" "unsigned int flags"
61 .Fn rtbl_add_column_by_id "rtbl_t table" "unsigned int column_id" "const char *column_header" "unsigned int flags"
63 .Fn rtbl_add_column_entry "rtbl_t table" "const char *column_name" "const char *cell_entry"
65 .Fn rtbl_add_column_entry_by_id "rtbl_t table" "unsigned int column_id" "const char *cell_entry"
67 .Fn rtbl_create "void"
69 .Fn rtbl_destroy "rtbl_t table"
71 .Fn rtbl_new_row "rtbl_t table"
73 .Fn rtbl_set_column_affix_by_id "rtbl_t table" "unsigned int column_id "const char *prefix" "const char *suffix"
75 .Fn rtbl_set_column_prefix "rtbl_t table" "const char *column_name" "const char *prefix"
77 .Fn rtbl_get_flags "rtbl_t table"
79 .Fn rtbl_set_flags "rtbl_t table" "unsigned int flags"
81 .Fn rtbl_set_prefix "rtbl_t table" "const char *prefix"
83 .Fn rtbl_set_separator "rtbl_t table" "const char *separator"
85 .Fn rtbl_format "rtbl_t table "FILE *file"
87 This set of functions assemble a simple table consisting of rows and
88 columns, allowing it to be printed with certain options. Typical use
89 would be output from tools such as
93 where you have a fixed number of columns, but don't know the column
96 A table is created with
101 Global flags on the table are set with
105 At present the only defined flag is
106 .Dv RTBL_HEADER_STYLE_NONE
107 which suppresses printing the header.
109 Before adding data to the table, one or more columns need to be
110 created. This would normally be done with
111 .Fn rtbl_add_column_by_id ,
113 is any number of your choice (it's used only to identify columns),
115 is the header to print at the top of the column, and
117 are flags specific to this column. Currently the only defined flag is
118 .Dv RTBL_ALIGN_RIGHT ,
119 aligning column entries to the right. Columns are printed in the order
122 There's also a way to add columns by column name with
123 .Fn rtbl_add_column ,
124 but this is less flexible (you need unique header names), and is
125 considered deprecated.
127 To add data to a column you use
128 .Fn rtbl_add_column_entry_by_id ,
131 is the same as when the column was added (adding data to a
132 non-existent column is undefined), and
134 is whatever string you wish to include in that cell. It should not
136 For columns added with
139 .Fn rtbl_add_column_entry
143 fills all columns with blank entries until they all have the same
146 Each column can have a separate prefix and suffix, set with
147 .Fa rtbl_set_column_affix_by_id ;
148 .Fa rtbl_set_column_prefix
149 allows setting the prefix only by column name. In addition to this,
150 columns may be separated by a string set with
151 .Fa rtbl_set_separator ( Ns
152 by default columns are not seprated by anything).
154 The finished table is printed to
160 .Bd -literal -offset xxxx
164 main(int argc, char **argv)
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");
183 rtbl_add_column_entry_by_id(table, 1, "B-4");
185 rtbl_add_column_entry_by_id(table, 2, "C-4");
187 rtbl_format(table, stdout);
193 will output the following:
194 .Bd -literal -offset xxxx
195 Column A Column B Column C