From: markus schnalke Date: Thu, 25 Sep 2014 10:20:11 +0000 (+0200) Subject: sbr/trim.c: New helper function trim(). Strips whitespace from a string. X-Git-Tag: mmh-0.2-RC1~107 X-Git-Url: http://git.marmaro.de/?p=mmh;a=commitdiff_plain;h=d854cd83e7b14f3bc686688ef7099b5d75157647 sbr/trim.c: New helper function trim(). Strips whitespace from a string. There is already trimcpy(), which does a bit more work, but adding trim() appears to be a useful investment. The static function trim() in slocal needed to be renamed to not collide. --- diff --git a/h/prototypes.h b/h/prototypes.h index a22eac6..ed7f1ae 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -103,6 +103,7 @@ int smatch(char *, struct swit *); char *snprintb(char *, size_t, unsigned, char *); int stringdex(char *, char *); char *toabsdir(char *); +char *trim(unsigned char *); char *trimcpy(unsigned char *); int unputenv(char *); int uprf(char *, char *); diff --git a/sbr/Makefile.in b/sbr/Makefile.in index 5da6f89..dfd6c38 100644 --- a/sbr/Makefile.in +++ b/sbr/Makefile.in @@ -66,7 +66,7 @@ SRCS = addrsbr.c ambigsw.c brkstring.c \ seq_print.c seq_read.c seq_save.c seq_setcur.c \ seq_setprev.c seq_setunseen.c signals.c \ smatch.c snprintb.c strcasecmp.c \ - strindex.c trimcpy.c uprf.c vfgets.c fmt_def.c \ + strindex.c trim.c trimcpy.c uprf.c vfgets.c fmt_def.c \ mf.c utils.c m_mktemp.c OBJS = $(SRCS:.c=.o) diff --git a/sbr/trim.c b/sbr/trim.c new file mode 100644 index 0000000..9f08d99 --- /dev/null +++ b/sbr/trim.c @@ -0,0 +1,32 @@ +/* +** trim.c -- strip leading and trailing whitespace ... inplace! +** +** This code is Copyright (c) 2002, by the authors of nmh. See the +** COPYRIGHT file in the root directory of the nmh distribution for +** complete copyright information. +*/ + +#include + + +char * +trim(unsigned char *cp) +{ + unsigned char *sp; + + /* skip over leading whitespace */ + while (isspace(*cp)) { + cp++; + } + + /* start at the end and zap trailing whitespace */ + for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) { + if (isspace(*sp)) { + *sp = '\0'; + } else { + break; + } + } + + return cp; +} diff --git a/uip/slocal.c b/uip/slocal.c index 107117f..837f038 100644 --- a/uip/slocal.c +++ b/uip/slocal.c @@ -162,7 +162,7 @@ static int copy_message(int, char *, int); static void verbose_printf(char *fmt, ...); static void adorn(char *, char *, ...); static void debug_printf(char *fmt, ...); -static char *trim(char *); +static char *trimstr(char *); int @@ -382,15 +382,15 @@ main(int argc, char **argv) } if (debug) { - debug_printf("addr=\"%s\"\n", trim(addr)); - debug_printf("user=\"%s\"\n", trim(user)); - debug_printf("info=\"%s\"\n", trim(info)); - debug_printf("sender=\"%s\"\n", trim(sender)); + debug_printf("addr=\"%s\"\n", trimstr(addr)); + debug_printf("user=\"%s\"\n", trimstr(user)); + debug_printf("info=\"%s\"\n", trimstr(info)); + debug_printf("sender=\"%s\"\n", trimstr(sender)); debug_printf("envelope=\"%s\"\n", - envelope ? trim(envelope) : ""); - debug_printf("mbox=\"%s\"\n", trim(mbox)); - debug_printf("home=\"%s\"\n", trim(home)); - debug_printf("ddate=\"%s\"\n", trim(ddate)); + envelope ? trimstr(envelope) : ""); + debug_printf("mbox=\"%s\"\n", trimstr(mbox)); + debug_printf("home=\"%s\"\n", trimstr(home)); + debug_printf("ddate=\"%s\"\n", trimstr(ddate)); debug_printf("now=%02d:%02d\n\n", now->tw_hour, now->tw_min); } @@ -483,7 +483,7 @@ usr_delivery(int fd, char *delivery, int su) if (debug) { for (i = 0; vec[i]; i++) { debug_printf("vec[%d]: \"%s\"\n", - i, trim(vec[i])); + i, trimstr(vec[i])); } } @@ -824,14 +824,14 @@ parse(int fd) p->p_flags &= ~P_CHK; if (debug) { debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n", - p - vars, p->p_name, trim(p->p_value)); + p - vars, p->p_name, trimstr(p->p_value)); } } if (debug) { for (p = hdrs; p->p_name; p++) { debug_printf("hdrs[%d]: name=\"%s\" value=\"%s\"\n", p - hdrs, p->p_name, - p->p_value ? trim(p->p_value) : ""); + p->p_value ? trimstr(p->p_value) : ""); } } return 0; @@ -909,7 +909,7 @@ glob(int fd) if (debug) { for (p = vars; p->p_name; p++) { debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n", - p - vars, p->p_name, trim(p->p_value)); + p - vars, p->p_name, trimstr(p->p_value)); } } } @@ -1237,7 +1237,7 @@ fputs_error: ** Trim strings for pretty printing of debugging output */ static char * -trim(char *cp) +trimstr(char *cp) { char buffer[BUFSIZ*4]; unsigned char *bp, *sp;