commit 86993965de423bdd9f4cfb146929a1cdd9fbc0b2
parent 43f02f646c97ddbc4dc41fe9e119506cf95e0b28
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Fri, 2 Jan 2015 21:14:07 +0100
trim string
Diffstat:
5 files changed, 37 insertions(+), 10 deletions(-)
diff --git 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
@@ -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("</table>\n", stdout);
fcur->next = f;
@@ -60,10 +59,10 @@ main(void)
/* set nosidebar class on div for styling */
fputs("\t\t<div id=\"items\" class=\"nosidebar\">\n", stdout);
showsidebar = 0;
- } else
+ } else {
fputs("\t\t<div id=\"items\">\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
@@ -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
@@ -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
@@ -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 *);