From 13aca67a4d7a4a1bebe51aec461ff677899387f5 Mon Sep 17 00:00:00 2001 From: Stefan Frederik Date: Sun, 13 Feb 2022 11:24:38 +0100 Subject: [PATCH] find_nth() generalized to accept multiple separators --- src/actions.c | 2 +- src/draw.c | 6 +++--- src/hilight.c | 34 +++++++++++++++++----------------- src/scheduler.c | 4 ++-- src/token.c | 14 +++++++------- src/xinit.c | 2 +- src/xschem.h | 2 +- 7 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/actions.c b/src/actions.c index 92a6efa6..d6ecc11e 100644 --- a/src/actions.c +++ b/src/actions.c @@ -1312,7 +1312,7 @@ void descend_schematic(int instnumber) if(inst_number <1 || inst_number > inst_mult) inst_number = 1; } 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); my_strcat(16, &xctx->sch_path[xctx->currsch+1], "."); xctx->sch_inst_number[xctx->currsch+1] = inst_number; diff --git a/src/draw.c b/src/draw.c index 347a881e..03f32e45 100644 --- a/src/draw.c +++ b/src/draw.c @@ -2108,8 +2108,8 @@ static void draw_graph_variables(int wcnt, int wave_color, int n_nodes, int swee } /* draw node labels in graph */ if(bus_msb) { - 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)); + 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 { if(xctx->graph_sim_type == 3) { 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*/ while( (ntok = my_strtok_r(nptr, "\n\t ", "\"", &saven)) ) { 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); stok = my_strtok_r(sptr, " ", "", &saves); diff --git a/src/hilight.c b/src/hilight.c index b382b15b..0e249c80 100644 --- a/src/hilight.c +++ b/src/hilight.c @@ -477,16 +477,16 @@ void hilight_parent_pins(void) for(k = 1; k<=mult; k++) { 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--; 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); } 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); } } @@ -523,19 +523,19 @@ void hilight_child_pins(void) for(k = 1; k<=mult; k++) { 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); - 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)); 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); xctx->currsch++; if(entry) { - 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)); + 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)); } else { - bus_hilight_hash_lookup(find_nth(pin_node, ',', k), 0, XDELETE); - dbg(1, "hilight_child_pins(): deleting: %s\n", find_nth(pin_node, ',', k)); + bus_hilight_hash_lookup(find_nth(pin_node, ",", k), 0, XDELETE); + dbg(1, "hilight_child_pins(): deleting: %s\n", find_nth(pin_node, ",", k)); } } /* for(k..) */ } @@ -795,7 +795,7 @@ void drill_hilight(int mode) const char *propag; dbg(1, "drill_hilight(): inst=%d propagate_str=%s\n", i, propagate_str); while(1) { - propag = find_nth(propagate_str, ',', n); + propag = find_nth(propagate_str, ",", n); n++; if(!propag[0]) break; if(entry) { @@ -861,7 +861,7 @@ static void send_net_to_bespice(int simtype, const char *node) expanded_tok = expandlabel(tok, &tok_mult); my_strdup2(1278, &p, xctx->sch_path[xctx->currsch]+1); 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 */ tclvareval( "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); my_strdup2(1499, &p, xctx->sch_path[xctx->currsch]+1); 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 */ 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); @@ -962,7 +962,7 @@ static void send_net_to_gaw(int simtype, const char *node) if(tclresult()[0] == '0') return; my_strdup2(254, &p, xctx->sch_path[xctx->currsch]+1); 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 */ tclvareval("puts $gaw_fd {copyvar v(", strtolower(p), strtolower(t), ") 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); my_strdup2(1282, &p, xctx->sch_path[xctx->currsch]+1); 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 */ tclvareval( "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); my_strdup2(523, &p, xctx->sch_path[xctx->currsch]+1); 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 */ my_snprintf(ss, S(ss), "i(%s%s%s) %d", xctx->currsch>0 ? "v." : "", 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; my_strdup2(1180, &p, xctx->sch_path[xctx->currsch]+1); 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 */ tclvareval("puts $gaw_fd {copyvar i(", xctx->currsch>0 ? "v." : "", 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); 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++; if(!propag[0]) break; propagate = atoi(propag); diff --git a/src/scheduler.c b/src/scheduler.c index b51a4f1a..15a4a54f 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -1057,7 +1057,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg int slot; if( (ss = strchr(xctx->inst[inst].instname, ':')) ) { 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); } @@ -1391,7 +1391,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg } for(p = 0;p < no_of_pins; p++) { 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(p >= no_of_pins) { diff --git a/src/token.c b/src/token.c index fe989953..1ff0e3a1 100644 --- a/src/token.c +++ b/src/token.c @@ -1889,8 +1889,8 @@ void print_tedax_element(FILE *fd, int inst) 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); for(n = 0; n < net_mult; n++) { - my_strdup(1204, &netbit, find_nth(net, ',', n+1)); - my_strdup(1205, &pinbit, find_nth(pin, ',', n+1)); + my_strdup(1204, &netbit, find_nth(net, ",", n+1)); + my_strdup(1205, &pinbit, find_nth(pin, ",", n+1)); fprintf(fd, "__map__ %s -> %s\n", pinbit ? pinbit : "__UNCONNECTED_PIN__", netbit ? netbit : "__UNCONNECTED_PIN__"); @@ -2075,7 +2075,7 @@ void print_tedax_element(FILE *fd, int inst) int slot; if( (ss=strchr(xctx->inst[inst].instname, ':')) ) { 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); } @@ -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("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 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); for(i=0, count=1, ptr=result; result[i] != 0; i++) { - if(result[i]==sep) { + if(strchr(sep, result[i])) { result[i]=0; if(count==n) { return ptr; } - while(result[++i] == sep) ; + while(strchr(sep, result[++i])) ; ptr=result+i; count++; } @@ -2886,7 +2886,7 @@ const char *translate(int inst, const char* s) sscanf(ss+1, "%s", tmpstr); if(isonlydigit(tmpstr)) { slot = atoi(tmpstr); - if(strstr(value,":")) value = find_nth(value, ':', slot); + if(strstr(value,":")) value = find_nth(value, ":", slot); } } my_free(1177, &tmpstr); diff --git a/src/xinit.c b/src/xinit.c index f078cbab..f6fa2c94 100644 --- a/src/xinit.c +++ b/src/xinit.c @@ -691,7 +691,7 @@ void xwin_exit(void) translate(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 */ - 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 */ save_ascii_string(NULL, NULL, 0); /* clear static data in function */ dbg(1, "xwin_exit(): removing font\n"); diff --git a/src/xschem.h b/src/xschem.h index d775b384..20eab625 100644 --- a/src/xschem.h +++ b/src/xschem.h @@ -1292,7 +1292,7 @@ extern void int_hash_free(Int_hashentry **table); extern Int_hashentry *int_hash_lookup(Int_hashentry **table, 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 const char *translate(int inst, const char* s); extern const char* translate2(Lcc *lcc, int level, char* s);