MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ByteSource.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  ByteSource.h Abstract 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 BYTESOURCE_H
28 #define BYTESOURCE_H
29 
30 #include "itype.h"
31 #include "Fail.h"
32 #include "Format.h"
33 #include "ByteSink.h"
34 #include <stdarg.h> /* For ... */
35 
36 namespace MFM
37 {
38 
39  class ByteSerializable; // FORWARD
40 
44  class ByteSource
45  {
46  public:
47 
52  m_read(0),
53  m_lastRead(-1),
54  m_unread(false)
55  { }
56 
66  s32 Read()
67  {
68  if (m_unread)
69  {
70  m_unread = false;
71  }
72  else
73  {
74  m_lastRead = ReadByte();
75  }
76 
77  ++m_read;
78  return m_lastRead;
79  }
80 
87  void Unread()
88  {
89  if (m_unread)
90  {
91  FAIL(ILLEGAL_STATE);
92  }
93  m_unread = true;
94  --m_read;
95  }
96 
104  s32 Peek()
105  {
106  s32 read = Read();
107  Unread();
108  return read;
109  }
110 
117  virtual int ReadByte() = 0;
118 
122  virtual ~ByteSource()
123  { }
124 
134  {
135  return m_read;
136  }
137 
149  bool Scan(u64 & result) ;
150 
169  bool Scan(s32 & result, Format::Type code = Format::DEC, u32 fieldWidth = U32_MAX) ;
170 
187  bool Scan(u32 & result, Format::Type code = Format::DEC, u32 fieldWidth = U32_MAX) ;
188 
205  bool Scan(ByteSerializable & byteSerializable, s32 argument = 0) ;
206 
207 
208  bool ScanLexDigits(u32 & digits) ;
209 
213  bool Scan(ByteSink & result, const u32 fieldWidth) ;
214 
215  s32 ScanSet(ByteSink & result, const char * setSpec)
216  {
217  return ScanSetFormat(result, setSpec);
218  }
219 
228  s32 SkipSet(const char * setSpec)
229  {
230  return ScanSet(DevNull, setSpec);
231  }
232 
233  s32 ScanSetFormat(ByteSink & result, const char * & setSpec) ;
234 
244  static const char * WHITESPACE_CHARS;
245 
251  static const char * WHITESPACE_SET;
252 
258  static const char * NON_WHITESPACE_SET;
259 
271  bool ScanIdentifier(ByteSink & result)
272  {
273  SkipWhitespace();
274  if (ScanSet(result, "[_a-zA-Z]") <= 0)
275  {
276  return false;
277  }
278  ScanSet(result, "[_a-zA-Z0-9]");
279  return true;
280  }
281 
294  bool ScanHex(ByteSink& result)
295  {
296  SkipWhitespace();
297 
298  return ScanSet(result, "[a-fA-F0-9]") > 0;
299  }
300 
312  bool ScanBinary(ByteSink& result)
313  {
314  SkipWhitespace();
315 
316  return ScanSet(result, "[0-1]") > 0;
317  }
318 
332  {
333  SkipWhitespace();
334  if (ScanSet(result, "[A-Z]") <= 0)
335  {
336  return false;
337  }
338  ScanSet(result, "[a-zA-Z0-9]");
339  return true;
340  }
341 
349  {
350  return SkipSet(WHITESPACE_SET);
351  }
352 
353  s32 Scanf(const char * format, ...) ; // NYI
354  s32 Vscanf(const char * format, va_list & ap) ;
355 
356  private:
357  s32 ReadCounted(u32 & maxLen)
358  {
359  if (maxLen == 0)
360  {
361  return -1;
362  }
363 
364  s32 ret = Read();
365  if (ret >= 0)
366  {
367  --maxLen;
368  }
369  return ret;
370  }
371 
372  u32 m_read;
373  s32 m_lastRead;
374  bool m_unread;
375  };
376 
377 }
378 
379 #endif /* BYTESOURCE_H */
Definition: Format.h:40
u32 GetBytesRead()
Definition: ByteSource.h:133
void Unread()
Definition: ByteSource.h:87
virtual ~ByteSource()
Definition: ByteSource.h:122
bool ScanCamelIdentifier(ByteSink &result)
Definition: ByteSource.h:331
Definition: ByteSource.h:44
static const char * NON_WHITESPACE_SET
Definition: ByteSource.h:258
static const char * WHITESPACE_SET
Definition: ByteSource.h:251
static const char * WHITESPACE_CHARS
Definition: ByteSource.h:244
virtual int ReadByte()=0
Definition: ByteSink.h:47
bool ScanHex(ByteSink &result)
Definition: ByteSource.h:294
bool ScanBinary(ByteSink &result)
Definition: ByteSource.h:312
Type
Definition: Format.h:39
s32 SkipSet(const char *setSpec)
Definition: ByteSource.h:228
bool ScanIdentifier(ByteSink &result)
Definition: ByteSource.h:271
s32 Vscanf(const char *format, va_list &ap)
Definition: ByteSource.cpp:256
s32 Read()
Definition: ByteSource.h:66
ByteSource()
Definition: ByteSource.h:51
s32 Peek()
Definition: ByteSource.h:104
bool Scan(u64 &result)
Definition: ByteSource.cpp:7
Definition: ByteSerializable.h:45
s32 SkipWhitespace()
Definition: ByteSource.h:348
#define U32_MAX
Definition: itype.h:95