From 86993965de423bdd9f4cfb146929a1cdd9fbc0b2 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 2 Jan 2015 21:14:07 +0100 Subject: trim string --- sfeed.c | 13 ++++++++----- sfeed_html.c | 5 ++--- sfeed_web.c | 2 +- util.c | 25 ++++++++++++++++++++++++- util.h | 2 ++ 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/sfeed.c b/sfeed.c index ab7a5b8..39b8a04 100644 --- a/sfeed.c +++ b/sfeed.c @@ -313,7 +313,7 @@ gettimetz(const char *s, char *buf, size_t bufsiz) buf[0] = '\0'; if(bufsiz < sizeof(tzname) + STRSIZ(" -00:00")) return 0; - for(; *p && isspace((int)*p); p++); /* skip whitespace */ + p = trimstart(p); /* loop until some common timezone delimiters are found */ for(; *p && (*p != '+' && *p != '-' && *p != 'Z' && *p != 'z'); p++); @@ -383,11 +383,13 @@ parsetime(const char *s, char *buf, size_t bufsiz) static void string_print(String *s) { - const char *p; + const char *p, *e; /* skip leading whitespace */ - for(p = s->data; *p && isspace((int)*p); p++); - for(; *p; p++) { + p = trimstart(s->data); + e = trimend(p); + + for(; *p && p != e; p++) { if(ISWSNOSPACE(*p)) { switch(*p) { case '\n': fputs("\\n", stdout); break; @@ -395,8 +397,9 @@ string_print(String *s) case '\t': fputs("\\t", stdout); break; default: break; /* ignore other whitespace chars */ } - } else + } else { putchar(*p); + } } } diff --git a/sfeed_html.c b/sfeed_html.c index 9604a98..f49637b 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -48,7 +48,6 @@ main(void) if(!totalfeeds || (fcur && strcmp(fcur->name, fields[FieldFeedName]))) { if(!(f = calloc(1, sizeof(struct feed)))) err(1, "calloc"); - /*f->next = NULL;*/ if(totalfeeds) { /* end previous one. */ fputs("\n", stdout); fcur->next = f; @@ -60,10 +59,10 @@ main(void) /* set nosidebar class on div for styling */ fputs("\t\t
\n", stdout); showsidebar = 0; - } else + } else { fputs("\t\t
\n", stdout); + } } - /* TODO: memcpy and make fcur->name static? */ if(!(fcur->name = strdup(fields[FieldFeedName]))) err(1, "strdup"); diff --git a/sfeed_web.c b/sfeed_web.c index e941825..77f22cb 100644 --- a/sfeed_web.c +++ b/sfeed_web.c @@ -34,7 +34,7 @@ xmltagstartparsed(XMLParser *p, const char *tag, size_t taglen, int isshort) if(isfeedlink) { if(*feedtype) { - fputs(feedtype, stdout); + printfeednameid(feedtype, stdout); putchar(' '); } printlink(feedlink, basehref, stdout); diff --git a/util.c b/util.c index b6c5fd2..ced4d9d 100644 --- a/util.c +++ b/util.c @@ -76,12 +76,35 @@ parseline(char **line, size_t *size, char **fields, return i; } +const char * +trimend(const char *s) +{ + size_t len = strlen(s); + + for(; len > 0 && isspace((int)s[len - 1]); len--) + ; + return &s[len]; +} + +const char * +trimstart(const char *s) +{ + for(; *s && isspace((int)*s); s++) + ; + return s; +} + /* print feed name for id; spaces and tabs in string as "-" * (spaces in anchors are not valid). */ void printfeednameid(const char *s, FILE *fp) { - for(; *s; s++) + const char *e; + + s = trimstart(s); + e = trimend(s); + + for(; *s && s != e; s++) fputc(isspace((int)*s) ? '-' : tolower((int)*s), fp); } diff --git a/util.h b/util.h index 5b876f0..7091798 100644 --- a/util.h +++ b/util.h @@ -27,3 +27,5 @@ void printfeednameid(const char *, FILE *); void printhtmlencoded(const char *, FILE *); void printlink(const char *, const char *, FILE *); void printutf8pad(FILE *, const char *, size_t, int); +const char *trimstart(const char *); +const char *trimend(const char *); -- cgit v1.2.3