2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file format.c \brief Functions for pretty-printing numbers and strings. */
9 #include <dirent.h> /* readdir() */
11 #include "gcc-compat.h"
17 enum alignment {ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER};
25 struct string_format {
34 struct string_format sf;
36 struct const_string cs;
40 struct atom *atom_ptr;
47 struct format_item **items;
56 static const char units[] = "bkmgth KMGTH";
58 * In a format string, find the next occurrence of %(atom).
60 static char *find_next(char *cp)
64 /* %( is the start of an atom;
65 * %% is a quoted per-cent.
69 else if (cp[1] == '%')
70 cp++; /* skip over two % */
71 /* otherwise this is a singleton, literal % */
78 static struct format_item *make_const_string(char *cp, char *ep)
80 struct format_item *fi = malloc(sizeof *fi);
85 op = fi->af.cs.string = malloc(fi->width + 1);
86 while (*cp && (!ep || cp < ep)) {
87 if (*cp == '%' && cp[1] == '%') {
99 static int parse_atom(char *ap, struct atom *atoms, struct format_item **result)
101 struct format_item *fi = NULL;
103 char *col, *ep = strchr(ap, ')');
104 int ret = -E_MALFORMED_FORMAT;
108 col = memchr(ap, ':', ep - ap);
113 for (i = 0; atoms[i].name; i++) {
115 struct atom *a = atoms + i;
116 if (strlen(a->name) != len)
118 if (strncmp(a->name, ap, len))
120 fi = malloc(sizeof(*fi));
126 fi->af.sf.align = ALIGN_LEFT;
129 fi->af.nf.align = ALIGN_RIGHT;
130 fi->af.nf.unit = ' ';
131 fi->af.nf.supress_unit = 0;
134 fi->af.nf.align = ALIGN_RIGHT;
135 fi->af.nf.unit = 'H';
136 fi->af.nf.supress_unit = 0;
139 fi->af.nf.align = ALIGN_RIGHT;
140 fi->af.nf.unit = 'h';
141 fi->af.nf.supress_unit = 0;
149 fi->af.sf.align = ALIGN_LEFT;
152 fi->af.nf.align = ALIGN_RIGHT;
155 fi->af.nf.align = ALIGN_CENTER;
157 case ':': /* no alignment spec is OK */
161 ret = -E_BAD_ALIGN_SPEC;
171 ret = -E_TRAILING_GARBAGE;
176 sscanf(col + 1, "%u%n", &fi->width, &n);
178 switch (col[1 + n]) {
185 ret = -E_TRAILING_GARBAGE;
188 if (a->type != AT_SIZE && a->type != AT_COUNT) {
193 fi->af.nf.supress_unit = 1;
196 for (j = 0; units[j]; j++) {
197 if (units[j] != col[1])
199 fi->af.nf.unit = units[j];
207 ret = -E_TRAILING_GARBAGE;
222 * Parse the given string according to the list of given atoms.
224 * \param fmt The format string.
225 * \param atoms The array of valid atoms.
226 * \param result Points to a format_info structure for later use.
230 int parse_format_string(char *fmt, struct atom *atoms, struct format_info **result)
234 struct format_info *info = malloc(sizeof(*info));
238 for (cp = fmt, num_items = 0; *cp && (ap = find_next(cp));
239 cp = ep + 1, num_items++) {
241 info->items = realloc(info->items,
242 (num_items + 1) * sizeof(struct format_info *));
243 info->items[num_items] = make_const_string(cp, ap);
246 info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
247 ret = parse_atom(ap + 2, atoms, info->items + num_items);
252 ep = strchr(ap, ')');
255 ep = cp + strlen(cp);
256 info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
257 info->items[num_items] = make_const_string(cp, ep);
260 info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
261 info->items[num_items] = NULL;
265 for (; num_items >= 0; num_items--) {
266 if (!info->items[num_items]->atom_ptr)
267 free(info->items[num_items]->af.cs.string);
268 free(info->items[num_items]);
277 * Free a struct of type \a format_info.
279 * \param info Pointer to the format info to be freed.
281 * It's OK to pass a \p NULL pointer to this function in which case the
282 * function does nothing.
284 void free_format_info(struct format_info *info)
287 struct format_item *item;
292 for (i = 0; (item = info->items[i]); i++) {
294 free(item->af.cs.string);
295 free(info->items[i]);
301 static char *align_string(const char *src, unsigned int width, enum alignment align)
306 return adu_strdup(src);
307 if (align == ALIGN_LEFT)
308 return make_message("%-*s", width, src);
309 if (align == ALIGN_RIGHT)
310 return make_message("%*s", width, src);
312 return make_message("%*s%*s", (width + len) / 2, src,
313 width - (width + len) / 2, "");
316 /** Compute the number of (decimal) digits of a number. */
317 #define GET_NUM_DIGITS(x, num) { \
318 typeof((x)) _tmp = x; \
321 while ((_tmp) > 9) { \
327 static long long unsigned normalize_number(long long unsigned num, char unit,
328 char *effective_unit)
330 long long unsigned normalized_num;
334 *effective_unit = ' ';
335 else if (num < 1024ULL * 1024ULL)
336 *effective_unit = 'k';
337 else if (num < 1024ULL * 1024ULL * 1024ULL)
338 *effective_unit = 'm';
339 else if (num < 1024ULL * 1024ULL * 1024ULL * 1024ULL)
340 *effective_unit = 'g';
342 *effective_unit = 't';
343 } else if (unit == 'H') {
345 *effective_unit = ' ';
346 else if (num < 1000ULL * 1000ULL)
347 *effective_unit = 'K';
348 else if (num < 1000ULL * 1000ULL * 1000ULL)
349 *effective_unit = 'M';
350 else if (num < 1000ULL * 1000ULL * 1000ULL * 1000ULL)
351 *effective_unit = 'G';
353 *effective_unit = 'T';
355 *effective_unit = unit;
356 switch (*effective_unit) {
358 case 'b': normalized_num = num; break;
359 case 'k': normalized_num = num / 1024; break;
360 case 'm': normalized_num = num / 1024 / 1024; break;
361 case 'g': normalized_num = num / 1024 / 1024 / 1024; break;
362 case 't': normalized_num = num / 1024 / 1024 / 1024 / 1024; break;
363 case 'K': normalized_num = num / 1000; break;
364 case 'M': normalized_num = num / 1000 / 1000; break;
365 case 'G': normalized_num = num / 1000 / 1000 / 1000; break;
366 case 'T': normalized_num = num / 1000 / 1000 / 1000 / 1000; break;
368 EMERG_LOG("BUG: invalid unit %c\n", *effective_unit);
371 return normalized_num;
374 static void get_unit_postfix(struct num_format *nf, char eu, enum atom_type type,
377 if (nf->supress_unit) {
385 if (nf->unit != 'h' && nf->unit != 'H')
389 postfix[0] = 'b'; /* bytes */
392 static char *align_unsigned_int(long long unsigned num, unsigned int width,
393 struct num_format *nf, enum atom_type type)
395 char eu; /* effective unit */
396 long long unsigned nnum = normalize_number(num, nf->unit, &eu);
397 char postfix[2] = "\0\0";
400 get_unit_postfix(nf, eu, type, postfix);
402 return make_message("%llu%s", nnum, postfix);
403 GET_NUM_DIGITS(nnum, &len);
404 len += strlen(postfix);
407 if (nf->align == ALIGN_LEFT)
408 return make_message("%llu%s%*s", nnum, postfix, width - len, "");
409 if (nf->align == ALIGN_RIGHT)
410 return make_message("%*s%llu%s", width - len, "", nnum, postfix);
411 return make_message("%*llu%s%*s", (width + len) / 2,
412 nnum, postfix, width - (width + len) / 2, "");
416 * Pretty-format the given values according to \a info.
418 * \param info The formating information.
419 * \param values The contents of the atoms.
421 * \return A string that must be freed by the caller.
423 char *format_items(struct format_info *info, union atom_value *values)
430 for (i = 0; info->items[i]; i++) {
432 struct format_item *fi = info->items[i];
433 union atom_format *af = &fi->af;
434 enum alignment align;
439 if (!fi->atom_ptr) { /* const string */
440 buf = adu_strcat(buf, af->cs.string);
445 idx = a - info->atoms;
447 if (type == AT_STRING) {
448 align = af->sf.align;
449 val = align_string(values[idx].string_value, fi->width, align);
452 align = af->nf.align;
454 val = align_unsigned_int(values[idx].num_value,
455 fi->width, &af->nf, type);
457 buf = adu_strcat(buf, val);
461 buf = adu_strdup("");