2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file format.c Functions for pretty-printing numbers and strings. */
9 #include <dirent.h> /* readdir() */
11 #include "gcc-compat.h"
16 enum alignment {ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER};
24 struct string_format {
33 struct string_format sf;
35 struct const_string cs;
39 struct atom *atom_ptr;
46 struct format_item **items;
55 static const char units[] = "bkmgth KMGTH";
57 * In a format string, find the next occurrence of %(atom).
59 static char *find_next(char *cp)
63 /* %( is the start of an atom;
64 * %% is a quoted per-cent.
68 else if (cp[1] == '%')
69 cp++; /* skip over two % */
70 /* otherwise this is a singleton, literal % */
77 static struct format_item *make_const_string(char *cp, char *ep)
79 struct format_item *fi = malloc(sizeof *fi);
84 op = fi->af.cs.string = malloc(fi->width + 1);
85 while (*cp && (!ep || cp < ep)) {
86 if (*cp == '%' && cp[1] == '%') {
98 static int parse_atom(char *ap, struct atom *atoms, struct format_item **result)
100 struct format_item *fi = NULL;
102 char *col, *ep = strchr(ap, ')');
103 int ret = -E_MALFORMED_FORMAT;
107 col = memchr(ap, ':', ep - ap);
112 for (i = 0; atoms[i].name; i++) {
114 struct atom *a = atoms + i;
115 if (strlen(a->name) != len)
117 if (strncmp(a->name, ap, len))
119 fi = malloc(sizeof(*fi));
125 fi->af.sf.align = ALIGN_LEFT;
128 fi->af.nf.align = ALIGN_RIGHT;
129 fi->af.nf.unit = ' ';
130 fi->af.nf.supress_unit = 0;
133 fi->af.nf.align = ALIGN_RIGHT;
134 fi->af.nf.unit = 'H';
135 fi->af.nf.supress_unit = 0;
138 fi->af.nf.align = ALIGN_RIGHT;
139 fi->af.nf.unit = 'h';
140 fi->af.nf.supress_unit = 0;
148 fi->af.sf.align = ALIGN_LEFT;
151 fi->af.nf.align = ALIGN_RIGHT;
154 fi->af.nf.align = ALIGN_CENTER;
156 case ':': /* no alignment spec is OK */
160 ret = -E_BAD_ALIGN_SPEC;
170 ret = -E_TRAILING_GARBAGE;
175 sscanf(col + 1, "%u%n", &fi->width, &n);
177 switch (col[1 + n]) {
184 ret = -E_TRAILING_GARBAGE;
187 if (a->type != AT_SIZE && a->type != AT_COUNT) {
192 fi->af.nf.supress_unit = 1;
195 for (j = 0; units[j]; j++) {
196 if (units[j] != col[1])
198 fi->af.nf.unit = units[j];
206 ret = -E_TRAILING_GARBAGE;
220 int parse_format_string(char *fmt, struct atom *atoms, struct format_info **result)
224 struct format_info *info = malloc(sizeof(*info));
228 for (cp = fmt, num_items = 0; *cp && (ap = find_next(cp));
229 cp = ep + 1, num_items++) {
231 info->items = realloc(info->items,
232 (num_items + 1) * sizeof(struct format_info *));
233 info->items[num_items] = make_const_string(cp, ap);
236 info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
237 ret = parse_atom(ap + 2, atoms, info->items + num_items);
242 ep = strchr(ap, ')');
245 ep = cp + strlen(cp);
246 info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
247 info->items[num_items] = make_const_string(cp, ep);
250 info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
251 info->items[num_items] = NULL;
255 for (; num_items >= 0; num_items--) {
256 if (!info->items[num_items]->atom_ptr)
257 free(info->items[num_items]->af.cs.string);
258 free(info->items[num_items]);
267 * It's OK to pass a \p NULL pointer to this function.
269 void free_format_info(struct format_info *info)
272 struct format_item *item;
277 for (i = 0; (item = info->items[i]); i++) {
279 free(item->af.cs.string);
280 free(info->items[i]);
286 static char *align_string(const char *src, unsigned int width, enum alignment align)
291 return adu_strdup(src);
292 if (align == ALIGN_LEFT)
293 return make_message("%-*s", width, src);
294 if (align == ALIGN_RIGHT)
295 return make_message("%*s", width, src);
297 return make_message("%*s%*s", (width + len) / 2, src,
298 width - (width + len) / 2, "");
301 /** Compute the number of (decimal) digits of a number. */
302 #define GET_NUM_DIGITS(x, num) { \
303 typeof((x)) _tmp = x; \
306 while ((_tmp) > 9) { \
312 static long long unsigned normalize_number(long long unsigned num, char unit,
313 char *effective_unit)
315 long long unsigned normalized_num;
319 *effective_unit = ' ';
320 else if (num < 1024ULL * 1024ULL)
321 *effective_unit = 'k';
322 else if (num < 1024ULL * 1024ULL * 1024ULL)
323 *effective_unit = 'm';
324 else if (num < 1024ULL * 1024ULL * 1024ULL * 1024ULL)
325 *effective_unit = 'g';
327 *effective_unit = 't';
328 } else if (unit == 'H') {
330 *effective_unit = ' ';
331 else if (num < 1000ULL * 1000ULL)
332 *effective_unit = 'K';
333 else if (num < 1000ULL * 1000ULL * 1000ULL)
334 *effective_unit = 'M';
335 else if (num < 1000ULL * 1000ULL * 1000ULL * 1000ULL)
336 *effective_unit = 'G';
338 *effective_unit = 'T';
340 *effective_unit = unit;
341 switch (*effective_unit) {
343 case 'b': normalized_num = num; break;
344 case 'k': normalized_num = num / 1024; break;
345 case 'm': normalized_num = num / 1024 / 1024; break;
346 case 'g': normalized_num = num / 1024 / 1024 / 1024; break;
347 case 't': normalized_num = num / 1024 / 1024 / 1024 / 1024; break;
348 case 'K': normalized_num = num / 1000; break;
349 case 'M': normalized_num = num / 1000 / 1000; break;
350 case 'G': normalized_num = num / 1000 / 1000 / 1000; break;
351 case 'T': normalized_num = num / 1000 / 1000 / 1000 / 1000; break;
353 EMERG_LOG("BUG: invalid unit %c\n", *effective_unit);
356 return normalized_num;
359 static void get_unit_postfix(struct num_format *nf, char eu, enum atom_type type,
362 if (nf->supress_unit) {
370 if (nf->unit != 'h' && nf->unit != 'H')
374 postfix[0] = 'b'; /* bytes */
377 static char *align_unsigned_int(long long unsigned num, unsigned int width,
378 struct num_format *nf, enum atom_type type)
380 char eu; /* effective unit */
381 long long unsigned nnum = normalize_number(num, nf->unit, &eu);
382 char postfix[2] = "\0\0";
385 get_unit_postfix(nf, eu, type, postfix);
387 return make_message("%llu%s", nnum, postfix);
388 GET_NUM_DIGITS(nnum, &len);
389 len += strlen(postfix);
392 if (nf->align == ALIGN_LEFT)
393 return make_message("%llu%s%*s", nnum, postfix, width - len, "");
394 if (nf->align == ALIGN_RIGHT)
395 return make_message("%*s%llu%s", width - len, "", nnum, postfix);
396 return make_message("%*llu%s%*s", (width + len) / 2,
397 nnum, postfix, width - (width + len) / 2, "");
400 char *format_items(struct format_info *info, union atom_value *values)
407 for (i = 0; info->items[i]; i++) {
409 struct format_item *fi = info->items[i];
410 union atom_format *af = &fi->af;
411 enum alignment align;
416 if (!fi->atom_ptr) { /* const string */
417 buf = adu_strcat(buf, af->cs.string);
422 idx = a - info->atoms;
424 if (type == AT_STRING) {
425 align = af->sf.align;
426 val = align_string(values[idx].string_value, fi->width, align);
429 align = af->nf.align;
431 val = align_unsigned_int(values[idx].num_value,
432 fi->width, &af->nf, type);
434 buf = adu_strcat(buf, val);