diff --git a/src/frontend/inpcom.c b/src/frontend/inpcom.c index 7fe96e13f..7d02903f1 100644 --- a/src/frontend/inpcom.c +++ b/src/frontend/inpcom.c @@ -5617,15 +5617,16 @@ inp_temper_compat(struct card *card) modified_exp = inp_modify_exp(exp_str); tfree(exp_str); /* add the intermediate string between previous and next expression to the new line */ - new_str = INPstrCat(new_str, copy_substring(beg_str, end_str), " "); + new_str = INPstrCat(new_str, ' ', + copy_substring(beg_str, end_str)); /* add the modified expression string to the new line */ - new_str = INPstrCat(new_str, modified_exp, " "); - new_str = INPstrCat(new_str, copy(" "), " "); + new_str = INPstrCat(new_str, ' ', modified_exp); + new_str = INPstrCat(new_str, ' ', copy(" ")); /* move on to the next intermediate string */ beg_str = beg_tstr = end_tstr; } if (*beg_str) - new_str = INPstrCat(new_str, copy(beg_str), " "); + new_str = INPstrCat(new_str, ' ', copy(beg_str)); tfree(card->line); card->line = inp_remove_ws(new_str); } @@ -6230,7 +6231,7 @@ inp_fix_temper_in_param(struct card *deck) } /* restore first part of the line */ - new_str = INPstrCat(firsttok_str, new_str, " "); + new_str = INPstrCat(firsttok_str, ' ', new_str); new_str = inp_remove_ws(new_str); /* if we have inserted into a .param line, convert to .func */ @@ -6448,7 +6449,7 @@ inp_fix_agauss_in_param(struct card *deck, char *fcn) } /* restore first part of the line */ - new_str = INPstrCat(firsttok_str, new_str, " "); + new_str = INPstrCat(firsttok_str, ' ', new_str); new_str = inp_remove_ws(new_str); *card->line = '*'; diff --git a/src/include/ngspice/inpdefs.h b/src/include/ngspice/inpdefs.h index e5f7b0554..e46c82d3b 100644 --- a/src/include/ngspice/inpdefs.h +++ b/src/include/ngspice/inpdefs.h @@ -94,7 +94,7 @@ char *INPdevParse(char **, CKTcircuit *, int, GENinstance *, double *, int *, IN char *INPdomodel(CKTcircuit *, struct card *, INPtables *); void INPdoOpts(CKTcircuit *, JOB *, struct card *, INPtables *); char *INPerrCat(char *, char *); -char *INPstrCat(char *, char *, char *); +char *INPstrCat(char *, char, char *); char *INPerror(int); double INPevaluate(char **, int *, int); char *INPfindLev(char *, int *); diff --git a/src/spicelib/parser/inperrc.c b/src/spicelib/parser/inperrc.c index b56b51912..cb3077ffe 100644 --- a/src/spicelib/parser/inperrc.c +++ b/src/spicelib/parser/inperrc.c @@ -8,50 +8,73 @@ Author: 1985 Thomas L. Quarles a new string is malloced, they are combined, both input strings are freed, and the new string is returned. */ +#include +#include #include "ngspice/ngspice.h" -#include #include "ngspice/inpdefs.h" #include "inpxx.h" +static char *INPcat(size_t n_a, const char *a, char sep_char, + size_t n_b, const char *b); + +/* This function returns the non-null string a or b if only one of them + * is not null. Otherwise it returns NULL if both are null or + * '\n' if both are non-null. */ char *INPerrCat(char *a, char *b) { - if (a != NULL) { - if (b == NULL) { /* a valid, b null, return a */ - return (a); - } else { /* both valid - hard work... */ - register char *errtmp; - errtmp = - TMALLOC(char, strlen(a) + strlen(b) + 2); - (void) strcpy(errtmp, a); - (void) strcat(errtmp, "\n"); - (void) strcat(errtmp, b); - FREE(a); - FREE(b); - return (errtmp); - } - } else /* a null, so return b */ - return (b); -} + return INPstrCat(a, '\n', b); +} /* end of function INPerrCat */ + -char *INPstrCat(char *a, char *b, char *c) +/* This function returns the non-null string a or b if only one of them + * is not null. Otherwise it returns NULL if both are null or + * if both are non-null. */ +char *INPstrCat(char *a, char sepchar, char *b) { if (a != NULL) { - if (b == NULL) { /* a valid, b null, return a */ - return (a); - } else { /* both valid - hard work... */ - register char *strtmp; - strtmp = - TMALLOC(char, strlen(a) + strlen(b) + 2); - (void) strcpy(strtmp, a); - (void) strcat(strtmp, c); /* single character only! */ - (void) strcat(strtmp, b); - FREE(a); - FREE(b); - return (strtmp); + if (b == NULL) { /* a valid, b null, return a */ + return a; } - } else /* a null, so return b */ - return (b); -} + else { /* both valid - hard work... */ + char *a_ch_b = INPcat(strlen(a), a, sepchar, + strlen(b), b); + txfree(a); + txfree(b); + return a_ch_b; + } + } + else { /* a null, so return b */ + return b; + } +} /* end of function INPstrCat */ + + + +/* This function concatenates strings a and b with sep_char added + * between them. Strings a and b need not be null-terminated. */ +static char *INPcat(size_t n_a, const char *a, char sepchar, + size_t n_b, const char *b) +{ + char *a_ch_b = TMALLOC(char, n_a + n_b + 2); + + /* Build string. Check not really requied since program exits + * if allocation in TMALLOC fails but would be if this behavior + * is changed. */ + if (a_ch_b != (char *) NULL) { + char *p_cur = a_ch_b; + (void) memcpy(p_cur, a, n_a); + p_cur += n_a; + *p_cur++ = sepchar; + (void) memcpy(p_cur, b, n_b); + p_cur += n_b; + *p_cur = '\0'; + } + + return a_ch_b; +} /* end of function INPcat */ + + +