/*
- * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
-/** \file format.c Functions for pretty-printing numbers and strings. */
+/** \file format.c \brief Functions for pretty-printing numbers and strings. */
#include <dirent.h> /* readdir() */
#include "adu.h"
#include "string.h"
#include "error.h"
#include "format.h"
+
+/** The three different alignment types. */
enum alignment {ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER};
struct num_format {
enum alignment align;
char unit;
- int supress_unit;
+ int suppress_unit;
};
struct string_format {
case AT_ID:
fi->af.nf.align = ALIGN_RIGHT;
fi->af.nf.unit = ' ';
- fi->af.nf.supress_unit = 0;
+ fi->af.nf.suppress_unit = 0;
break;
case AT_COUNT:
fi->af.nf.align = ALIGN_RIGHT;
fi->af.nf.unit = 'H';
- fi->af.nf.supress_unit = 0;
+ fi->af.nf.suppress_unit = 0;
break;
case AT_SIZE:
fi->af.nf.align = ALIGN_RIGHT;
fi->af.nf.unit = 'h';
- fi->af.nf.supress_unit = 0;
+ fi->af.nf.suppress_unit = 0;
break;
}
if (!col)
goto err;
}
if (col[1] == '*') {
- fi->af.nf.supress_unit = 1;
+ fi->af.nf.suppress_unit = 1;
col++;
}
for (j = 0; units[j]; j++) {
return 1;
}
+/**
+ * Parse the given string according to the list of given atoms.
+ *
+ * \param fmt The format string.
+ * \param atoms The array of valid atoms.
+ * \param result Points to a format_info structure for later use.
+ *
+ * \return Standard.
+ */
int parse_format_string(char *fmt, struct atom *atoms, struct format_info **result)
{
char *cp, *ap, *ep;
}
free(info->items);
free(info);
+ *result = NULL;
return ret;
}
+/**
+ * Free a struct of type \a format_info.
+ *
+ * \param info Pointer to the format info to be freed.
+ *
+ * It's OK to pass a \p NULL pointer to this function in which case the
+ * function does nothing.
+ */
void free_format_info(struct format_info *info)
{
int i;
struct format_item *item;
+ if (!info)
+ return;
+
for (i = 0; (item = info->items[i]); i++) {
if (!item->atom_ptr)
free(item->af.cs.string);
static void get_unit_postfix(struct num_format *nf, char eu, enum atom_type type,
char postfix[2])
{
- if (nf->supress_unit) {
+ if (nf->suppress_unit) {
*postfix = '\0';
return;
}
nnum, postfix, width - (width + len) / 2, "");
}
+/**
+ * Pretty-format the given values according to \a info.
+ *
+ * \param info The formating information.
+ * \param values The contents of the atoms.
+ *
+ * \return A string that must be freed by the caller.
+ */
char *format_items(struct format_info *info, union atom_value *values)
{
int i;
char *buf = NULL;
+ if (!info)
+ return NULL;
for (i = 0; info->items[i]; i++) {
struct atom *a;
struct format_item *fi = info->items[i];
align = af->sf.align;
val = align_string(values[idx].string_value, fi->width, align);
} else {
- char unit;
align = af->nf.align;
- unit = af->nf.unit;
val = align_unsigned_int(values[idx].num_value,
fi->width, &af->nf, type);
}
buf = adu_strcat(buf, val);
+ free(val);
}
+ if (!buf)
+ buf = adu_strdup("");
return buf;
}