MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LineCountingByteSource.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  LineCountingByteSource.h Wrapper class to count line and byte positions
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 LINECOUNTINGBYTESOURCE_H
28 #define LINECOUNTINGBYTESOURCE_H
29 
30 #include "itype.h"
31 #include "Fail.h"
32 #include "Logger.h" /* For Logger::Level */
33 #include "ByteSource.h"
34 
35 namespace MFM
36 {
43  {
44  public:
50  m_bs(NULL),
51  m_errs(&DevNull),
52  m_label("unknown source"),
53  m_lineNum(1),
54  m_byteNum(0)
55  { }
56 
64  {
65  m_bs = &bs;
66  }
67 
76  {
77  m_errs = &bs;
78  }
79 
86  void SetLabel(const char * label)
87  {
88  if (!label)
89  {
90  FAIL(NULL_POINTER);
91  }
92  m_label = label;
93  }
94 
111  bool Msg(Logger::Level type, const char * format, ...)
112  {
113  va_list ap;
114  va_start(ap, format);
115  VMsg(type, format, ap);
116  va_end(ap);
117  return type >= Logger::MESSAGE;
118  }
119 
138  bool VMsg(Logger::Level type, const char * format, va_list & ap)
139  {
140 
141  PrintPosition(*m_errs);
142  m_errs->Print(" ");
143 
144  switch (type)
145  {
146  case Logger::ERROR: m_errs->Print("error"); break;
147  case Logger::WARNING: m_errs->Print("warning"); break;
148  case Logger::MESSAGE: m_errs->Print("message"); break;
149  default: FAIL(ILLEGAL_ARGUMENT);
150  }
151 
152  m_errs->Print(": ");
153  m_errs->Vprintf(format, ap);
154  m_errs->Println();
155 
156  return type >= Logger::MESSAGE;
157  }
158 
167  void PrintPosition(ByteSink & b) const
168  {
169  b.Printf("%s:%d:%d:", m_label, m_lineNum, m_byteNum);
170  }
171 
179  u32 GetLineNum() const
180  {
181  return m_lineNum;
182  }
183 
191  u32 GetByteNum() const
192  {
193  return m_byteNum;
194  }
195 
196  virtual int ReadByte()
197  {
198  if (!m_bs)
199  {
200  FAIL(ILLEGAL_STATE);
201  }
202  // Note this sucker doesn't currently deal with Unread! Counts
203  // can be off by a byte or a line!
204  s32 byte = m_bs->ReadByte();
205  if (byte == '\n')
206  {
207  ++m_lineNum;
208  m_byteNum = 0;
209  }
210  else
211  {
212  ++m_byteNum;
213  }
214  return byte;
215  }
216 
217  private:
218  ByteSource * m_bs;
219  ByteSink * m_errs;
220  const char * m_label;
221  u32 m_lineNum;
222  u32 m_byteNum;
223  };
224 }
225 
226 #endif /* LINECOUNTINGBYTESOURCE_H */
u32 GetByteNum() const
Definition: LineCountingByteSource.h:191
Level
Definition: Logger.h:52
void PrintPosition(ByteSink &b) const
Definition: LineCountingByteSource.h:167
bool VMsg(Logger::Level type, const char *format, va_list &ap)
Definition: LineCountingByteSource.h:138
Definition: LineCountingByteSource.h:42
LineCountingByteSource()
Definition: LineCountingByteSource.h:49
u32 GetLineNum() const
Definition: LineCountingByteSource.h:179
Definition: ByteSource.h:44
virtual int ReadByte()
Definition: LineCountingByteSource.h:196
virtual int ReadByte()=0
Definition: ByteSink.h:47
void SetByteSource(ByteSource &bs)
Definition: LineCountingByteSource.h:63
void Print(const char *str, s32 fieldWidth=-1, u8 padChar= ' ')
Definition: ByteSink.cpp:31
void SetLabel(const char *label)
Definition: LineCountingByteSource.h:86
void SetErrorByteSink(ByteSink &bs)
Definition: LineCountingByteSource.h:75
bool Msg(Logger::Level type, const char *format,...)
Definition: LineCountingByteSource.h:111