librnr  1.14.5
RoadNarrows Robotics Common Library 1
simplebuf.h File Reference

Simple [io] buffer declarations and operations. More...

#include <unistd.h>
#include "rnrconfig.h"

Go to the source code of this file.

Classes

struct  SimpleBuf_T
 

Macros

#define SIMPLEBUF_ABS   0
 absolute
 
#define SIMPLEBUF_REL   1
 relative
 

Functions

SimpleBuf_TSimpleBufNew ()
 Allocate a new simple buffer with no internal buffer allocation. More...
 
SimpleBuf_TSimpleBufNewWithBuf (size_t nBufSize)
 Allocate a new simple buffer with internal buffer of nBufSize. More...
 
void SimpleBufDelete (SimpleBuf_T *pBuf)
 Delete a simple buffer along with the internal buffer. More...
 
void SimpleBufSetBuf (SimpleBuf_T *pBuf, byte_t *pRWBuf, size_t nBufSize, size_t nLen)
 Set simple buffer's internal read/write buffer. More...
 
size_t SimpleBufCopy (SimpleBuf_T *pTgt, SimpleBuf_T *pSrc)
 Copy contents of source simple buffer to the end of the target simple buffer. More...
 
static void SimpleBufClear (SimpleBuf_T *pBuf)
 Clear contents of buffer. More...
 
static size_t SimpleBufReadSeek (SimpleBuf_T *pBuf, size_t rpos, int how)
 Seek buffer read head to the new read position. More...
 
static size_t SimpleBufWriteSeek (SimpleBuf_T *pBuf, size_t wpos, int how)
 Seek buffer write head to the new write position. More...
 
static size_t SimpleBufHasSize (SimpleBuf_T *pBuf)
 Returns size of buffer. More...
 
static bool_t SimpleBufIsEmpty (SimpleBuf_T *pBuf)
 Test if simple buffer is empty. More...
 
static bool_t SimpleBufIsFull (SimpleBuf_T *pBuf)
 Test if simple buffer is full. More...
 
static size_t SimpleBufHasLen (SimpleBuf_T *pBuf)
 Returns number of bytes currently in buffer. More...
 
static size_t SimpleBufHasAvail (SimpleBuf_T *pBuf)
 Returns number of bytes available in buffer for writing. More...
 
static byte_tSimpleBufGetReadPtr (SimpleBuf_T *pBuf)
 Returns pointer to internal I/O buffer at the current read position. More...
 
static byte_tSimpleBufGetWritePtr (SimpleBuf_T *pBuf)
 Returns pointer to internal I/O buffer at the current write position. More...
 
static int SimpleBufGetC (SimpleBuf_T *pBuf)
 Gets byte from buffer at current read position, advancing read position. More...
 
static int SimpleBufPutC (SimpleBuf_T *pBuf, byte_t c)
 Puts byte into buffer at current write position, advancing write position. More...
 

Detailed Description

Simple [io] buffer declarations and operations.

The buffer is orgainized as a simple linear buffer with independent read and write positions. Ideal for multi-tasking.

* [----------buffer-----------------]
*     ^         ^                   ^
*     RPos      WPos                BufSize
* 
Package
RoadNarrows Robotics Common Library 1
Library
librnr
File
rnr/simplebuf.h
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
License
MIT
EULA
See the README and EULA files for any copyright and licensing information.

Definition in file simplebuf.h.

Function Documentation

static void SimpleBufClear ( SimpleBuf_T pBuf)
inlinestatic

Clear contents of buffer.

Parameters
pBufPointer to simple buffer.

Definition at line 89 of file simplebuf.h.

References SimpleBuf_T::m_nRPos, and SimpleBuf_T::m_nWPos.

Referenced by SocketBufClear().

90 {
91  pBuf->m_nWPos = (size_t)0;
92  pBuf->m_nRPos = (size_t)0;
93 }
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
size_t SimpleBufCopy ( SimpleBuf_T pTgt,
SimpleBuf_T pSrc 
)

Copy contents of source simple buffer to the end of the target simple buffer.

Parameters
pTgtPointer to target simple buffer.
pSrcPointer to source simple buffer.

Return Value:

Returns
Returns Number of bytes copied.

Definition at line 165 of file simplebuf.c.

References SimpleBuf_T::m_nBufSize, SimpleBuf_T::m_nRPos, SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, and NULL.

166 {
167  size_t rpos, nBytes;
168 
169  if( (pTgt == NULL) || (pSrc == NULL) )
170  {
171  return (size_t)0;
172  }
173 
174  for(rpos=pSrc->m_nRPos, nBytes=0;
175  (rpos < pSrc->m_nWPos) && (pTgt->m_nWPos < pSrc->m_nBufSize);
176  ++rpos, ++nBytes)
177  {
178  pTgt->m_pRWBuf[pTgt->m_nWPos++] = pSrc->m_pRWBuf[rpos];
179  }
180 
181  return nBytes;
182 }
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
void SimpleBufDelete ( SimpleBuf_T pBuf)

Delete a simple buffer along with the internal buffer.

Actual deletion on occurs if buffer is owned by this simple buffer.

Parameters
pBufPointer to simple buffer to be deleted.

Definition at line 111 of file simplebuf.c.

References SimpleBuf_T::m_bBufDel, SimpleBuf_T::m_pRWBuf, and NULL.

Referenced by SocketBufNewBuf(), and SocketDelete().

112 {
113  if( pBuf == NULL )
114  {
115  return;
116  }
117 
118  if( pBuf->m_bBufDel )
119  {
120  delete(pBuf->m_pRWBuf);
121  }
122  delete(pBuf);
123 }
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
static int SimpleBufGetC ( SimpleBuf_T pBuf)
inlinestatic

Gets byte from buffer at current read position, advancing read position.

Parameters
pBufPointer to simple buffer.
Returns
Returns Read byte on success, -1 if buffer is empty.

Definition at line 227 of file simplebuf.h.

References SimpleBuf_T::m_nRPos, SimpleBuf_T::m_pRWBuf, and SimpleBufIsEmpty().

228 {
229  if( !SimpleBufIsEmpty(pBuf) )
230  {
231  return (int)pBuf->m_pRWBuf[pBuf->m_nRPos++];
232  }
233  return -1;
234 }
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
static bool_t SimpleBufIsEmpty(SimpleBuf_T *pBuf)
Test if simple buffer is empty.
Definition: simplebuf.h:154
static byte_t* SimpleBufGetReadPtr ( SimpleBuf_T pBuf)
inlinestatic

Returns pointer to internal I/O buffer at the current read position.

Parameters
pBufPointer to simple buffer.
Returns
Returns byte* on success, NULL if buffer empty.

Definition at line 202 of file simplebuf.h.

References SimpleBuf_T::m_nRPos, SimpleBuf_T::m_pRWBuf, NULL, and SimpleBufIsEmpty().

Referenced by SocketWrite().

203 {
204  return SimpleBufIsEmpty(pBuf)? NULL: pBuf->m_pRWBuf+pBuf->m_nRPos;
205 }
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
static bool_t SimpleBufIsEmpty(SimpleBuf_T *pBuf)
Test if simple buffer is empty.
Definition: simplebuf.h:154
static byte_t* SimpleBufGetWritePtr ( SimpleBuf_T pBuf)
inlinestatic

Returns pointer to internal I/O buffer at the current write position.

Parameters
pBufPointer to simple buffer.
Returns
Returns Returns byte* on success, NULL if buffer empty.

Definition at line 214 of file simplebuf.h.

References SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, NULL, and SimpleBufIsFull().

Referenced by SocketRead().

215 {
216  return SimpleBufIsFull(pBuf)? NULL: pBuf->m_pRWBuf+pBuf->m_nWPos;
217 }
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
static bool_t SimpleBufIsFull(SimpleBuf_T *pBuf)
Test if simple buffer is full.
Definition: simplebuf.h:166
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
static size_t SimpleBufHasAvail ( SimpleBuf_T pBuf)
inlinestatic

Returns number of bytes available in buffer for writing.

Parameters
pBufPointer to simple buffer.
Returns
Returns ≥ 0.

Definition at line 190 of file simplebuf.h.

References SimpleBuf_T::m_nBufSize, and SimpleBuf_T::m_nWPos.

Referenced by SocketRead().

191 {
192  return pBuf->m_nBufSize - pBuf->m_nWPos;
193 }
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
static size_t SimpleBufHasLen ( SimpleBuf_T pBuf)
inlinestatic

Returns number of bytes currently in buffer.

Parameters
pBufPointer to simple buffer.
Returns
Returns ≥ 0.

Definition at line 178 of file simplebuf.h.

References SimpleBuf_T::m_nRPos, and SimpleBuf_T::m_nWPos.

Referenced by SocketBufGetLen(), and SocketWrite().

179 {
180  return pBuf->m_nWPos - pBuf->m_nRPos;
181 }
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
static size_t SimpleBufHasSize ( SimpleBuf_T pBuf)
inlinestatic

Returns size of buffer.

Parameters
pBufPointer to simple buffer.
Returns
Returns ≥ 0.

Definition at line 142 of file simplebuf.h.

References SimpleBuf_T::m_nBufSize.

Referenced by SocketBufGetSize().

143 {
144  return pBuf->m_nBufSize;
145 }
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
static bool_t SimpleBufIsEmpty ( SimpleBuf_T pBuf)
inlinestatic

Test if simple buffer is empty.

Parameters
pBufPointer to simple buffer.
Returns
Returns true or false.

Definition at line 154 of file simplebuf.h.

References SimpleBuf_T::m_nRPos, and SimpleBuf_T::m_nWPos.

Referenced by SimpleBufGetC(), and SimpleBufGetReadPtr().

155 {
156  return pBuf->m_nRPos>=pBuf->m_nWPos? true: false;
157 }
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
static bool_t SimpleBufIsFull ( SimpleBuf_T pBuf)
inlinestatic

Test if simple buffer is full.

Parameters
pBufPointer to simple buffer.
Returns
Returns true or false.

Definition at line 166 of file simplebuf.h.

References SimpleBuf_T::m_nBufSize, and SimpleBuf_T::m_nWPos.

Referenced by SimpleBufGetWritePtr(), and SimpleBufPutC().

167 {
168  return pBuf->m_nWPos>=pBuf->m_nBufSize? true: false;
169 }
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
SimpleBuf_T* SimpleBufNew ( )

Allocate a new simple buffer with no internal buffer allocation.

Returns
Returns SimpleBuf_T* to newly allocated simple buffer.

Definition at line 70 of file simplebuf.c.

References SimpleBuf_T::m_bBufDel, SimpleBuf_T::m_nBufSize, SimpleBuf_T::m_nRPos, SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, NEW, and NULL.

Referenced by SocketNew().

71 {
72  SimpleBuf_T *pBuf = NEW(SimpleBuf_T);
73 
74  pBuf->m_pRWBuf = NULL;
75  pBuf->m_nBufSize = (size_t)0;
76  pBuf->m_nRPos = (size_t)0;
77  pBuf->m_nWPos = (size_t)0;
78  pBuf->m_bBufDel = false;
79 
80  return pBuf;
81 }
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
#define NEW(T)
Allocate new type.
Definition: new.h:49
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
SimpleBuf_T* SimpleBufNewWithBuf ( size_t  nBufSize)

Allocate a new simple buffer with internal buffer of nBufSize.

Parameters
nBufSizeSize in bytes of internal buffer to allocate with simple buffer.
Returns
Returns SimpleBuf_T* to newly allocated simple buffer.

Definition at line 91 of file simplebuf.c.

References SimpleBuf_T::m_bBufDel, SimpleBuf_T::m_nBufSize, SimpleBuf_T::m_nRPos, SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, and NEW.

Referenced by SocketBufNewBuf().

92 {
93  SimpleBuf_T *pBuf = NEW(SimpleBuf_T);
94 
95  pBuf->m_pRWBuf = (byte_t *)new(nBufSize);
96  pBuf->m_nBufSize = nBufSize;
97  pBuf->m_nRPos = (size_t)0;
98  pBuf->m_nWPos = (size_t)0;
99  pBuf->m_bBufDel = true;
100 
101  return pBuf;
102 }
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
#define NEW(T)
Allocate new type.
Definition: new.h:49
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
u8_t byte_t
8-bit byte
Definition: rnrconfig.h:177
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
static int SimpleBufPutC ( SimpleBuf_T pBuf,
byte_t  c 
)
inlinestatic

Puts byte into buffer at current write position, advancing write position.

Parameters
pBufPointer to simple buffer.
cByte to put.
Returns
Returns Wrote byte on success, -1 if buffer is full.

Definition at line 245 of file simplebuf.h.

References C_DECLS_END, SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, and SimpleBufIsFull().

246 {
247  if( !SimpleBufIsFull(pBuf) )
248  {
249  pBuf->m_pRWBuf[pBuf->m_nWPos++] = c;
250  return (int)c;
251  }
252  return -1;
253 }
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
static bool_t SimpleBufIsFull(SimpleBuf_T *pBuf)
Test if simple buffer is full.
Definition: simplebuf.h:166
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
static size_t SimpleBufReadSeek ( SimpleBuf_T pBuf,
size_t  rpos,
int  how 
)
inlinestatic

Seek buffer read head to the new read position.

Parameters
pBufPointer to simple buffer.
rposNew read position.
howSeek to absolute position (default) or relative to current position.
Returns
Returns new read position.

Definition at line 105 of file simplebuf.h.

References SimpleBuf_T::m_nRPos, SimpleBuf_T::m_nWPos, and SIMPLEBUF_REL.

Referenced by SocketWrite().

106 {
107  if( how == SIMPLEBUF_REL )
108  {
109  rpos += pBuf->m_nRPos;
110  }
111  pBuf->m_nRPos = rpos<=pBuf->m_nWPos? rpos: pBuf->m_nWPos;
112  return pBuf->m_nRPos;
113 }
#define SIMPLEBUF_REL
relative
Definition: simplebuf.h:53
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
void SimpleBufSetBuf ( SimpleBuf_T pBuf,
byte_t pRWBuf,
size_t  nBufSize,
size_t  nLen 
)

Set simple buffer's internal read/write buffer.

Parameters
pBufPointer to simple buffer.
pRWBufPointer to buffer to be set as the internal read/write buffer.
nBufSizeSize in bytes of pRWBuf.
nLenCurrent length (bytes) of data in pRWBuf.

Definition at line 134 of file simplebuf.c.

References SimpleBuf_T::m_bBufDel, SimpleBuf_T::m_nBufSize, SimpleBuf_T::m_nRPos, SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, and NULL.

Referenced by SocketBufSetBuf().

136 {
137  if( pBuf == NULL )
138  {
139  return;
140  }
141 
142  if( pBuf->m_bBufDel )
143  {
144  delete(pBuf->m_pRWBuf);
145  }
146 
147  pBuf->m_pRWBuf = pRWBuf;
148  pBuf->m_nBufSize = nBufSize;
149  pBuf->m_nRPos = (size_t)0;
150  pBuf->m_nWPos = nLen<=nBufSize? nLen: nBufSize;
151  pBuf->m_bBufDel = false;
152 }
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
static size_t SimpleBufWriteSeek ( SimpleBuf_T pBuf,
size_t  wpos,
int  how 
)
inlinestatic

Seek buffer write head to the new write position.

Parameters
pBufPointer to simple buffer.
wposNew write position.
howSeek to absolute position (default) or relative to current position.
Returns
Returns new write position.

Definition at line 125 of file simplebuf.h.

References SimpleBuf_T::m_nBufSize, SimpleBuf_T::m_nWPos, and SIMPLEBUF_REL.

Referenced by SocketRead().

126 {
127  if( how == SIMPLEBUF_REL )
128  {
129  wpos += pBuf->m_nWPos;
130  }
131  pBuf->m_nWPos = wpos<=pBuf->m_nBufSize? wpos: pBuf->m_nBufSize;
132  return pBuf->m_nWPos;
133 }
#define SIMPLEBUF_REL
relative
Definition: simplebuf.h:53
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61