MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LineTailByteSink.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  LineTailByteSink.h ByteSink that remembers most recent lines
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 LINETAILBYTESINK_H
29 #define LINETAILBYTESINK_H
30 
32 #include <string.h> /* For memchr */
33 
34 namespace MFM
35 {
36 
44  template <u32 LINES, u32 BYTES_PER_LINE>
45  class LineTailByteSink : public ByteSink
46  {
47  public:
48 
52  LineTailByteSink() : m_bytesWritten(0)
53  {
54  Reset();
55  }
56 
69  virtual void WriteBytes(const u8 * data, const u32 len)
70  {
71  m_bytesWritten += len;
72 
73  const u8 * lineStart = data;
74  const u8 * lineEnd;
75 
76  u32 effLen = len;
77 
78  while ((lineEnd = (const u8 *) memchr(lineStart, '\n', effLen)))
79  {
80 
81  u32 thisLen = (u32) (lineEnd - lineStart);
82  m_lines[m_nextLine].WriteBytes((const u8 *) lineStart, thisLen);
83  newline();
84 
85  lineStart += thisLen + 1;
86  effLen -= thisLen + 1;
87  }
88 
89  m_lines[m_nextLine].WriteBytes(lineStart, effLen);
90  }
91 
99  virtual s32 CanWrite()
100  {
101  return true;
102  }
103 
111  u32 GetBytesWritten() const
112  {
113  return m_bytesWritten;
114  }
115 
123  u32 GetLines() const
124  {
125  return (LINES + m_nextLine - m_firstLine) % LINES + 1;
126  }
127 
143  const char * GetZString(u32 whichLine)
144  {
145  if (whichLine >= GetLines())
146  {
147  return 0;
148  }
149 
150  u32 index = (m_firstLine + whichLine) % LINES;
151  return m_lines[index].GetZString();
152  }
153 
166  void Trim(u32 lines)
167  {
168  if (lines >= GetLines())
169  {
170  Reset();
171  }
172  else m_firstLine = (m_firstLine + lines) % LINES;
173  }
174 
178  void Reset()
179  {
180  m_firstLine = m_nextLine = 0;
181  m_lines[m_nextLine].Reset();
182  }
183 
184  private:
185  void newline()
186  {
187  m_nextLine = (m_nextLine + 1) % LINES;
188  m_lines[m_nextLine].Reset();
189 
190  if (m_nextLine == m_firstLine)
191  {
192  m_firstLine = (m_firstLine + 1) % LINES;
193  }
194  }
195 
196  OverflowableCharBufferByteSink<BYTES_PER_LINE> m_lines[LINES];
197  u32 m_bytesWritten;
198  u32 m_firstLine;
199  u32 m_nextLine;
200  };
201 }
202 
203 #endif /* LINETAILBYTESINK_H */
void Reset()
Definition: LineTailByteSink.h:178
u32 GetLines() const
Definition: LineTailByteSink.h:123
void Reset()
Definition: OverflowableCharBufferByteSink.h:192
const char * GetZString()
Definition: OverflowableCharBufferByteSink.h:143
virtual void WriteBytes(const u8 *data, const u32 len)
Definition: OverflowableCharBufferByteSink.h:64
LineTailByteSink()
Definition: LineTailByteSink.h:52
Definition: LineTailByteSink.h:45
void Trim(u32 lines)
Definition: LineTailByteSink.h:166
Definition: ByteSink.h:47
u32 GetBytesWritten() const
Definition: LineTailByteSink.h:111
const char * GetZString(u32 whichLine)
Definition: LineTailByteSink.h:143
virtual void WriteBytes(const u8 *data, const u32 len)
Definition: LineTailByteSink.h:69
virtual s32 CanWrite()
Definition: LineTailByteSink.h:99