diff --git a/src/frontend/inp.c b/src/frontend/inp.c index f9adf3fde..917695d7f 100644 --- a/src/frontend/inp.c +++ b/src/frontend/inp.c @@ -555,8 +555,8 @@ inp_spsource(FILE *fp, bool comfile, char *filename) *s='*'; s = dd->li_line + 8; while ( isspace(*s) ) s++; - cstoken[0]=gettok_char(&s, '=', FALSE); - cstoken[1]=gettok_char(&s, '=', TRUE); + cstoken[0]=gettok_char(&s, '=', FALSE, FALSE); + cstoken[1]=gettok_char(&s, '=', TRUE, FALSE); cstoken[2]=gettok(&s); for (i=0; i<3;i++) { wl_append_word(&wlist, &wl, cstoken[i]); diff --git a/src/frontend/inpcom.c b/src/frontend/inpcom.c index c192dc408..7277c7882 100644 --- a/src/frontend/inpcom.c +++ b/src/frontend/inpcom.c @@ -4032,8 +4032,8 @@ static void inp_compat(struct line *deck) card->li_linenum_orig, card->li_line); controlled_exit(EXIT_BAD); } - str_ptr = gettok_char(&cut_line, '{', FALSE); - expression = gettok_char(&cut_line, '}', TRUE); /* expression */ + str_ptr = gettok_char(&cut_line, '{', FALSE, FALSE); + expression = gettok_char(&cut_line, '}', TRUE, TRUE); /* expression */ if ((!expression) || (!str_ptr)) { fprintf(stderr, "Error: bad sytax in line %d\n %s\n", card->li_linenum_orig, card->li_line); @@ -4230,8 +4230,8 @@ static void inp_compat(struct line *deck) card->li_linenum_orig, card->li_line); controlled_exit(EXIT_BAD); } - str_ptr = gettok_char(&cut_line, '{', FALSE); - expression = gettok_char(&cut_line, '}', TRUE); /* expression */ + str_ptr = gettok_char(&cut_line, '{', FALSE, FALSE); + expression = gettok_char(&cut_line, '}', TRUE, TRUE); /* expression */ if ((!expression) || (!str_ptr)) { fprintf(stderr, "Error: bad sytax in line %d\n %s\n", card->li_linenum_orig, card->li_line); @@ -4413,7 +4413,7 @@ static void inp_compat(struct line *deck) fprintf(stderr,"ERROR: mal formed R line: %s\n", curr_line); controlled_exit(EXIT_FAILURE); } - equation = gettok_char(&str_ptr, '}', TRUE); + equation = gettok_char(&str_ptr, '}', TRUE, TRUE); str_ptr = strstr(cut_line, "tc1"); if (str_ptr) { /* We need to have 'tc1=something */ @@ -4505,7 +4505,7 @@ static void inp_compat(struct line *deck) fprintf(stderr,"ERROR: mal formed C line: %s\n",curr_line); controlled_exit(EXIT_FAILURE); } - equation = gettok_char(&str_ptr, '}', TRUE); + equation = gettok_char(&str_ptr, '}', TRUE, TRUE); str_ptr = strstr(cut_line, "tc1"); if (str_ptr) { /* We need to have 'tc1=something */ @@ -4618,7 +4618,7 @@ static void inp_compat(struct line *deck) fprintf(stderr,"ERROR: mal formed L line: %s\n", curr_line); controlled_exit(EXIT_FAILURE); } - equation = gettok_char(&str_ptr, '}', TRUE); + equation = gettok_char(&str_ptr, '}', TRUE, TRUE); str_ptr = strstr(cut_line, "tc1"); if (str_ptr) { /* We need to have 'tc1=something */ diff --git a/src/include/ngspice/stringutil.h b/src/include/ngspice/stringutil.h index f03e34721..de09438c5 100644 --- a/src/include/ngspice/stringutil.h +++ b/src/include/ngspice/stringutil.h @@ -22,7 +22,7 @@ void strtoupper(char *str); char * stripWhiteSpacesInsideParens(char *str); char * gettok(char **s); char * gettok_instance(char **); -char * gettok_char(char **s, char p, bool inc_p); +char * gettok_char(char **s, char p, bool inc_p, bool nested); #ifdef CIDER diff --git a/src/misc/string.c b/src/misc/string.c index 08249dd47..c2ead4e8c 100644 --- a/src/misc/string.c +++ b/src/misc/string.c @@ -362,10 +362,12 @@ gettok_instance(char **s) /* get the next token starting at next non white spice, stopping at p, if inc_p is true, then including p, else excluding p, - return NULL if p is not found + return NULL if p is not found. + If '}', ']' or ')' and nested is true, find corresponding p + */ char * -gettok_char(char **s, char p, bool inc_p) +gettok_char(char **s, char p, bool inc_p, bool nested) { char c; char *token ; /* return token */ @@ -378,15 +380,38 @@ gettok_char(char **s, char p, bool inc_p) return (NULL); /* return NULL if we come to end of line */ spice_dstring_init(&buf) ; - while ((c = **s) != '\0' && - ( **s != p ) - ) { - spice_dstring_append_char( &buf, *(*s)++ ) ; + if (nested && (( p == '}' ) || ( p == ')' ) || ( p == ']'))) { + char q; + int count = 0; + /* find opening bracket */ + if (( p == '}' ) || ( p == ']' )) q = p - 2; + else q = p - 1; + /* add string in front of q, excluding q */ + while ((c = **s) != '\0' && ( **s != q )) { + spice_dstring_append_char( &buf, *(*s)++ ) ; + } + /* return if nested bracket found, excluding its character */ + while ((c = **s) != '\0') { + if (c == q) count++; + else if (c == p) count--; + if (count == 0) { + break; + } + spice_dstring_append_char( &buf, *(*s)++ ) ; + } } + else + /* just look for p and return string, excluding p */ + while ((c = **s) != '\0' && ( **s != p )) { + spice_dstring_append_char( &buf, *(*s)++ ) ; + } + if (c == '\0') /* p not found */ return (NULL); + if (inc_p) + /* add p */ spice_dstring_append_char( &buf, *(*s)++ ) ; /* Now iterate up to next non-whitespace char */