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/ChangeLog | 6 ++++++ camel/camel-stream-buffer.c | 43 +++++++++++++++++++++++++++++++++++++++++++ camel/camel-stream-buffer.h | 6 ++++-- camel/camel-types.h | 1 + camel/camel.h | 1 + 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/camel/ChangeLog b/camel/ChangeLog index 0c11050043..3bb7c1529b 100644 --- a/camel/ChangeLog +++ b/camel/ChangeLog @@ -1,3 +1,9 @@ +2000-03-23 Dan Winship + + * 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. + 2000-03-22 Dan Winship * camel-service.c (camel_service_query_auth_types): New function 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; +} diff --git a/camel/camel-stream-buffer.h b/camel/camel-stream-buffer.h index c4b7e4b2e7..967c33e553 100644 --- a/camel/camel-stream-buffer.h +++ b/camel/camel-stream-buffer.h @@ -53,7 +53,7 @@ typedef enum CAMEL_STREAM_BUFFER_MODE = 0x80 } CamelStreamBufferMode; -typedef struct +struct _CamelStreamBuffer { CamelStream parent_object; @@ -67,7 +67,7 @@ typedef struct CamelStreamBufferMode mode; unsigned int flags; /* internal flags */ -} CamelStreamBuffer; +}; @@ -95,6 +95,8 @@ CamelStream *camel_stream_buffer_set_vbuf (CamelStreamBuffer *b, CamelStreamBuff /* read a line of characters */ int camel_stream_buffer_gets(CamelStreamBuffer *b, char *buf, int max); +char *camel_stream_buffer_read_line (CamelStreamBuffer *sbf); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/camel/camel-types.h b/camel/camel-types.h index c070404e95..924c51db6d 100644 --- a/camel/camel-types.h +++ b/camel/camel-types.h @@ -45,6 +45,7 @@ typedef struct _CamelSimpleDataWrapper CamelSimpleDataWrapper; typedef struct _CamelSimpleDataWrapperStream CamelSimpleDataWrapperStream; typedef struct _CamelStore CamelStore; typedef struct _CamelStream CamelStream; +typedef struct _CamelStreamBuffer CamelStreamBuffer; typedef struct _CamelStreamBufferedFs CamelStreamBufferedFs; typedef struct _CamelStreamDataWrapper CamelStreamDataWrapper; typedef struct _CamelStreamFs CamelStreamFs; diff --git a/camel/camel.h b/camel/camel.h index 382326f2e3..5145b58348 100644 --- a/camel/camel.h +++ b/camel/camel.h @@ -53,6 +53,7 @@ extern "C" { #include #include #include +#include #include #include #include -- cgit v1.2.3