From d87bffd89b9c0a26a65f0c629250fa87902b6cb8 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 25 Sep 2020 20:42:45 +0200 Subject: Add a more usefull getline-like function to libfstream Signed-off-by: David Oberhollenzer --- include/fstream.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'include') diff --git a/include/fstream.h b/include/fstream.h index 8693fff..07ee5a7 100644 --- a/include/fstream.h +++ b/include/fstream.h @@ -55,6 +55,12 @@ enum { OSTREAM_OPEN_SPARSE = 0x02, }; +enum { + ISTREAM_LINE_LTRIM = 0x01, + ISTREAM_LINE_RTRIM = 0x02, + ISTREAM_LINE_SKIP_EMPTY = 0x04, +}; + enum { /** * @brief Deflate compressor with gzip headers. @@ -257,6 +263,35 @@ SQFS_INTERNAL const char *ostream_get_filename(ostream_t *strm); */ SQFS_INTERNAL int ostream_printf(ostream_t *strm, const char *fmt, ...); +/** + * @brief Read a line of text from an input stream + * + * @memberof istream_t + * + * The line returned is allocated using malloc and must subsequently be + * freed when it is no longer needed. The line itself is always null-terminated + * and never includes the line break characters (LF or CR-LF). + * + * If the flag @ref ISTREAM_LINE_LTRIM is set, leading white space characters + * are removed. If the flag @ref ISTREAM_LINE_RTRIM is set, trailing white space + * characters are remvoed. + * + * If the flag @ref ISTREAM_LINE_SKIP_EMPTY is set and a line is discovered to + * be empty (after the optional trimming), the function discards the empty line + * and retries. The given line_num pointer is used to increment the line + * number. + * + * @param strm A pointer to an input stream. + * @param out Returns a pointer to a line on success. + * @param line_num This is incremented if lines are skipped. + * @param flags A combination of flags controling the functions behaviour. + * + * @return Zero on success, a negative value on error, a positive value if + * end-of-file was reached without reading any data. + */ +SQFS_INTERNAL int istream_get_line(istream_t *strm, char **out, + size_t *line_num, int flags); + /** * @brief Read data from an input stream * -- cgit v1.2.3