summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2015-08-02 13:45:43 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2015-08-02 13:45:43 +0200
commitce3a22c641a9a7f1ced983d407e49ba31c71bfd2 (patch)
treea68272ab87076c6609cf8ee1a2acad04029cc597
parent3d972927d748267b0e999ef85bc56bab9e771b5d (diff)
util: simplify parseline() and check t for strtotime().
-rw-r--r--sfeed_frames.c2
-rw-r--r--sfeed_html.c2
-rw-r--r--sfeed_mbox.c2
-rw-r--r--sfeed_plain.c2
-rw-r--r--util.c25
-rw-r--r--util.h14
6 files changed, 23 insertions, 24 deletions
diff --git a/sfeed_frames.c b/sfeed_frames.c
index b333f04..4ac9f29 100644
--- a/sfeed_frames.c
+++ b/sfeed_frames.c
@@ -112,7 +112,7 @@ printfeed(FILE *fpitems, FILE *fpin, struct feed *f)
}
fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n", fpitems);
- while (parseline(&line, &linesize, fields, FieldLast, '\t', fpin) > 0) {
+ while (parseline(&line, &linesize, fields, fpin) > 0) {
/* write content */
if (!(namelen = normalizepath(fields[FieldTitle], name, sizeof(name))))
continue;
diff --git a/sfeed_html.c b/sfeed_html.c
index 36113c0..fb4ed8b 100644
--- a/sfeed_html.c
+++ b/sfeed_html.c
@@ -33,7 +33,7 @@ printfeed(FILE *fp, struct feed *f)
}
fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n", stdout);
- while (parseline(&line, &linesize, fields, FieldLast, '\t', fp) > 0) {
+ while (parseline(&line, &linesize, fields, fp) > 0) {
r = strtotime(fields[FieldUnixTimestamp], &parsedtime);
isnew = (r != -1 && parsedtime >= comparetime) ? 1 : 0;
islink = (fields[FieldLink][0] != '\0') ? 1 : 0;
diff --git a/sfeed_mbox.c b/sfeed_mbox.c
index 10b31c8..6e171cb 100644
--- a/sfeed_mbox.c
+++ b/sfeed_mbox.c
@@ -76,7 +76,7 @@ printfeed(FILE *fp, const char *feedname)
if (!strftime(mtimebuf, sizeof(mtimebuf), "%a %b %d %H:%M:%S %Y", &tm))
errx(1, "can't format current time");
- while (parseline(&line, &linesize, fields, FieldLast, '\t', fp) > 0) {
+ while (parseline(&line, &linesize, fields, fp) > 0) {
if ((r = strtotime(fields[FieldUnixTimestamp], &parsedtime)) == -1)
continue; /* invalid date */
if (!gmtime_r(&parsedtime, &tm))
diff --git a/sfeed_plain.c b/sfeed_plain.c
index ff54df5..6bd93a3 100644
--- a/sfeed_plain.c
+++ b/sfeed_plain.c
@@ -44,7 +44,7 @@ printfeed(FILE *fp, const char *feedname)
char *fields[FieldLast];
time_t parsedtime;
- while (parseline(&line, &size, fields, FieldLast, '\t', fp) > 0) {
+ while (parseline(&line, &size, fields, fp) > 0) {
if (strtotime(fields[FieldUnixTimestamp], &parsedtime) != -1 &&
parsedtime >= comparetime)
fputs(" N ", stdout);
diff --git a/util.c b/util.c
index 4837070..a22b235 100644
--- a/util.c
+++ b/util.c
@@ -155,23 +155,21 @@ encodeuri(const char *s, char *buf, size_t bufsiz)
/* Read a field-separated line from 'fp',
* separated by a character 'separator',
- * 'fields' is a list of pointer with a maximum size of 'maxfields'.
- * 'maxfields' must be > 0.
- * 'line' buffer is allocated using malloc, 'size' will contain the
- * allocated buffer size.
- * returns: amount of fields read or -1 on error. */
-int
-parseline(char **line, size_t *size, char **fields,
- unsigned int maxfields, int separator, FILE *fp)
+ * 'fields' is a list of pointers with a size of FieldLast (must be >0).
+ * 'line' buffer is allocated using malloc, 'size' will contain the allocated
+ * buffer size.
+ * returns: amount of fields read (>0) or -1 on error. */
+ssize_t
+parseline(char **line, size_t *size, char *fields[FieldLast], FILE *fp)
{
char *prev, *s;
- unsigned int i;
+ size_t i;
if (getline(line, size, fp) <= 0)
return -1;
for (prev = *line, i = 0;
- (s = strchr(prev, separator)) && i < maxfields - 1;
+ (s = strchr(prev, '\t')) && i < FieldLast - 1;
i++) {
*s = '\0';
fields[i] = prev;
@@ -179,10 +177,10 @@ parseline(char **line, size_t *size, char **fields,
}
fields[i++] = prev;
/* make non-parsed fields empty. */
- for (; i < maxfields; i++)
+ for (; i < FieldLast; i++)
fields[i] = "";
- return (int)i;
+ return (ssize_t)i;
}
/* Parse time to time_t, assumes time_t is signed. */
@@ -195,7 +193,8 @@ strtotime(const char *s, time_t *t)
l = strtol(s, NULL, 10);
if (errno != 0)
return -1;
- *t = (time_t)l;
+ if (t)
+ *t = (time_t)l;
return 0;
}
diff --git a/util.h b/util.h
index 58a8428..5e66cb3 100644
--- a/util.h
+++ b/util.h
@@ -25,12 +25,12 @@ enum { FieldUnixTimestamp = 0, FieldTimeFormatted, FieldTitle, FieldLink,
FieldContent, FieldContentType, FieldId, FieldAuthor, FieldFeedType,
FieldLast };
-int absuri(const char *, const char *, char *, size_t);
-int encodeuri(const char *, char *, size_t);
-int parseline(char **, size_t *, char **, unsigned int, int, FILE *);
-int parseuri(const char *, struct uri *, int);
-int strtotime(const char *, time_t *);
-char * xbasename(const char *);
-void xmlencode(const char *, FILE *);
+int absuri(const char *, const char *, char *, size_t);
+int encodeuri(const char *, char *, size_t);
+ssize_t parseline(char **, size_t *, char *[FieldLast], FILE *);
+int parseuri(const char *, struct uri *, int);
+int strtotime(const char *, time_t *);
+char * xbasename(const char *);
+void xmlencode(const char *, FILE *);