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