From 97268e8137611f2fa2c1cfc0f543f6b3dfe764e7 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Tue, 21 Apr 2009 02:37:46 +0000 Subject: [PATCH] * Fixed striplt() to only remove leading and trailing blanks, but no others. It is still O(1) with no calls to strlen(). --- citadel/user_ops.c | 3 +++ libcitadel/lib/libcitadel.h | 2 +- libcitadel/lib/tools.c | 47 +++++++++++++++---------------------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/citadel/user_ops.c b/citadel/user_ops.c index b61247d0c..1b2eddd5f 100644 --- a/citadel/user_ops.c +++ b/citadel/user_ops.c @@ -617,8 +617,11 @@ void cmd_user(char *cmdbuf) char username[256]; int a; + CtdlLogPrintf(CTDL_DEBUG, "cmd_user(%s)\n", cmdbuf); extract_token(username, cmdbuf, 0, '|', sizeof username); + CtdlLogPrintf(CTDL_DEBUG, "username: %s\n", username); striplt(username); + CtdlLogPrintf(CTDL_DEBUG, "username: %s\n", username); a = CtdlLoginExistingUser(NULL, username); switch (a) { diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 0f4f6e2d1..12be21c1c 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -343,7 +343,7 @@ size_t CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int li int CtdlDecodeBase64(char *dest, const char *source, size_t length); unsigned int decode_hex(char *Source); int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen); -long striplt(char *); +size_t striplt(char *); int haschar(const char *st, int ch); void remove_token(char *source, int parmnum, char separator); void fmt_date(char *buf, size_t n, time_t thetime, int seconds); diff --git a/libcitadel/lib/tools.c b/libcitadel/lib/tools.c index 6cb0646ea..14cf43ec9 100644 --- a/libcitadel/lib/tools.c +++ b/libcitadel/lib/tools.c @@ -460,46 +460,37 @@ char *rfc2047encode(char *line, long length) /* * Strip leading and trailing spaces from a string */ -long striplt(char *buf) -{ - int CountTrail = 0; - int FromStart = 1; - char *aptr, *bptr; +size_t striplt(char *buf) { + char *first_nonspace = NULL; + char *last_nonspace = NULL; + char *ptr; + size_t new_len = 0; - if ((buf==NULL) || (IsEmptyStr(buf))) + if (!buf) { return 0; + } - bptr = aptr = buf; - - while (!IsEmptyStr(aptr)) { - if (isspace(*aptr)) { - if (FromStart) - aptr ++; - else { - CountTrail ++; - *bptr = *aptr; - aptr++; bptr++; + for (ptr=buf; *ptr!=0; ++ptr) { + if (!isspace(*ptr)) { + if (!first_nonspace) { + first_nonspace = ptr; } - } - else { - CountTrail = 0; - *bptr = *aptr; - aptr++; bptr++; + last_nonspace = ptr; } } - if (CountTrail > 0) { - bptr -= CountTrail; + if ((!first_nonspace) || (!last_nonspace)) { + buf[0] = 0; + return 0; } - *bptr = '\0'; - return bptr - buf; + new_len = last_nonspace - first_nonspace + 1; + memcpy(buf, first_nonspace, new_len); + buf[new_len] = 0; + return new_len; } - - - /** * \brief check for the presence of a character within a string (returns count) * \param st the string to examine -- 2.30.2