Browse Source

repair broken ternary function in B-Source

failed if parameters were included in function.
fixed by finding nested parens in gettok_char()
h_vogt 14 years ago
parent
commit
4d0d0fafb9
  1. 4
      src/frontend/inp.c
  2. 14
      src/frontend/inpcom.c
  3. 2
      src/include/ngspice/stringutil.h
  4. 37
      src/misc/string.c

4
src/frontend/inp.c

@ -555,8 +555,8 @@ inp_spsource(FILE *fp, bool comfile, char *filename)
*s='*'; *s='*';
s = dd->li_line + 8; s = dd->li_line + 8;
while ( isspace(*s) ) s++; 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); cstoken[2]=gettok(&s);
for (i=0; i<3;i++) { for (i=0; i<3;i++) {
wl_append_word(&wlist, &wl, cstoken[i]); wl_append_word(&wlist, &wl, cstoken[i]);

14
src/frontend/inpcom.c

@ -4032,8 +4032,8 @@ static void inp_compat(struct line *deck)
card->li_linenum_orig, card->li_line); card->li_linenum_orig, card->li_line);
controlled_exit(EXIT_BAD); 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)) { if ((!expression) || (!str_ptr)) {
fprintf(stderr, "Error: bad sytax in line %d\n %s\n", fprintf(stderr, "Error: bad sytax in line %d\n %s\n",
card->li_linenum_orig, card->li_line); 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); card->li_linenum_orig, card->li_line);
controlled_exit(EXIT_BAD); 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)) { if ((!expression) || (!str_ptr)) {
fprintf(stderr, "Error: bad sytax in line %d\n %s\n", fprintf(stderr, "Error: bad sytax in line %d\n %s\n",
card->li_linenum_orig, card->li_line); 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); fprintf(stderr,"ERROR: mal formed R line: %s\n", curr_line);
controlled_exit(EXIT_FAILURE); controlled_exit(EXIT_FAILURE);
} }
equation = gettok_char(&str_ptr, '}', TRUE);
equation = gettok_char(&str_ptr, '}', TRUE, TRUE);
str_ptr = strstr(cut_line, "tc1"); str_ptr = strstr(cut_line, "tc1");
if (str_ptr) { if (str_ptr) {
/* We need to have 'tc1=something */ /* 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); fprintf(stderr,"ERROR: mal formed C line: %s\n",curr_line);
controlled_exit(EXIT_FAILURE); controlled_exit(EXIT_FAILURE);
} }
equation = gettok_char(&str_ptr, '}', TRUE);
equation = gettok_char(&str_ptr, '}', TRUE, TRUE);
str_ptr = strstr(cut_line, "tc1"); str_ptr = strstr(cut_line, "tc1");
if (str_ptr) { if (str_ptr) {
/* We need to have 'tc1=something */ /* 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); fprintf(stderr,"ERROR: mal formed L line: %s\n", curr_line);
controlled_exit(EXIT_FAILURE); controlled_exit(EXIT_FAILURE);
} }
equation = gettok_char(&str_ptr, '}', TRUE);
equation = gettok_char(&str_ptr, '}', TRUE, TRUE);
str_ptr = strstr(cut_line, "tc1"); str_ptr = strstr(cut_line, "tc1");
if (str_ptr) { if (str_ptr) {
/* We need to have 'tc1=something */ /* We need to have 'tc1=something */

2
src/include/ngspice/stringutil.h

@ -22,7 +22,7 @@ void strtoupper(char *str);
char * stripWhiteSpacesInsideParens(char *str); char * stripWhiteSpacesInsideParens(char *str);
char * gettok(char **s); char * gettok(char **s);
char * gettok_instance(char **); 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 #ifdef CIDER

37
src/misc/string.c

@ -362,10 +362,12 @@ gettok_instance(char **s)
/* get the next token starting at next non white spice, stopping /* get the next token starting at next non white spice, stopping
at p, if inc_p is true, then including p, else excluding p, 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 * char *
gettok_char(char **s, char p, bool inc_p)
gettok_char(char **s, char p, bool inc_p, bool nested)
{ {
char c; char c;
char *token ; /* return token */ 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 */ return (NULL); /* return NULL if we come to end of line */
spice_dstring_init(&buf) ; 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') if (c == '\0')
/* p not found */ /* p not found */
return (NULL); return (NULL);
if (inc_p) if (inc_p)
/* add p */
spice_dstring_append_char( &buf, *(*s)++ ) ; spice_dstring_append_char( &buf, *(*s)++ ) ;
/* Now iterate up to next non-whitespace char */ /* Now iterate up to next non-whitespace char */

Loading…
Cancel
Save