From b6775d1c9b4e6db7e2ddff6fda1809bf978e7d5e Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 2 Feb 2014 10:13:41 +0100 Subject: [PATCH] nsc: Turn common patterns into CA_IS_ARRAY() and CA_ARRAY_LEN() Signed-off-by: Markus Armbruster --- include/nsc.h | 6 +++++- src/lib/common/ef_verify.c | 6 +++--- src/lib/common/xdump.c | 4 ++-- src/lib/common/xundump.c | 10 +++++----- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/include/nsc.h b/include/nsc.h index e2b4b33c7..3583c9ee8 100644 --- a/include/nsc.h +++ b/include/nsc.h @@ -235,7 +235,11 @@ struct castr { int ca_flags; }; -/* variables using the above */ +/* Is CA an array? */ +#define CA_IS_ARRAY(ca) ((ca)->ca_type != NSC_STRINGY && (ca)->ca_len != 0) + +/* If CA is an array, return its length, else zero */ +#define CA_ARRAY_LEN(ca) ((ca)->ca_type != NSC_STRINGY ? (ca)->ca_len : 0) extern struct castr ichr_ca[]; extern struct castr pchr_ca[]; diff --git a/src/lib/common/ef_verify.c b/src/lib/common/ef_verify.c index 17057c598..2345efdb7 100644 --- a/src/lib/common/ef_verify.c +++ b/src/lib/common/ef_verify.c @@ -28,7 +28,7 @@ * * Known contributors to this file: * Ron Koenderink, 2005 - * Markus Armbruster, 2006-2013 + * Markus Armbruster, 2006-2014 */ #include @@ -56,7 +56,7 @@ verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...) ef_nameof(type), row); if (ca) { fprintf(stderr, " field %s", ca->ca_name); - if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0) + if (CA_IS_ARRAY(ca)) fprintf(stderr, "(%d)", idx); } fprintf(stderr, ": "); @@ -163,7 +163,7 @@ verify_row(int type, int row) for (i = 0; ca[i].ca_name; ++i) { if (ca[i].ca_get) continue; /* virtual */ - n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0; + n = CA_ARRAY_LEN(&ca[i]); j = 0; do { if (ca[i].ca_table == EF_BAD) diff --git a/src/lib/common/xdump.c b/src/lib/common/xdump.c index ff1ee6deb..5ed558447 100644 --- a/src/lib/common/xdump.c +++ b/src/lib/common/xdump.c @@ -244,7 +244,7 @@ xdflds(struct xdstr *xd, struct castr ca[], void *ptr) continue; if (ca[i].ca_flags & NSC_EXTRA) continue; - n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0; + n = CA_ARRAY_LEN(&ca[i]); j = 0; do { xdeval(&val, xd, &ca[i], ptr, j); @@ -287,7 +287,7 @@ xdcolhdr(struct xdstr *xd, struct castr ca[]) continue; if (ca[i].ca_flags & NSC_EXTRA) continue; - n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0; + n = CA_ARRAY_LEN(&ca[i]); if (n) { for (j = 0; j < n; j++) { xd->pr("%s%s(%d)", sep, ca[i].ca_name, j); diff --git a/src/lib/common/xundump.c b/src/lib/common/xundump.c index 33b726804..1b89308ed 100644 --- a/src/lib/common/xundump.c +++ b/src/lib/common/xundump.c @@ -436,7 +436,7 @@ xufld(FILE *fp, int i) case '\n': CANT_HAPPEN(i > nflds); if (i < nflds) { - if (fldca[i]->ca_type != NSC_STRINGY && fldca[i]->ca_len) + if (CA_IS_ARRAY(fldca[i])) return gripe("Field %s(%d) missing", fldca[i]->ca_name, fldidx[i]); return gripe("Field %s missing", fldca[i]->ca_name); @@ -534,7 +534,7 @@ deffld(int fldno, char *name, int idx) res == M_NOTUNIQUE ? "ambiguous" : "unknown"); if ((ca[res].ca_flags & NSC_EXTRA) || CANT_HAPPEN(ca[res].ca_get)) return gripe("Extraneous header %s in field %d", name, fldno + 1); - if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) { + if (CA_IS_ARRAY(&ca[res])) { if (idx < 0) return gripe("Header %s requires an index in field %d", ca[res].ca_name, fldno + 1); @@ -614,7 +614,7 @@ chkflds(void) cafldsmax = MAX(caflds[i], cafldspp[i]); if (ca[i].ca_flags & NSC_EXTRA) continue; - len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0; + len = CA_ARRAY_LEN(&ca[i]); if (!len && !cafldsmax) res = gripe("Header field %s missing", ca[i].ca_name); else if (len && cafldsmax == len - 1) @@ -1012,7 +1012,7 @@ xufldhdr(FILE *fp, struct castr ca[]) for (i = 0; ca[i].ca_name; i++) { if ((ca[i].ca_flags & NSC_EXTRA)) continue; - n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0; + n = CA_ARRAY_LEN(&ca[i]); j = 0; do { *fca++ = &ca[i]; @@ -1094,7 +1094,7 @@ xundump(FILE *fp, char *file, int *plno, int expected_table) for (i = 0; ca[i].ca_name; i++) { nca++; if (!(ca[i].ca_flags & NSC_EXTRA)) { - nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0); + nf += MAX(1, CA_ARRAY_LEN(&ca[i])); if (ca[i].ca_flags & NSC_CONST) may_omit_id = may_trunc = 0; } -- 2.43.0