From bdf3f0f5cb84a326a5e6f35d61cf88e22fa9dd3f Mon Sep 17 00:00:00 2001 From: ac-stef Date: Sat, 18 Apr 2015 19:38:23 +0200 Subject: [PATCH] option to crop leading and trailing whitespace for filtertext --- source/src/protocol.cpp | 8 ++++++++ source/src/protocol.h | 1 + 2 files changed, 9 insertions(+) diff --git a/source/src/protocol.cpp b/source/src/protocol.cpp index 45282faa..c7866169 100644 --- a/source/src/protocol.cpp +++ b/source/src/protocol.cpp @@ -139,6 +139,7 @@ char *filtertext(char *dst, const char *src, int flags, int len) tolow = (flags & FTXT_TOLOWER) != 0, // translates to all-lowercase filename = (flags & FTXT_FILENAME) != 0, // removes characters, that are not allowed in filenames on all supported systems - does not filter COM, PRN, etc. mapname = (flags & FTXT_MAPNAME) != 0, // only allows lowercase chars, digits, '_', '-' and '.'; probably should be used in combination with TOLOWER + cropwhite = (flags & FTXT_CROPWHITE) != 0, // removes leading and trailing whitespace pass = false; #if 1 // classic mode (will be removed, as soon as all sources are clear of it) switch(flags) @@ -153,6 +154,8 @@ char *filtertext(char *dst, const char *src, int flags, int len) #endif if(leet || mapname) nocolor = true; bool trans = toupp || tolow || leet || filename || fillblanks; + bool leadingwhite = cropwhite; + char *lastwhite = NULL; for(int c = *src; c; c = *++src) { c &= 0x7F; // 7-bit ascii. not negotiable. @@ -199,12 +202,17 @@ char *filtertext(char *dst, const char *src, int flags, int len) if(mapname && !isalnum(c) && !strchr("_-./\\", c)) continue; if(isspace(c)) { + if(leadingwhite) continue; + if(!lastwhite) lastwhite = dst; if(nowhite && !((c == ' ' && allowblanks) || (c == '\n' && allownl)) && !pass) continue; } else if(!pass && !isprint(c)) continue; + leadingwhite = false; + lastwhite = NULL; *dst++ = c; if(!--len || !*src) break; } + if(cropwhite && lastwhite) *lastwhite = '\0'; *dst = '\0'; return res; } diff --git a/source/src/protocol.h b/source/src/protocol.h index b489304c..fa02b093 100644 --- a/source/src/protocol.h +++ b/source/src/protocol.h @@ -151,6 +151,7 @@ enum #define FTXT_TOLOWER (1<<18) #define FTXT_FILENAME (1<<19) #define FTXT_MAPNAME (1<<20) +#define FTXT_CROPWHITE (1<<21) // character classes #define FTXT__SERVDESC (FTXT_NOWHITE | FTXT_ALLOWBLANKS | FTXT_TABTOBLANK) #define FTXT__MAPMSG (FTXT_NOWHITE | FTXT_ALLOWBLANKS | FTXT_TABTOBLANK)