X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=format.c;h=f1d1247074da8c97c41368aa3663b037dbbfc4bd;hp=48aa37111c2986d6262686723dd766c9f0ed45ef;hb=f638c88fad8a1350cf56d5f60ddef297ece92805;hpb=bd06800e612996d0c4b4c2ec52646a842d301452 diff --git a/format.c b/format.c index 48aa371..f1d1247 100644 --- a/format.c +++ b/format.c @@ -4,7 +4,7 @@ * 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 /* readdir() */ #include "adu.h" @@ -13,6 +13,7 @@ #include "string.h" #include "error.h" #include "format.h" + enum alignment {ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER}; struct num_format { @@ -217,6 +218,15 @@ success: 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; @@ -259,14 +269,26 @@ err: } 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); @@ -390,11 +412,21 @@ static char *align_unsigned_int(long long unsigned num, unsigned int width, 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]; @@ -423,6 +455,9 @@ char *format_items(struct format_info *info, union atom_value *values) fi->width, &af->nf, type); } buf = adu_strcat(buf, val); + free(val); } + if (!buf) + buf = adu_strdup(""); return buf; }