MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CharBufferByteSink.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  CharBufferByteSink.h Character based ByteSink
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 
28 #ifndef CHARBUFFERBYTESINK_H
29 #define CHARBUFFERBYTESINK_H
30 
31 #include "ByteSink.h"
32 #include <string.h> /* For memcpy */
33 
34 namespace MFM
35 {
43  template <int BUFSIZE>
45  {
46  public:
47 
49  m_written(0)
50  { }
51 
64  virtual void WriteBytes(const u8 * data, const u32 len)
65  {
66  if (m_written + len < BUFSIZE-1)
67  {
68  memcpy(&m_buf[m_written], data, len);
69  m_written += len;
70  return;
71  }
72  FAIL(OUT_OF_ROOM);
73  }
74 
82  virtual s32 CanWrite()
83  {
84  return BUFSIZE - m_written - 1;
85  }
86 
99  bool Equals(const char * str)
100  {
101  return strcmp(GetZString(), str)==0;
102  }
103 
112  const char * GetZString()
113  {
114  m_buf[m_written] = '\0';
115  return (const char *) m_buf;
116  }
117 
123  u32 GetLength() const
124  {
125  return m_written;
126  }
127 
135  u32 GetCapacity() const
136  {
137  return BUFSIZE;
138  }
139 
144  void Reset()
145  {
146  m_written = 0;
147  }
148 
149  private:
154  u8 m_buf[BUFSIZE];
155 
160  u32 m_written;
161  };
162 }
163 
164 #endif /* CHARBUFFERBYTESINK_H */
u32 GetLength() const
Definition: CharBufferByteSink.h:123
virtual s32 CanWrite()
Definition: CharBufferByteSink.h:82
Definition: ByteSink.h:47
void Reset()
Definition: CharBufferByteSink.h:144
Definition: CharBufferByteSink.h:44
virtual void WriteBytes(const u8 *data, const u32 len)
Definition: CharBufferByteSink.h:64
const char * GetZString()
Definition: CharBufferByteSink.h:112
bool Equals(const char *str)
Definition: CharBufferByteSink.h:99
u32 GetCapacity() const
Definition: CharBufferByteSink.h:135