summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-09-12 21:25:58 -0400
committerDan McGee <dan@archlinux.org>2011-09-28 10:17:15 -0500
commitbcc6a5d72df3cb3ee9f3d2347526c3dc03d5f55a (patch)
tree6588c75a79d7e7a78137b9efae58bbcb4d8c4a8c /src
parent1b8bb7c1cd81c1f8f0dcffc952cd464b6a436f06 (diff)
downloadpacman-bcc6a5d72df3cb3ee9f3d2347526c3dc03d5f55a.tar.xz
Table format creation code cleanup
Better scoping of variables for the most part, and ensure we are using string_length() and not strlen() as appropriate. Also refactor the longest cell code to call string_length() a lot less; by simply using an array of max sizes we don't have to recompute values nearly as much. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'src')
-rw-r--r--src/pacman/util.c48
1 files changed, 27 insertions, 21 deletions
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 592754f8..3dedf9f0 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -459,7 +459,7 @@ static size_t string_length(const char *s)
int len;
wchar_t *wcstr;
- if(!s) {
+ if(!s || s[0] == '\0') {
return 0;
}
/* len goes from # bytes -> # chars -> # cols */
@@ -506,47 +506,53 @@ static void table_print_line(const alpm_list_t *line,
static alpm_list_t *table_create_format(const alpm_list_t *header,
const alpm_list_t *rows)
{
- alpm_list_t *longest_str, *longest_strs = NULL;
alpm_list_t *formats = NULL;
- const alpm_list_t *i, *row, *cell;
- char *str, *formatstr;
+ const alpm_list_t *i;
const unsigned short padding = 2;
- size_t colwidth, totalwidth = 0;
- size_t curcol = 0;
+ size_t curcol, totalcols, totalwidth = 0;
+ size_t *colwidths;
+ totalcols = alpm_list_count(header);
+ colwidths = malloc(totalcols * sizeof(size_t));
+ if(!colwidths) {
+ return NULL;
+ }
/* header determines column count and initial values of longest_strs */
- for(i = header; i; i = alpm_list_next(i)) {
- longest_strs = alpm_list_add(longest_strs, alpm_list_getdata(i));
+ for(i = header, curcol = 0; i; i = alpm_list_next(i), curcol++) {
+ colwidths[curcol] = string_length(alpm_list_getdata(i));
}
/* now find the longest string in each column */
- for(longest_str = longest_strs; longest_str;
- longest_str = alpm_list_next(longest_str), curcol++) {
- for(i = rows; i; i = alpm_list_next(i)) {
- row = alpm_list_getdata(i);
- cell = alpm_list_nth(row, curcol);
- str = alpm_list_getdata(cell);
-
- if(strlen(str) > strlen(alpm_list_getdata(longest_str))) {
- longest_str->data = str;
+ for(i = rows; i; i = alpm_list_next(i)) {
+ /* grab first column of each row and iterate through columns */
+ const alpm_list_t *j = alpm_list_getdata(i);
+ for(curcol = 0; j; j = alpm_list_next(j), curcol++) {
+ char *str = alpm_list_getdata(j);
+ size_t str_len = string_length(str);
+
+ if(str_len > colwidths[curcol]) {
+ fprintf(stderr, "replace width: %zd (%zd -> %zd) %s\n",
+ curcol, colwidths[curcol], str_len, str);
+ colwidths[curcol] = str_len;
}
}
}
/* now use the column width info to generate format strings */
- for(i = longest_strs; i; i = alpm_list_next(i)) {
+ for(curcol = 0; curcol < totalcols; curcol++) {
const char *display;
- colwidth = string_length(alpm_list_getdata(i)) + padding;
+ char *formatstr;
+ size_t colwidth = colwidths[curcol] + padding;
totalwidth += colwidth;
/* right align the last column for a cleaner table display */
- display = (alpm_list_next(i) != NULL) ? "%%-%ds" : "%%%ds";
+ display = (curcol + 1 < totalcols) ? "%%-%ds" : "%%%ds";
pm_asprintf(&formatstr, display, colwidth);
formats = alpm_list_add(formats, formatstr);
}
- alpm_list_free(longest_strs);
+ free(colwidths);
/* return NULL if terminal is not wide enough */
if(totalwidth > getcols()) {