translate3(): add eat_escapes parameter

This commit is contained in:
stefan schippers 2024-04-14 11:38:35 +02:00
parent 9c766c5260
commit 3d20b44ced
5 changed files with 19 additions and 13 deletions

View File

@ -1506,9 +1506,10 @@ C {verilog_timescale.sym} 1050 -100 0 0 {name=s1 timestep="1ns" precision="1ns"
Translate string 'str' replacing @xxx tokens with values in instance 'n' attributes
Example: xschem translate vref {the voltage is @value}
the voltage is 1.8 </pre>
<li><kbd> translate3 str s1 [s2]</kbd></li><pre>
<li><kbd> translate3 str eat_escapes s1 [s2] [s3]</kbd></li><pre>
Translate string 'str' replacing @xxx tokens with values in string s1 or if
not found in string s2
not found in string s2 or if not found in string s3
eat_escapes should be either 1 (remove backslashes) or 0 (keep them)
Example: xschem translate3 {the voltage is @value} {name=x12} {name=x1 value=1.8}
the voltage is 1.8 </pre>
<li><kbd> trim_chars str sep</kbd></li><pre>

View File

@ -737,7 +737,7 @@ void draw_symbol(int what,int c, int n,int layer,short tmp_flip, short rot,
#endif
my_strdup2(_ALLOC_ID_, &txtptr, translate(n, text.txt_ptr));
/* do another round of substitutions if some @var are found, but if not found leave @var as is */
my_strdup2(_ALLOC_ID_, &txtptr, translate3(txtptr, xctx->inst[n].prop_ptr,
my_strdup2(_ALLOC_ID_, &txtptr, translate3(txtptr, 0, xctx->inst[n].prop_ptr,
xctx->sym[xctx->inst[n].ptr].templ, NULL ));
dbg(1, "drawing string: str=%s prop=%s\n", txtptr, text.prop_ptr ? text.prop_ptr : "NULL");
draw_string(textlayer, what, txtptr,
@ -907,7 +907,7 @@ void draw_temp_symbol(int what, GC gc, int n,int layer,short tmp_flip, short rot
#endif
my_strdup2(_ALLOC_ID_, &txtptr, translate(n, text.txt_ptr));
/* do another round of substitutions if some @var are found, but if not found leave @var as is */
my_strdup2(_ALLOC_ID_, &txtptr, translate3(txtptr, xctx->inst[n].prop_ptr,
my_strdup2(_ALLOC_ID_, &txtptr, translate3(txtptr, 0, xctx->inst[n].prop_ptr,
xctx->sym[xctx->inst[n].ptr].templ, NULL ));
if(txtptr[0]) draw_temp_string(gc, what, txtptr,
(text.rot + ( (flip && (text.rot & 1) ) ? rot+2 : rot) ) & 0x3,

View File

@ -5588,18 +5588,21 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg
}
}
/* translate3 str s1 [s2]
/* translate3 str eat_escapes s1 [s2] [s3]
* Translate string 'str' replacing @xxx tokens with values in string s1 or if
* not found in string s2
* not found in string s2 or if not found in string s3
* eat_escapes should be either 1 (remove backslashes) or 0 (keep them)
* Example: xschem translate3 {the voltage is @value} {name=x12} {name=x1 value=1.8}
* the voltage is 1.8 */
else if(!strcmp(argv[1], "translate3") )
{
char *s = NULL;
int eat_escapes = 0;
if(!xctx) {Tcl_SetResult(interp, not_avail, TCL_STATIC); return TCL_ERROR;}
if(argc > 5) my_strdup2(_ALLOC_ID_, &s, translate3(argv[2], argv[3], argv[4], argv[5]));
if(argc > 4) my_strdup2(_ALLOC_ID_, &s, translate3(argv[2], argv[3], argv[4], NULL));
else if(argc > 3) my_strdup2(_ALLOC_ID_, &s, translate3(argv[2], argv[3], NULL, NULL));
if(argc > 3) eat_escapes = atoi(argv[3]);
if(argc > 6) my_strdup2(_ALLOC_ID_, &s, translate3(argv[2], eat_escapes, argv[4], argv[5], argv[6]));
else if(argc > 5) my_strdup2(_ALLOC_ID_, &s, translate3(argv[2], eat_escapes, argv[4], argv[5], NULL));
else if(argc > 4) my_strdup2(_ALLOC_ID_, &s, translate3(argv[2], eat_escapes, argv[4], NULL, NULL));
else {
Tcl_SetResult(interp, "xschem translate3: missing arguments", TCL_STATIC);
return TCL_ERROR;

View File

@ -2178,7 +2178,7 @@ int print_spice_element(FILE *fd, int inst)
parent_templ = xctx->hier_attr[xctx->currsch - 1].templ;
}
dbg(1, "print_spice_element(): before translate3(): value=%s\n", value);
value = translate3(val, xctx->inst[inst].prop_ptr, parent_prop_ptr, parent_templ);
value = translate3(val, 0, xctx->inst[inst].prop_ptr, parent_prop_ptr, parent_templ);
dbg(1, "print_spice_element(): after translate3(): value=%s\n", value);
}
tok_val_len = strlen(value);
@ -4339,7 +4339,7 @@ const char *translate2(Lcc *lcc, int level, char* s)
/* using s1, s2, s3 in turn to resolve @tokens */
/* if no definition for @token is found return @token as is in s */
/* if s==NULL return emty string */
const char *translate3(const char *s, const char *s1, const char *s2, const char *s3)
const char *translate3(const char *s, int eat_escapes, const char *s1, const char *s2, const char *s3)
{
static const char *empty="";
static char *translated_tok = NULL;
@ -4365,7 +4365,7 @@ const char *translate3(const char *s, const char *s1, const char *s2, const char
c=*s++;
if(c=='\\') {
escape=1;
/* c=*s++; */ /* translate3 should not eat backslashes, to mitigate the quoting hell */
if(eat_escapes) c=*s++;
}
else escape=0;
space=SPACE(c);
@ -4381,6 +4381,7 @@ const char *translate3(const char *s, const char *s1, const char *s2, const char
if(state==TOK_TOKEN) token[token_pos++]=(char)c;
else if(state==TOK_SEP) {
token[token_pos]='\0';
dbg(1, "translate3(): token=|%s|\n", token);
value = get_tok_value(s1, token+1, 0);
if(!xctx->tok_size && s2) {
value=get_tok_value(s2, token+1, 0);
@ -4422,6 +4423,7 @@ const char *translate3(const char *s, const char *s1, const char *s2, const char
/* if result is like: 'tcleval(some_string)' pass it thru tcl evaluation so expressions
* can be calculated */
dbg(1, "translate3(): result=|%s|\n", result);
my_strdup2(_ALLOC_ID_, &translated_tok, tcl_hook2(result));
return translated_tok;
}

View File

@ -1587,7 +1587,7 @@ extern char *find_nth(const char *str, const char *sep, const char *quote, int k
extern int isonlydigit(const char *s);
extern const char *translate(int inst, const char* s);
extern const char* translate2(Lcc *lcc, int level, char* s);
extern const char *translate3(const char* s, const char *s1, const char *s2, const char *s3);
extern const char *translate3(const char* s, int eat_escapes, const char *s1, const char *s2, const char *s3);
extern void print_tedax_element(FILE *fd, int inst);
extern int print_spice_element(FILE *fd, int inst);
extern void print_spice_subckt_nodes(FILE *fd, int symbol);