librnr  1.14.5
RoadNarrows Robotics Common Library 1
simplebuf.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Common Library 1
4 //
5 // Library: librnr
6 //
7 // File: simplebuf.c
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2010-03-24 10:19:36 -0600 (Wed, 24 Mar 2010) $
12  * $Rev: 307 $
13  *
14  * \brief Simple [io] buffer declarations and operations.
15  *
16  * The buffer is orgainized as a simple linear buffer with independent read
17  * and write positions. Ideal for multi-tasking.
18  *
19  * \author Robin Knight (robin.knight@roadnarrows.com)
20  *
21  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
22  */
23 // Permission is hereby granted, without written agreement and without
24 // license or royalty fees, to use, copy, modify, and distribute this
25 // software and its documentation for any purpose, provided that
26 // (1) The above copyright notice and the following two paragraphs
27 // appear in all copies of the source code and (2) redistributions
28 // including binaries reproduces these notices in the supporting
29 // documentation. Substantial modifications to this software may be
30 // copyrighted by their authors and need not follow the licensing terms
31 // described here, provided that the new terms are clearly indicated in
32 // all files where they apply.
33 //
34 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
35 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
36 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
37 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
38 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
39 // THE POSSIBILITY OF SUCH DAMAGE.
40 //
41 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
42 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
44 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
45 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
46 //
47 ////////////////////////////////////////////////////////////////////////////////
48 
49 #include <unistd.h>
50 
51 #include "rnr/rnrconfig.h"
52 #include "rnr/new.h"
53 #include "rnr/simplebuf.h"
54 
55 
56 // ---------------------------------------------------------------------------
57 // Private Interface
58 // ---------------------------------------------------------------------------
59 
60 
61 // ---------------------------------------------------------------------------
62 // Public Interface
63 // ---------------------------------------------------------------------------
64 
65 /*!
66  * \brief Allocate a new simple buffer with no internal buffer allocation.
67  *
68  * \return Returns SimpleBuf_T* to newly allocated simple buffer.
69  */
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 }
82 
83 /*!
84  * \brief Allocate a new simple buffer with internal buffer of nBufSize.
85  *
86  * \param nBufSize Size in bytes of internal buffer to allocate with simple
87  * buffer.
88  *
89  * \return Returns SimpleBuf_T* to newly allocated simple buffer.
90  */
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 }
103 
104 /*!
105  * \brief Delete a simple buffer along with the internal buffer.
106  *
107  * Actual deletion on occurs if buffer is owned by this simple buffer.
108  *
109  * \param pBuf Pointer to simple buffer to be deleted.
110  */
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 }
124 
125 /*!
126  * \brief Set simple buffer's internal read/write buffer.
127  *
128  * \param pBuf Pointer to simple buffer.
129  * \param pRWBuf Pointer to buffer to be set as the internal read/write
130  * buffer.
131  * \param nBufSize Size in bytes of pRWBuf.
132  * \param nLen Current length (bytes) of data in pRWBuf.
133  */
134 void SimpleBufSetBuf(SimpleBuf_T *pBuf, byte_t *pRWBuf,
135  size_t nBufSize, size_t nLen)
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 }
153 
154 /*!
155  * \brief Copy contents of source simple buffer to the end of the target simple
156  * buffer.
157  *
158  * \param pTgt Pointer to target simple buffer.
159  * \param pSrc Pointer to source simple buffer.
160  *
161  * Return Value:
162  * \return Returns
163  * Number of bytes copied.
164  */
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 }
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
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
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
Memory allocation and deallocation declarations.
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
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
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
RoadNarrows Robotics common configuration file.
SimpleBuf_T * SimpleBufNew()
Allocate a new simple buffer with no internal buffer allocation.
Definition: simplebuf.c:70
u8_t byte_t
8-bit byte
Definition: rnrconfig.h:177
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
Simple [io] buffer declarations and operations.