Qore Programming Language Reference Manual  0.8.12.3
Qore::InputStreamLineIterator Class Reference

This class defines a line iterator for input streams. More...

Inheritance diagram for Qore::InputStreamLineIterator:

Public Member Functions

 constructor (Qore::InputStream is, *string encoding, *string eol, bool trim=True)
 Creates the InputStreamLineIterator for iterating over the given InputStream. More...
 
string getEncoding ()
 Returns the character encoding for the InputStreamLineIterator. More...
 
string getLine ()
 Returns the current line in the data or throws an ITERATOR-ERROR exception if the iterator is invalid. More...
 
string getValue ()
 Returns the current line in the data or throws an ITERATOR-ERROR exception if the iterator is invalid. More...
 
int index ()
 Returns the current iterator line number in the data (the first line is line 1) or 0 if not pointing at a valid element. More...
 
bool next ()
 Moves the current position to the next line in the data; returns 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. More...
 
bool valid ()
 Returns True if the iterator is currently pointing at a valid element, False if not. More...
 

Detailed Description

This class defines a line iterator for input streams.

Since
Qore 0.8.13
Example: InputStreamLineIterator basic usage
1 FileInputStream fis("log.txt");
2 InputStreamLineIterator it(fis, "\n");
3 while (it.next()) {
4  printf("line %d = %n\n", it.index(), it.getLine());
5 }
See also
Qore::DataLineIterator
Qore::FileLineIterator

Member Function Documentation

Qore::InputStreamLineIterator::constructor ( Qore::InputStream  is,
*string  encoding,
*string  eol,
bool  trim = True 
)

Creates the InputStreamLineIterator for iterating over the given InputStream.

Parameters
isthe InputStream to iterate over
encodingcharacter encoding of the data from input stream; if not ASCII-compatible, all data will be converted to UTF-8; if not present, the default character encoding is assumed
eolthe 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 "\n", "\r", or "\r\n"
trimif True the string return values for the lines iterated will be trimmed of the eol bytes
Exceptions
ENCODING-CONVERSION-ERRORthis exception could be thrown if the eol argument has a different character encoding from the data's and an error occurs during encoding conversion
string Qore::InputStreamLineIterator::getEncoding ( )

Returns the character encoding for the InputStreamLineIterator.

Code Flags:
CONSTANT
Example:
1 string encoding = i.getEncoding();
Returns
the character encoding for the InputStreamLineIterator
string Qore::InputStreamLineIterator::getLine ( )
virtual

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

Returns
the current line in the data or throws an ITERATOR-ERROR exception if the iterator is invalid
Code Flags:
RET_VALUE_ONLY
Example:
1 while (i.next()) {
2  printf("+ %y\n", i.getLine());
3 }
Exceptions
ITERATOR-ERRORthe iterator is not pointing at a valid element
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object
See also
InputStreamLineIterator::getValue()

Implements Qore::AbstractLineIterator.

string Qore::InputStreamLineIterator::getValue ( )
virtual

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

Returns
the current line in the data or throws an ITERATOR-ERROR exception if the iterator is invalid
Code Flags:
RET_VALUE_ONLY
Example:
1 while (i.next()) {
2  printf("+ %y\n", i.getValue());
3 }
Exceptions
ITERATOR-ERRORthe iterator is not pointing at a valid element
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object
See also
InputStreamLineIterator::getLine()

Implements Qore::AbstractLineIterator.

int Qore::InputStreamLineIterator::index ( )
virtual

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

Returns
the current iterator line number in the data (the first line is line 1) or 0 if not pointing at a valid element
Code Flags:
CONSTANT
Example:
1 while (i.next()) {
2  printf("+ %d: %y\n", i.index(), i.getValue());
3 }

Implements Qore::AbstractLineIterator.

bool Qore::InputStreamLineIterator::next ( )
virtual

Moves the current position to the next line in the data; returns 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.

The iterator object should not be used after this method returns False.

Returns
False if there are no more lines in the data (in which case the iterator object is invalid and should not be used); True if successful (meaning that the iterator object is valid)
Example:
1 while (i.next()) {
2  printf("line: %y\n", i.getValue());
3 }
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

Implements Qore::AbstractLineIterator.

bool Qore::InputStreamLineIterator::valid ( )
virtual

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
Code Flags:
CONSTANT
Example:
1 if (i.valid())
2  printf("current value: %y\n", i.getValue());

Implements Qore::AbstractLineIterator.