diff options
author | Hiltjo Posthuma <hiltjo@codemadness.org> | 2015-05-16 15:56:00 +0200 |
---|---|---|
committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2015-05-16 15:56:00 +0200 |
commit | 02f91017fdfa8c90a25991bf1cb4b6efcb0bc7c6 (patch) | |
tree | e7b926cd918c5c462cbcee5f681c86c9f986beee | |
parent | 5c8840493f8cd52ba2224db6ed644f4933ac2212 (diff) |
util: parseline can return error, unsigned int -> int
-rw-r--r-- | util.c | 30 | ||||
-rw-r--r-- | util.h | 2 |
2 files changed, 18 insertions, 14 deletions
@@ -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 * @@ -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 *); |