summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2015-05-16 15:56:00 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2015-05-16 15:56:00 +0200
commit02f91017fdfa8c90a25991bf1cb4b6efcb0bc7c6 (patch)
treee7b926cd918c5c462cbcee5f681c86c9f986beee
parent5c8840493f8cd52ba2224db6ed644f4933ac2212 (diff)
util: parseline can return error, unsigned int -> int
-rw-r--r--util.c30
-rw-r--r--util.h2
2 files changed, 18 insertions, 14 deletions
diff --git a/util.c b/util.c
index 3ed2f4d..85a7211 100644
--- a/util.c
+++ b/util.c
@@ -56,26 +56,30 @@ printlink(const char *link, const char *baseurl, FILE *fp)
* 'fields' is a list of pointer with a maximum size of 'maxfields'.
* 'line' buffer is allocated using malloc, 'size' will contain the
* allocated buffer size.
- * returns: amount of fields read. */
-unsigned int
+ * 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)
{
- unsigned int i = 0;
char *prev, *s;
+ unsigned int i;
- if(getline(line, size, fp) > 0) {
- for(prev = *line; (s = strchr(prev, separator)) && i <= maxfields; i++) {
- *s = '\0';
- fields[i] = prev;
- prev = s + 1;
- }
+ if(getline(line, size, fp) <= 0)
+ return -1;
+
+ for(prev = *line, i = 0;
+ (s = strchr(prev, separator)) && i <= maxfields;
+ i++) {
+ *s = '\0';
fields[i] = prev;
- /* make non-parsed fields empty. */
- for(i++; i < maxfields; i++)
- fields[i] = "";
+ prev = s + 1;
}
- return i;
+ fields[i++] = prev;
+ /* make non-parsed fields empty. */
+ for(; i < maxfields; i++)
+ fields[i] = "";
+
+ return (int)i;
}
const char *
diff --git a/util.h b/util.h
index 3390589..eb73395 100644
--- a/util.h
+++ b/util.h
@@ -23,7 +23,7 @@ enum { FieldUnixTimestamp = 0, FieldTimeFormatted, FieldTitle, FieldLink,
FieldContent, FieldContentType, FieldId, FieldAuthor, FieldFeedType,
FieldFeedName, FieldFeedUrl, FieldBaseSiteUrl, FieldLast };
-unsigned int parseline(char **, size_t *, char **, unsigned int, int, FILE *);
+int parseline(char **, size_t *, char **, unsigned int, int, FILE *);
void printfeednameid(const char *, FILE *);
void printhtmlencoded(const char *, FILE *);
void printlink(const char *, const char *, FILE *);