find_nth() generalized to accept multiple separators

This commit is contained in:
Stefan Frederik 2022-02-13 11:24:38 +01:00
parent 1a1c6371cc
commit 13aca67a4d
7 changed files with 32 additions and 32 deletions

View File

@ -1312,7 +1312,7 @@ void descend_schematic(int instnumber)
if(inst_number <1 || inst_number > inst_mult) inst_number = 1; if(inst_number <1 || inst_number > inst_mult) inst_number = 1;
} }
dbg(1,"descend_schematic(): inst_number=%d\n", inst_number); dbg(1,"descend_schematic(): inst_number=%d\n", inst_number);
my_strcat(15, &xctx->sch_path[xctx->currsch+1], find_nth(str, ',', inst_number)); my_strcat(15, &xctx->sch_path[xctx->currsch+1], find_nth(str, ",", inst_number));
dbg(1,"descend_schematic(): inst_number=%d\n", inst_number); dbg(1,"descend_schematic(): inst_number=%d\n", inst_number);
my_strcat(16, &xctx->sch_path[xctx->currsch+1], "."); my_strcat(16, &xctx->sch_path[xctx->currsch+1], ".");
xctx->sch_inst_number[xctx->currsch+1] = inst_number; xctx->sch_inst_number[xctx->currsch+1] = inst_number;

View File

@ -2108,8 +2108,8 @@ static void draw_graph_variables(int wcnt, int wave_color, int n_nodes, int swee
} }
/* draw node labels in graph */ /* draw node labels in graph */
if(bus_msb) { if(bus_msb) {
if(gr->unity != 1.0) my_snprintf(tmpstr, S(tmpstr), "%s[%c]", find_nth(ntok, ',', 1), gr->unity_suffix); if(gr->unity != 1.0) my_snprintf(tmpstr, S(tmpstr), "%s[%c]", find_nth(ntok, ",", 1), gr->unity_suffix);
else my_snprintf(tmpstr, S(tmpstr), "%s",find_nth(ntok, ',', 1)); else my_snprintf(tmpstr, S(tmpstr), "%s",find_nth(ntok, ",", 1));
} else { } else {
if(xctx->graph_sim_type == 3) { if(xctx->graph_sim_type == 3) {
if(strstr(ntok, "ph(") == ntok || strstr(ntok, "_ph")) if(strstr(ntok, "ph(") == ntok || strstr(ntok, "_ph"))
@ -2404,7 +2404,7 @@ void draw_graph(int i, const int flags, Graph_ctx *gr)
/* process each node given in "node" attribute, get also associated color/sweep var if any*/ /* process each node given in "node" attribute, get also associated color/sweep var if any*/
while( (ntok = my_strtok_r(nptr, "\n\t ", "\"", &saven)) ) { while( (ntok = my_strtok_r(nptr, "\n\t ", "\"", &saven)) ) {
if(strstr(ntok, ",")) { if(strstr(ntok, ",")) {
my_strdup2(1452, &bus_msb, find_nth(ntok, ',', 2)); my_strdup2(1452, &bus_msb, find_nth(ntok, ",", 2));
} }
ctok = my_strtok_r(cptr, " ", "", &savec); ctok = my_strtok_r(cptr, " ", "", &savec);
stok = my_strtok_r(sptr, " ", "", &saves); stok = my_strtok_r(sptr, " ", "", &saves);

View File

@ -477,16 +477,16 @@ void hilight_parent_pins(void)
for(k = 1; k<=mult; k++) { for(k = 1; k<=mult; k++) {
xctx->currsch++; xctx->currsch++;
entry = bus_hilight_hash_lookup(find_nth(pin_node, ',', k), 0, XLOOKUP); entry = bus_hilight_hash_lookup(find_nth(pin_node, ",", k), 0, XLOOKUP);
xctx->currsch--; xctx->currsch--;
if(entry) if(entry)
{ {
bus_hilight_hash_lookup(find_nth(net_node, ',', bus_hilight_hash_lookup(find_nth(net_node, ",",
((inst_number - 1) * mult + k - 1) % net_mult + 1), entry->value, XINSERT); ((inst_number - 1) * mult + k - 1) % net_mult + 1), entry->value, XINSERT);
} }
else else
{ {
bus_hilight_hash_lookup(find_nth(net_node, ',', bus_hilight_hash_lookup(find_nth(net_node, ",",
((inst_number - 1) * mult + k - 1) % net_mult + 1), 0, XDELETE); ((inst_number - 1) * mult + k - 1) % net_mult + 1), 0, XDELETE);
} }
} }
@ -523,19 +523,19 @@ void hilight_child_pins(void)
for(k = 1; k<=mult; k++) { for(k = 1; k<=mult; k++) {
dbg(1, "hilight_child_pins(): looking nth net:%d, k=%d, inst_number=%d, mult=%d\n", dbg(1, "hilight_child_pins(): looking nth net:%d, k=%d, inst_number=%d, mult=%d\n",
(inst_number-1)*mult+k, k, inst_number, mult); (inst_number-1)*mult+k, k, inst_number, mult);
dbg(1, "hilight_child_pins(): looking net:%s\n", find_nth(net_node, ',', dbg(1, "hilight_child_pins(): looking net:%s\n", find_nth(net_node, ",",
((inst_number - 1) * mult + k - 1) % net_mult + 1)); ((inst_number - 1) * mult + k - 1) % net_mult + 1));
xctx->currsch--; xctx->currsch--;
entry = bus_hilight_hash_lookup(find_nth(net_node, ',', entry = bus_hilight_hash_lookup(find_nth(net_node, ",",
((inst_number - 1) * mult + k - 1) % net_mult + 1), 0, XLOOKUP); ((inst_number - 1) * mult + k - 1) % net_mult + 1), 0, XLOOKUP);
xctx->currsch++; xctx->currsch++;
if(entry) { if(entry) {
bus_hilight_hash_lookup(find_nth(pin_node, ',', k), entry->value, XINSERT_NOREPLACE); bus_hilight_hash_lookup(find_nth(pin_node, ",", k), entry->value, XINSERT_NOREPLACE);
dbg(1, "hilight_child_pins(): inserting: %s\n", find_nth(pin_node, ',', k)); dbg(1, "hilight_child_pins(): inserting: %s\n", find_nth(pin_node, ",", k));
} }
else { else {
bus_hilight_hash_lookup(find_nth(pin_node, ',', k), 0, XDELETE); bus_hilight_hash_lookup(find_nth(pin_node, ",", k), 0, XDELETE);
dbg(1, "hilight_child_pins(): deleting: %s\n", find_nth(pin_node, ',', k)); dbg(1, "hilight_child_pins(): deleting: %s\n", find_nth(pin_node, ",", k));
} }
} /* for(k..) */ } /* for(k..) */
} }
@ -795,7 +795,7 @@ void drill_hilight(int mode)
const char *propag; const char *propag;
dbg(1, "drill_hilight(): inst=%d propagate_str=%s\n", i, propagate_str); dbg(1, "drill_hilight(): inst=%d propagate_str=%s\n", i, propagate_str);
while(1) { while(1) {
propag = find_nth(propagate_str, ',', n); propag = find_nth(propagate_str, ",", n);
n++; n++;
if(!propag[0]) break; if(!propag[0]) break;
if(entry) { if(entry) {
@ -861,7 +861,7 @@ static void send_net_to_bespice(int simtype, const char *node)
expanded_tok = expandlabel(tok, &tok_mult); expanded_tok = expandlabel(tok, &tok_mult);
my_strdup2(1278, &p, xctx->sch_path[xctx->currsch]+1); my_strdup2(1278, &p, xctx->sch_path[xctx->currsch]+1);
for(k=1; k<=tok_mult; k++) { for(k=1; k<=tok_mult; k++) {
my_strdup(1277, &t, find_nth(expanded_tok, ',', k)); my_strdup(1277, &t, find_nth(expanded_tok, ",", k));
if(simtype == 0 ) { /* spice */ if(simtype == 0 ) { /* spice */
tclvareval( tclvareval(
"puts $bespice_server_getdata(sock) ", "puts $bespice_server_getdata(sock) ",
@ -919,7 +919,7 @@ static void send_net_to_graph(char **s, int simtype, const char *node)
expanded_tok = expandlabel(tok, &tok_mult); expanded_tok = expandlabel(tok, &tok_mult);
my_strdup2(1499, &p, xctx->sch_path[xctx->currsch]+1); my_strdup2(1499, &p, xctx->sch_path[xctx->currsch]+1);
for(k=1; k<=tok_mult; k++) { for(k=1; k<=tok_mult; k++) {
my_strdup(1503, &t, find_nth(expanded_tok, ',', k)); my_strdup(1503, &t, find_nth(expanded_tok, ",", k));
if(simtype == 0 ) { /* ngspice */ if(simtype == 0 ) { /* ngspice */
dbg(1, "%s%s color=%d\n", strtolower(p), strtolower(t), c); dbg(1, "%s%s color=%d\n", strtolower(p), strtolower(t), c);
my_snprintf(ss, S(ss), "%s%s %d ", strtolower(p), strtolower(t), c); my_snprintf(ss, S(ss), "%s%s %d ", strtolower(p), strtolower(t), c);
@ -962,7 +962,7 @@ static void send_net_to_gaw(int simtype, const char *node)
if(tclresult()[0] == '0') return; if(tclresult()[0] == '0') return;
my_strdup2(254, &p, xctx->sch_path[xctx->currsch]+1); my_strdup2(254, &p, xctx->sch_path[xctx->currsch]+1);
for(k=1; k<=tok_mult; k++) { for(k=1; k<=tok_mult; k++) {
my_strdup(246, &t, find_nth(expanded_tok, ',', k)); my_strdup(246, &t, find_nth(expanded_tok, ",", k));
if(simtype == 0 ) { /* ngspice */ if(simtype == 0 ) { /* ngspice */
tclvareval("puts $gaw_fd {copyvar v(", strtolower(p), strtolower(t), tclvareval("puts $gaw_fd {copyvar v(", strtolower(p), strtolower(t),
") sel #", color_str, "}\nvwait gaw_fd\n", NULL); ") sel #", color_str, "}\nvwait gaw_fd\n", NULL);
@ -1002,7 +1002,7 @@ static void send_current_to_bespice(int simtype, const char *node)
expanded_tok = expandlabel(tok, &tok_mult); expanded_tok = expandlabel(tok, &tok_mult);
my_strdup2(1282, &p, xctx->sch_path[xctx->currsch]+1); my_strdup2(1282, &p, xctx->sch_path[xctx->currsch]+1);
for(k=1; k<=tok_mult; k++) { for(k=1; k<=tok_mult; k++) {
my_strdup(1281, &t, find_nth(expanded_tok, ',', k)); my_strdup(1281, &t, find_nth(expanded_tok, ",", k));
if(!simtype) { /* spice */ if(!simtype) { /* spice */
tclvareval( tclvareval(
"puts $bespice_server_getdata(sock) ", "puts $bespice_server_getdata(sock) ",
@ -1061,7 +1061,7 @@ static void send_current_to_graph(char **s, int simtype, const char *node)
expanded_tok = expandlabel(tok, &tok_mult); expanded_tok = expandlabel(tok, &tok_mult);
my_strdup2(523, &p, xctx->sch_path[xctx->currsch]+1); my_strdup2(523, &p, xctx->sch_path[xctx->currsch]+1);
for(k=1; k<=tok_mult; k++) { for(k=1; k<=tok_mult; k++) {
my_strdup(376, &t, find_nth(expanded_tok, ',', k)); my_strdup(376, &t, find_nth(expanded_tok, ",", k));
if(!simtype) { /* ngspice */ if(!simtype) { /* ngspice */
my_snprintf(ss, S(ss), "i(%s%s%s) %d", xctx->currsch>0 ? "v." : "", my_snprintf(ss, S(ss), "i(%s%s%s) %d", xctx->currsch>0 ? "v." : "",
strtolower(p), strtolower(t), c); strtolower(p), strtolower(t), c);
@ -1100,7 +1100,7 @@ static void send_current_to_gaw(int simtype, const char *node)
if(tclresult()[0] == '0') return; if(tclresult()[0] == '0') return;
my_strdup2(1180, &p, xctx->sch_path[xctx->currsch]+1); my_strdup2(1180, &p, xctx->sch_path[xctx->currsch]+1);
for(k=1; k<=tok_mult; k++) { for(k=1; k<=tok_mult; k++) {
my_strdup(1179, &t, find_nth(expanded_tok, ',', k)); my_strdup(1179, &t, find_nth(expanded_tok, ",", k));
if(!simtype) { /* spice */ if(!simtype) { /* spice */
tclvareval("puts $gaw_fd {copyvar i(", xctx->currsch>0 ? "v." : "", tclvareval("puts $gaw_fd {copyvar i(", xctx->currsch>0 ? "v." : "",
strtolower(p), strtolower(t), strtolower(p), strtolower(t),
@ -1483,7 +1483,7 @@ void propagate_logic()
} }
dbg(1, "propagate_logic(): inst=%d pin %d, goto=%s\n", i,j, xctx->simdata[i].pin[j].go_to); dbg(1, "propagate_logic(): inst=%d pin %d, goto=%s\n", i,j, xctx->simdata[i].pin[j].go_to);
while(1) { while(1) {
propag = find_nth(xctx->simdata[i].pin[j].go_to, ',', n); propag = find_nth(xctx->simdata[i].pin[j].go_to, ",", n);
n++; n++;
if(!propag[0]) break; if(!propag[0]) break;
propagate = atoi(propag); propagate = atoi(propag);

View File

@ -1057,7 +1057,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg
int slot; int slot;
if( (ss = strchr(xctx->inst[inst].instname, ':')) ) { if( (ss = strchr(xctx->inst[inst].instname, ':')) ) {
sscanf(ss + 1, "%d", &slot); sscanf(ss + 1, "%d", &slot);
if(strstr(value, ":")) value = find_nth(value, ':', slot); if(strstr(value, ":")) value = find_nth(value, ":", slot);
} }
Tcl_SetResult(interp, (char *)value, TCL_VOLATILE); Tcl_SetResult(interp, (char *)value, TCL_VOLATILE);
} }
@ -1391,7 +1391,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg
} }
for(p = 0;p < no_of_pins; p++) { for(p = 0;p < no_of_pins; p++) {
pin = get_tok_value(rct[p].prop_ptr,argv[3],0); pin = get_tok_value(rct[p].prop_ptr,argv[3],0);
if(slot > 0 && !strcmp(argv[3], "pinnumber") && strstr(pin,":")) pin = find_nth(pin, ':', slot); if(slot > 0 && !strcmp(argv[3], "pinnumber") && strstr(pin,":")) pin = find_nth(pin, ":", slot);
if(!strcmp(pin, argv[4])) break; if(!strcmp(pin, argv[4])) break;
} }
if(p >= no_of_pins) { if(p >= no_of_pins) {

View File

@ -1889,8 +1889,8 @@ void print_tedax_element(FILE *fd, int inst)
my_strdup2(1197, &pin, expandlabel(pinname, &pin_mult)); my_strdup2(1197, &pin, expandlabel(pinname, &pin_mult));
dbg(1, "#net=%s pinname=%s pin=%s net_mult=%d pin_mult=%d\n", net, pinname, pin, net_mult, pin_mult); dbg(1, "#net=%s pinname=%s pin=%s net_mult=%d pin_mult=%d\n", net, pinname, pin, net_mult, pin_mult);
for(n = 0; n < net_mult; n++) { for(n = 0; n < net_mult; n++) {
my_strdup(1204, &netbit, find_nth(net, ',', n+1)); my_strdup(1204, &netbit, find_nth(net, ",", n+1));
my_strdup(1205, &pinbit, find_nth(pin, ',', n+1)); my_strdup(1205, &pinbit, find_nth(pin, ",", n+1));
fprintf(fd, "__map__ %s -> %s\n", fprintf(fd, "__map__ %s -> %s\n",
pinbit ? pinbit : "__UNCONNECTED_PIN__", pinbit ? pinbit : "__UNCONNECTED_PIN__",
netbit ? netbit : "__UNCONNECTED_PIN__"); netbit ? netbit : "__UNCONNECTED_PIN__");
@ -2075,7 +2075,7 @@ void print_tedax_element(FILE *fd, int inst)
int slot; int slot;
if( (ss=strchr(xctx->inst[inst].instname, ':')) ) { if( (ss=strchr(xctx->inst[inst].instname, ':')) ) {
sscanf(ss+1, "%d", &slot); sscanf(ss+1, "%d", &slot);
if(strstr(value, ":")) value = find_nth(value, ':', slot); if(strstr(value, ":")) value = find_nth(value, ":", slot);
} }
fprintf(fd, "%s", value); fprintf(fd, "%s", value);
} }
@ -2698,7 +2698,7 @@ int isonlydigit(const char *s)
/* find nth occurrence of substring in str separated by sep. 1st substring is position 1 /* find nth occurrence of substring in str separated by sep. 1st substring is position 1
* find_nth("aaa,bbb,ccc,ddd", ',', 2) --> "bbb" * find_nth("aaa,bbb,ccc,ddd", ',', 2) --> "bbb"
*/ */
const char *find_nth(const char *str, char sep, int n) const char *find_nth(const char *str, char *sep, int n)
{ {
static char *result=NULL; /* safe to keep even with multiple schematic windows */ static char *result=NULL; /* safe to keep even with multiple schematic windows */
static int result_size = 0; /* safe to keep even with multiple schematic windows */ static int result_size = 0; /* safe to keep even with multiple schematic windows */
@ -2719,12 +2719,12 @@ const char *find_nth(const char *str, char sep, int n)
} }
memcpy(result, str, len); memcpy(result, str, len);
for(i=0, count=1, ptr=result; result[i] != 0; i++) { for(i=0, count=1, ptr=result; result[i] != 0; i++) {
if(result[i]==sep) { if(strchr(sep, result[i])) {
result[i]=0; result[i]=0;
if(count==n) { if(count==n) {
return ptr; return ptr;
} }
while(result[++i] == sep) ; while(strchr(sep, result[++i])) ;
ptr=result+i; ptr=result+i;
count++; count++;
} }
@ -2886,7 +2886,7 @@ const char *translate(int inst, const char* s)
sscanf(ss+1, "%s", tmpstr); sscanf(ss+1, "%s", tmpstr);
if(isonlydigit(tmpstr)) { if(isonlydigit(tmpstr)) {
slot = atoi(tmpstr); slot = atoi(tmpstr);
if(strstr(value,":")) value = find_nth(value, ':', slot); if(strstr(value,":")) value = find_nth(value, ":", slot);
} }
} }
my_free(1177, &tmpstr); my_free(1177, &tmpstr);

View File

@ -691,7 +691,7 @@ void xwin_exit(void)
translate(0, NULL); /* clear static data in function */ translate(0, NULL); /* clear static data in function */
translate2(NULL, 0, NULL); /* clear static data in function */ translate2(NULL, 0, NULL); /* clear static data in function */
subst_token(NULL, NULL, NULL); /* clear static data in function */ subst_token(NULL, NULL, NULL); /* clear static data in function */
find_nth(NULL, '\0', 0); /* clear static data in function */ find_nth(NULL, "", 0); /* clear static data in function */
tcl_hook2(NULL); /* clear static data in function */ tcl_hook2(NULL); /* clear static data in function */
save_ascii_string(NULL, NULL, 0); /* clear static data in function */ save_ascii_string(NULL, NULL, 0); /* clear static data in function */
dbg(1, "xwin_exit(): removing font\n"); dbg(1, "xwin_exit(): removing font\n");

View File

@ -1292,7 +1292,7 @@ extern void int_hash_free(Int_hashentry **table);
extern Int_hashentry *int_hash_lookup(Int_hashentry **table, extern Int_hashentry *int_hash_lookup(Int_hashentry **table,
const char *token, const int value, int what); const char *token, const int value, int what);
extern const char *find_nth(const char *str, char sep, int n); extern const char *find_nth(const char *str, char *sep, int n);
extern int isonlydigit(const char *s); extern int isonlydigit(const char *s);
extern const char *translate(int inst, 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* translate2(Lcc *lcc, int level, char* s);