summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2015-01-02 21:14:07 +0100
committerHiltjo Posthuma <hiltjo@codemadness.org>2015-01-02 21:14:07 +0100
commit86993965de423bdd9f4cfb146929a1cdd9fbc0b2 (patch)
tree22657797ec44c475d3ae29e24a77764dfef390bb
parent43f02f646c97ddbc4dc41fe9e119506cf95e0b28 (diff)
trim string
-rw-r--r--sfeed.c13
-rw-r--r--sfeed_html.c5
-rw-r--r--sfeed_web.c2
-rw-r--r--util.c25
-rw-r--r--util.h2
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("</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
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 *);