151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)/*
251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * Copyright (C) 2013, Opera Software ASA. All rights reserved.
351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *
451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * modification, are permitted provided that the following conditions are
651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * met:
751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *
851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
1051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
1151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
1251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * in the documentation and/or other materials provided with the
1351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * distribution.
1451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
1551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
1651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * this software without specific prior written permission.
1751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *
1851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) */
3051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
3151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#ifndef BufferedLineReader_h
3251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#define BufferedLineReader_h
3351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
3451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/text/SegmentedString.h"
3551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "wtf/text/StringBuilder.h"
3651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
37c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
3851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
3951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)// Line collection helper for the WebVTT Parser.
4051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)//
4151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)// Converts a stream of data (== a sequence of Strings) into a set of
4251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)// lines. CR, LR or CRLF are considered linebreaks. Normalizes NULs (U+0000)
4351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)// to 'REPLACEMENT CHARACTER' (U+FFFD) and does not return the linebreaks as
4451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)// part of the result.
4551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)class BufferedLineReader {
4651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    WTF_MAKE_NONCOPYABLE(BufferedLineReader);
4751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)public:
4851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    BufferedLineReader()
4951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        : m_endOfStream(false)
5051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        , m_maybeSkipLF(false) { }
5151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
5251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // Append data to the internal buffer.
5351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    void append(const String& data)
5451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    {
5551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        ASSERT(!m_endOfStream);
5651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        m_buffer.append(SegmentedString(data));
5751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
5851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
5951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // Indicate that no more data will be appended. This will cause any
6051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // potentially "unterminated" line to be returned from getLine.
6151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    void setEndOfStream() { m_endOfStream = true; }
6251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
6351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // Attempt to read a line from the internal buffer (fed via append).
6451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // If successful, true is returned and |line| is set to the line that was
6551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // read. If no line could be read false is returned.
6651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    bool getLine(String& line);
6751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
6851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // Returns true if EOS has been reached proper.
6951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    bool isAtEndOfStream() const { return m_endOfStream && m_buffer.isEmpty(); }
7051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
7151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)private:
7251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // Consume the next character the buffer if it is the character |c|.
7351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    void scanCharacter(UChar c)
7451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    {
7551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        ASSERT(!m_buffer.isEmpty());
7651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        if (m_buffer.currentChar() == c)
7751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            m_buffer.advance();
7851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
7951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
8051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    SegmentedString m_buffer;
8151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    StringBuilder m_lineBuffer;
8251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    bool m_endOfStream;
8351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    bool m_maybeSkipLF;
8451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)};
8551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
86c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
8751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
8851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#endif
89