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

#include <evo/impl/sysio.h>

Inheritance diagram for IoWriter:
Inheritance graph
[legend]

Detailed Description

Buffered writer for IoDevice (used internally).

Public Member Functions

 IoWriter (ulong newsize=0, Newline nl=NL_SYS)
 Constructor to set new buffer size and newline value. More...
 
ulong avail () const
 Get size available. More...
 
ulong avail (ulong checksize) const
 Get size up to size available. More...
 
RawBufferclear ()
 Clear buffer. More...
 
void close ()
 Close output. More...
 
void flush (ulong &offset)
 Flush data already read from buffer using offset. More...
 
template<class T >
Error flush (T &out)
 Flush buffer by writing to device. More...
 
ulong insert (ulong index, ulong size=1)
 Make room to insert items. More...
 
RawBufferminsize (ulong min)
 Resize buffer if smaller than minimum size. More...
 
void open (bool flushlines_val=false)
 Initialize and open for output (writing). More...
 
ulong read (ulong &offset, char *buf, ulong readsize)
 Read from buffer using offset. More...
 
RawBufferref (char *data, ulong size, ulong used=0)
 Reference another buffer. More...
 
RawBufferremove (ulong index, ulong size=1)
 Remove items. More...
 
RawBufferreset ()
 Reset to empty state without a buffer. More...
 
RawBufferresize (ulong newsize)
 Resize buffer. More...
 
ulong write (const char *buf, ulong writesize)
 Write at end of buffer. More...
 
ulong write (char ch, ulong count=1)
 Write character at end of buffer. More...
 
template<class T >
ulong writebin (Error &err, T &out, const void *buf, ulong bufsize)
 Write data to device using buffer. More...
 
template<class T >
ulong writebin2 (Error &err, T &out, const void *buf, ulong bufsize, ulong count)
 Write repeated data to device using buffer. More...
 
template<class T >
ulong writebin_char (Error &err, T &out, char ch, ulong count)
 Write repeated character to device using buffer. More...
 
template<class TOut >
Error writefmtchar (TOut &out, char ch, ulong count, const FmtSetField &field)
 Write repeated character data to device using buffer, formatted with field alignment. More...
 
template<class TOut >
Error writefmtdump (TOut &out, const FmtDump &fmt, const char *newline, uint newlinesize)
 Write formatted buffer dump in hex. More...
 
template<class TOut , class TNum >
Error writefmtnum (TOut &out, TNum num, const FmtSetInt &fmt, const FmtSetField *field=NULL)
 Write formatted signed number to device using buffer, using field attributes. More...
 
template<class TOut , class TNum >
Error writefmtnumf (TOut &out, TNum num, const FmtSetFloat &fmt, const FmtSetField *field=NULL)
 Write formatted floating point number to device using buffer, using field attributes. More...
 
template<class TOut , class TNum >
Error writefmtnumu (TOut &out, TNum num, const FmtSetInt &fmt, const FmtSetField *field=NULL)
 Write formatted unsigned number to device using buffer, using field attributes. More...
 
template<class TOut >
Error writefmtstr (TOut &out, const char *str, ulong strsize, const FmtSetField &field)
 Write string data to device using buffer, formatted with field alignment. More...
 
template<class TOut , class TNum >
Error writenum (TOut &out, TNum num, int base=fDEC)
 Write formatted signed number to device using buffer. More...
 
template<class TOut , class TNum >
Error writenumf (TOut &out, TNum num, int precision=fPREC_AUTO)
 Write formatted floating-point number to device using buffer. More...
 
template<class TOut , class TNum >
Error writenumu (TOut &out, TNum num, int base=fDEC)
 Write formatted unsigned number to device using buffer. More...
 
template<class T >
ulong writetext (Error &err, T &out, const char *buf, ulong bufsize)
 Write data to device using buffer. More...
 
template<class T >
ulong writetext_char (Error &err, T &out, char ch, ulong count)
 Write data to device using buffer. More...
 

Public Attributes

char * data
 Buffer data pointer, NULL if empty (size=0) More...
 
bool flushlines
 Whether to flush after each line (aka line buffering) – only applies to text writes, i.e. writetext() More...
 
const char * newline
 Default newline string for formatting – do not modify. More...
 
uint newlinesize
 Size of default newline string to use, i.e. strlen(newline) – do not modify. More...
 
bool owned
 Whether this owns the buffer and must free it. More...
 
char partnl
 Used internally for handling partial newlines between writetext() calls. More...
 
ulong size
 Buffer size (capacity) in bytes – do not modify, use: resize(), minsize(), reset(), ref() More...
 
ulong timeout_ms
 Write timeout in milliseconds, 0 for none (no timeout) More...
 
ulong used
 Buffer size in use in bytes. More...
 

Static Public Attributes

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

Constructor & Destructor Documentation

◆ IoWriter()

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

Constructor to set new buffer size and newline value.

Parameters
newsizeNew buffer size
nlDefault newline value for formatting (defaults to NL_SYS)

Member Function Documentation

◆ avail() [1/2]

ulong avail ( ) const
inlineinherited

Get size available.

Returns
Size available at end (size - used)

◆ avail() [2/2]

ulong avail ( ulong  checksize) const
inlineinherited

Get size up to size available.

Parameters
checksizeSize to check against size available
Returns
Size available at end (size - used) or checksize, whichever is smaller

◆ clear()

RawBuffer& clear ( )
inlineinherited

Clear buffer.

  • This sets 'used' to 0
Returns
This

◆ close()

void close ( )
inline

Close output.

◆ flush() [1/2]

void flush ( ulong &  offset)
inlineinherited

Flush data already read from buffer using offset.

  • This moves data at offset to beginning of buffer to make room for more data
Parameters
offsetOffset to use and update, will be set to 0 [in/out]

◆ flush() [2/2]

Error flush ( T &  out)
inline

Flush buffer by writing to device.

  • This will clear out buffered data
Template Parameters
TIoDevice to flush and write to
Parameters
outDevice to write to
Returns
ENone on success, error code on error

◆ insert()

ulong insert ( ulong  index,
ulong  size = 1 
)
inlineinherited

Make room to insert items.

  • This shifts items over to make room and updates 'used' but doesn't actually insert
Parameters
indexInsert index, END to append
sizeInsert size in bytes, must be postive
Returns
Actual insert index

◆ minsize()

RawBuffer& minsize ( ulong  min)
inlineinherited

Resize buffer if smaller than minimum size.

Parameters
minMinimum size in bytes
Returns
This

◆ open()

void open ( bool  flushlines_val = false)
inline

Initialize and open for output (writing).

  • This creates the write buffer, if needed
Parameters
flushlines_valWhether to flush on each newline

◆ read()

ulong read ( ulong &  offset,
char *  buf,
ulong  readsize 
)
inlineinherited

Read from buffer using offset.

  • This copies from offset (data+offset) and increments offset
Parameters
offsetOffset to use and update [in/out]
bufBuffer to read/copy to
readsizeRead size in bytes, reduced if too large
Returns
Actual size read/copied, may be less than readsize if end reached (offset=used)

◆ ref()

RawBuffer& ref ( char *  data,
ulong  size,
ulong  used = 0 
)
inlineinherited

Reference another buffer.

  • Use to access and/or modify another buffer
  • This will not take ownership of the buffer (will not free it)
Parameters
dataBuffer data pointer, must not be NULL
sizeBuffer size (capacity) in bytes, must be > 0
usedBuffer size in use in bytes, must be <= size
Returns
This

◆ remove()

RawBuffer& remove ( ulong  index,
ulong  size = 1 
)
inlineinherited

Remove items.

  • This removes items and updates 'used'
Parameters
indexInsert index
sizeRemove size in bytes
Returns
This

◆ reset()

RawBuffer& reset ( )
inlineinherited

Reset to empty state without a buffer.

  • This is useful for clearing a reference to another buffer
Returns
This

◆ resize()

RawBuffer& resize ( ulong  newsize)
inlineinherited

Resize buffer.

  • This updates 'used' if the new size truncates items
  • If newsize is the same as current size this does nothing
  • If newsize is 0 this will free the buffer (if owned) and reset the data
  • If newsize is positive this will: allocate a new buffer, copy existing data if needed (up to 'used'), and free the old buffer (if owned)
Parameters
newsizeNew size in bytes, 0 to free
Returns
This

◆ write() [1/2]

ulong write ( const char *  buf,
ulong  writesize 
)
inlineinherited

Write at end of buffer.

  • This appends to buffer and increments used
  • If write is too big for buffer it will be truncated to fit
Parameters
bufBuffer to write from
writesizeWrite size in bytes, reduced if too large
Returns
Actual size written, may be less than writesize if buffer is full

◆ write() [2/2]

ulong write ( char  ch,
ulong  count = 1 
)
inlineinherited

Write character at end of buffer.

  • This appends to buffer and increments used
  • If count is too big for buffer it will be truncated to fit
Parameters
chCharacter to write
countCharacter repeat count, reduced if too large
Returns
Actual size written, may be less than count if buffer is full

◆ writebin()

ulong writebin ( Error err,
T &  out,
const void *  buf,
ulong  bufsize 
)
inline

Write data to device using buffer.

  • This will flush the buffer when full
  • Note: If writetext() was previously called then set partnl=0 before calling writetext() again
Template Parameters
TIoDevice to write to
Parameters
errStores ENone on success, error code on error [out]
outDevice to write to
bufData buffer to write from
bufsizeData size to write in bytes
Returns
Size actually written, 0 on error, see err

◆ writebin2()

ulong writebin2 ( Error err,
T &  out,
const void *  buf,
ulong  bufsize,
ulong  count 
)
inline

Write repeated data to device using buffer.

  • This will flush the buffer when full
  • Note: If writetext() was previously called then set partnl=0 before calling writetext() again
Template Parameters
TIoDevice to write to
Parameters
errStores ENone on success, error code on error [out]
outDevice to write to
bufData buffer to write repeatedly from
bufsizeData size to write in bytes
countData repeat count (2 = 2*bufsize)
Returns
Size actually written, 0 on error, see err

◆ writebin_char()

ulong writebin_char ( Error err,
T &  out,
char  ch,
ulong  count 
)
inline

Write repeated character to device using buffer.

  • This will flush the buffer when full
  • Note: If writetext() was previously called then set partnl=0 before calling writetext() again
Template Parameters
TIoDevice to write to
Parameters
errStores ENone on success, error code on error [out]
outDevice to write to
chCharacter to write
countCharacter repeat count to write
Returns
Size actually written, 0 on error, see err

◆ writefmtchar()

Error writefmtchar ( TOut &  out,
char  ch,
ulong  count,
const FmtSetField field 
)
inline

Write repeated character data to device using buffer, formatted with field alignment.

Template Parameters
TIoDevice to write to
Parameters
outDevice to write to
chCharacter to write
countCharacter repeat count to use
fieldField attributes to use
Returns
ENone on success, ELength if string and/or field-width are too long for buffer, error code on other error [out]

◆ writefmtdump()

Error writefmtdump ( TOut &  out,
const FmtDump fmt,
const char *  newline,
uint  newlinesize 
)
inline

Write formatted buffer dump in hex.

  • Output may span multiple lines, and always ends with a newline (unless dump data is empty)
  • This flushes the buffer as needed to make room
Template Parameters
TOutIoDevice to write to
Parameters
outDevice to write to
fmtFormat data, including buffer to dump
newlineNewline string to use
newlinesizeNewline string size
Returns
ENone on success, error code on error

◆ writefmtnum()

Error writefmtnum ( TOut &  out,
TNum  num,
const FmtSetInt fmt,
const FmtSetField field = NULL 
)
inline

Write formatted signed number to device using buffer, using field attributes.

  • This formats directly to buffer and requires the buffer is large enough
  • If needed this will flush buffer to make room
Template Parameters
TOutIoDevice to write to
TNumSigned number type
Parameters
outDevice to write to
numNumber to format and write
fmtInteger formatting attributes to use
fieldField formatting attributes to use, NULL for none
Returns
ENone on success, error code on error

◆ writefmtnumf()

Error writefmtnumf ( TOut &  out,
TNum  num,
const FmtSetFloat fmt,
const FmtSetField field = NULL 
)
inline

Write formatted floating point number to device using buffer, using field attributes.

  • This formats directly to buffer and requires the buffer is large enough
  • If needed this will flush buffer to make room
Template Parameters
TOutIoDevice to write to
TNumSigned number type
Parameters
outDevice to write to
numNumber to format and write
fmtFloating point formatting attributes to use
fieldField formatting attributes to use, NULL for none
Returns
ENone on success, error code on error

◆ writefmtnumu()

Error writefmtnumu ( TOut &  out,
TNum  num,
const FmtSetInt fmt,
const FmtSetField field = NULL 
)
inline

Write formatted unsigned number to device using buffer, using field attributes.

  • This formats directly to buffer and requires the buffer is large enough
  • If needed this will flush buffer to make room
Template Parameters
TOutIoDevice to write to
TNumSigned number type
Parameters
outDevice to write to
numNumber to format and write
fmtInteger formatting attributes to use
fieldField formatting attributes to use, NULL for none
Returns
ENone on success, error code on error

◆ writefmtstr()

Error writefmtstr ( TOut &  out,
const char *  str,
ulong  strsize,
const FmtSetField field 
)
inline

Write string data to device using buffer, formatted with field alignment.

  • This will flush the buffer when full
  • This does a text write, converting newlines per newline member
  • Newline conversion may change the number of bytes written internally
  • If readtext() was used on buf (newline conversion already done) then use write() instead for best performance
Template Parameters
TIoDevice to write to
Parameters
outDevice to write to
strString data buffer to write from
strsizeString data size to write in bytes
fieldField attributes to use
Returns
ENone on success, ELength if string and/or field-width are too long for buffer, error code on other error [out]

◆ writenum()

Error writenum ( TOut &  out,
TNum  num,
int  base = fDEC 
)
inline

Write formatted signed number to device using buffer.

  • This formats directly to buffer and requires the buffer is large enough
  • If needed this will flush buffer to make room
Template Parameters
TOutIoDevice to write to
TNumSigned number type
Parameters
outDevice to write to
numNumber to format and write
baseBase to use for formatting
Returns
ENone on success, error code on error

◆ writenumf()

Error writenumf ( TOut &  out,
TNum  num,
int  precision = fPREC_AUTO 
)
inline

Write formatted floating-point number to device using buffer.

  • This formats directly to buffer and requires the buffer is large enough
  • If needed this will flush buffer to make room
Template Parameters
TOutIoDevice to write to
TNumFloating-point number number type
Parameters
outDevice to write to
numNumber to format and write
precisionFormatting precision (number of fractional digits), 0 for none, fPREC_AUTO for automatic
Returns
ENone on success, error code on error

◆ writenumu()

Error writenumu ( TOut &  out,
TNum  num,
int  base = fDEC 
)
inline

Write formatted unsigned number to device using buffer.

  • This formats directly to buffer and requires the buffer is large enough
  • If needed this will flush buffer to make room
Template Parameters
TOutIoDevice to write to
TNumUnsigned number type
Parameters
outDevice to write to
numNumber to format and write
baseBase to use for formatting
Returns
ENone on success, error code on error

◆ writetext()

ulong writetext ( Error err,
T &  out,
const char *  buf,
ulong  bufsize 
)
inline

Write data to device using buffer.

  • This will flush the buffer when full
  • This does a text write, converting newlines per newline member
  • Newline conversion may change the number of bytes written internally
  • If readtext() was used on buf (newline conversion already done) then use write() instead for best performance
Template Parameters
TIoDevice to write to
Parameters
errStores ENone on success, error code on error [out]
outDevice to write to
bufData buffer to write from
bufsizeData size to write in bytes
Returns
Size actually written, 0 on error, see err

◆ writetext_char()

ulong writetext_char ( Error err,
T &  out,
char  ch,
ulong  count 
)
inline

Write data to device using buffer.

  • This will flush the buffer when full
  • This does a text write, converting newlines per newline member
  • Newline conversion may change the number of bytes written internally
    • Note that writing newline text characters 1 char at a time or by string will give the same end result either way – the edge cases are covered
  • This handles newline/newline-pair conversion consistently with calling writetext() with same data
  • If readtext() was used on buf (newline conversion already done) then use write() instead for best performance
Template Parameters
TIoDevice to write to
Parameters
errStores ENone on success, error code on error [out]
outDevice to write to
chData buffer to write from
countData size to write in bytes
Returns
Size actually written (could be 0 in certain newline-pair cases with count=1), 0 on error, see err

Member Data Documentation

◆ data

char* data
inherited

Buffer data pointer, NULL if empty (size=0)

◆ DEFSIZE

const ulong DEFSIZE = 16384
static

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

◆ flushlines

bool flushlines

Whether to flush after each line (aka line buffering) – only applies to text writes, i.e. writetext()

◆ newline

const char* newline

Default newline string for formatting – do not modify.

◆ newlinesize

uint newlinesize

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

◆ owned

bool owned
inherited

Whether this owns the buffer and must free it.

◆ partnl

char partnl

Used internally for handling partial newlines between writetext() calls.

◆ size

ulong size
inherited

Buffer size (capacity) in bytes – do not modify, use: resize(), minsize(), reset(), ref()

◆ timeout_ms

ulong timeout_ms

Write timeout in milliseconds, 0 for none (no timeout)

◆ used

ulong used
inherited

Buffer size in use in bytes.


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