From 6d99be7149ce15e10460f80a218b0f4333b8c4be Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Thu, 23 Mar 2000 16:22:29 +0000 Subject: Function to read one line of any size from a stream and return it in * camel-stream-buffer.c (camel_stream_buffer_read_line): Function to read one line of any size from a stream and return it in allocated memory. Also add camel-stream-buffer.h to camel.h and CamelStreamBuffer to camel-types.h. svn path=/trunk/; revision=2152 --- camel/camel-stream-buffer.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'camel/camel-stream-buffer.c') diff --git a/camel/camel-stream-buffer.c b/camel/camel-stream-buffer.c index cb4f0d4831..8dbe50a133 100644 --- a/camel/camel-stream-buffer.c +++ b/camel/camel-stream-buffer.c @@ -452,3 +452,46 @@ int camel_stream_buffer_gets(CamelStreamBuffer *sbf, char *buf, int max) return outptr-buf; } + +/** + * camel_stream_buffer_read_line: read a complete line from the stream + * @sbf: A CamelStreamBuffer + * + * This function reads a complete newline-terminated line from the stream + * and returns it in allocated memory. The trailing newline (and carriage + * return if any) are not included in the returned string. + * + * Return value: the line read, which the caller must free when done with, + * or NULL on eof or error. + **/ +char * +camel_stream_buffer_read_line (CamelStreamBuffer *sbf) +{ + char *buf, *p; + int bufsiz, nread; + + bufsiz = 80; + p = buf = g_malloc (bufsiz); + + while (1) { + nread = camel_stream_buffer_gets (sbf, p, bufsiz - (p - buf)); + if (nread == 0) { + g_free (buf); + return NULL; + } + + p += nread; + if (*(p - 1) == '\n') + break; + + nread = p - buf; + bufsiz *= 2; + buf = g_realloc (buf, bufsiz); + p = buf + nread; + } + + *--p = '\0'; + if (*(p - 1) == '\r') + *--p = '\0'; + return buf; +} -- cgit v1.2.3