dovecot-1.2: Added i_stream_set_return_partial_line().

dovecot at dovecot.org dovecot at dovecot.org
Wed Nov 19 16:28:52 EET 2008


details:   http://hg.dovecot.org/dovecot-1.2/rev/1baa595093f5
changeset: 8438:1baa595093f5
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Nov 19 16:28:22 2008 +0200
description:
Added i_stream_set_return_partial_line().

diffstat:

3 files changed, 26 insertions(+), 5 deletions(-)
src/lib/istream-internal.h |    1 +
src/lib/istream.c          |   23 ++++++++++++++++++++---
src/lib/istream.h          |    7 +++++--

diffs (92 lines):

diff -r 8f41c9f3f392 -r 1baa595093f5 src/lib/istream-internal.h
--- a/src/lib/istream-internal.h	Wed Nov 19 16:11:01 2008 +0200
+++ b/src/lib/istream-internal.h	Wed Nov 19 16:28:22 2008 +0200
@@ -34,6 +34,7 @@ struct istream_private {
 	uoff_t parent_start_offset;
 
 	string_t *line_str; /* for i_stream_next_line() if w_buffer == NULL */
+	unsigned int return_nolf_line:1;
 };
 
 struct istream *
diff -r 8f41c9f3f392 -r 1baa595093f5 src/lib/istream.c
--- a/src/lib/istream.c	Wed Nov 19 16:11:01 2008 +0200
+++ b/src/lib/istream.c	Wed Nov 19 16:28:22 2008 +0200
@@ -57,6 +57,11 @@ void i_stream_set_max_buffer_size(struct
 void i_stream_set_max_buffer_size(struct istream *stream, size_t max_size)
 {
 	io_stream_set_max_buffer_size(&stream->real_stream->iostream, max_size);
+}
+
+void i_stream_set_return_partial_line(struct istream *stream, bool set)
+{
+	stream->real_stream->return_nolf_line = set;
 }
 
 ssize_t i_stream_read(struct istream *stream)
@@ -213,10 +218,21 @@ static char *i_stream_next_line_finish(s
 		ret = str_c_modifiable(stream->line_str);
 	}
 
-	i++;
+	if (i < stream->pos)
+		i++;
 	stream->istream.v_offset += i - stream->skip;
 	stream->skip = i;
 	return ret;
+}
+
+static char *i_stream_last_line(struct istream_private *_stream)
+{
+	if (_stream->istream.eof && _stream->skip != _stream->pos &&
+	    _stream->return_nolf_line) {
+		/* the last line is missing LF and we want to return it. */
+		return i_stream_next_line_finish(_stream, _stream->pos);
+	}
+	return NULL;
 }
 
 char *i_stream_next_line(struct istream *stream)
@@ -244,7 +260,8 @@ char *i_stream_next_line(struct istream 
                         break;
 		}
 	}
-
+	if (ret_buf == NULL)
+		return i_stream_last_line(_stream);
         return ret_buf;
 }
 
@@ -258,7 +275,7 @@ char *i_stream_read_next_line(struct ist
 			break;
 
 		if (i_stream_read(stream) <= 0)
-			return NULL;
+			return i_stream_last_line(stream->real_stream);
 	}
 	return line;
 }
diff -r 8f41c9f3f392 -r 1baa595093f5 src/lib/istream.h
--- a/src/lib/istream.h	Wed Nov 19 16:11:01 2008 +0200
+++ b/src/lib/istream.h	Wed Nov 19 16:28:22 2008 +0200
@@ -56,6 +56,9 @@ void i_stream_sync(struct istream *strea
 /* Change the maximum size for stream's input buffer to grow. Useful only
    for buffered streams (currently only file). */
 void i_stream_set_max_buffer_size(struct istream *stream, size_t max_size);
+/* Enable/disable i_stream[_read]_next_line() returning the last line if it
+   doesn't end with LF. */
+void i_stream_set_return_partial_line(struct istream *stream, bool set);
 
 /* Returns number of bytes read if read was ok, -1 if EOF or error, -2 if the
    input buffer is full. */
@@ -83,8 +86,8 @@ bool i_stream_have_bytes_left(const stru
 bool i_stream_have_bytes_left(const struct istream *stream) ATTR_PURE;
 
 /* Gets the next line from stream and returns it, or NULL if more data is
-   needed to make a full line. Note that if the stream ends with LF not being
-   the last character, this function doesn't return the last line. */
+   needed to make a full line. i_stream_set_return_partial_line() specifies
+   if the last line should be returned if it doesn't end with LF. */
 char *i_stream_next_line(struct istream *stream);
 /* Like i_stream_next_line(), but reads for more data if needed. Returns NULL
    if more data is needed or error occurred. */


More information about the dovecot-cvs mailing list