From patchwork Mon Feb 18 21:04:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 2160391 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 264C83FCFC for ; Mon, 18 Feb 2013 21:04:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755908Ab3BRVEF (ORCPT ); Mon, 18 Feb 2013 16:04:05 -0500 Received: from nm19-vm0.bullet.mail.ird.yahoo.com ([77.238.189.92]:44306 "HELO nm19-vm0.bullet.mail.ird.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1755727Ab3BRVEC (ORCPT ); Mon, 18 Feb 2013 16:04:02 -0500 Received: from [77.238.189.55] by nm19.bullet.mail.ird.yahoo.com with NNFMP; 18 Feb 2013 21:04:00 -0000 Received: from [46.228.39.42] by tm8.bullet.mail.ird.yahoo.com with NNFMP; 18 Feb 2013 21:04:00 -0000 Received: from [127.0.0.1] by smtp175.mail.ir2.yahoo.com with NNFMP; 18 Feb 2013 21:04:00 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1361221440; bh=0jujs4FHgTzlieNtuKGdTtFk87KBhIzj5M591jZCEus=; h=X-Yahoo-Newman-Id:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References:Reply-to; b=h3x9iCHlozL+OLRtEO3YXR9MpNf4hRC2a039vbPwOphQ69ngpcsmTlmIRKEvbU52oxOH3K/0Yzw90xH/i8jaD5/86DmArYSnFySvWTNS1wbvbf01a+GBnh+b8wTZyYUjskl4Zqhseed5dDsYzHSUfqMyiCijMym2+cWqXmoNoPA= X-Yahoo-Newman-Id: 823640.7055.bm@smtp175.mail.ir2.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: e6fm6GwVM1lcxcQOuMUy70zNre54HRgSBSc62VgqKYbcTbF m1AeTc4HnGC8qLXnyXRKC4wlmT4HIL5SlMIGw0NClyp1guZpGBxEJbQUZL.A QRA5xCUIxpKLtSatrD1m5fiqO_NCiUYE470tdQVFKXelOBnZsLGGMb.G_1l7 j166CEdseYu5ybiTFFGg5V3FsI20JxhVqPyrzFe6SsUPIVZ77JuLvkksRrn4 EgRBY28clTet8JcL9dPSTyuTEfHtbKnYcrNOXEKi93kpY6FBnuxetTUyHuch ZFWSbf5.OwQyLGSVOWQxSGGvYevuaykMWX3GmkpSuKGY3Y0tadTEIIhNNvVE rw9w515Y6WDWu3wYc52rii_teVUgGp_qCI_4Yu.rM8V2ZPjcUG4coJ68PADR tHc5nLPILPYPhQq4ezC2PrymIS_hSI45O7083OYIrgvyoCAMGfEgKV_VrDNH boMC1vwATewB9Cb8rVqACY8M- X-Yahoo-SMTP: ssXiUn.swBDBJsD4bjmtGfj6buOv3E4378JD.MOD2Xo- Received: from venice..bhome (goffredo.baroncelli@95.242.133.103 with login) by smtp175.mail.ir2.yahoo.com with SMTP; 18 Feb 2013 21:04:00 +0000 UTC From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Hugo Mills , Michael =?utf-8?q?Kj=C3=B6rling?= , Martin Steigerwald , cwillu , Chris Murphy , David Sterba Subject: [PATCH 4/8] Add helpers functions to handle the printing of data in tabular format. Date: Mon, 18 Feb 2013 22:04:29 +0100 Message-Id: <1361221473-20347-5-git-send-email-goffredo.baroncelli@yahoo.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1361221473-20347-1-git-send-email-goffredo.baroncelli@yahoo.com> References: <1361221473-20347-1-git-send-email-goffredo.baroncelli@yahoo.com> Reply-to: kreijack@inwind.it Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org This patch adds some functions to manage the printing of the data in tabular format. The function struct string_table *table_create(int columns, int rows) creates an (empty) table. The functions char *table_printf(struct string_table *tab, int column, int row, char *fmt, ...) char *table_vprintf(struct string_table *tab, int column, int row, char *fmt, va_list ap) populate the table with text. To align the text to the left, the text shall be prefixed with '<', otherwise the text shall be prefixed by a '>'. If the first character is a '=', the the text is replace by a sequence of '=' to fill the column width. The function void table_free(struct string_table *) frees all the data associated to the table. The function void table_dump(struct string_table *tab) prints the table on stdout. Signed-off-by: Goffredo Baroncelli --- Makefile | 2 +- string_table.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ string_table.h | 36 +++++++++++++ 3 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 string_table.c create mode 100644 string_table.h diff --git a/Makefile b/Makefile index bd792b6..fd1b312 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \ inode-map.o crc32c.o rbtree.o extent-cache.o extent_io.o \ volumes.o utils.o btrfs-list.o btrfslabel.o repair.o \ send-stream.o send-utils.o qgroup.o raid6.o \ - string_list.o + string_list.o string_table.o cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \ cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \ cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-fi-disk_usage.o diff --git a/string_table.c b/string_table.c new file mode 100644 index 0000000..9784422 --- /dev/null +++ b/string_table.c @@ -0,0 +1,157 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#include +#include +#include +#include + +#include "string_table.h" + +/* + * This function create an array of char * which will represent a table + */ +struct string_table *table_create(int columns, int rows) +{ + struct string_table *p; + int size; + + + size = sizeof( struct string_table ) + + rows * columns* sizeof(char *); + p = calloc(1, size); + + if (!p) return NULL; + + p->ncols = columns; + p->nrows = rows; + + return p; +} + +/* + * This function is like a vprintf, but store the results in a cell of + * the table. + * If fmt starts with '<', the text is left aligned; if fmt starts with + * '>' the text is right aligned. If fmt is equal to '=' the text will + * be replaced by a '=====' dimensioned in the basis of the column width + */ +char *table_vprintf(struct string_table *tab, int column, int row, + char *fmt, va_list ap) +{ + int idx = tab->ncols*row+column; + char *msg = calloc(100, sizeof(char)); + + if (!msg) + return NULL; + + if (tab->cells[idx]) + free(tab->cells[idx]); + tab->cells[idx] = msg; + vsnprintf(msg, 99, fmt, ap); + + return msg; +} + + +/* + * This function is like a printf, but store the results in a cell of + * the table. + */ +char *table_printf(struct string_table *tab, int column, int row, + char *fmt, ...) +{ + va_list ap; + char *ret; + + va_start(ap, fmt); + ret = table_vprintf(tab, column, row, fmt, ap); + va_end(ap); + + return ret; +} + +/* + * This function dumps the table. Every "=" string will be replaced by + * a "=======" length as the column + */ +void table_dump(struct string_table *tab) +{ + int sizes[tab->ncols]; + int i, j; + + for (i = 0 ; i < tab->ncols ; i++) { + sizes[i] = 0; + for (j = 0 ; j < tab->nrows ; j++) { + int idx = i + j*tab->ncols; + int s; + + if (!tab->cells[idx]) + continue; + + s = strlen(tab->cells[idx]) - 1; + if (s < 1 || tab->cells[idx][0] == '=') + continue; + + if (s > sizes[i]) + sizes[i] = s; + } + } + + + for (j = 0 ; j < tab->nrows ; j++) { + for (i = 0 ; i < tab->ncols ; i++) { + + int idx = i + j*tab->ncols; + char *s = tab->cells[idx]; + + if (!s|| !strlen(s)) { + printf("%*s", sizes[i], ""); + } else if (s && s[0] == '=') { + int k = sizes[i]; + while(k--) + putchar('='); + } else { + printf("%*s", + s[0] == '<' ? -sizes[i] : sizes[i], + s+1); + } + if (i != (tab->ncols - 1)) + putchar(' '); + } + putchar('\n'); + } + +} + +/* + * Deallocate a tabular and all its content + */ + +void table_free(struct string_table *tab) +{ + + int i, count; + + count = tab->ncols * tab->nrows; + + for (i=0 ; i < count ; i++) + if (tab->cells[i]) + free(tab->cells[i]); + + free(tab); + +} diff --git a/string_table.h b/string_table.h new file mode 100644 index 0000000..83c4425 --- /dev/null +++ b/string_table.h @@ -0,0 +1,36 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#ifndef STRING_TABLE_H +#define STRING_TABLE_H + +struct string_table { + + int ncols, nrows; + char *cells[]; + +}; + + +struct string_table *table_create(int columns, int rows); +char *table_printf(struct string_table *tab, int column, int row, + char *fmt, ...); +char *table_vprintf(struct string_table *tab, int column, int row, + char *fmt, va_list ap); +void table_dump(struct string_table *tab); +void table_free(struct string_table *); + +#endif