/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_AbstractLineIterator.qpp AbstractLineIterator class (interface) definition */
/*
    Qore Programming Language

    Copyright (C) 2016 - 2023 Qore Technologies, s.r.o.

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

    Note that the Qore library is released under a choice of three open-source
    licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
    information.
*/

#include <qore/Qore.h>

//! This class defines an abstract interface for line iterators.
/** Classes inheriting this class can be used to iterate lines from various sources.

    @since %Qore 0.8.13
 */
qclass AbstractLineIterator [arg=void* i; ns=Qore; vparent=AbstractIterator];

//! Moves the current position to the next line; returns @ref False if there are no more lines to read.
/** This method will return @ref True again after it returns @ref False once if data is not empty, otherwise it will always return @ref False.
    The iterator object should not be used after this method returns @ref False.

    @return @ref False if there are no more lines in the source (in which case the iterator object is invalid and should not be used); @ref True if successful (meaning that the iterator object is valid)

    @par Example:
    @code{.py}
while (i.next()) {
    printf("line: %y\n", i.getLine());
}
    @endcode
 */
abstract bool AbstractLineIterator::next();

//! Returns the current line in the data or throws an \c INVALID-ITERATOR exception if the iterator is invalid.
/** This method calls AbstractLineIterator::getLine() internally.
    @return the current line in the data

    @par Example:
    @code{.py}
while (i.next()) {
    printf("+ %y\n", i.getValue());
}
    @endcode

    @throw INVALID-ITERATOR the iterator is not pointing at a valid element

    @see AbstractLineIterator::getLine()
 */
abstract string AbstractLineIterator::getValue();

//! Returns the current line and splits the string into a list of components based on a separator string and a quote character
/** Retrieves additional lines if the last is quoted and is not terimated; this way fields can contain multiple lines

    The quote character can appear as the first part of a field, in which case it is assumed to designate the entire
    field.  If instances of the quote character are found in the field preceded by a backquote character (\c "\"),
    then these quote characters are included as part of the field's text and not treated as quote characters.\n
    Also the separator character can appear as a part of a field with this variant.\n
    This variant is useful for parsing CSV files, for example.

    @param sep the separator string; if the separator string is not found in the string to split, then a list with
    only one element containing the entire string argument is returned'; if this string has a different
    @ref character_encoding "character encoding" than \a str, then it will be converted to <em>str</em>'s
    @ref character_encoding "character encoding"
    @param quote the quote character
    @param eol the end of line string to be readded in case of a field split between lines
    @param trim_unquoted remove leading and trailing whitespace from unquoted fields

    @return a list of each component of a string separated by a separator string, with the separator and any enclosing
    quote characters removed

    @par Example:
    @code{.py}
list<string> list = "some,'text with spaces, and commas, here is another one! ,',here".split(",", "'");
# returns ("some", ", and commas, here is another one! ,", "here")
    @endcode

    @throw ENCODING-CONVERSION-ERROR this exception could be thrown if the string arguments have different
    @ref character_encoding "character encodings" and an error occurs during encoding conversion
    @throw SPLIT-ERROR field missing closing quote character; extra text following quoted field

    @since
    - %Qore 0.9.8 added this method
 */
list<string> AbstractLineIterator::getSplitLine(string sep, string quote, string eol = "\n",
        bool trim_unquoted = False) [flags=RET_VALUE_ONLY] {
    AbstractIteratorHelper h(xsink, "AbstractLineIterator::getSplitLine() method", self);
    if (*xsink) {
        return QoreValue();
    }
    QoreValue v = h.getValue(xsink);
    if (*xsink) {
        return QoreValue();
    }
    if (v.getType() != NT_STRING) {
        xsink->raiseException("SPLIT-ERROR", "expecting iterator value type 'string'; got type '%s' instead",
            v.getTypeName());
        return QoreValue();
    }
    const QoreStringNode* str = v.get<const QoreStringNode>();
    ReferenceHolder<QoreListNode> rv(split_with_quote(xsink, sep, str, quote, trim_unquoted, &h, eol), xsink);
    if (*xsink) {
        ExceptionSink xsink2;
        ValueHolder line_num(self->evalMethod("index", nullptr, &xsink2), xsink);
        if (line_num->getType() == NT_INT) {
            xsink->appendLastDescription(": error iterating input line " QLLD, (int)line_num->getAsBigInt());
        }
        return QoreValue();
    }
    return rv.release();
}

//! Returns the current line in the data or throws an \c INVALID-ITERATOR exception if the iterator is invalid.
/** @return the current line in the data

    @par Example:
    @code{.py}
while (i.next()) {
    printf("+ %y\n", i.getLine());
}
    @endcode

    @throw INVALID-ITERATOR the iterator is not pointing at a valid element
 */
abstract string AbstractLineIterator::getLine();

//! Returns @ref True if the iterator is currently pointing at a valid element, @ref False if not.
/** @return @ref True if the iterator is currently pointing at a valid element, @ref False if not

    @par Example:
    @code{.py}
if (i.valid())
    printf("current value: %y\n", i.getValue());
    @endcode
 */
abstract bool AbstractLineIterator::valid();

//! Returns the current iterator line number (the first line is line 1) or 0 if not pointing at a valid element.
/** @return the current iterator line number (the first line is line 1) or 0 if not pointing at a valid element

    @par Example:
    @code{.py}
while (i.next()) {
    printf("+ %d: %y\n", i.index(), i.getLine());
}
    @endcode
 */
abstract int AbstractLineIterator::index();

