fastbuffer.h File Reference
`#include <gwenhywfar/syncio.h>`
`#include <gwenhywfar/buffer.h>`

Go to the source code of this file.

## Data Structures

struct | GWEN_FAST_BUFFER

## Macros

#define | GWEN_FAST_BUFFER_FLAGS_DOSMODE 0x00000001
#define | GWEN_FASTBUFFER_COPYBYTES(fb1, fb2, var, len)
#define | GWEN_FASTBUFFER_COPYFORCED(fb1, fb2, var, p, len)
#define | GWEN_FASTBUFFER_FLUSH(fb, var)
#define | GWEN_FASTBUFFER_PEEKBYTE(fb, var)
#define | GWEN_FASTBUFFER_READBYTE(fb, var)
#define | GWEN_FASTBUFFER_READBYTES(fb, var, p, len)
#define | GWEN_FASTBUFFER_READFORCED(fb, var, p, len)
#define | GWEN_FASTBUFFER_READLINE(fb, var, p, len)
#define | GWEN_FASTBUFFER_READLINEFORCED(fb, var, p, len)
#define | GWEN_FASTBUFFER_WRITEBYTE(fb, var, chr)
#define | GWEN_FASTBUFFER_WRITEBYTES(fb, var, p, len)
#define | GWEN_FASTBUFFER_WRITEFORCED(fb, var, p, len)
#define | GWEN_FASTBUFFER_WRITELINE(fb, var, p)

## Functions

GWENHYWFAR_API void | GWEN_FastBuffer_AddFlags (GWEN_FAST_BUFFER *fb, uint32_t fl)
GWENHYWFAR_API void | GWEN_FastBuffer_free (GWEN_FAST_BUFFER *fb)
GWENHYWFAR_API uint32_t | GWEN_FastBuffer_GetBytesRead (const GWEN_FAST_BUFFER *fb)
GWENHYWFAR_API uint32_t | GWEN_FastBuffer_GetBytesWritten (const GWEN_FAST_BUFFER *fb)
GWENHYWFAR_API uint32_t | GWEN_FastBuffer_GetFlags (const GWEN_FAST_BUFFER *fb)
GWENHYWFAR_API GWEN_FAST_BUFFER * | GWEN_FastBuffer_new (uint32_t bsize, GWEN_SYNCIO *io)
GWENHYWFAR_API int | GWEN_FastBuffer_ReadLine (GWEN_FAST_BUFFER *fb, uint8_t *p, int len)
GWENHYWFAR_API int | GWEN_FastBuffer_ReadLineToBuffer (GWEN_FAST_BUFFER *fb, GWEN_BUFFER *buf)
GWENHYWFAR_API void | GWEN_FastBuffer_SetFlags (GWEN_FAST_BUFFER *fb, uint32_t fl)
GWENHYWFAR_API void | GWEN_FastBuffer_SubFlags (GWEN_FAST_BUFFER *fb, uint32_t fl)

## Macro Definition Documentation

## GWEN_FAST_BUFFER_FLAGS_DOSMODE

#define GWEN_FAST_BUFFER_FLAGS_DOSMODE 0x00000001

Definition at line 19 of file fastbuffer.h.

## GWEN_FASTBUFFER_COPYBYTES

#define GWEN_FASTBUFFER_COPYBYTES | ( |  | fb1,
|  |  | fb2,
|  |  | var,
|  |  | len )

Copy a number of bytes from one buffer to another one.

Parameters

  fb1 | source fast buffer
  fb2 | destination fast buffer
  var | variable to receive the result (<0: error code, number of bytes read otherwise)
  len | number of bytes to copy

Definition at line 437 of file fastbuffer.h.

## GWEN_FASTBUFFER_COPYFORCED

#define GWEN_FASTBUFFER_COPYFORCED | ( |  | fb1,
|  |  | fb2,
|  |  | var,
|  |  | p,
|  |  | len )

Value:
{\
int fb_copyforced_len;\
uint8_t *fb_copyforced_p;\
\
fb_copyforced_len=len;\
fb_copyforced_p=(uint8_t*)p;\
var=0;\
while(fb_copyforced_len && var==0) {\
int fb_copyforced_rv;\
\
GWEN_FASTBUFFER_COPYBYTES(fb1, fb2, fb_copyforced_rv, fb_copyforced_p, fb_copyforced_len);\
if (fb_copyforced_rv<0) {\
var=fb_copyforced_rv;\
break;\
}\
else if (fb_copyforced_rv==0)\
var=GWEN_ERROR_EOF;\
else {\
fb_len-=fb_copyforced_rv;\
fb_p+=fb_copyforced_rv;\
}\
}\
}
GWEN_ERROR_EOF
#define GWEN_ERROR_EOF
Definition error.h:96

Copy a number of bytes to the buffer and make sure that all bytes are copied.

Parameters

  fb1 | source fast buffer
  fb2 | destination fast buffer
  var | variable to receive the result (<0: error code, 0 on success)
  p | pointer to the location to write the bytes from
  len | number of bytes to copy

Definition at line 481 of file fastbuffer.h.

## GWEN_FASTBUFFER_FLUSH

#define GWEN_FASTBUFFER_FLUSH | ( |  | fb,
|  |  | var )

Value:
{\
int fb_flush_rv; \
\
fb_flush_rv=GWEN_SyncIo_WriteForced(fb->io, fb->buffer, fb->bufferWritePos); \
if (fb_flush_rv<(int)(fb->bufferWritePos)) { \
DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", fb_flush_rv); \
var=fb_flush_rv; \
} \
else { \
var=0; \
fb->bufferWritePos=0; \
} \
}
GWEN_LOGDOMAIN
#define GWEN_LOGDOMAIN
Definition logger.h:32
GWEN_SyncIo_WriteForced
GWENHYWFAR_API int GWEN_SyncIo_WriteForced(GWEN_SYNCIO *sio, const uint8_t *buffer, uint32_t size)

Flushes the write buffer (i.e. write all remaining bytes from the buffer to the io layer with the flag GWEN_IO_REQUEST_FLAGS_FLUSH set).

Definition at line 162 of file fastbuffer.h.

## GWEN_FASTBUFFER_PEEKBYTE

#define GWEN_FASTBUFFER_PEEKBYTE | ( |  | fb,
|  |  | var )

Value:
{\
if (fb->bufferReadPos>=fb->bufferWritePos) { \
int fb_peekbyte_rv; \
\
fb_peekbyte_rv=GWEN_SyncIo_Read(fb->io, fb->buffer, fb->bufferSize); \
if (fb_peekbyte_rv<0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", fb_peekbyte_rv); \
var=fb_peekbyte_rv; \
} \
else if (fb_peekbyte_rv==0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "EOF met"); \
var=GWEN_ERROR_EOF; \
} \
else { \
fb->bufferWritePos=fb_peekbyte_rv; \
fb->bufferReadPos=0; \
var=((int)((fb->buffer[fb->bufferReadPos])) & 0xff); \
} \
} \
else { \
var=((int)((fb->buffer[fb->bufferReadPos])) & 0xff); \
} \
}
GWEN_SyncIo_Read
GWENHYWFAR_API int GWEN_SyncIo_Read(GWEN_SYNCIO *sio, uint8_t *buffer, uint32_t size)

This macro peeks at the read buffer and returns the next available byte (if any). Consecutive peeks will always return the same byte. Also, the next GWEN_FASTBUFFER_READBYTE will return the same byte as well.

Definition at line 74 of file fastbuffer.h.

## GWEN_FASTBUFFER_READBYTE

#define GWEN_FASTBUFFER_READBYTE | ( |  | fb,
|  |  | var )

Value:
{\
if (fb->bufferReadPos>=fb->bufferWritePos) { \
int fb_readbyte_rv; \
\
fb_readbyte_rv=GWEN_SyncIo_Read(fb->io, fb->buffer, fb->bufferSize); \
if (fb_readbyte_rv<0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", fb_readbyte_rv); \
var=fb_readbyte_rv; \
} \
else if (fb_readbyte_rv==0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "EOF met"); \
var=GWEN_ERROR_EOF; \
} \
else { \
fb->bufferWritePos=fb_readbyte_rv; \
fb->bufferReadPos=0; \
var=((int)((fb->buffer[fb->bufferReadPos++])) & 0xff); \
fb->bytesRead++; \
} \
} \
else { \
var=((int)((fb->buffer[fb->bufferReadPos++])) & 0xff); \
fb->bytesRead++; \
} \
}

Returns the next byte from the buffer (if any). That byte will be placed into "var". In case of an error var will contain an error code instead.

Definition at line 103 of file fastbuffer.h.

## GWEN_FASTBUFFER_READBYTES

#define GWEN_FASTBUFFER_READBYTES | ( |  | fb,
|  |  | var,
|  |  | p,
|  |  | len )

Value:
{ \
int fb_readbyte_bytes;\
\
var=0; \
if (fb->bufferReadPos>=fb->bufferWritePos) { \
int fb_readbyte_rv; \
\
fb_readbyte_rv=GWEN_SyncIo_Read(fb->io, fb->buffer, fb->bufferSize); \
if (fb_readbyte_rv<0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", fb_readbyte_rv); \
var=fb_readbyte_rv; \
} \
else {\
fb->bufferWritePos=fb_readbyte_rv; \
fb->bufferReadPos=0; \
}\
}\
if (var==0) {\
fb_readbyte_bytes=fb->bufferWritePos-fb->bufferReadPos;\
if (fb_readbyte_bytes>len)\
fb_readbyte_bytes=len;\
if (fb_readbyte_bytes) {\
memmove(p, fb->buffer+fb->bufferReadPos, fb_readbyte_bytes);\
fb->bufferReadPos+=fb_readbyte_bytes;\
fb->bytesRead+=fb_readbyte_bytes; \
}\
var=fb_readbyte_bytes;\
}\
}

Reads a number of bytes from the buffer and stores it at the given memory location.

Parameters

  fb | fast buffer
  var | variable to receive the result (<0: error code, number of bytes read otherwise)
  p | pointer to the location to read the bytes to
  len | number of bytes to read

Definition at line 184 of file fastbuffer.h.

## GWEN_FASTBUFFER_READFORCED

#define GWEN_FASTBUFFER_READFORCED | ( |  | fb,
|  |  | var,
|  |  | p,
|  |  | len )

Value:
{\
int fb_readforced_len;\
uint8_t *fb_readforced_p;\
\
fb_readforced_len=len;\
fb_readforced_p=(uint8_t*)p;\
var=0;\
while(fb_readforced_len && var==0) {\
int fb_readforced_rv;\
\
GWEN_FASTBUFFER_READBYTES(fb, fb_readforced_rv, fb_readforced_p, fb_readforced_len);\
if (fb_readforced_rv<0) {\
var=fb_readforced_rv;\
break;\
}\
else if (fb_readforced_rv==0) {\
var=GWEN_ERROR_EOF;\
break;\
}\
else {\
fb_readforced_len-=fb_readforced_rv;\
fb_readforced_p+=fb_readforced_rv;\
}\
}\
}

Definition at line 301 of file fastbuffer.h.

## GWEN_FASTBUFFER_READLINE

#define GWEN_FASTBUFFER_READLINE | ( |  | fb,
|  |  | var,
|  |  | p,
|  |  | len )

Definition at line 216 of file fastbuffer.h.

## GWEN_FASTBUFFER_READLINEFORCED

#define GWEN_FASTBUFFER_READLINEFORCED | ( |  | fb,
|  |  | var,
|  |  | p,
|  |  | len )

Definition at line 264 of file fastbuffer.h.

## GWEN_FASTBUFFER_WRITEBYTE

#define GWEN_FASTBUFFER_WRITEBYTE | ( |  | fb,
|  |  | var,
|  |  | chr )

Value:
{\
if (fb->bufferWritePos>=fb->bufferSize) { \
int fb_writeByte_rv; \
\
fb_writeByte_rv=GWEN_SyncIo_WriteForced(fb->io, fb->buffer, fb->bufferWritePos); \
if (fb_writeByte_rv<(int)(fb->bufferWritePos)) { \
DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", fb_writeByte_rv); \
var=fb_writeByte_rv; \
} \
else { \
var=0; \
fb->bufferWritePos=0; \
fb->buffer[fb->bufferWritePos++]=chr; \
fb->bytesWritten++; \
} \
} \
else { \
var=0; \
fb->buffer[fb->bufferWritePos++]=chr; \
fb->bytesWritten++; \
} \
}

Writes a byte into the buffer (flushing it if necessary) and returns the result of this operation in "var".

Definition at line 134 of file fastbuffer.h.

## GWEN_FASTBUFFER_WRITEBYTES

#define GWEN_FASTBUFFER_WRITEBYTES | ( |  | fb,
|  |  | var,
|  |  | p,
|  |  | len )

Write a number of bytes to the buffer and stores it at the given memory location.

Parameters

  fb | fast buffer
  var | variable to receive the result (<0: error code, number of bytes read otherwise)
  p | pointer to the location to write the bytes from
  len | number of bytes to write

Definition at line 336 of file fastbuffer.h.

## GWEN_FASTBUFFER_WRITEFORCED

#define GWEN_FASTBUFFER_WRITEFORCED | ( |  | fb,
|  |  | var,
|  |  | p,
|  |  | len )

Value:
{\
int fb_writeforced_len;\
const uint8_t *fb_writeforced_p;\
\
fb_writeforced_len=len;\
if (fb_writeforced_len==-1) \
fb_writeforced_len=strlen((const char*)p);\
fb_writeforced_p=(const uint8_t*)p;\
var=0;\
while(fb_writeforced_len && var==0) {\
int fb_writeforced_rv;\
\
GWEN_FASTBUFFER_WRITEBYTES(fb, fb_writeforced_rv, fb_writeforced_p, fb_writeforced_len);\
if (fb_writeforced_rv<0) {\
var=fb_writeforced_rv;\
break;\
}\
else if (fb_writeforced_rv==0) {\
var=GWEN_ERROR_EOF;\
break;\
}\
else {\
fb_writeforced_len-=fb_writeforced_rv;\
fb_writeforced_p+=fb_writeforced_rv;\
}\
}\
}

Write a number of bytes to the buffer and make sure that all bytes are written.

Parameters

  fb | fast buffer
  var | variable to receive the result (<0: error code, 0 on success)
  p | pointer to the location to write the bytes from
  len | number of bytes to write

Definition at line 377 of file fastbuffer.h.

## GWEN_FASTBUFFER_WRITELINE

#define GWEN_FASTBUFFER_WRITELINE | ( |  | fb,
|  |  | var,
|  |  | p )

Value:
{\
int fb_writeline_rv;\
int fb_writeline_len=strlen((const char*)p);\
\
GWEN_FASTBUFFER_WRITEFORCED(fb, fb_writeline_rv, p, fb_writeline_len);\
if (fb_writeline_rv<0)\
var=fb_writeline_rv;\
else {\
if (fb->flags & GWEN_FAST_BUFFER_FLAGS_DOSMODE) {\
GWEN_FASTBUFFER_WRITEFORCED(fb, fb_writeline_rv, "\r\n", 2);\
}\
else {\
GWEN_FASTBUFFER_WRITEFORCED(fb, fb_writeline_rv, "\n", 1);\
}\
if (fb_writeline_rv<0)\
var=fb_writeline_rv;\
else\
var=0;\
}\
}
GWEN_FAST_BUFFER_FLAGS_DOSMODE
#define GWEN_FAST_BUFFER_FLAGS_DOSMODE
Definition fastbuffer.h:19

Definition at line 407 of file fastbuffer.h.

## Function Documentation

## GWEN_FastBuffer_AddFlags()

GWENHYWFAR_API void GWEN_FastBuffer_AddFlags | ( | GWEN_FAST_BUFFER * | fb,
|  | uint32_t | fl )

References GWENHYWFAR_API.

## GWEN_FastBuffer_free()

GWENHYWFAR_API void GWEN_FastBuffer_free | ( | GWEN_FAST_BUFFER * | fb | ) |

References GWENHYWFAR_API.

## GWEN_FastBuffer_GetBytesRead()

GWENHYWFAR_API uint32_t GWEN_FastBuffer_GetBytesRead | ( | const GWEN_FAST_BUFFER * | fb | ) |

References GWENHYWFAR_API.

## GWEN_FastBuffer_GetBytesWritten()

GWENHYWFAR_API uint32_t GWEN_FastBuffer_GetBytesWritten | ( | const GWEN_FAST_BUFFER * | fb | ) |

References GWENHYWFAR_API.

## GWEN_FastBuffer_GetFlags()

GWENHYWFAR_API uint32_t GWEN_FastBuffer_GetFlags | ( | const GWEN_FAST_BUFFER * | fb | ) |

References GWENHYWFAR_API.

## GWEN_FastBuffer_new()

GWENHYWFAR_API GWEN_FAST_BUFFER * GWEN_FastBuffer_new | ( | uint32_t | bsize,
|  | GWEN_SYNCIO * | io )

References GWENHYWFAR_API.

## GWEN_FastBuffer_ReadLine()

GWENHYWFAR_API int GWEN_FastBuffer_ReadLine | ( | GWEN_FAST_BUFFER * | fb,
|  | uint8_t * | p,
|  | int | len )

References GWENHYWFAR_API.

## GWEN_FastBuffer_ReadLineToBuffer()

GWENHYWFAR_API int GWEN_FastBuffer_ReadLineToBuffer | ( | GWEN_FAST_BUFFER * | fb,
|  | GWEN_BUFFER * | buf )

References GWENHYWFAR_API.

## GWEN_FastBuffer_SetFlags()

GWENHYWFAR_API void GWEN_FastBuffer_SetFlags | ( | GWEN_FAST_BUFFER * | fb,
|  | uint32_t | fl )

References GWENHYWFAR_API.

## GWEN_FastBuffer_SubFlags()

GWENHYWFAR_API void GWEN_FastBuffer_SubFlags | ( | GWEN_FAST_BUFFER * | fb,
|  | uint32_t | fl )

References GWENHYWFAR_API.