btrfs-progs: string table: cleanup, rename single letter variables
[platform/upstream/btrfs-progs.git] / string-table.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <stdarg.h>
21
22 #include "string-table.h"
23
24 /*
25  *  This function create an array of char * which will represent a table
26  */
27 struct string_table *table_create(int columns, int rows)
28 {
29         struct string_table *tab;
30         int size;
31
32         size = sizeof(struct string_table) + rows * columns * sizeof(char*);
33         tab = calloc(1, size);
34
35         if (!tab)
36                 return NULL;
37
38         tab->ncols = columns;
39         tab->nrows = rows;
40
41         return tab;
42 }
43
44 /*
45  * This function is like a vprintf, but store the results in a cell of
46  * the table.
47  * If fmt  starts with '<', the text is left aligned; if fmt starts with
48  * '>' the text is right aligned. If fmt is equal to '=' the text will
49  * be replaced by a '=====' dimensioned on the basis of the column width
50  */
51 char *table_vprintf(struct string_table *tab, int column, int row,
52                           char *fmt, va_list ap)
53 {
54         int idx = tab->ncols * row + column;
55         char *msg = calloc(100, 1);
56
57         if (!msg)
58                 return NULL;
59
60         if (tab->cells[idx])
61                 free(tab->cells[idx]);
62         tab->cells[idx] = msg;
63         vsnprintf(msg, 99, fmt, ap);
64
65         return msg;
66 }
67
68 /*
69  * This function is like a printf, but store the results in a cell of
70  * the table.
71  */
72 char *table_printf(struct string_table *tab, int column, int row,
73                           char *fmt, ...)
74 {
75         va_list ap;
76         char *ret;
77
78         va_start(ap, fmt);
79         ret = table_vprintf(tab, column, row, fmt, ap);
80         va_end(ap);
81
82         return ret;
83 }
84
85 /*
86  * This function dumps the table. Every "=" string will be replaced by
87  * a "=======" length as the column
88  */
89 void table_dump(struct string_table *tab)
90 {
91         int sizes[tab->ncols];
92         int i, j;
93
94         for (i = 0; i < tab->ncols; i++) {
95                 sizes[i] = 0;
96                 for (j = 0; j < tab->nrows; j++) {
97                         int idx = i + j * tab->ncols;
98                         int len;
99
100                         if (!tab->cells[idx])
101                                 continue;
102
103                         len = strlen(tab->cells[idx]) - 1;
104                         if (len < 1 || tab->cells[idx][0] == '=')
105                                 continue;
106
107                         if (len > sizes[i])
108                                 sizes[i] = len;
109                 }
110         }
111
112         for (j = 0; j < tab->nrows; j++) {
113                 for (i = 0; i < tab->ncols; i++) {
114                         int idx = i + j * tab->ncols;
115                         char *cell = tab->cells[idx];
116
117                         if (!cell || !strlen(cell)) {
118                                 printf("%*s", sizes[i], "");
119                         } else if (cell && cell[0] == '=') {
120                                 int k = sizes[i];
121
122                                 while (k--)
123                                         putchar('=');
124                         } else {
125                                 printf("%*s",
126                                         cell[0] == '<' ? -sizes[i] : sizes[i],
127                                         cell + 1);
128                         }
129                         if (i != (tab->ncols - 1))
130                                 putchar(' ');
131                 }
132                 putchar('\n');
133         }
134 }
135
136 /*
137  *  Deallocate a table and all of its content
138  */
139 void table_free(struct string_table *tab)
140 {
141         int i, count;
142
143         count = tab->ncols * tab->nrows;
144
145         for (i = 0; i < count; i++)
146                 if (tab->cells[i])
147                         free(tab->cells[i]);
148
149         free(tab);
150 }