/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_DataLineIterator.qpp DataLineIterator class definition */
/*
  Qore Programming Language

  Copyright (C) 2003 - 2023 David Nichols

  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 <cerrno>
#include <cstring>

#include "qore/Qore.h"
#include "qore/intern/DataLineIterator.h"

//! This class defines a line iterator for string data
/** @since %Qore 0.8.12

    @par Example: DataLineIterator basic usage
    @code{.py}
string str = "a2ps-4.13-1332.1.x86_64
a2ps-devel-4.13-1332.1.x86_64
aaa_base-11.3-7.2.x86_64";

DataLineIterator it(str);
while (it.next()) {
    printf("line %d = %n\n", it.index(), it.getValue());
}

line 1 = "a2ps-4.13-1332.1.x86_64"
line 2 = "a2ps-devel-4.13-1332.1.x86_64"
line 3 = "aaa_base-11.3-7.2.x86_64"
    @endcode

    @see @ref Qore::InputStreamLineIterator
    @see @ref Qore::FileLineIterator
 */
qclass DataLineIterator [arg=DataLineIterator* i; ns=Qore; vparent=AbstractLineIterator];

//! creates the DataLineIterator based on the string given
/** @param str the string to iterate over; note that if this string is in a non-ascii-compatible character encoding, it will be converted to UTF-8 for processing in the constructor and the UTF-8 version of the string will be used for iterating in this object
    @param eol the optional end of line character(s) to use to detect lines in the data; if this string is not passed, then the end of line character(s) are detected automatically, and can be either \c "\n", \c "\r", or \c "\r\n"; if this string is passed and has a different @ref character_encoding "character encoding" from this object's (as determined by the \c encoding parameter), then it will be converted to the DataLineIterator's @ref character_encoding "character encoding"
    @param trim if @ref True "True" the string return values for the lines iterated will be trimmed of the eol bytes

    @throw ENCODING-CONVERSION-ERROR this exception could be thrown if the eol argument has a different @ref character_encoding "character encoding" from the data's and an error occurs during encoding conversion
 */
DataLineIterator::constructor(string str, *string eol, bool trim = True) {
   if (eol && eol->empty())
      eol = 0;

   SimpleRefHolder<DataLineIterator> dli(new DataLineIterator(xsink, str, eol, trim));
   if (*xsink)
      return;

   self->setPrivate(CID_DATALINEITERATOR, dli.release());
}

//! Creates a new DataLineIterator object, based on the same object being iterated in the original object
/** @par Example:
    @code{.py}
DataLineIterator ni = i.copy();
    @endcode
 */
DataLineIterator::copy() {
   SimpleRefHolder<DataLineIterator> dli(new DataLineIterator(xsink, *i));

   if (!*xsink)
      self->setPrivate(CID_DATALINEITERATOR, dli.release());
}

//! Moves the current position to the next line in the data; returns @ref False if there are no more lines to read; if the iterator is not pointing at a valid element before this call, the iterator will be positioned to the beginning of the data
/** 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 data (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.getValue());
}
    @endcode

    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object
 */
bool DataLineIterator::next() {
   if (i->check(xsink))
      return false;
   return i->next(xsink);
}

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

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

    @throw ITERATOR-ERROR the iterator is not pointing at a valid element
    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object

    @see DataLineIterator::getLine()
 */
string DataLineIterator::getValue() [flags=RET_VALUE_ONLY] {
   return i->checkValid(xsink) ? 0 : i->getValue();
}

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

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

    @throw ITERATOR-ERROR the iterator is not pointing at a valid element
    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object

    @see DataLineIterator::getValue()
 */
string DataLineIterator::getLine() [flags=RET_VALUE_ONLY] {
   return i->checkValid(xsink) ? 0 : i->getValue();
}

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

    @par Example:
    @code{.py}
if (i.valid())
    printf("current value: %y\n", i.getValue());
    @endcode
 */
bool DataLineIterator::valid() [flags=CONSTANT] {
   return i->valid();
}

//! returns the current iterator line number in the data (the first line is line 1) or 0 if not pointing at a valid element
/** @return the current iterator line number in the data (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.getValue());
}
    @endcode
 */
int DataLineIterator::index() [flags=CONSTANT] {
   return i->index();
}

//! Returns the @ref character_encoding "character encoding" for the %DataLineIterator
/** @par Example:
    @code{.py}
string encoding = f.getEncoding();
    @endcode

    @return the @ref character_encoding "character encoding" for the %DataLineIterator
 */
string DataLineIterator::getEncoding() [flags=CONSTANT] {
   return new QoreStringNode(i->getEncoding()->getCode());
}

//! Reset the iterator instance to its initial state
/** Reset the iterator instance to its initial state

   @par Example
   @code{.py}
i.reset();
   @endcode

    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object
 */
DataLineIterator::reset() {
   if (i->check(xsink))
      return QoreValue();
   i->reset(xsink);
}
