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 */ /* x=0 use tcl text widget x=1 use vim editor x=2 only view data */
void edit_property(int x) void edit_property(int x)
{ {

View File

@ -123,14 +123,17 @@ const char *tcl_hook2(char **res)
{ {
static char *result = NULL; static char *result = NULL;
static const char *empty=""; static const char *empty="";
char *unescaped_res;
if(res == NULL || *res == NULL) { if(res == NULL || *res == NULL) {
my_free(1285, &result); my_free(1285, &result);
return empty; return empty;
} }
if(strstr(*res, "tcleval(") == *res) { 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_strdup2(1286, &result, tclresult());
my_free(1245, &unescaped_res);
return result; return result;
} else { } else {
return *res; return *res;
@ -508,12 +511,14 @@ const char *get_sym_template(char *s,char *extra)
/* 2: eat backslashes */ /* 2: eat backslashes */
/* 3: 1+2 :) */ /* 3: 1+2 :) */
dbg(1, "get_sym_template(): s=%s, extra=%s\n", s, extra);
if(s==NULL) { if(s==NULL) {
my_free(978, &result); my_free(978, &result);
return ""; return "";
} }
l = strlen(s); l = strlen(s);
STR_ALLOC(&result, l+1, &sizeres); STR_ALLOC(&result, l+1, &sizeres);
result[0] = '\0';
sizetok = sizeval = CADCHUNKALLOC; sizetok = sizeval = CADCHUNKALLOC;
my_realloc(438, &value,sizeval); my_realloc(438, &value,sizeval);
my_realloc(439, &token,sizetok); my_realloc(439, &token,sizetok);
@ -1610,14 +1615,17 @@ void print_spice_subckt(FILE *fd, int symbol)
int i=0, multip; int i=0, multip;
const char *str_ptr=NULL; const char *str_ptr=NULL;
register int c, state=TOK_BEGIN, space; register int c, state=TOK_BEGIN, space;
char *format=NULL,*s, *token=NULL; char *format=NULL, *format1 = NULL, *s, *token=NULL;
int pin_number; int pin_number;
size_t sizetok=0; size_t sizetok=0;
size_t token_pos=0; size_t token_pos=0;
int escape=0; int escape=0;
int no_of_pins=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 ) { if( format==NULL ) {
my_free(1012, &format); my_free(1012, &format);
return; /* no format */ return; /* no format */
@ -1712,6 +1720,7 @@ void print_spice_subckt(FILE *fd, int symbol)
break ; break ;
} }
} }
my_free(1072, &format1);
my_free(1013, &format); my_free(1013, &format);
my_free(1014, &token); my_free(1014, &token);
} }
@ -1724,7 +1733,7 @@ int print_spice_element(FILE *fd, int inst)
register int c, state=TOK_BEGIN, space; register int c, state=TOK_BEGIN, space;
char *template=NULL,*format=NULL,*s, *name=NULL, *token=NULL; char *template=NULL,*format=NULL,*s, *name=NULL, *token=NULL;
const char *lab, *value = NULL; const char *lab, *value = NULL;
char *translatedvalue = NULL; /* char *translatedvalue = NULL; */
int pin_number; int pin_number;
size_t sizetok=0; size_t sizetok=0;
size_t token_pos=0; size_t token_pos=0;
@ -2012,14 +2021,14 @@ int print_spice_element(FILE *fd, int inst)
#endif #endif
fprintf(fd, "%s", result); if(result) fprintf(fd, "%s", result);
my_free(1019, &template); my_free(1019, &template);
my_free(1020, &format); my_free(1020, &format);
my_free(1021, &name); my_free(1021, &name);
my_free(1022, &token); my_free(1022, &token);
my_free(1194, &result); my_free(1194, &result);
my_free(298, &spiceprefixtag); my_free(298, &spiceprefixtag);
my_free(455, &translatedvalue); /* my_free(455, &translatedvalue); */
return 1; return 1;
} }

View File

@ -1374,6 +1374,7 @@ extern void hilight_parent_pins(void);
extern void hilight_net_pin_mismatches(void); extern void hilight_net_pin_mismatches(void);
extern Node_hashentry **get_node_table_ptr(void); extern Node_hashentry **get_node_table_ptr(void);
extern void change_elem_order(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 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 print_hilight_net(int show);
extern void list_hilights(void); extern void list_hilights(void);