From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/trim.c | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/trim.c (limited to 'reference/C/CONTRIB/SNIP/trim.c') diff --git a/reference/C/CONTRIB/SNIP/trim.c b/reference/C/CONTRIB/SNIP/trim.c new file mode 100755 index 0000000..dd331db --- /dev/null +++ b/reference/C/CONTRIB/SNIP/trim.c @@ -0,0 +1,78 @@ +/* +** TRIM.C - Remove leading, trailing, & excess embedded spaces +** +** public domain by Bob Stout +*/ + +#include +#include + +#define NUL '\0' + +char *trim(char *str) +{ + char *ibuf = str, *obuf = str; + int i = 0, cnt = 0; + + /* + ** Trap NULL + */ + + if (str) + { + /* + ** Remove leading spaces (from RMLEAD.C) + */ + + for (ibuf = str; *ibuf && isspace(*ibuf); ++ibuf) + ; + if (str != ibuf) + memmove(str, ibuf, ibuf - str); + + /* + ** Collapse embedded spaces (from LV1WS.C) + */ + + while (*ibuf) + { + if (isspace(*ibuf) && cnt) + ibuf++; + else + { + if (!isspace(*ibuf)) + cnt = 0; + else + { + *ibuf = ' '; + cnt = 1; + } + obuf[i++] = *ibuf++; + } + } + obuf[i] = NUL; + + /* + ** Remove trailing spaces (from RMTRAIL.C) + */ + + while (--i >= 0) + { + if (!isspace(obuf[i])) + break; + } + obuf[++i] = NUL; + } + return str; +} + +#ifdef TEST + +#include + +main(int argc, char *argv[]) +{ + printf("trim(\"%s\") ", argv[1]); + printf("returned \"%s\"\n", trim(argv[1])); +} + +#endif /* TEST */ -- cgit v1.2.3-54-g00ecf