MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OverflowableCharBufferByteSink.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  OverflowableCharBufferByteSink.h Overflowable character stream
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef OVERFLOWABLECHARBUFFERBYTESINK_H
28 #define OVERFLOWABLECHARBUFFERBYTESINK_H
29 
30 #include "ByteSink.h"
31 #include <string.h> /* For memcpy */
32 
33 namespace MFM {
34 
40  template <u32 BUFSIZE>
42  {
43  public:
44 
49  m_written(0),
50  m_overflowed(false)
51  { }
52 
64  virtual void WriteBytes(const u8 * data, const u32 len)
65  {
66  if (m_overflowed)
67  {
68  return;
69  }
70 
71  u32 effLen = len;
72  if (m_written + effLen > BUFSIZE - 2)
73  {
74  effLen = BUFSIZE - 2 - m_written;
75  m_overflowed = true;
76  }
77 
78  memcpy(&m_buf[m_written], data, effLen);
79  m_written += effLen;
80 
81  if (m_overflowed)
82  {
83  m_buf[m_written++] = 'X';
84  }
85  }
86 
94  virtual s32 CanWrite()
95  {
96  return BUFSIZE - m_written - 1;
97  }
98 
111  bool Equals(const char * str) const
112  {
113  return GetLength() == strlen(str) && !memcmp(m_buf, str, GetLength());
114  }
115 
129  template <u32 OTHER_SIZE>
131  {
132  return GetLength() == str.GetLength() && !memcmp(m_buf, str.m_buf, GetLength());
133  }
134 
143  const char * GetZString()
144  {
145  m_buf[m_written] = '\0';
146  return GetBuffer();
147  }
148 
158  const char * GetBuffer() const
159  {
160  return (const char *) m_buf;
161  }
162 
170  u32 GetLength() const
171  {
172  return m_written;
173  }
174 
182  u32 GetCapacity() const
183  {
184  return BUFSIZE;
185  }
186 
192  void Reset()
193  {
194  m_written = 0;
195  m_overflowed = false;
196  }
197 
206  bool HasOverflowed() const
207  {
208  return m_overflowed;
209  }
210 
222  {
223  /* Check for >= to account for null byte written by snprintf */
224  if((m_written = strlen(zstr)) >= BUFSIZE)
225  {
226  FAIL(OUT_OF_ROOM);
227  }
228 
229  snprintf((char*)m_buf, BUFSIZE, "%s", zstr);
230 
231  return *this;
232  }
233 
234  private:
235  u32 m_written;
236  bool m_overflowed;
237  u8 m_buf[BUFSIZE];
238  };
239 
243  typedef OverflowableCharBufferByteSink<16 + 2> OString16;
244 
248  typedef OverflowableCharBufferByteSink<32 + 2> OString32;
249 
253  typedef OverflowableCharBufferByteSink<64 + 2> OString64;
254 
258  typedef OverflowableCharBufferByteSink<128 + 2> OString128;
259 }
260 
261 #endif /* OVERFLOWABLECHARBUFFERBYTESINK_H */
u32 GetCapacity() const
Definition: OverflowableCharBufferByteSink.h:182
void Reset()
Definition: OverflowableCharBufferByteSink.h:192
const char * GetZString()
Definition: OverflowableCharBufferByteSink.h:143
virtual s32 CanWrite()
Definition: OverflowableCharBufferByteSink.h:94
virtual void WriteBytes(const u8 *data, const u32 len)
Definition: OverflowableCharBufferByteSink.h:64
bool Equals(const char *str) const
Definition: OverflowableCharBufferByteSink.h:111
bool HasOverflowed() const
Definition: OverflowableCharBufferByteSink.h:206
Definition: OverflowableCharBufferByteSink.h:41
Definition: ByteSink.h:47
const char * GetBuffer() const
Definition: OverflowableCharBufferByteSink.h:158
OverflowableCharBufferByteSink< BUFSIZE > & operator=(const char *zstr)
Definition: OverflowableCharBufferByteSink.h:221
OverflowableCharBufferByteSink()
Definition: OverflowableCharBufferByteSink.h:48
bool Equals(const OverflowableCharBufferByteSink< OTHER_SIZE > &str) const
Definition: OverflowableCharBufferByteSink.h:130
u32 GetLength() const
Definition: OverflowableCharBufferByteSink.h:170