changeset 33906:3c172a874b4b

Convert stream_read_line function into more generic stream_read_until function.
author reimar
date Mon, 15 Aug 2011 20:32:26 +0000
parents c0cb8c5e7fdb
children 30aade4fd660
files stream/stream.c stream/stream.h
diffstat 2 files changed, 14 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/stream/stream.c	Mon Aug 15 20:19:39 2011 +0000
+++ b/stream/stream.c	Mon Aug 15 20:32:26 2011 +0000
@@ -588,30 +588,30 @@
 }
 
 /**
- * Find a newline character in buffer
+ * Find a termination character in buffer
  * \param buf buffer to search
  * \param len amount of bytes to search in buffer, may not overread
  * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
  *              0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
  */
-static const uint8_t *find_newline(const uint8_t *buf, int len, int utf16)
+static const uint8_t *find_term_char(const uint8_t *buf, int len, uint8_t term, int utf16)
 {
   uint32_t c;
   const uint8_t *end = buf + len;
   switch (utf16) {
   case 0:
-    return (uint8_t *)memchr(buf, '\n', len);
+    return (uint8_t *)memchr(buf, term, len);
   case 1:
     while (buf < end - 1) {
       GET_UTF16(c, buf < end - 1 ? get_le16_inc(&buf) : 0, return NULL;)
-      if (buf <= end && c == '\n')
+      if (buf <= end && c == term)
         return buf - 1;
     }
     break;
   case 2:
     while (buf < end - 1) {
       GET_UTF16(c, buf < end - 1 ? get_be16_inc(&buf) : 0, return NULL;)
-      if (buf <= end && c == '\n')
+      if (buf <= end && c == term)
         return buf - 1;
     }
     break;
@@ -660,7 +660,9 @@
   return 0;
 }
 
-unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max, int utf16) {
+uint8_t *stream_read_until(stream_t *s, uint8_t *mem, int max,
+                           uint8_t term, int utf16)
+{
   int len;
   const unsigned char *end;
   unsigned char *ptr = mem;
@@ -672,7 +674,7 @@
     if(len <= 0 &&
        (!cache_stream_fill_buffer(s) ||
         (len = s->buf_len-s->buf_pos) <= 0)) break;
-    end = find_newline(s->buffer+s->buf_pos, len, utf16);
+    end = find_term_char(s->buffer+s->buf_pos, len, term, utf16);
     if(end) len = end - (s->buffer+s->buf_pos) + 1;
     if(len > 0 && max > 0) {
       int l = copy_characters(ptr, max, s->buffer+s->buf_pos, &len, utf16);
--- a/stream/stream.h	Mon Aug 15 20:19:39 2011 +0000
+++ b/stream/stream.h	Mon Aug 15 20:32:26 2011 +0000
@@ -278,7 +278,11 @@
   return total;
 }
 
-unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max, int utf16);
+uint8_t *stream_read_until(stream_t *s, uint8_t *mem, int max, uint8_t term, int utf16);
+inline static uint8_t *stream_read_line(stream_t *s, uint8_t *mem, int max, int utf16)
+{
+  return stream_read_until(s, mem, max, '\n', utf16);
+}
 
 inline static int stream_eof(stream_t *s){
   return s->eof;