From edca38f94261e3de74d36e9a8c5cc51b50c4b0f4 Mon Sep 17 00:00:00 2001 From: Stefan Schippers Date: Wed, 19 Aug 2020 09:00:44 +0200 Subject: [PATCH] subst_tok() compacted, more comments --- src/token.c | 47 +++++++++++++---------------------------------- 1 file changed, 13 insertions(+), 34 deletions(-) diff --git a/src/token.c b/src/token.c index 4fa6ff20..24525896 100644 --- a/src/token.c +++ b/src/token.c @@ -760,9 +760,9 @@ void new_prop_string(int i, const char *old_prop, int fast, int disable_unique_n char *subst_token(const char *s, const char *tok, const char *new_val) /* given a string with multiple "token=value ..." assignments */ /* substitute 's value with */ -/* if tok not found in s add tok=new_val at end. 04052001 */ +/* if tok not found in s and new_val!=NULL add tok=new_val at end.*/ /* if new_val is empty ('\0') set token value to "" (token="") */ -/* if new_val is NULL *remove* 'token=val' from s */ +/* if new_val is NULL *remove* 'token (and =val if any)' from s */ { static char *result=NULL; int size=0; @@ -826,35 +826,6 @@ char *subst_token(const char *s, const char *tok, const char *new_val) } else if(state == XENDTOK && (!space || c == '\0') && c != '=' ) { if(!done_subst && matched_tok) { if(new_val) { /* add new_val to matching token with no value */ - if(new_val[0]) { - tmp = strlen(new_val); - } else { - new_val = "\"\""; - tmp = 2; - } - if(result_pos+tmp+2 >= size) { - size = (1+(result_pos+tmp+2) / CADCHUNKALLOC) * CADCHUNKALLOC; - my_realloc(1154, &result, size); - } - memcpy(result + result_pos-1, "=", 1); - memcpy(result + result_pos, new_val, tmp); - memcpy(result + result_pos+tmp, " ", 1); - result_pos += tmp + 1; - done_subst = 1; - } else { /* remove token (and value if any) */ - result_pos = result_save_pos; - done_subst = 1; - } - } - result_save_pos = result_pos; - state = XTOKEN; - } else if( state == XTOKEN && space) { - token[token_pos] = '\0'; - token_pos = 0; - matched_tok = !strcmp(token, tok) && !done_subst; - - if(c == '\0' && !done_subst && matched_tok) { - if(new_val) { /* add new_val to matching token with no value at end of string: "....token\0"*/ if(new_val[0]) { tmp = strlen(new_val); } else { @@ -875,9 +846,17 @@ char *subst_token(const char *s, const char *tok, const char *new_val) done_subst = 1; } } - - - + result_save_pos = result_pos; + if(c != '\0') state = XTOKEN; /* if end of string remain in XENDTOK state */ + } else if( state == XTOKEN && space) { + token[token_pos] = '\0'; + token_pos = 0; + matched_tok = !strcmp(token, tok) && !done_subst; + if(c == '\0') { + state=XENDTOK; + s--; /* go to next iteration and process \0 as XENDTOK */ + continue; + } state=XENDTOK; } else if(state == XTOKEN && c=='=') { token[token_pos] = '\0';