VirtualBox

source: vbox/trunk/src/VBox/ImageMounter/vboximg-mount/SelfSizingTable.h@ 103068

Last change on this file since 103068 was 100108, checked in by vboxsync, 18 months ago

*: Fix build issues when setting VBOX_WITH_WARNINGS_AS_ERRORS=1 on darwin.arm64 and make it a default, bugref:10469

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1/* $Id: SelfSizingTable.h 100108 2023-06-07 20:05:13Z vboxsync $ $Revision: 100108 $ */
2/** @file
3 * vboxraw header file
4 */
5
6/*
7 * Copyright (C) 2018-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/* SELFSIZINGTABLE
29 *
30 * An ANSI text-display oriented table, whose column widths conform to width of
31 * their contents. The goal is to optimize whitespace usage, so there's neither too
32 * much nor too little whitespace (e.g. min. necessary for optimal readability).
33 *
34 * Contents can only be added to and redisplayed, not manipulated after adding.
35 *
36 * Simple API (see example below):
37 *
38 * 1. Create table instance.
39 * 2. Add column definitions.
40 * 3. Add each row and set data for each column in a row.
41 * 4. Invoke the displayTable() method.
42 *
43 * Each time the table is [re]displayed its contents are [re]evaluated to determine
44 * the column sizes and header and data padding.
45 *
46 * Example:
47 *
48 * SELFSIZINGTABLE tbl(2);
49 * void *colPlanet = tbl.addCol("Planet" "%s", 1);
50 * void *colInhabit = tbl.addCol("Inhabitability", "%-12s = %s");
51 *
52 * // This is an 'unrolled loop' example. More typical would be to iterate,
53 * // providing data content from arrays, indicies, in-place calculations,
54 * // databases, etc... rather than just hardcoded literals.
55 *
56 * void *row = tbl.addRow();
57 * tbl.setCell(row, colPlanet, "Earth");
58 * tbl.setCell(row, colInhabit, "Viability", "Decreasing");
59 * row = tbl.addRow();
60 * tbl.setCell(row, colPlanet, "Mars");
61 * tbl.setCell(row, colInhabit, "Tolerability", "Miserable");
62 * row = tbl.addRow();
63 * tbl.setCell(row, colPlanet, "Neptune");
64 * tbl.setCell(row, colInhabit, "Plausibility", "Forget it");
65 *
66 * tbl.displayTable();
67 *
68 * Planet Inhabitability
69 * Earth Viability = Decreasing
70 * Mars Tolerability = Miserable
71 * Neptune Plausibility = Forget it
72 *
73 * (note:
74 * Column headers displayed in bold red to distinguish from data)
75 *
76 */
77
78#ifndef VBOX_INCLUDED_SRC_vboximg_mount_SelfSizingTable_h
79#define VBOX_INCLUDED_SRC_vboximg_mount_SelfSizingTable_h
80#ifndef RT_WITHOUT_PRAGMA_ONCE
81# pragma once
82#endif
83
84#include <iprt/types.h>
85#include <iprt/string.h>
86#include <iprt/assert.h>
87#include <iprt/message.h>
88#include <iprt/stream.h>
89
90#define ANSI_BOLD "\x1b[1m" /** ANSI terminal esc. seq [CSI] to switch font to bold */
91#define ANSI_BLACK "\x1b[30m" /** ANSI terminal esc. seq [CSI] to switch font to black */
92#define ANSI_RED "\x1b[31m" /** ANSI terminal esc. seq [CSI] to switch font to red */
93#define ANSI_RESET "\x1b[m" /** ANSI terminal esc. seq to reset terminal attributes mode */
94
95#define HDRLABEL_MAX 30 /** Maximum column header label length (for RTStrNLen()) */
96#define COLUMN_WIDTH_MAX 256 /** Maximum width of a display column */
97
98typedef class SelfSizingTable
99{
100 public:
101 SelfSizingTable(int cbDefaultPadding = 1);
102 ~SelfSizingTable();
103 void *addCol(const char *pszHdr, const char *pszFmt, int8_t align = LEFT, int8_t padRight = 0);
104 void *addRow();
105 void setCell(void *row, void *col, ...);
106 void displayTable();
107
108 private:
109 typedef struct ColDesc {
110 struct ColDesc *next;
111 char *pszHdr;
112 uint8_t hdrLen;
113 char *pszFmt;
114 int8_t alignment;
115 uint8_t cbPadRightOpt;
116 uint8_t cbWidestDataInCol;
117 } COLDESC;
118
119 typedef struct ColData
120 {
121 struct ColData *next;
122 COLDESC *pColDesc;
123 char *pszData;
124 uint8_t cbData;
125 } COLDATA;
126
127 typedef struct Row
128 {
129 struct Row *next;
130 uint32_t id;
131 COLDATA colDataListhead;
132 } ROW;
133
134 int cbDefaultColPadding;
135 COLDESC colDescListhead;
136 ROW rowListhead;
137
138 public:
139 enum Alignment /* column/cell alignment */
140 {
141 CENTER = 0, RIGHT = 1, LEFT = -1,
142 };
143
144} SELFSIZINGTABLE;
145
146SELFSIZINGTABLE::SelfSizingTable(int cbDefaultPadding)
147{
148 this->cbDefaultColPadding = cbDefaultPadding;
149 colDescListhead.next = NULL;
150 rowListhead.next = NULL;
151}
152SELFSIZINGTABLE::~SelfSizingTable()
153{
154 COLDESC *pColDesc = colDescListhead.next;
155 while (pColDesc)
156 {
157 COLDESC *pColDescNext = pColDesc->next;
158 RTMemFree(pColDesc->pszHdr);
159 RTMemFree(pColDesc->pszFmt);
160 delete pColDesc;
161 pColDesc = pColDescNext;
162 }
163 ROW *pRow = rowListhead.next;
164 while(pRow)
165 {
166 ROW *pRowNext = pRow->next;
167 COLDATA *pColData = pRow->colDataListhead.next;
168 while (pColData)
169 {
170 COLDATA *pColDataNext = pColData->next;
171 delete[] pColData->pszData;
172 delete pColData;
173 pColData = pColDataNext;
174 }
175 delete pRow;
176 pRow = pRowNext;
177 }
178}
179
180void *SELFSIZINGTABLE::addCol(const char *pszHdr, const char *pszFmt, int8_t align, int8_t padRight)
181{
182 COLDESC *pColDescNew = new COLDESC();
183 if (!pColDescNew)
184 {
185 RTMsgErrorExitFailure("out of memory");
186 return NULL;
187 }
188 pColDescNew->pszHdr = RTStrDup(pszHdr);
189 pColDescNew->hdrLen = RTStrNLen(pszHdr, HDRLABEL_MAX);
190 pColDescNew->pszFmt = RTStrDup(pszFmt);
191 pColDescNew->alignment = align;
192 pColDescNew->cbPadRightOpt = padRight;
193 COLDESC *pColDesc = &colDescListhead;
194
195 while (pColDesc->next)
196 pColDesc = pColDesc->next;
197
198 pColDesc->next = pColDescNew;
199 return (void *)pColDescNew;
200}
201
202void *SELFSIZINGTABLE::addRow()
203{
204 ROW *pNewRow = new Row();
205 COLDESC *pColDesc = colDescListhead.next;
206 COLDATA *pCurColData = &pNewRow->colDataListhead;
207 while (pColDesc)
208 {
209 COLDATA *pNewColData = new COLDATA();
210 pNewColData->pColDesc = pColDesc;
211 pCurColData = pCurColData->next = pNewColData;
212 pColDesc = pColDesc->next;
213 }
214 ROW *pRow = &rowListhead;
215 while (pRow->next)
216 pRow = pRow->next;
217 pRow->next = pNewRow;
218 return (void *)pNewRow;
219}
220
221void SELFSIZINGTABLE::setCell(void *row, void *col, ...)
222{
223 ROW *pRow = (ROW *)row;
224 COLDESC *pColDesc = (COLDESC *)col;
225 va_list ap;
226 va_start(ap, col);
227
228 char *pszData = new char[COLUMN_WIDTH_MAX];
229 int cbData = RTStrPrintfV(pszData, COLUMN_WIDTH_MAX, pColDesc->pszFmt, ap);
230 COLDATA *pColData = pRow->colDataListhead.next;
231 while (pColData)
232 {
233 if (pColData->pColDesc == pColDesc)
234 {
235 pColData->pszData = pszData;
236 pColData->cbData = cbData;
237 break;
238 }
239 pColData = pColData->next;
240 }
241}
242
243void SELFSIZINGTABLE::displayTable()
244{
245 /* Determine max cell (and column header) length for each column */
246
247 COLDESC *pColDesc = colDescListhead.next;
248 while (pColDesc)
249 {
250 pColDesc->cbWidestDataInCol = pColDesc->hdrLen;
251 pColDesc = pColDesc->next;
252 }
253 ROW *pRow = rowListhead.next;
254 while(pRow)
255 {
256 COLDATA *pColData = pRow->colDataListhead.next;
257 while (pColData)
258 {
259 pColDesc = pColData->pColDesc;
260 if (pColData->cbData > pColDesc->cbWidestDataInCol)
261 pColDesc->cbWidestDataInCol = pColData->cbData;;
262 pColData = pColData->next;
263 }
264 pRow = pRow->next;
265 }
266
267 /* Display col headers based on actual column size w/alignment & padding */
268 pColDesc = colDescListhead.next;
269 while (pColDesc)
270 {
271 uint8_t colWidth = pColDesc->cbWidestDataInCol;
272 char *pszColHdr = new char[colWidth + 1];
273 switch (pColDesc->alignment)
274 {
275 case RIGHT:
276 RTStrPrintf(pszColHdr, colWidth + 1, "%*s", colWidth, pColDesc->pszHdr);
277 break;
278 case LEFT:
279 RTStrPrintf(pszColHdr, colWidth + 1, "%-*s", colWidth, pColDesc->pszHdr);
280 break;
281 case CENTER:
282 int cbPad = (colWidth - pColDesc->hdrLen) / 2;
283 RTStrPrintf(pszColHdr, colWidth + 1, "%*s%s%*s", cbPad, "", pColDesc->pszHdr, cbPad, "");
284 }
285 RTPrintf(ANSI_BOLD ANSI_RED);
286 uint8_t cbPad = pColDesc->cbPadRightOpt ? pColDesc->cbPadRightOpt : cbDefaultColPadding;
287 RTPrintf("%s%*s", pszColHdr, cbPad, " ");
288 RTPrintf(ANSI_RESET);
289 pColDesc = pColDesc->next;
290 delete[] pszColHdr;
291 }
292 RTPrintf("\n");
293 /*
294 * Display each of the column data items for the row
295 */
296 pRow = rowListhead.next;
297 while(pRow)
298 {
299 COLDATA *pColData = pRow->colDataListhead.next;
300 while (pColData)
301 { pColDesc = pColData->pColDesc;
302 uint8_t colWidth = pColDesc->cbWidestDataInCol;
303 char *aCell = new char[colWidth + 1];
304 switch (pColDesc->alignment)
305 {
306 case RIGHT:
307 RTStrPrintf(aCell, colWidth + 1, "%*s", colWidth, pColData->pszData);
308 break;
309 case LEFT:
310 RTStrPrintf(aCell, colWidth + 1, "%-*s", colWidth, pColData->pszData);
311 break;
312 case CENTER:
313 int cbPad = (colWidth - pColData->cbData) / 2;
314 RTStrPrintf(aCell, colWidth + 1, "%*s%s%*s", cbPad, "", pColData->pszData, cbPad, "");
315 }
316 uint8_t cbPad = pColDesc->cbPadRightOpt ? pColDesc->cbPadRightOpt : this->cbDefaultColPadding;
317 RTPrintf("%s%*s", aCell, cbPad, " ");
318 pColData = pColData->next;
319 delete[] aCell;
320 }
321 RTPrintf("\n");
322 pRow = pRow->next;
323 }
324}
325#endif /* !VBOX_INCLUDED_SRC_vboximg_mount_SelfSizingTable_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette