BRL-CAD
tbl.h
Go to the documentation of this file.
1 /* T B L . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2020-2024 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #ifndef BU_TBL_H
22 #define BU_TBL_H
23 
24 #include "common.h"
25 
26 #include "bu/defines.h"
27 #include "bu/vls.h"
28 
29 
30 __BEGIN_DECLS
31 
32 /**
33  * @addtogroup bu_tbl
34  *
35  * @brief Routines for generally (i.e., non-mathematically) handling
36  * numbers, strings, and other data for tabular printing.
37  */
38 /** @{ */
39 /** @file bu/tbl.h */
40 
41 /**
42  * Example 1:
43  @code
44 my matrix
45 [
46  [ -1.1231231231229999, 0, 0, inf ]
47  [ 0, 123, 0, 0 ]
48  [ 0, -2345, 123123.12312312312, 0 ]
49  [ 123123123.12312312, 21, inf, 1 ]
50 ]
51  @endcode
52  *
53  * generated from this code:
54  *
55  @code
56  double vals[16] = {-1.123123123123, 0, 0, 1.0/0.0, 0, 123, 0, 0, 0, -2345, 123123.123123123123, 0, 123123123.123123123, 21, 1.0/0.0, 1};
57 
58  struct bu_tbl *t = bu_tbl_create();
59 
60  bu_tbl_style(t, BU_TBL_STYLE_NONE);
61  bu_tbl_printf(t, "my matrix");
62  bu_tbl_style(t, BU_TBL_ROW_END);
63  bu_tbl_style(t, BU_TBL_ROW_ALIGN_LEFT);
64  bu_tbl_printf(t, "[");
65  bu_tbl_style(t, BU_TBL_ROW_END);
66  bu_tbl_style(t, BU_TBL_ROW_ALIGN_RIGHT);
67  bu_tbl_printf(t, "[|%.17g,|%.17g,|%.17g,|%.17g|]", vals[0], vals[1], vals[2], vals[3]);
68  bu_tbl_style(t, BU_TBL_ROW_END);
69  bu_tbl_style(t, BU_TBL_ROW_ALIGN_RIGHT);
70  bu_tbl_printf(t, "[|%.17g,|%.17g,|%.17g,|%.17g|]", vals[4], vals[5], vals[6], vals[7]);
71  bu_tbl_style(t, BU_TBL_ROW_END);
72  bu_tbl_style(t, BU_TBL_ROW_ALIGN_RIGHT);
73  bu_tbl_printf(t, "[|%.17g,|%.17g,|%.17g,|%.17g|]", vals[8], vals[9], vals[10], vals[11]);
74  bu_tbl_style(t, BU_TBL_ROW_END);
75  bu_tbl_style(t, BU_TBL_ROW_ALIGN_RIGHT);
76  bu_tbl_printf(t, "[|%.17g,|%.17g,|%.17g,|%.17g|]", vals[12], vals[13], vals[14], vals[15]);
77  bu_tbl_style(t, BU_TBL_ROW_END);
78  bu_tbl_style(t, BU_TBL_ROW_ALIGN_LEFT);
79  bu_tbl_printf(t, "]");
80 
81  bu_tbl_destroy(t);
82  @endcode
83  *
84  * Example 2:
85  @code
86 -0000000000001.12 00000000000000.00 00000000000000.00 inf
87 00000000000000.00 00000000000123.00 00000000000000.00 00000000000000.00
88 00000000000000.00 -0000000002345.00 00000000123123.12 00000000000000.00
89 00000123123123.12 00000000000021.00 inf 00000000000001.00
90  @endcode
91  *
92  *
93  *
94  @code
95  double vals[16] = {-1.123123123123, 0, 0, 1.0/0.0, 0, 123, 0, 0, 0, -2345, 123123.123123123123, 0, 123123123.123123123, 21, 1.0/0.0, 1};
96 
97  struct bu_tbl *t = bu_tbl_create();
98 
99  bu_tbl_style(t, BU_TBL_STYLE_LIST);
100  bu_tbl_printf(t, "%017.2lf|%017.2lf|%017.2lf|%017.2lf", vals[0], vals[1], vals[2], vals[3]);
101  bu_tbl_style(t, BU_TBL_ROW_END);
102  bu_tbl_printf(t, "%017.2lf|%017.2lf|%017.2lf|%017.2lf", vals[4], vals[5], vals[6], vals[7]);
103  bu_tbl_style(t, BU_TBL_ROW_END);
104  bu_tbl_printf(t, "%017.2lf|%017.2lf|%017.2lf|%017.2lf", vals[8], vals[9], vals[10], vals[11]);
105  bu_tbl_style(t, BU_TBL_ROW_END);
106  bu_tbl_printf(t, "%017.2lf|%017.2lf|%017.2lf|%017.2lf", vals[12], vals[13], vals[14], vals[15]);
107 
108  bu_tbl_destroy(t);
109  @endcode
110  *
111  */
112 
113 
114 struct bu_tbl;
115 
116 
117 /**
118  * static-initializer for bu_tbl objects on the stack
119  */
120 #define BU_TBL_INIT_ZERO {0}
121 
122 /**
123  * always returns a pointer to a newly allocated table
124  */
125 BU_EXPORT extern struct bu_tbl *
127 
128 
129 /**
130  * releases all dynamic memory associated with the specified table
131  */
132 BU_EXPORT extern void
133 bu_tbl_destroy(struct bu_tbl *);
134 
135 
136 /**
137  * erases all cells in a table, but doesn't erase the table itself
138  */
139 BU_EXPORT extern int
140 bu_tbl_clear(struct bu_tbl *);
141 
142 
143 /**
144  * Table styles:
145  *
146  * NONE 1 ------ --- +-------+---+ ╭────────┬───╮ ╔════════╦═══╗
147  * 2 3 LIST 1 | BASIC | 1 | │ SINGLE │ 1 │ ║ DOUBLE ║ 1 ║
148  * 4 5 ------ --- +-------+---+ ├────────┼───┤ ╠════════╬═══╣
149  * 2 3 | 2 | 3 | │ 2 │ 3 │ ║ 2 ║ 3 ║
150  * 4 5 | 4 | 5 | │ 4 │ 5 │ ║ 4 ║ 5 ║
151  * +-------+---+ ╰────────┴───╯ ╚════════╩═══╝
152  */
154  /* table border style */
160 
161  /* table alignment */
165 
166  /* cell styling */
168 
169  /* insert a horizontal separator */
171 
172  /* cell alignment */
179 
180  /* go to next row beginning */
182 };
183 
184 
185 /**
186  * sets table styling or formatting on cells printed next.
187  */
188 BU_EXPORT extern struct bu_tbl *
189 bu_tbl_style(struct bu_tbl *, enum bu_tbl_style);
190 
191 
192 /**
193  * get cell position for the current table insertion point.
194  */
195 BU_EXPORT extern struct bu_tbl *
196 bu_tbl_is_at(struct bu_tbl *, size_t *row, size_t *col);
197 
198 
199 /**
200  * set cell position of the current table insertion point.
201  */
202 BU_EXPORT extern struct bu_tbl *
203 bu_tbl_go_to(struct bu_tbl *, size_t row, size_t col);
204 
205 
206 /**
207  * print values into the table at the current insertion point.
208  *
209  * each column of the 'fmt' printf-style format specfier must be
210  * delimited by a '|' character.
211  *
212  * any existing values will be overwritten.
213  */
214 BU_EXPORT extern struct bu_tbl *
215 bu_tbl_printf(struct bu_tbl *, const char *fmt, ...);
216 
217 
218 /**
219  * print a table into a vls
220  */
221 BU_EXPORT extern void
222 bu_tbl_vls(struct bu_vls *str, const struct bu_tbl *t);
223 
224 
225 __END_DECLS
226 
227 /** @} */
228 
229 #endif /* BU_NUM_H */
230 
231 /*
232  * Local Variables:
233  * mode: C
234  * tab-width: 8
235  * indent-tabs-mode: t
236  * c-file-style: "stroustrup"
237  * End:
238  * ex: shiftwidth=4 tabstop=8
239  */
Header file for the BRL-CAD common definitions.
struct bu_tbl * bu_tbl_create(void)
void bu_tbl_destroy(struct bu_tbl *)
bu_tbl_style
Definition: tbl.h:153
void bu_tbl_vls(struct bu_vls *str, const struct bu_tbl *t)
struct bu_tbl * bu_tbl_go_to(struct bu_tbl *, size_t row, size_t col)
struct bu_tbl * bu_tbl_is_at(struct bu_tbl *, size_t *row, size_t *col)
struct bu_tbl * bu_tbl_printf(struct bu_tbl *, const char *fmt,...)
int bu_tbl_clear(struct bu_tbl *)
@ BU_TBL_ALIGN_LEFT
Definition: tbl.h:162
@ BU_TBL_STYLE_SINGLE
Definition: tbl.h:158
@ BU_TBL_ROW_SEPARATOR
Definition: tbl.h:170
@ BU_TBL_ROW_END
Definition: tbl.h:181
@ BU_TBL_ALIGN_RIGHT
Definition: tbl.h:164
@ BU_TBL_ALIGN_CENTER
Definition: tbl.h:163
@ BU_TBL_ROW_HEADER
Definition: tbl.h:167
@ BU_TBL_COL_ALIGN_LEFT
Definition: tbl.h:176
@ BU_TBL_COL_ALIGN_RIGHT
Definition: tbl.h:178
@ BU_TBL_STYLE_LIST
Definition: tbl.h:156
@ BU_TBL_ROW_ALIGN_RIGHT
Definition: tbl.h:175
@ BU_TBL_STYLE_DOUBLE
Definition: tbl.h:159
@ BU_TBL_ROW_ALIGN_LEFT
Definition: tbl.h:173
@ BU_TBL_STYLE_BASIC
Definition: tbl.h:157
@ BU_TBL_STYLE_NONE
Definition: tbl.h:155
@ BU_TBL_COL_ALIGN_CENTER
Definition: tbl.h:177
@ BU_TBL_ROW_ALIGN_CENTER
Definition: tbl.h:174
Definition: vls.h:53