2 * TODO: - make right and centered alignment possible
5 parted - a frontend to libparted
6 Copyright (C) 2006-2010 Free Software Foundation, Inc.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
36 # define L_(str) L##str
43 # define wcslen strlen
44 # define wcswidth strnlen
45 # define wcscat strcat
46 # define wcsdup xstrdup
50 static const unsigned int MAX_WIDTH = 512;
51 static const wchar_t* DELIMITER = L_(" ");
52 static const wchar_t* COLSUFFIX = L_("\n");
63 Table* table_new(int ncols)
65 assert ( ncols >= 0 );
67 Table *t = xmalloc (sizeof(*t));
71 t->rows = (wchar_t***)NULL;
78 void table_destroy (Table* t)
83 assert (t->ncols > 0);
85 for (r = 0; r < t->nrows; ++r)
87 for (c = 0; c < t->ncols; ++c)
98 static int max (int x, int y)
100 return x > y ? x : y;
104 static void table_calc_column_widths (Table* t)
109 assert(t->ncols > 0);
112 t->widths = xmalloc (t->ncols * sizeof(t->widths[0]));
114 for (c = 0; c < t->ncols; ++c)
117 for (r = 0; r < t->nrows; ++r)
118 for (c = 0; c < t->ncols; ++c)
120 t->widths[c] = max ( t->widths[c],
121 wcswidth(t->rows[r][c],
128 * add a row which is a string array of ncols elements.
129 * 'row' will get freed by table_destroy; you must not free it
132 void table_add_row (Table* t, wchar_t** row)
137 fputs ("adding row: ", stdout);
138 for (i = 0; i < t->ncols; ++i)
139 printf("[%s]", row[i]);
142 t->rows = xrealloc (t->rows, (t->nrows + 1) * sizeof(wchar_t***));
144 t->rows[t->nrows] = row;
148 table_calc_column_widths (t);
152 void table_add_row_from_strlist (Table* t, StrList* list)
154 wchar_t** row = xmalloc (str_list_length(list) * sizeof(*row));
159 row[i] = wcsdup (list->str);
168 table_add_row (t, row);
173 static void table_render_row (Table* t, int rownum, int ncols, wchar_t** s)
175 wchar_t** row = t->rows[rownum];
182 for (i = 0; i < ncols; ++i)
183 len += t->widths[i] + wcslen(DELIMITER);
185 len += wcslen(COLSUFFIX);
187 newsize = (wcslen(*s) + len + 1) * sizeof(wchar_t);
188 *s = xrealloc (*s, newsize);
190 for (i = 0; i < ncols; ++i)
197 int nspaces = max(t->widths[i] - wcswidth(row[i], MAX_WIDTH),
199 wchar_t* pad = xmalloc ((nspaces + 1) * sizeof(*pad));
201 for (j = 0; j < nspaces; ++j)
204 pad[nspaces] = L_('\0');
208 wcscat (*s, DELIMITER);
213 /* Remove any trailing blanks. */
215 size_t k = wcslen (p);
216 while (k && p[k-1] == L_(' '))
221 wcscat (*s, COLSUFFIX);
227 * \p s must be a null-terminated string.
229 static void table_render_rows (Table* t, wchar_t** s)
233 assert (**s == L_('\0'));
234 for (i = 0; i < t->nrows; ++i)
235 table_render_row (t, i, t->ncols, s);
239 * Render the table to a string.
240 * You are responsible for freeing the returned string.
242 wchar_t* table_render(Table* t)
244 wchar_t* s = xmalloc (sizeof(*s));
247 table_render_rows (t, &s);