librnr  1.14.5
RoadNarrows Robotics Common Library 1
simplebuf.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 /*! \file
3  *
4  * \brief Simple [io] buffer declarations and operations.
5  *
6  * The buffer is orgainized as a simple linear buffer with independent read
7  * and write positions. Ideal for multi-tasking.
8  *
9  * \verbatim
10  * [----------buffer-----------------]
11  * ^ ^ ^
12  * RPos WPos BufSize
13  * \endverbatim
14  *
15  * \pkgsynopsis
16  * RoadNarrows Robotics Common Library 1
17  *
18  * \pkgcomponent{Library}
19  * librnr
20  *
21  * \pkgfile{rnr/simplebuf.h}
22  *
23  * \author Robin Knight (robin.knight@roadnarrows.com)
24  *
25  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
26  *
27  * \license{MIT}
28  *
29  * \EulaBegin
30  * See the README and EULA files for any copyright and licensing information.
31  * \EulaEnd
32  */
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef _RNR_SIMPLEBUF_H
36 #define _RNR_SIMPLEBUF_H
37 
38 #include <unistd.h>
39 
40 #include "rnrconfig.h"
41 
42 
43 //-----------------------------------------------------------------------------
44 // Defines and Types
45 //-----------------------------------------------------------------------------
46 
48 
49 //
50 // Defines
51 //
52 #define SIMPLEBUF_ABS 0 ///< absolute
53 #define SIMPLEBUF_REL 1 ///< relative
54 
55 /*!
56  * Simple Read/Write Buffer Structure.
57  */
58 typedef struct
59 {
60  byte_t *m_pRWBuf; ///< internal read/write buffer
61  size_t m_nBufSize; ///< buffer total size
62  size_t m_nRPos; ///< current read buffer position
63  size_t m_nWPos; ///< current write buffer position
64  bool_t m_bBufDel; ///< do [not] delete internal R/W buffer
65 } SimpleBuf_T;
66 
67 
68 //-----------------------------------------------------------------------------
69 // Prototypes
70 //-----------------------------------------------------------------------------
71 
72 extern SimpleBuf_T *SimpleBufNew();
73 
74 extern SimpleBuf_T *SimpleBufNewWithBuf(size_t nBufSize);
75 
76 extern void SimpleBufDelete(SimpleBuf_T *pBuf);
77 
78 extern void SimpleBufSetBuf(SimpleBuf_T *pBuf, byte_t *pRWBuf,
79  size_t nBufSize, size_t nLen);
80 
81 extern size_t SimpleBufCopy(SimpleBuf_T *pTgt, SimpleBuf_T *pSrc);
82 
83 /*!
84  * \brief Clear contents of buffer.
85  *
86  * \param pBuf Pointer to simple buffer.
87  *
88  */
90 {
91  pBuf->m_nWPos = (size_t)0;
92  pBuf->m_nRPos = (size_t)0;
93 }
94 
95 /*!
96  * \brief Seek buffer read head to the new read position.
97  *
98  * \param pBuf Pointer to simple buffer.
99  * \param rpos New read position.
100  * \param how Seek to absolute position (default) or relative to current
101  * position.
102  *
103  * \return Returns new read position.
104  */
105 INLINE_IN_H size_t SimpleBufReadSeek(SimpleBuf_T *pBuf, size_t rpos, int how)
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 }
114 
115 /*!
116  * \brief Seek buffer write head to the new write position.
117  *
118  * \param pBuf Pointer to simple buffer.
119  * \param wpos New write position.
120  * \param how Seek to absolute position (default) or relative to current
121  * position.
122  *
123  * \return Returns new write position.
124  */
125 INLINE_IN_H size_t SimpleBufWriteSeek(SimpleBuf_T *pBuf, size_t wpos, int how)
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 }
134 
135 /*!
136  * \brief Returns size of buffer.
137  *
138  * \param pBuf Pointer to simple buffer.
139  *
140  *\return Returns \h_ge 0.
141  */
143 {
144  return pBuf->m_nBufSize;
145 }
146 
147 /*!
148  * \brief Test if simple buffer is empty.
149  *
150  * \param pBuf Pointer to simple buffer.
151  *
152  * \return Returns true or false.
153  */
155 {
156  return pBuf->m_nRPos>=pBuf->m_nWPos? true: false;
157 }
158 
159 /*!
160  * \brief Test if simple buffer is full.
161  *
162  * \param pBuf Pointer to simple buffer.
163  *
164  * \return Returns true or false.
165  */
167 {
168  return pBuf->m_nWPos>=pBuf->m_nBufSize? true: false;
169 }
170 
171 /*!
172  * \brief Returns number of bytes currently in buffer.
173  *
174  * \param pBuf Pointer to simple buffer.
175  *
176  * \return Returns \h_ge 0.
177  */
179 {
180  return pBuf->m_nWPos - pBuf->m_nRPos;
181 }
182 
183 /*!
184  * \brief Returns number of bytes available in buffer for writing.
185  *
186  * \param pBuf Pointer to simple buffer.
187  *
188  * \return Returns \h_ge 0.
189  */
191 {
192  return pBuf->m_nBufSize - pBuf->m_nWPos;
193 }
194 
195 /*!
196  * \brief Returns pointer to internal I/O buffer at the current read position.
197  *
198  * \param pBuf Pointer to simple buffer.
199  *
200  * \return Returns byte* on success, NULL if buffer empty.
201  */
203 {
204  return SimpleBufIsEmpty(pBuf)? NULL: pBuf->m_pRWBuf+pBuf->m_nRPos;
205 }
206 
207 /*!
208  * \brief Returns pointer to internal I/O buffer at the current write position.
209  *
210  * \param pBuf Pointer to simple buffer.
211  *
212  * \return Returns Returns byte* on success, NULL if buffer empty.
213  */
215 {
216  return SimpleBufIsFull(pBuf)? NULL: pBuf->m_pRWBuf+pBuf->m_nWPos;
217 }
218 
219 /*!
220  * \brief Gets byte from buffer at current read position, advancing read
221  * position.
222  *
223  * \param pBuf Pointer to simple buffer.
224  *
225  * \return Returns Read byte on success, -1 if buffer is empty.
226  */
228 {
229  if( !SimpleBufIsEmpty(pBuf) )
230  {
231  return (int)pBuf->m_pRWBuf[pBuf->m_nRPos++];
232  }
233  return -1;
234 }
235 
236 /*!
237  * \brief Puts byte into buffer at current write position, advancing write
238  * position.
239  *
240  * \param pBuf Pointer to simple buffer.
241  * \param c Byte to put.
242  *
243  * \return Returns Wrote byte on success, -1 if buffer is full.
244  */
246 {
247  if( !SimpleBufIsFull(pBuf) )
248  {
249  pBuf->m_pRWBuf[pBuf->m_nWPos++] = c;
250  return (int)c;
251  }
252  return -1;
253 }
254 
256 
257 
258 #endif // _RNR_SIMPLEBUF_H
static byte_t * SimpleBufGetReadPtr(SimpleBuf_T *pBuf)
Returns pointer to internal I/O buffer at the current read position.
Definition: simplebuf.h:202
SimpleBuf_T * SimpleBufNew()
Allocate a new simple buffer with no internal buffer allocation.
Definition: simplebuf.c:70
static size_t SimpleBufHasSize(SimpleBuf_T *pBuf)
Returns size of buffer.
Definition: simplebuf.h:142
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
#define SIMPLEBUF_REL
relative
Definition: simplebuf.h:53
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
static size_t SimpleBufHasLen(SimpleBuf_T *pBuf)
Returns number of bytes currently in buffer.
Definition: simplebuf.h:178
static int SimpleBufGetC(SimpleBuf_T *pBuf)
Gets byte from buffer at current read position, advancing read position.
Definition: simplebuf.h:227
static bool_t SimpleBufIsFull(SimpleBuf_T *pBuf)
Test if simple buffer is full.
Definition: simplebuf.h:166
static int SimpleBufPutC(SimpleBuf_T *pBuf, byte_t c)
Puts byte into buffer at current write position, advancing write position.
Definition: simplebuf.h:245
SimpleBuf_T * SimpleBufNewWithBuf(size_t nBufSize)
Allocate a new simple buffer with internal buffer of nBufSize.
Definition: simplebuf.c:91
void SimpleBufDelete(SimpleBuf_T *pBuf)
Delete a simple buffer along with the internal buffer.
Definition: simplebuf.c:111
static size_t SimpleBufHasAvail(SimpleBuf_T *pBuf)
Returns number of bytes available in buffer for writing.
Definition: simplebuf.h:190
#define C_DECLS_BEGIN
C declaration block begin in C.
Definition: rnrconfig.h:71
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 SimpleBufCopy(SimpleBuf_T *pTgt, SimpleBuf_T *pSrc)
Copy contents of source simple buffer to the end of the target simple buffer.
Definition: simplebuf.c:165
void SimpleBufSetBuf(SimpleBuf_T *pBuf, byte_t *pRWBuf, size_t nBufSize, size_t nLen)
Set simple buffer&#39;s internal read/write buffer.
Definition: simplebuf.c:134
int bool_t
"boolean" T/F
Definition: rnrconfig.h:187
RoadNarrows Robotics common configuration file.
#define INLINE_IN_H
inline C funtion in C header
Definition: rnrconfig.h:157
u8_t byte_t
8-bit byte
Definition: rnrconfig.h:177
#define C_DECLS_END
C declaration block end in C.
Definition: rnrconfig.h:72
static size_t SimpleBufWriteSeek(SimpleBuf_T *pBuf, size_t wpos, int how)
Seek buffer write head to the new write position.
Definition: simplebuf.h:125
static size_t SimpleBufReadSeek(SimpleBuf_T *pBuf, size_t rpos, int how)
Seek buffer read head to the new read position.
Definition: simplebuf.h:105
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 void SimpleBufClear(SimpleBuf_T *pBuf)
Clear contents of buffer.
Definition: simplebuf.h:89
static byte_t * SimpleBufGetWritePtr(SimpleBuf_T *pBuf)
Returns pointer to internal I/O buffer at the current write position.
Definition: simplebuf.h:214