From 82a955f8397a6622d2e4ee11e0df8e83ddf44cfd Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Tue, 7 Dec 2010 11:31:35 -0500 Subject: [PATCH] Rewrote stripallbut() again, tested extensively --- libcitadel/lib/tools.c | 49 ++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/libcitadel/lib/tools.c b/libcitadel/lib/tools.c index 35871b677..4a8eb6baa 100644 --- a/libcitadel/lib/tools.c +++ b/libcitadel/lib/tools.c @@ -772,40 +772,29 @@ int stripout(char *str, char leftboundary, char rightboundary) { * parentheses and anything outside them). */ long stripallbut(char *str, char leftboundary, char rightboundary) { - int a; - long orglen, len; - char *pchs; - long min; - - orglen = len = strlen(str); - pchs = NULL; - for (a = 0; a < len; ++ a) { - if (str[a] == leftboundary) - pchs = &str[a+1]; - } - - if (pchs == NULL) - min = 0; - else - min = pchs - str; - - for (a = min; a < len; ++ a) { - if (str[a] == rightboundary) - len = a - 1; + long len = 0; + + char *lb = NULL; + char *rb = NULL; + + lb = strrchr(str, leftboundary); + if (lb != NULL) { + ++lb; + rb = strchr(str, rightboundary); + if ((rb != NULL) && (rb >= lb)) { + *rb = 0; + fflush(stderr); + len = (long)rb - (long)lb; + memmove(str, lb, len); + str[len] = 0; + return(len); + } } - if (len != orglen) - str[len] = '\0'; - if (pchs != NULL) - { - orglen = len - min; - memmove(str, pchs, orglen); - return orglen; - } - else - return len; + return (long)strlen(str); } + char *myfgets(char *s, int size, FILE *stream) { char *ret = fgets(s, size, stream); char *nl; -- 2.30.2