[flashrom] [PATCH 3/5] print_wiki.c fix columns calculations

Stefan Tauner stefan.tauner at student.tuwien.ac.at
Sun Feb 19 21:26:55 CET 2012


for
- chipsets

---

i noticed that the chipset columns were not uniformly divided at all
and looked at the code in more detail... out came this series of
patches that changes all three major sections of the wiki page
similarly (hopefully to the better) and it split respectively for
easier reviewing. getting this right is not trivial due to the
possible corner cases... and it makes one cry when aware of real
programming languages ;)

Signed-off-by: Stefan Tauner <stefan.tauner at student.tuwien.ac.at>
---
 print_wiki.c |   36 +++++++++++++++++++++++-------------
 1 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/print_wiki.c b/print_wiki.c
index 58f1612..b9fed47 100644
--- a/print_wiki.c
+++ b/print_wiki.c
@@ -91,18 +91,26 @@ wrong (a working backup flash chip and/or good soldering skills).\n</div>\n";
 
 static void print_supported_chipsets_wiki(int cols)
 {
-	int i, j, enablescount = 0, color = 1;
+	int enablescount = 0, color = 1;
+	int i;
+	unsigned int lines_per_col;
 	const struct penable *e;
 
 	for (e = chipset_enables; e->vendor_name != NULL; e++)
 		enablescount++;
 
+	/* +1 to force the resulting number of columns to be < cols */
+	lines_per_col = enablescount / cols + ((enablescount%cols) > 0 ? 1 : 0);
+
 	printf("\n== Supported chipsets ==\n\nTotal amount of supported "
-	       "chipsets: '''%d'''\n\n{| border=\"0\" valign=\"top\"\n| "
-	       "valign=\"top\"|\n\n%s", enablescount, chipset_th);
+	       "chipsets: '''%d'''\n\n{| border=\"0\" valign=\"top\"\n",
+	       enablescount);
 
 	e = chipset_enables;
-	for (i = 0, j = 0; e[i].vendor_name != NULL; i++, j++) {
+	for (i = 0; e[i].vendor_name != NULL; i++) {
+		if ((i % lines_per_col) == 0)
+			printf("| valign=\"top\"|\n\n%s", chipset_th);
+
 		/* Alternate colors if the vendor changes. */
 		if (i > 0 && strcmp(e[i].vendor_name, e[i - 1].vendor_name))
 			color = !color;
@@ -113,30 +121,32 @@ static void print_supported_chipsets_wiki(int cols)
 		       e[i].vendor_id, e[i].device_id,
 		       (e[i].status == OK) ? "{{OK}}" : "{{?3}}");
 
-		/* Split table in 'cols' columns. */
-		if (j >= (enablescount / cols + 1)) {
-			printf("\n|}\n\n| valign=\"top\"|\n\n%s", chipset_th);
-			j = 0;
-		}
+		if (((i % lines_per_col) + 1) == lines_per_col)
+			printf("\n|}\n\n");
 	}
 
-	printf("\n|}\n\n|}\n");
+	/* end inner table if it did not fill the last column fully */
+	if (((i % lines_per_col)) > 0)
+		printf("\n|}\n\n");
+	printf("\n\n|}\n");
 }
 
 static void print_supported_boards_wiki_helper(const char *devicetype, int cols,
 			const struct board_info boards[])
 {
-	int i, j, k = 0, boardcount_good = 0, boardcount_bad = 0, color = 1;
+	int i, j, k = 0, color = 1;
 	int num_notes = 0;
 	char *notes = calloc(1, 1);
-	char tmp[900 + 1];
 	const struct board_match *b = board_matches;
+	unsigned int boardcount = 0, boardcount_good = 0, boardcount_bad = 0;
+	char tmp[900 + 1];
 
 	for (i = 0; boards[i].vendor != NULL; i++) {
 		if (boards[i].working == OK)
 			boardcount_good++;
 		if (boards[i].working == BAD)
 			boardcount_bad++;
+		boardcount++;
 	}
 
 	printf("\n\nTotal amount of supported %s: '''%d'''. "
@@ -181,7 +191,7 @@ static void print_supported_boards_wiki_helper(const char *devicetype, int cols,
 		}
 
 		/* Split table in 'cols' columns. */
-		if (j >= ((boardcount_good + boardcount_bad) / cols + 1)) {
+		if (j >= (boardcount / cols + 1)) {
 			printf("\n|}\n\n| valign=\"top\"|\n\n%s", board_th);
 			j = 0;
 		}
-- 
1.7.1





More information about the flashrom mailing list