@@@ misc wip
[mLib] / test / tvec-types.c
index 270f409..dc70dbd 100644 (file)
@@ -462,7 +462,9 @@ static void format_size(const struct gprintf_ops *gops, void *go,
 {
   const char *unit;
 
-  if (!u || u%1024)
+  if (style&TVSF_RAW)
+    gprintf(gops, go, "%lu", u);
+  else if (!u || u%1024)
     gprintf(gops, go, "%lu%sB", u, style&TVSF_COMPACT ? "" : " ");
   else {
     for (unit = size_units, u /= 1024;
@@ -1220,6 +1222,7 @@ static int read_compound_string(void **p_inout, size_t *sz_inout,
        if (cdc && flush_codec(cdc, &d, tv)) { rc = -1; goto end; }
        cdc = 0;
        DRESET(&w); tvec_readword(tv, &w, 0, ";", "character name");
+       if (STRCMP(w.buf, ==, "#empty")) break;
        if (read_charname(&ch, w.buf, RCF_EOFOK)) {
          rc = tvec_error(tv, "unknown character name `%s'", d.buf);
          goto end;
@@ -1502,9 +1505,9 @@ static void dump_int(const union tvec_regval *rv,
                     unsigned style,
                     const struct gprintf_ops *gops, void *go)
 {
-
+  if (style&TVSF_RAW) gprintf(gops, go, "int:");
   gprintf(gops, go, "%ld", rv->i);
-  if (!(style&TVSF_COMPACT)) {
+  if (!(style&(TVSF_COMPACT | TVSF_RAW))) {
     gprintf(gops, go, " ; = ");
     format_signed_hex(gops, go, rv->i);
     maybe_format_signed_char(gops, go, rv->i);
@@ -1516,8 +1519,9 @@ static void dump_uint(const union tvec_regval *rv,
                      unsigned style,
                      const struct gprintf_ops *gops, void *go)
 {
+  if (style&TVSF_RAW) gprintf(gops, go, "uint:");
   gprintf(gops, go, "%lu", rv->u);
-  if (!(style&TVSF_COMPACT)) {
+  if (!(style&(TVSF_COMPACT | TVSF_RAW))) {
     gprintf(gops, go, " ; = ");
     format_unsigned_hex(gops, go, rv->u);
     maybe_format_unsigned_char(gops, go, rv->u);
@@ -1657,8 +1661,9 @@ static void dump_size(const union tvec_regval *rv,
                      unsigned style,
                      const struct gprintf_ops *gops, void *go)
 {
+  if (style&TVSF_RAW) gprintf(gops, go, "size:");
   format_size(gops, go, rv->u, style);
-  if (!(style&TVSF_COMPACT)) {
+  if (!(style&(TVSF_COMPACT | TVSF_RAW))) {
     gprintf(gops, go, " ; = %lu", (unsigned long)rv->u);
     gprintf(gops, go, " = "); format_unsigned_hex(gops, go, rv->u);
     maybe_format_unsigned_char(gops, go, rv->u);
@@ -1824,7 +1829,10 @@ static void dump_float(const union tvec_regval *rv,
                       const struct tvec_regdef *rd,
                       unsigned style,
                       const struct gprintf_ops *gops, void *go)
-  { format_floating(gops, go, rv->f); }
+{
+  if (style&TVSF_RAW) gprintf(gops, go, "float:");
+  format_floating(gops, go, rv->f);
+}
 
 /* Floating-point type definition. */
 const struct tvec_regty tvty_float = {
@@ -2067,17 +2075,23 @@ static void dump_duration(const union tvec_regval *rv,
   const struct duration_unit *u;
   double t = rv->f;
 
-  if (!t) u = 0;
-  else {
-    for (u = duration_units; u->scale > t && u[1].unit; u++);
-    t /= u->scale;
-  }
-
-  gprintf(gops, go, "%.4g %s", t, u ? u->unit : "s");
-  if (!(style&TVSF_COMPACT)) {
-    gprintf(gops, go, "; = ");
+  if (style&TVSF_RAW) {
+    gprintf(gops, go, "duration:");
     format_floating(gops, go, rv->f);
-    gprintf(gops, go, " s");
+    gprintf(gops, go, "s");
+  } else {
+    if (!t) u = 0;
+    else {
+      for (u = duration_units; u->scale > t && u[1].unit; u++);
+      t /= u->scale;
+    }
+    gprintf(gops, go, "%.4g %s", t, u ? u->unit : "s");
+
+    if (!(style&TVSF_COMPACT)) {
+      gprintf(gops, go, "; = ");
+      format_floating(gops, go, rv->f);
+      gprintf(gops, go, " s");
+    }
   }
 }
 
@@ -2294,7 +2308,8 @@ static int frombuf_penum(buf *b, union tvec_regval *rv,
     dstr d = DSTR_INIT;                                                        \
     int rc;                                                            \
                                                                        \
-    if (tvec_readword(tv, &d, 0, ";", "enumeration tag or " LITSTR_##tag_)) \
+    if (tvec_readword(tv, &d, 0,                                       \
+                     ";", "%s tag or " LITSTR_##tag_, ei->name))       \
       { rc = -1; goto end; }                                           \
     for (a = ei->av; a->tag; a++)                                      \
       if (STRCMP(a->tag, ==, d.buf)) { FOUND_##tag_ goto done; }       \
@@ -2383,6 +2398,7 @@ TVEC_MISCSLOTS(DEFPARSE_ENUM)
     const struct tvec_##slot##enuminfo *ei = rd->arg.p;                        \
     const struct tvec_##slot##assoc *a;                                        \
                                                                        \
+    if (style&TVSF_RAW) gprintf(gops, go, #slot "enum/%s:", ei->name); \
     for (a = ei->av; a->tag; a++)                                      \
       if (rv->slot == a->slot) {                                       \
        gprintf(gops, go, "%s", a->tag);                                \
@@ -2555,7 +2571,7 @@ static int parse_flags(union tvec_regval *rv, const struct tvec_regdef *rd,
 
     /* Read the next item. */
     DRESET(&d);
-    if (tvec_readword(tv, &d, 0, "|;", "flag name or integer"))
+    if (tvec_readword(tv, &d, 0, "|;", "%s flag name or integer", fi->name))
       { rc = -1; goto end; }
 
     /* Try to find a matching entry in the table. */
@@ -2579,8 +2595,10 @@ static int parse_flags(union tvec_regval *rv, const struct tvec_regdef *rd,
     if (tvec_nexttoken(tv)) break;
     ch = getc(tv->fp);
       if (ch != '|') { tvec_syntax(tv, ch, "`|'"); rc = -1; goto end; }
-      if (tvec_nexttoken(tv))
-      { tvec_syntax(tv, '\n', "flag name or integer"); rc = -1; goto end; }
+      if (tvec_nexttoken(tv)) {
+       tvec_syntax(tv, '\n', "%s flag name or integer", fi->name);
+       rc = -1; goto end;
+      }
   }
 
   /* Done. */
@@ -2625,6 +2643,8 @@ static void dump_flags(const union tvec_regval *rv,
   unsigned long m = ~0ul, v = rv->u;
   const char *sep;
 
+  if (style&TVSF_RAW) gprintf(gops, go, "flags/%s:", fi->name);
+
   for (f = fi->fv, sep = ""; f->tag; f++)
     if ((m&f->m) && (v&f->m) == f->v) {
       gprintf(gops, go, "%s%s", sep, f->tag); m &= ~f->m;
@@ -2633,7 +2653,7 @@ static void dump_flags(const union tvec_regval *rv,
 
   if (v&m) gprintf(gops, go, "%s0x%0*lx", sep, hex_width(v), v&m);
 
-  if (m != ~0ul && !(style&TVSF_COMPACT))
+  if (m != ~0ul && !(style&(TVSF_COMPACT | TVSF_RAW)))
     gprintf(gops, go, " ; = 0x%0*lx", hex_width(rv->u), rv->u);
 }
 
@@ -2914,35 +2934,45 @@ static void dump_char(const union tvec_regval *rv,
   unsigned f = 0;
 #define f_semi 1u
 
-  /* Print a character name if we can find one. */
-  p = find_charname(rv->i, (style&TVSF_COMPACT) ? CTF_SHORT : CTF_PREFER);
-  if (p) {
-    gprintf(gops, go, "%s", p);
-    if (style&TVSF_COMPACT) return;
-    else { gprintf(gops, go, " ;"); f |= f_semi; }
-  }
+  if (style&TVSF_RAW) {
+    /* Print the raw character unconditionally in single quotes. */
 
-  /* If the character isn't @EOF@ then print it as a single-quoted thing.
-   * In compact style, see if we can omit the quotes.
-   */
-  if (rv->i >= 0) {
-    if (f&f_semi) gprintf(gops, go, " = ");
-    switch (rv->i) {
-      case ' ': case '\\': case '\'': quote:
-       format_char(gops, go, rv->i);
-       break;
-      default:
-       if (!(style&TVSF_COMPACT) || !isprint(rv->i)) goto quote;
-       gprintf(gops, go, "%c", (int)rv->i);
-       return;
+    gprintf(gops, go, "char:'");
+    format_char(gops, go, rv->i);
+    gprintf(gops, go, "'");
+  } else {
+    /* Print ina pleasant human-readable way. */
+
+    /* Print a character name if we can find one. */
+    p = find_charname(rv->i, (style&TVSF_COMPACT) ? CTF_SHORT : CTF_PREFER);
+    if (p) {
+      gprintf(gops, go, "%s", p);
+      if (style&TVSF_COMPACT) return;
+      else { gprintf(gops, go, " ;"); f |= f_semi; }
     }
-  }
 
-  /* And the character code as an integer. */
-  if (!(style&TVSF_COMPACT)) {
-    if (!(f&f_semi)) gprintf(gops, go, " ;");
-    gprintf(gops, go, " = %ld = ", rv->i);
-    format_signed_hex(gops, go, rv->i);
+    /* If the character isn't @EOF@ then print it as a single-quoted thing.
+     * In compact style, see if we can omit the quotes.
+     */
+    if (rv->i >= 0) {
+      if (f&f_semi) gprintf(gops, go, " = ");
+      switch (rv->i) {
+       case ' ': case '\\': case '\'': quote:
+         format_char(gops, go, rv->i);
+         break;
+       default:
+         if (!(style&TVSF_COMPACT) || !isprint(rv->i)) goto quote;
+         gprintf(gops, go, "%c", (int)rv->i);
+         return;
+      }
+    }
+
+    /* And the character code as an integer. */
+    if (!(style&TVSF_COMPACT)) {
+      if (!(f&f_semi)) gprintf(gops, go, " ;");
+      gprintf(gops, go, " = %ld = ", rv->i);
+      format_signed_hex(gops, go, rv->i);
+    }
   }
 
 #undef f_semi
@@ -3220,9 +3250,19 @@ static int parse_bytes(union tvec_regval *rv, const struct tvec_regdef *rd,
  *             groups of hex digits, with comments showing the offset (if
  *             the string is long enough) and the corresponding plain text.
  *
- *             Empty strings are dumped as %|""|%.
+ *             Empty strings are dumped as %|#empty|%.
  */
 
+static void dump_empty(const char *ty, unsigned style,
+                      const struct gprintf_ops *gops, void *go)
+{
+  if (style&TVSF_RAW) gprintf(gops, go, "%s:", ty);
+  if (!(style&TVSF_COMPACT)) gprintf(gops, go, "#empty");
+  if (!(style&(TVSF_COMPACT | TVSF_RAW))) gprintf(gops, go, " ; = ");
+  if (!(style&TVSF_RAW)) gprintf(gops, go, "\"\"");
+}
+
+
 static void dump_text(const union tvec_regval *rv,
                      const struct tvec_regdef *rd,
                      unsigned style,
@@ -3233,9 +3273,12 @@ static void dump_text(const union tvec_regval *rv,
 #define f_nonword 1u
 #define f_newline 2u
 
-  if (!rv->text.sz) { gprintf(gops, go, "\"\""); return; }
+  if (!rv->text.sz) { dump_empty("text", style, gops, go); return; }
 
   p = (const unsigned char *)rv->text.p; l = p + rv->text.sz;
+  if (style&TVSF_RAW) { gprintf(gops, go, "text:"); goto quote; }
+  else if (style&TVSF_COMPACT) goto quote;
+
   switch (*p) {
     case '!': case '#': case ';': case '"': case '\'':
     case '(': case '{': case '[': case ']': case '}': case ')':
@@ -3243,7 +3286,7 @@ static void dump_text(const union tvec_regval *rv,
   }
   for (q = p; q < l; q++)
     if (*q == '\n' && q != l - 1) f |= f_newline;
-    else if (!*q || !isgraph(*q) || *q == '\\') f |= f_nonword;
+    else if (!*q || !ISGRAPH(*q) || *q == '\\') f |= f_nonword;
   if (f&f_newline) { gprintf(gops, go, "\n\t"); goto quote; }
   else if (f&f_nonword) goto quote;
 
@@ -3253,7 +3296,7 @@ static void dump_text(const union tvec_regval *rv,
 quote:
   gprintf(gops, go, "\"");
   for (q = p; q < l; q++)
-    if (!isprint(*q) || *q == '"') {
+    if (!ISPRINT(*q) || *q == '"') {
       if (p < q) gops->putm(go, (const char *)p, q - p);
       if (*q != '\n' || (style&TVSF_COMPACT))
        format_charesc(gops, go, *q, FCF_BRACE);
@@ -3280,12 +3323,9 @@ static void dump_bytes(const union tvec_regval *rv,
   unsigned i, n;
   int wd;
 
-  if (!sz) {
-    gprintf(gops, go, style&TVSF_COMPACT ? "\"\"" : "\"\" ; empty");
-    return;
-  }
+  if (!rv->text.sz) { dump_empty("bytes", style, gops, go); return; }
 
-  if (style&TVSF_COMPACT) {
+  if (style&(TVSF_COMPACT | TVSF_RAW)) {
     while (p < l) gprintf(gops, go, "%02x", *p++);
     return;
   }
@@ -3635,10 +3675,10 @@ static void dump_buffer(const union tvec_regval *rv,
 {
   format_size(gops, go, rv->buf.sz, style);
   if (rv->buf.m) {
-    gprintf(gops, go, style&TVSF_COMPACT ? "@" : " @ ");
+    gprintf(gops, go, style&(TVSF_COMPACT | TVSF_RAW) ? "@" : " @ ");
     format_size(gops, go, rv->buf.m, style);
     if (rv->buf.a) {
-      gprintf(gops, go, style&TVSF_COMPACT ? "+" : " + ");
+      gprintf(gops, go, style&(TVSF_COMPACT | TVSF_RAW) ? "+" : " + ");
       format_size(gops, go, rv->buf.a, style);
     }
   }