michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsReadLine_h__ michael@0: #define nsReadLine_h__ michael@0: michael@0: #include "nsIInputStream.h" michael@0: #include "mozilla/Likely.h" michael@0: michael@0: /** michael@0: * @file michael@0: * Functions to read complete lines from an input stream. michael@0: * michael@0: * To properly use the helper function in here (NS_ReadLine) the caller should michael@0: * create a nsLineBuffer with new, and pass it to NS_ReadLine every time it michael@0: * wants a line out. michael@0: * michael@0: * When done, the object should be deleted. michael@0: */ michael@0: michael@0: /** michael@0: * @internal michael@0: * Buffer size. This many bytes will be buffered. If a line is longer than this, michael@0: * the partial line will be appended to the out parameter of NS_ReadLine and the michael@0: * buffer will be emptied. michael@0: * Note: if you change this constant, please update the regression test in michael@0: * netwerk/test/unit/test_readline.js accordingly (bug 397850). michael@0: */ michael@0: #define kLineBufferSize 4096 michael@0: michael@0: /** michael@0: * @internal michael@0: * Line buffer structure, buffers data from an input stream. michael@0: * The buffer is empty when |start| == |end|. michael@0: * Invariant: |start| <= |end| michael@0: */ michael@0: template michael@0: class nsLineBuffer { michael@0: public: michael@0: nsLineBuffer() : start(buf), end(buf) { } michael@0: michael@0: CharT buf[kLineBufferSize+1]; michael@0: CharT* start; michael@0: CharT* end; michael@0: }; michael@0: michael@0: /** michael@0: * Read a line from an input stream. Lines are separated by '\r' (0x0D) or '\n' michael@0: * (0x0A), or "\r\n" or "\n\r". michael@0: * michael@0: * @param aStream michael@0: * The stream to read from michael@0: * @param aBuffer michael@0: * The line buffer to use. A single line buffer must not be used with michael@0: * different input streams. michael@0: * @param aLine [out] michael@0: * The string where the line will be stored. michael@0: * @param more [out] michael@0: * Whether more data is available in the buffer. If true, NS_ReadLine may michael@0: * be called again to read further lines. Otherwise, further calls to michael@0: * NS_ReadLine will return an error. michael@0: * michael@0: * @retval NS_OK michael@0: * Read successful michael@0: * @retval error michael@0: * Input stream returned an error upon read. See michael@0: * nsIInputStream::read. michael@0: */ michael@0: template michael@0: nsresult michael@0: NS_ReadLine (StreamType* aStream, nsLineBuffer * aBuffer, michael@0: StringType & aLine, bool *more) michael@0: { michael@0: CharT eolchar = 0; // the first eol char or 1 after \r\n or \n\r is found michael@0: michael@0: aLine.Truncate(); michael@0: michael@0: while (1) { // will be returning out of this loop on eol or eof michael@0: if (aBuffer->start == aBuffer->end) { // buffer is empty. Read into it. michael@0: uint32_t bytesRead; michael@0: nsresult rv = aStream->Read(aBuffer->buf, kLineBufferSize, &bytesRead); michael@0: if (NS_FAILED(rv) || MOZ_UNLIKELY(bytesRead == 0)) { michael@0: *more = false; michael@0: return rv; michael@0: } michael@0: aBuffer->start = aBuffer->buf; michael@0: aBuffer->end = aBuffer->buf + bytesRead; michael@0: *(aBuffer->end) = '\0'; michael@0: } michael@0: michael@0: /* michael@0: * Walk the buffer looking for an end-of-line. michael@0: * There are 3 cases to consider: michael@0: * 1. the eol char is the last char in the buffer michael@0: * 2. the eol char + one more char at the end of the buffer michael@0: * 3. the eol char + two or more chars at the end of the buffer michael@0: * we need at least one char after the first eol char to determine if michael@0: * it's a \r\n or \n\r sequence (and skip over it), and we need one michael@0: * more char after the end-of-line to set |more| correctly. michael@0: */ michael@0: CharT* current = aBuffer->start; michael@0: if (MOZ_LIKELY(eolchar == 0)) { michael@0: for ( ; current < aBuffer->end; ++current) { michael@0: if (*current == '\n' || *current == '\r') { michael@0: eolchar = *current; michael@0: *current++ = '\0'; michael@0: aLine.Append(aBuffer->start); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (MOZ_LIKELY(eolchar != 0)) { michael@0: for ( ; current < aBuffer->end; ++current) { michael@0: if ((eolchar == '\r' && *current == '\n') || michael@0: (eolchar == '\n' && *current == '\r')) { michael@0: eolchar = 1; michael@0: continue; michael@0: } michael@0: aBuffer->start = current; michael@0: *more = true; michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: if (eolchar == 0) michael@0: aLine.Append(aBuffer->start); michael@0: aBuffer->start = aBuffer->end; // mark the buffer empty michael@0: } michael@0: } michael@0: michael@0: #endif // nsReadLine_h__