netwerk/base/public/nsReadLine.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef nsReadLine_h__
michael@0 8 #define nsReadLine_h__
michael@0 9
michael@0 10 #include "nsIInputStream.h"
michael@0 11 #include "mozilla/Likely.h"
michael@0 12
michael@0 13 /**
michael@0 14 * @file
michael@0 15 * Functions to read complete lines from an input stream.
michael@0 16 *
michael@0 17 * To properly use the helper function in here (NS_ReadLine) the caller should
michael@0 18 * create a nsLineBuffer<T> with new, and pass it to NS_ReadLine every time it
michael@0 19 * wants a line out.
michael@0 20 *
michael@0 21 * When done, the object should be deleted.
michael@0 22 */
michael@0 23
michael@0 24 /**
michael@0 25 * @internal
michael@0 26 * Buffer size. This many bytes will be buffered. If a line is longer than this,
michael@0 27 * the partial line will be appended to the out parameter of NS_ReadLine and the
michael@0 28 * buffer will be emptied.
michael@0 29 * Note: if you change this constant, please update the regression test in
michael@0 30 * netwerk/test/unit/test_readline.js accordingly (bug 397850).
michael@0 31 */
michael@0 32 #define kLineBufferSize 4096
michael@0 33
michael@0 34 /**
michael@0 35 * @internal
michael@0 36 * Line buffer structure, buffers data from an input stream.
michael@0 37 * The buffer is empty when |start| == |end|.
michael@0 38 * Invariant: |start| <= |end|
michael@0 39 */
michael@0 40 template<typename CharT>
michael@0 41 class nsLineBuffer {
michael@0 42 public:
michael@0 43 nsLineBuffer() : start(buf), end(buf) { }
michael@0 44
michael@0 45 CharT buf[kLineBufferSize+1];
michael@0 46 CharT* start;
michael@0 47 CharT* end;
michael@0 48 };
michael@0 49
michael@0 50 /**
michael@0 51 * Read a line from an input stream. Lines are separated by '\r' (0x0D) or '\n'
michael@0 52 * (0x0A), or "\r\n" or "\n\r".
michael@0 53 *
michael@0 54 * @param aStream
michael@0 55 * The stream to read from
michael@0 56 * @param aBuffer
michael@0 57 * The line buffer to use. A single line buffer must not be used with
michael@0 58 * different input streams.
michael@0 59 * @param aLine [out]
michael@0 60 * The string where the line will be stored.
michael@0 61 * @param more [out]
michael@0 62 * Whether more data is available in the buffer. If true, NS_ReadLine may
michael@0 63 * be called again to read further lines. Otherwise, further calls to
michael@0 64 * NS_ReadLine will return an error.
michael@0 65 *
michael@0 66 * @retval NS_OK
michael@0 67 * Read successful
michael@0 68 * @retval error
michael@0 69 * Input stream returned an error upon read. See
michael@0 70 * nsIInputStream::read.
michael@0 71 */
michael@0 72 template<typename CharT, class StreamType, class StringType>
michael@0 73 nsresult
michael@0 74 NS_ReadLine (StreamType* aStream, nsLineBuffer<CharT> * aBuffer,
michael@0 75 StringType & aLine, bool *more)
michael@0 76 {
michael@0 77 CharT eolchar = 0; // the first eol char or 1 after \r\n or \n\r is found
michael@0 78
michael@0 79 aLine.Truncate();
michael@0 80
michael@0 81 while (1) { // will be returning out of this loop on eol or eof
michael@0 82 if (aBuffer->start == aBuffer->end) { // buffer is empty. Read into it.
michael@0 83 uint32_t bytesRead;
michael@0 84 nsresult rv = aStream->Read(aBuffer->buf, kLineBufferSize, &bytesRead);
michael@0 85 if (NS_FAILED(rv) || MOZ_UNLIKELY(bytesRead == 0)) {
michael@0 86 *more = false;
michael@0 87 return rv;
michael@0 88 }
michael@0 89 aBuffer->start = aBuffer->buf;
michael@0 90 aBuffer->end = aBuffer->buf + bytesRead;
michael@0 91 *(aBuffer->end) = '\0';
michael@0 92 }
michael@0 93
michael@0 94 /*
michael@0 95 * Walk the buffer looking for an end-of-line.
michael@0 96 * There are 3 cases to consider:
michael@0 97 * 1. the eol char is the last char in the buffer
michael@0 98 * 2. the eol char + one more char at the end of the buffer
michael@0 99 * 3. the eol char + two or more chars at the end of the buffer
michael@0 100 * we need at least one char after the first eol char to determine if
michael@0 101 * it's a \r\n or \n\r sequence (and skip over it), and we need one
michael@0 102 * more char after the end-of-line to set |more| correctly.
michael@0 103 */
michael@0 104 CharT* current = aBuffer->start;
michael@0 105 if (MOZ_LIKELY(eolchar == 0)) {
michael@0 106 for ( ; current < aBuffer->end; ++current) {
michael@0 107 if (*current == '\n' || *current == '\r') {
michael@0 108 eolchar = *current;
michael@0 109 *current++ = '\0';
michael@0 110 aLine.Append(aBuffer->start);
michael@0 111 break;
michael@0 112 }
michael@0 113 }
michael@0 114 }
michael@0 115 if (MOZ_LIKELY(eolchar != 0)) {
michael@0 116 for ( ; current < aBuffer->end; ++current) {
michael@0 117 if ((eolchar == '\r' && *current == '\n') ||
michael@0 118 (eolchar == '\n' && *current == '\r')) {
michael@0 119 eolchar = 1;
michael@0 120 continue;
michael@0 121 }
michael@0 122 aBuffer->start = current;
michael@0 123 *more = true;
michael@0 124 return NS_OK;
michael@0 125 }
michael@0 126 }
michael@0 127
michael@0 128 if (eolchar == 0)
michael@0 129 aLine.Append(aBuffer->start);
michael@0 130 aBuffer->start = aBuffer->end; // mark the buffer empty
michael@0 131 }
michael@0 132 }
michael@0 133
michael@0 134 #endif // nsReadLine_h__

mercurial