MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileByteSource.h
1 /* -*- mode:C++ -*-
2  FileByteSource.h MFM ByteSource backed by a file
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 FILEBYTESOURCE_H
29 #define FILEBYTESOURCE_H
30 
31 #include <stdio.h>
32 #include "ByteSource.h"
33 
34 namespace MFM
35 {
39  class FileByteSource : public ByteSource
40  {
41  private:
46  FILE* m_fp;
47 
48  public:
54  ByteSource(),
55  m_fp(NULL)
56  { }
57 
66  FileByteSource(const char* filename) :
67  ByteSource(),
68  m_fp(NULL)
69  {
70  Open(filename);
71  }
72 
83  void Open(const char* filename)
84  {
85  if(!m_fp)
86  {
87  m_fp = fopen(filename, "r");
88  }
89  }
90 
98  bool IsOpen()
99  {
100  return m_fp != NULL;
101  }
102 
107  void Close()
108  {
109  if(m_fp)
110  {
111  fclose(m_fp);
112  m_fp = NULL;
113  }
114  }
115 
116  virtual int ReadByte()
117  {
118  if (!m_fp)
119  {
120  FAIL(ILLEGAL_STATE);
121  }
122  return fgetc(m_fp);
123  }
124  };
125 }
126 
127 #endif /* FILEBYTESOURCE_H */
virtual int ReadByte()
Definition: FileByteSource.h:116
Definition: ByteSource.h:44
Definition: FileByteSource.h:39
bool IsOpen()
Definition: FileByteSource.h:98
void Open(const char *filename)
Definition: FileByteSource.h:83
void Close()
Definition: FileByteSource.h:107
FileByteSource(const char *filename)
Definition: FileByteSource.h:66
FileByteSource()
Definition: FileByteSource.h:53