redo r2216 change in a different way to avoid regressions

This commit is contained in:
Stefan Frederik 2022-08-18 01:45:02 +02:00
parent 15d8b8f1c8
commit a3828e6641
3 changed files with 67 additions and 9 deletions

View File

@ -1263,6 +1263,54 @@ void change_elem_order(void)
}
}
/* You must free the result if result is non-NULL. */
char *str_replace(char *orig, char *rep, char *with)
{
char *result; /* the return string */
char *ins; /* the next insert point */
char *tmp; /* varies */
size_t len_rep; /* length of rep (the string to remove) */
size_t len_with; /* length of with (the string to replace rep with) */
size_t len_front; /* distance between rep and end of last rep */
int count; /* number of replacements */
/* sanity checks and initialization */
if (!orig || !rep)
return NULL;
len_rep = strlen(rep);
if (len_rep == 0)
return NULL; /* empty rep causes infinite loop during count */
if (!with)
with = "";
len_with = strlen(with);
/* count the number of replacements needed */
ins = orig;
for (count = 0; (tmp = strstr(ins, rep)); ++count) {
ins = tmp + len_rep;
}
tmp = result = my_malloc(1244, strlen(orig) + (len_with - len_rep) * count + 1);
if (!result)
return NULL;
/* first time through the loop, all the variable are set correctly */
/* from here on, */
/* tmp points to the end of the result string */
/* ins points to the next occurrence of rep in orig */
/* orig points to the remainder of orig after "end of rep" */
while (count--) {
ins = strstr(orig, rep);
len_front = ins - orig;
tmp = strncpy(tmp, orig, len_front) + len_front;
tmp = strcpy(tmp, with) + len_with;
orig += len_front + len_rep; /* move to next "end of rep" */
}
strcpy(tmp, orig);
return result;
}
/* x=0 use tcl text widget x=1 use vim editor x=2 only view data */
void edit_property(int x)
{

View File

@ -123,14 +123,17 @@ const char *tcl_hook2(char **res)
{
static char *result = NULL;
static const char *empty="";
char *unescaped_res;
if(res == NULL || *res == NULL) {
my_free(1285, &result);
return empty;
}
if(strstr(*res, "tcleval(") == *res) {
tclvareval("tclpropeval2 {", *res, "}" , NULL);
unescaped_res = str_replace(*res, "\\}", "}");
tclvareval("tclpropeval2 {", unescaped_res, "}" , NULL);
my_strdup2(1286, &result, tclresult());
my_free(1245, &unescaped_res);
return result;
} else {
return *res;
@ -508,12 +511,14 @@ const char *get_sym_template(char *s,char *extra)
/* 2: eat backslashes */
/* 3: 1+2 :) */
dbg(1, "get_sym_template(): s=%s, extra=%s\n", s, extra);
if(s==NULL) {
my_free(978, &result);
return "";
}
l = strlen(s);
STR_ALLOC(&result, l+1, &sizeres);
result[0] = '\0';
sizetok = sizeval = CADCHUNKALLOC;
my_realloc(438, &value,sizeval);
my_realloc(439, &token,sizetok);
@ -1610,14 +1615,17 @@ void print_spice_subckt(FILE *fd, int symbol)
int i=0, multip;
const char *str_ptr=NULL;
register int c, state=TOK_BEGIN, space;
char *format=NULL,*s, *token=NULL;
char *format=NULL, *format1 = NULL, *s, *token=NULL;
int pin_number;
size_t sizetok=0;
size_t token_pos=0;
int escape=0;
int no_of_pins=0;
my_strdup(103, &format, get_tok_value(xctx->sym[symbol].prop_ptr,"format",2));
my_strdup(103, &format1, get_tok_value(xctx->sym[symbol].prop_ptr,"format",2));
dbg(1, "print_spice_subckt(): format1=%s\n", format1);
my_strdup(455, &format, tcl_hook2(&format1));
dbg(1, "print_spice_subckt(): format=%s\n", format);
if( format==NULL ) {
my_free(1012, &format);
return; /* no format */
@ -1680,7 +1688,7 @@ void print_spice_subckt(FILE *fd, int symbol)
if(!strcmp(get_tok_value(prop, "name",0), token + 2)) break;
}
if(i<no_of_pins && strcmp(get_tok_value(prop,"spice_ignore",0), "true")) {
fprintf(fd, "%s ", expandlabel(token+2, &multip));
fprintf(fd, "%s", expandlabel(token+2, &multip));
}
}
/* reference by pin number instead of pin name, allows faster lookup of the attached net name 20180911 */
@ -1712,6 +1720,7 @@ void print_spice_subckt(FILE *fd, int symbol)
break ;
}
}
my_free(1072, &format1);
my_free(1013, &format);
my_free(1014, &token);
}
@ -1724,7 +1733,7 @@ int print_spice_element(FILE *fd, int inst)
register int c, state=TOK_BEGIN, space;
char *template=NULL,*format=NULL,*s, *name=NULL, *token=NULL;
const char *lab, *value = NULL;
char *translatedvalue = NULL;
/* char *translatedvalue = NULL; */
int pin_number;
size_t sizetok=0;
size_t token_pos=0;
@ -1988,9 +1997,9 @@ int print_spice_element(FILE *fd, int inst)
/* do one level of substitutions to resolve @params and equations*/
if(result && strstr(result, "tcleval(")== result) {
dbg(1, "print_spice_element(): before translate()result=%s\n", result);
dbg(1, "print_spice_element(): before translate() result=%s\n", result);
my_strdup(22, &result, translate(inst, result));
dbg(1, "print_spice_element(): after translate()result=%s\n", result);
dbg(1, "print_spice_element(): after translate() result=%s\n", result);
}
@ -2012,14 +2021,14 @@ int print_spice_element(FILE *fd, int inst)
#endif
fprintf(fd, "%s", result);
if(result) fprintf(fd, "%s", result);
my_free(1019, &template);
my_free(1020, &format);
my_free(1021, &name);
my_free(1022, &token);
my_free(1194, &result);
my_free(298, &spiceprefixtag);
my_free(455, &translatedvalue);
/* my_free(455, &translatedvalue); */
return 1;
}

View File

@ -1374,6 +1374,7 @@ extern void hilight_parent_pins(void);
extern void hilight_net_pin_mismatches(void);
extern Node_hashentry **get_node_table_ptr(void);
extern void change_elem_order(void);
extern char *str_replace(char *orig, char *rep, char *with);
extern int set_different_token(char **s,const char *new, const char *old, int object, int n);
extern void print_hilight_net(int show);
extern void list_hilights(void);