From d6e0607577ac3d487bf8611185f66d6085b68756 Mon Sep 17 00:00:00 2001 From: mhx Date: Sat, 31 Dec 2016 17:02:06 +0100 Subject: [PATCH] Added a trailing type character to nupa's numparm__________xxxxxxxt inserts. With this, it is now possible to replace not only the standard {xxxx} with a double, but also {%xxxx} with an integer and {$xxxx} with a boolean. Example application: replace node names with .param equates. --- src/frontend/numparam/numparam.h | 2 ++ src/frontend/numparam/spicenum.c | 12 +++++++++--- src/frontend/numparam/xpressn.c | 33 ++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/frontend/numparam/numparam.h b/src/frontend/numparam/numparam.h index 9ce703026..57c07b0ff 100644 --- a/src/frontend/numparam/numparam.h +++ b/src/frontend/numparam/numparam.h @@ -65,3 +65,5 @@ dico_t *nupa_fetchinstance(void); char getidtype(dico_t *, char *s); entry_t *attrib(dico_t *, NGHASHPTR htable, char *t, char op, struct nscope *level); void del_attrib(void *); + +#define ACT_CHARACTS 24 /* actual string length to be inserted and replaced */ diff --git a/src/frontend/numparam/spicenum.c b/src/frontend/numparam/spicenum.c index ed37d4429..c38466c76 100644 --- a/src/frontend/numparam/spicenum.c +++ b/src/frontend/numparam/spicenum.c @@ -105,6 +105,7 @@ stripbraces(SPICE_DSTRINGPTR dstr_p) char *s; /* value of dynamic string */ char *t_p; /* value of t dynamic string */ SPICE_DSTRING tstr; /* temporary dynamic string */ + char type; n = 0; spice_dstring_init(&tstr); @@ -117,7 +118,12 @@ stripbraces(SPICE_DSTRINGPTR dstr_p) if (s[i] == '{') { /* something to strip */ - j = i + 1; + switch (s[i + 1]) { + case '%': type = '%'; break; + case '$': type = '$'; break; + default: type = '0'; + } + j = i + 1; nest = 1; n++; @@ -139,8 +145,8 @@ stripbraces(SPICE_DSTRINGPTR dstr_p) cadd(&tstr, ' '); { - char buf[25+1]; - sprintf(buf, "numparm__________%08lx", placeholder); + char buf[ACT_CHARACTS + 1]; + sprintf(buf, "numparm__________%07lx%c", placeholder, type); /* mhx */ sadd(&tstr, buf); } cadd(&tstr, ' '); diff --git a/src/frontend/numparam/xpressn.c b/src/frontend/numparam/xpressn.c index 876a86f8a..bb0f003ca 100644 --- a/src/frontend/numparam/xpressn.c +++ b/src/frontend/numparam/xpressn.c @@ -23,7 +23,6 @@ extern char *nupa_inst_name; /* see spicenum.c */ extern long dynsubst; /* see inpcom.c */ -#define ACT_CHARACTS 25 /* actual string length to be inserted and replaced */ #define S_init 0 #define S_atom 1 @@ -1142,11 +1141,16 @@ evaluate(dico_t *dico, SPICE_DSTRINGPTR qstr_p, char *t, unsigned char mode) entry_t *entry; bool numeric, done, nolookup; bool err; + char type = '0'; spice_dstring_reinit(qstr_p); numeric = 0; err = 0; + if (t[0] == '%') { type = '%'; t++; } + else if (t[0] == '$') { type = '$'; t++; } + else type = '0'; + if (mode == 1) { /* string? */ stupcase(t); @@ -1205,8 +1209,16 @@ evaluate(dico_t *dico, SPICE_DSTRINGPTR qstr_p, char *t, unsigned char mode) */ char buf[ACT_CHARACTS + 1]; - if (snprintf(buf, sizeof(buf), "% 25.17e", u) != ACT_CHARACTS) { - fprintf(stderr, "ERROR: xpressn.c, %s(%d)\n", __FUNCTION__, __LINE__); + bool errs = NO; + switch (type) { + case '0': errs = (snprintf(buf, sizeof(buf), "% *.17e", ACT_CHARACTS, u) != ACT_CHARACTS); break; + case '$': errs = (snprintf(buf, sizeof(buf), "% *s", ACT_CHARACTS, u ? "yes" : "no") != ACT_CHARACTS); break; + case '%': errs = (snprintf(buf, sizeof(buf), "% *d", ACT_CHARACTS, (int)u) != ACT_CHARACTS); break; + default : errs = YES; + } + if (errs) { + fprintf(cp_err, "ERROR: xpressn.c, %s(%d)\n", __FUNCTION__, __LINE__); + fprintf(cp_err, "Expression under consideration = `%s', value to be substituted = %g\n", t, u); controlled_exit(1); } scopys(qstr_p, buf); @@ -1227,18 +1239,15 @@ insertnumber(dico_t *dico, int i, char *s, SPICE_DSTRINGPTR ustr_p) char buf[ACT_CHARACTS+1]; long id = 0; - int n = 0; + int n = 0, res = 0; char *p = strstr(s+i, "numparm__________"); - if (p && - (1 == sscanf(p, "numparm__________%8lx%n", &id, &n)) && - (n == ACT_CHARACTS) && - (id > 0) && (id < dynsubst + 1) && - (snprintf(buf, sizeof(buf), "%-25s", u) == ACT_CHARACTS)) - { - memcpy(p, buf, ACT_CHARACTS); - return (int)(p - s) + ACT_CHARACTS; + if (p) res = sscanf(p, "numparm__________%7lx%n", &id, &n); + if (p && (res == 1) && (n == ACT_CHARACTS) && (id > 0) && (id < dynsubst + 1) && + (snprintf(buf, sizeof(buf), "%-*s", ACT_CHARACTS, u) == ACT_CHARACTS)) { + memcpy(p, buf, ACT_CHARACTS); *(p + ACT_CHARACTS) = ' '; + return (int)(p - s) + ACT_CHARACTS; } message