| Qore Programming Language Reference Manual 2.2.0

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Qore::AbstractLineIterator Class Reference

This class defines an abstract interface for line iterators. More...

`#include <QC_AbstractLineIterator.dox.h>`

Inheritance diagram for Qore::AbstractLineIterator:
[legend]

## Public Member Functions
abstract string | getLine ()
| Returns the current line in the data or throws an `INVALID-ITERATOR` exception if the iterator is invalid.
list< string > | getSplitLine (string sep, string quote, string eol="\n", bool trim_unquoted=False)
| Returns the current line and splits the string into a list of components based on a separator string and a quote character.
abstract string | getValue ()
| Returns the current line in the data or throws an `INVALID-ITERATOR` exception if the iterator is invalid.
abstract int | index ()
| Returns the current iterator line number (the first line is line 1) or 0 if not pointing at a valid element.
abstract bool | next ()
| Moves the current position to the next line; returns False if there are no more lines to read.
abstract bool | valid ()
| Returns True if the iterator is currently pointing at a valid element, False if not.
Public Member Functions inherited from Qore::AbstractIterator
abstract auto | getValue ()
| returns the current value
abstract bool | next ()
| Moves the current position to the next element; returns False if there are no more elements.
abstract bool | valid ()
| returns True if the iterator is currently pointing at a valid element, False if not

## Detailed Description

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

## Member Function Documentation

## ◆ getLine()

abstract string Qore::AbstractLineIterator::getLine | ( |  | ) |

Returns the current line in the data or throws an `INVALID-ITERATOR` exception if the iterator is invalid.

Returns
  the current line in the data

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

Exceptions

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

## ◆ getSplitLine()

list< string > Qore::AbstractLineIterator::getSplitLine | ( | string | sep,
|  | string | quote,
|  | string | eol = "\n",
|  | bool | trim_unquoted = False )

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 (`"\"), 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. @par Code Flags: @ref RET_VALUE_ONLY @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

Exceptions

  SPLIT-ERROR | field missing closing quote character; extra text following quoted field

Since
  - Qore 0.9.8 added this method

## ◆ getValue()

abstract string Qore::AbstractLineIterator::getValue | ( |  | ) |

Returns the current line in the data or throws an `INVALID-ITERATOR` exception if the iterator is invalid.

This method calls AbstractLineIterator::getLine() internally.

Returns
  the current line in the data

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

Exceptions

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

See also
  AbstractLineIterator::getLine()

## ◆ index()

abstract int Qore::AbstractLineIterator::index | ( |  | ) |

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

Returns
  the current iterator line number (the first line is line 1) or 0 if not pointing at a valid element

Example:
  while (i.next()) {
  printf("+ %d: %y\n", i.index(), i.getLine());
  }

## ◆ next()

abstract bool Qore::AbstractLineIterator::next | ( |  | ) |

Moves the current position to the next line; returns False if there are no more lines to read.

This method will return True again after it returns False once if data is not empty, otherwise it will always return False. The iterator object should not be used after this method returns False.

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

Example:
  while (i.next()) {
  printf("line: %y\n", i.getLine());
  }

## ◆ valid()

abstract bool Qore::AbstractLineIterator::valid | ( |  | ) |

Returns True if the iterator is currently pointing at a valid element, False if not.

Returns
  True if the iterator is currently pointing at a valid element, False if not

Example:
  if (i.valid())
  printf("current value: %y\n", i.getValue());