[GRASS-dev] g.list: extra whitespace issue

Ivan Shmakov ivan at theory.asu.ru
Fri Feb 22 06:14:33 EST 2008


>>>>> Ivan Shmakov <ivan at theory.asu.ru> writes:

[...]

 > As for the padding problem, I believe that a more generic solution
 > should be implemented.

	Such as the following (rather obscure, though actually obvious)
	one.

	BTW, GNU `ls' seems to produce quite nice columnar lists.  In
	particular, the columns are allowed to be of different width.
	May the respective code be borrowed for G_ls_format ()?

diff --git a/lib/gis/ls.c b/lib/gis/ls.c
index 3ef14e9..b556212 100644
--- a/lib/gis/ls.c
+++ b/lib/gis/ls.c
@@ -124,7 +124,7 @@ void G_ls(const char *dir, FILE *stream)
 
 void G_ls_format(const char **list, int num_items, int perline, FILE *stream)
 {
-    int i, j;
+    int i;
 
     int field_width, column_height;
     int screen_width = 80; /* Default width of 80 columns */
@@ -163,20 +163,25 @@ void G_ls_format(const char **list, int num_items, int perline, FILE *stream)
     /* Longest column height (i.e. num_items <= perline * column_height) */
     column_height = (num_items / perline) + ((num_items % perline) > 0);
 
-    for (i=0; i < column_height; i++)     
-    {	
-        for (j=0; j < perline; j++)
-	{
-	    int cur = j * column_height + i;
-	   
-	    if (cur >= num_items)
-	        continue; /* No more items to print in this row */
-	   
-            /* Print filenames in left-justified fixed-width fields */
-            fprintf(stream, "%-*s", field_width, list[cur]);
-	}       
-        fprintf(stream, "\n");
-    }   
+    {
+	const int max
+	    = num_items + column_height - (num_items % column_height);
+	const char **next;
+
+	for (i = 1, next = list; i <= num_items; i++) {
+	    const char **cur = next;
+
+	    next += column_height;
+	    if (next >= list + num_items) {
+		/* the next item has to be on the other line */
+		next -= (max - 1
+			 - (next < list + max ? column_height : 0));
+		fprintf (stream, "%s\n", *cur);
+	    } else {
+		fprintf (stream, "%-*s", field_width, *cur);
+	    }
+	}
+    }
    
     return;
 }



More information about the grass-dev mailing list