MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileByteSink.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  FileByteSink.h Byte sink 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 FILEBYTESINK_H
29 #define FILEBYTESINK_H
30 
31 #include "itype.h"
32 #include <stdio.h>
33 #include "ByteSink.h"
34 
35 namespace MFM
36 {
41  class FileByteSink : public ByteSink
42  {
43  private:
48  FILE * m_file;
49 
50  public:
51 
59  FileByteSink(FILE * file) :
60  m_file(file)
61  {
62  if (!file)
63  {
64  FAIL(NULL_POINTER);
65  }
66  }
67 
68  virtual void WriteBytes(const u8 * data, const u32 len)
69  {
70  if(!m_file)
71  {
72  FAIL(NULL_POINTER);
73  }
74  size_t wrote = fwrite(data, 1, len, m_file);
75  if (wrote != len)
76  FAIL(IO_ERROR);
77  }
78 
83  void Close()
84  {
85  fclose(m_file);
86  m_file = NULL;
87  }
88 
89  virtual s32 CanWrite()
90  {
91  return (m_file != NULL) ? 1 : -1; /* Can't write to a closed stream */
92  }
93  };
94 
95  extern FileByteSink STDOUT;
96  extern FileByteSink STDERR;
97 }
98 
99 #endif /* FILEBYTESINK_H */
virtual s32 CanWrite()
Definition: FileByteSink.h:89
virtual void WriteBytes(const u8 *data, const u32 len)
Definition: FileByteSink.h:68
Definition: FileByteSink.h:41
Definition: ByteSink.h:47
void Close()
Definition: FileByteSink.h:83
FileByteSink(FILE *file)
Definition: FileByteSink.h:59