aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-stream-buffer.c
diff options
context:
space:
mode:
authorDan Winship <danw@src.gnome.org>2000-03-24 00:22:29 +0800
committerDan Winship <danw@src.gnome.org>2000-03-24 00:22:29 +0800
commit6d99be7149ce15e10460f80a218b0f4333b8c4be (patch)
treeb681cbf085ee9e6733a6476ebc859d4d771b2991 /camel/camel-stream-buffer.c
parent58aa78c64b937828eb30550239eda4a429e66b19 (diff)
downloadgsoc2013-evolution-6d99be7149ce15e10460f80a218b0f4333b8c4be.tar
gsoc2013-evolution-6d99be7149ce15e10460f80a218b0f4333b8c4be.tar.gz
gsoc2013-evolution-6d99be7149ce15e10460f80a218b0f4333b8c4be.tar.bz2
gsoc2013-evolution-6d99be7149ce15e10460f80a218b0f4333b8c4be.tar.lz
gsoc2013-evolution-6d99be7149ce15e10460f80a218b0f4333b8c4be.tar.xz
gsoc2013-evolution-6d99be7149ce15e10460f80a218b0f4333b8c4be.tar.zst
gsoc2013-evolution-6d99be7149ce15e10460f80a218b0f4333b8c4be.zip
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
Diffstat (limited to 'camel/camel-stream-buffer.c')
-rw-r--r--camel/camel-stream-buffer.c43
1 files changed, 43 insertions, 0 deletions
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;
+}