Evo C++ Library v0.5.1
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
IoReader Struct Reference

#include <evo/impl/sysio.h>

Detailed Description

Buffered reader for IoDevice (used internally).

Public Member Functions

 IoReader (ulong newsize=0, Newline nl=NL_SYS)
 Constructor to set new buffer size. More...
 
void close ()
 Close input. More...
 
template<class T >
Error fill (T &in, ulong minsize=0)
 Reset and fill buffer by reading from file. More...
 
void open ()
 Initialize and open for input (reading). More...
 
template<class T >
ulong readbin (Error &err, T &in, void *buf, ulong bufsize)
 Read from file using buffer. More...
 
template<class T >
Error readline (String &str, T &in, ulong maxlen=0)
 Read a line from file using buffer. More...
 
template<class T >
ulong readtext (Error &err, T &in, char *buf, ulong bufsize)
 Read from file using buffer. More...
 

Public Attributes

RawBuffercurbuf
 Pointer to current buffer, either primary buffer or from last filter applied. More...
 
ulong curbuf_offset
 Bytes read from curbuf, i.e. buffer start offset. More...
 
const char * newline
 Newline string to convert to when reading text – do not modify. More...
 
uint newlinesize
 Size of newlines string to use, i.e. strlen(newline) – do not modify. More...
 
char rd_partnl
 Used by readtext() in special case, holds end of converted newline that didn't fit in buf or 0. More...
 
RawBuffer readbuf
 Primary read buffer – filtering may involve additional buffers. More...
 
char rl_partnl
 Used by readline() on partial newlines, holds next expected char for newline pair or 0. More...
 
ulong timeout_ms
 Read timeout in milliseconds, 0 for none (don't timeout) More...
 

Static Public Attributes

static const ulong DEFSIZE = 8192
 Default buffer size (8KB, power of 2 and multiple of common filesystem block size 4KB) More...
 

Constructor & Destructor Documentation

◆ IoReader()

IoReader ( ulong  newsize = 0,
Newline  nl = NL_SYS 
)
inline

Constructor to set new buffer size.

Parameters
newsizeNew buffer size, 0 for default
nlNewline value for text reading to convert newlines to (defaults to NL_SYS), doesn't affect reading by line

Member Function Documentation

◆ close()

void close ( )
inline

Close input.

◆ fill()

Error fill ( T &  in,
ulong  minsize = 0 
)
inline

Reset and fill buffer by reading from file.

  • This will first reset buffer moving data at pos to beginning (and set pos to 0), data before pos is lost
  • Next this will read from file and fill buffer until full, minsize reached, end-of-file is reached, or an error occurs
  • Data may still be in buffer after end-of-file is reached
  • The minsize parameter is useful when input is trickling in (slow network or user typing)
Template Parameters
TIoDevice to read from
Parameters
inFile to read from
minsizeMinimum size to fill (capped to buffer size if greater), 0 for whole buffer
Returns
ENone on success, EEnd if end-of-file reached, error code on other error

◆ open()

void open ( )
inline

Initialize and open for input (reading).

  • This creates the read buffer, if needed

◆ readbin()

ulong readbin ( Error err,
T &  in,
void *  buf,
ulong  bufsize 
)
inline

Read from file using buffer.

  • May return less than requested
  • This does a binary read – no conversion on newlines
Template Parameters
TIoDevice to read from
Parameters
errStores ENone on success, error code on error [out]
inFile to read from
bufBuffer to store data read
bufsizeSize in bytes to read from file (must be positive)
Returns
Bytes read, 0 if end-of-file or error (check err)

◆ readline()

Error readline ( String str,
T &  in,
ulong  maxlen = 0 
)
inline

Read a line from file using buffer.

  • This will read and return the next line from file as a string, not including the newline
  • This recognizes '\n', '\r', or either combination of the two as a newline
Template Parameters
TIoDevice to read from
Parameters
strString to store line (cleared first) [out]
inFile to read from
maxlenMaximum line length, 0 for no limit
Returns
ENone on success, EEnd if no more lines (end-of-file), EOutOfBounds if line exceeds maxlen, or error code on error

◆ readtext()

ulong readtext ( Error err,
T &  in,
char *  buf,
ulong  bufsize 
)
inline

Read from file using buffer.

  • May return less than requested
  • This does a text read, converting newlines per newline member
  • This recognizes '\n', '\r', or either combination of the two as a newline
  • Note: This will try to avoid stopping in the middle of a newline pair by reading 1 less byte, if possible
    • If forced to break up a newline pair due to bufsize=1 (not recommended), the remaining newline byte is saved until the next call to readtext()
    • However, calling readline() after readtext() in this special case will cause readline() to return an ELoss error
    • If this happens the solution is to read the remaining newline byte with bufsize=1, to detect this case:
    • This is only a problem if you mix readtext() (with bufsize=1) and readline() calls under these conditions
Template Parameters
TIoDevice to read from
Parameters
errStores ENone on success, error code on error [out]
inFile to read from
bufBuffer to store data read
bufsizeSize in bytes to read from file (must be positive)
Returns
Bytes read, 0 if end-of-file or error (check err)

Member Data Documentation

◆ curbuf

RawBuffer* curbuf

Pointer to current buffer, either primary buffer or from last filter applied.

◆ curbuf_offset

ulong curbuf_offset

Bytes read from curbuf, i.e. buffer start offset.

◆ DEFSIZE

const ulong DEFSIZE = 8192
static

Default buffer size (8KB, power of 2 and multiple of common filesystem block size 4KB)

◆ newline

const char* newline

Newline string to convert to when reading text – do not modify.

◆ newlinesize

uint newlinesize

Size of newlines string to use, i.e. strlen(newline) – do not modify.

◆ rd_partnl

char rd_partnl

Used by readtext() in special case, holds end of converted newline that didn't fit in buf or 0.

◆ readbuf

RawBuffer readbuf

Primary read buffer – filtering may involve additional buffers.

◆ rl_partnl

char rl_partnl

Used by readline() on partial newlines, holds next expected char for newline pair or 0.

◆ timeout_ms

ulong timeout_ms

Read timeout in milliseconds, 0 for none (don't timeout)


The documentation for this struct was generated from the following file: