* incomplete line) is moved to the beginning of the buffer if FELF_READ_ONLY is
* unset.
*
- * \return If \p FELF_READ_ONLY is not set in \a flags, this function returns
- * the number of bytes not handled to \p line_handler, otherwise the number of
- * complete lines. On errors the negative error code of the \p line_handler is
- * returned.
+ * \return On success this function returns the number of bytes not handled to
+ * \p line_handler. The only possible error is a negative return value from the
+ * line handler. In this case processing stops and the return value of the line
+ * handler is returned to indicate failure.
*
* \sa \ref for_each_line_flags.
*/
} else
end = next_cr;
num_lines++;
- if (flags & FELF_READ_ONLY) {
- size_t s = end - start;
- char *b = para_malloc(s + 1);
- memcpy(b, start, s);
- b[s] = '\0';
-// PARA_NOTICE_LOG("b: %s, start: %s\n", b, start);
- ret = line_handler(b, private_data);
- free(b);
- } else {
- *end = '\0';
- ret = line_handler(start, private_data);
+ if (!(flags & FELF_DISCARD_FIRST) || start != buf) {
+ if (flags & FELF_READ_ONLY) {
+ size_t s = end - start;
+ char *b = para_malloc(s + 1);
+ memcpy(b, start, s);
+ b[s] = '\0';
+ ret = line_handler(b, private_data);
+ free(b);
+ } else {
+ *end = '\0';
+ ret = line_handler(start, private_data);
+ }
+ if (ret < 0)
+ return ret;
}
- if (ret < 0)
- return ret;
start = ++end;
}
- if (flags & FELF_READ_ONLY)
- return num_lines;
i = buf + size - start;
- if (i && i != size)
+ if (i && i != size && !(flags & FELF_READ_ONLY))
memmove(buf, start, i);
return i;
}