Revision: 77827
          http://sourceforge.net/p/brlcad/code/77827
Author:   brlcad
Date:     2020-11-30 13:35:59 +0000 (Mon, 30 Nov 2020)
Log Message:
-----------
add support for empty cells in formatting.

this is something strtok() doesn't intrinsically support, so we augment
behavior by looking for sequences of nulls or separators within the
strtok-modified buffer.  strsep() does support empties, but it's not
portably guaranteed.  the reason for implementing this is two-fold: 
first, it lets authors skip columns while printing and second, it
avoids a bug in libfort (reported) that precludes indexing directly
into empty cells.  this was a bit nasty to figure out and not the
best coding to say the least, but not seeing a good way to reduce the
complexity just yet.  it does seem to work well (at least on mac's bsd
strtok), but does warrant portability / unit testing.

Modified Paths:
--------------
    brlcad/trunk/src/libbu/tbl.c

Modified: brlcad/trunk/src/libbu/tbl.c
===================================================================
--- brlcad/trunk/src/libbu/tbl.c        2020-11-30 11:06:57 UTC (rev 77826)
+++ brlcad/trunk/src/libbu/tbl.c        2020-11-30 13:35:59 UTC (rev 77827)
@@ -177,7 +177,10 @@
 
     va_list ap;
 #define BUFSZ 4096
-    char buf[BUFSZ] = {0};
+    char buf[BUFSZ];
+    char *back;
+    char *last;
+    size_t zeros;
 
     if (!fmt)
        return tbl;
@@ -185,16 +188,60 @@
     BU_ASSERT(tbl);
     BU_ASSERT(tbl->t);
 
+    memset(buf, 255, BUFSZ);
+
     va_start(ap, fmt);
     vsnprintf(buf, BUFSZ, fmt, ap);
     va_end(ap);
 
     cstr = strtok(buf, "|");
+    if (cstr) {
+       /* strtok collapses empty tokens, so check */
+       back = cstr;
+       zeros = 0;
+       back--;
+       while ((*back == '\0' || *back == '|') && buf <= back) {
+           zeros++;
+           back--;
+       }
+       while (zeros--) {
+           ft_printf(tbl->t, "");
+       }
+       ft_printf(tbl->t, "%s", cstr);
+       last = cstr;
+    }
+
     while (cstr) {
-       ft_printf(tbl->t, "%s", cstr);
        cstr = strtok(NULL, "|");
+
+       if (cstr) {
+           /* strtok collapses empty tokens, so check */
+           back = cstr;
+           zeros = 0;
+           back -= 2;
+           while ((*back == '\0' || *back == '|') && buf <= back) {
+               zeros++;
+               back--;
+           }
+           while (zeros--) {
+               ft_printf(tbl->t, "");
+           }
+
+           ft_printf(tbl->t, "%s", cstr);
+           last = cstr;
+       }
     }
 
+    zeros = 0;
+    last += 2;
+    while ((*last == '\0' || *last == '|')) {
+       zeros++;
+       last++;
+    }
+    while (zeros--) {
+       ft_printf(tbl->t, "");
+    }
+
     return tbl;
 }
 

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to