From e457292e048d9ecc98bdebf9d608ceb8ca8b79c4 Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Wed, 11 Oct 2023 16:31:42 +0200 Subject: [PATCH] trim_chars() function for various string operations --- src/draw.c | 4 ++-- src/scheduler.c | 12 +++++++++++- src/token.c | 37 +++++++++++++++++++++++++++++++++++++ src/xinit.c | 1 + src/xschem.h | 2 +- 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/draw.c b/src/draw.c index e2bb3290..f2089be7 100644 --- a/src/draw.c +++ b/src/draw.c @@ -3163,8 +3163,8 @@ void draw_graph(int i, const int flags, Graph_ctx *gr, void *ct) /* process each node given in "node" attribute, get also associated color/sweep var if any*/ while( (ntok = my_strtok_r(nptr, "\n\t ", "\"", 4, &saven)) ) { if(strstr(ntok, ",")) { - my_strdup2(_ALLOC_ID_, &bus_msb, find_nth(ntok, ";,", "\"", 0, 2)); - my_strdup2(_ALLOC_ID_, &bus_msb, find_nth(bus_msb, " ", "", 0, 1)); /* chop spaces */ + /* also trim spaces */ + my_strdup2(_ALLOC_ID_, &bus_msb, trim_chars(find_nth(ntok, ";,", "\"", 0, 2), " ")); } dbg(1, "ntok=|%s|, bus_msb=|%s|\n", ntok, bus_msb ? bus_msb : "NULL"); ctok = my_strtok_r(cptr, " ", "", 0, &savec); diff --git a/src/scheduler.c b/src/scheduler.c index 77f823bb..d1c5a36c 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -2493,7 +2493,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg if(argc > 6) { int i; if((i = get_instance(argv[2])) < 0 ) { - Tcl_SetResult(interp, "xschem pinlist: instance not found", TCL_STATIC); + Tcl_SetResult(interp, "xschem move_instance: instance not found", TCL_STATIC); return TCL_ERROR; } if(undo) xctx->push_undo(); @@ -4588,6 +4588,16 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg } } + /* trim_chars str sep + * Remove leading and trailing chars matching any character in 'sep' from str */ + else if(!strcmp(argv[1], "trim_chars")) + { + if(argc > 3) { + char *s = trim_chars(argv[2], argv[3]); + Tcl_SetResult(interp, s, TCL_VOLATILE); + } + } + /* trim_wires * Remove operlapping wires, join lines, trim wires at intersections */ else if(!strcmp(argv[1], "trim_wires")) diff --git a/src/token.c b/src/token.c index 981f977e..d41b9cc0 100644 --- a/src/token.c +++ b/src/token.c @@ -3185,6 +3185,43 @@ int isonlydigit(const char *s) return 1; } + +/* remove leading and trailing characters specified in 'sep' */ +char *trim_chars(const char *str, const char *sep) +{ + static char *result = NULL; + static size_t result_size = 0; + size_t len; + char *ptr; + char *last; + + if(str == NULL) { + my_free(_ALLOC_ID_, &result); + result_size = 0; + return NULL; + } + len = strlen(str) + 1; + /* allocate storage for result */ + if(len > result_size) { + result_size = len + CADCHUNKALLOC; + my_realloc(_ALLOC_ID_, &result, result_size); + } + memcpy(result, str, len); + if(*str == '\0')return result; + ptr = result; + while (*ptr) { + if(!strchr(sep, *ptr)) break; + ptr++; + } + if(*ptr == '\0') return ptr; + last = ptr + strlen(ptr) -1; + while(strchr(sep, *last) && last > result) { + last--; + } + last[1] = '\0'; + return ptr; +} + /* find nth field in str separated by sep. 1st field is position 1 * separators inside quotes are not considered as field separators * if keep_quote == 1 keep quoting characters and backslashes in returned field diff --git a/src/xinit.c b/src/xinit.c index d31beb9a..bee38597 100644 --- a/src/xinit.c +++ b/src/xinit.c @@ -894,6 +894,7 @@ static void xwin_exit(void) 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 */ + trim_chars(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 */ dbg(1, "xwin_exit(): removing font\n"); diff --git a/src/xschem.h b/src/xschem.h index 518bf4e3..6043d0e3 100644 --- a/src/xschem.h +++ b/src/xschem.h @@ -1480,7 +1480,7 @@ extern void ptr_hash_init(Ptr_hashtable *hashtable, int size); extern void ptr_hash_free(Ptr_hashtable *hashtable); extern Ptr_hashentry *ptr_hash_lookup(Ptr_hashtable *hashtable, const char *token, void * const value, int what); - +extern char *trim_chars(const char *str, const char *sep); extern char *find_nth(const char *str, const char *sep, const char *quote, int keep_quote, int n); extern int isonlydigit(const char *s); extern const char *translate(int inst, const char* s);