Monarch  v3.8.2
Project 8 Data File Format Library
M2IO.hh
Go to the documentation of this file.
1 #ifndef M2_IO_HH_
2 #define M2_IO_HH_
3 
4 #include "M2Types.hh"
5 
6 #include <string>
7 
8 #include <iostream>
9 
10 #include <cstdio>
11 
12 namespace monarch2
13 {
14 
15  class M2IO
16  {
17  FILE *fFile;
19  public:
20  // Constructors and Destructors
21  M2IO( AccessModeType aMode );
22  ~M2IO();
23 
24  // Open the file in whatever mode was given the constructor
25  bool Open( const std::string& aFilename );
26 
27  // Write nbytes of data from the byte array wbuf to the
28  // current position of the file pointer.
29  bool Write( byte_type* anArray, size_t aCount );
30  template< class XType >
31  bool Write( XType* aDatum );
32  template< class XType >
33  bool Write( XType* aDatum, size_t aCount );
34 
35  // Seek by offset aCount bytes
36  bool Seek( long int aCount );
37 
38  // Read aCout bytes of data from the file pointer and store
39  // the result in the byte array anArray.
40  bool Read( byte_type* anArray, size_t aCount );
41  template< class XType >
42  bool Read( XType* aDatum );
43  template< class XType >
44  bool Read( XType* aDatum, size_t aCount );
45 
46  // File is at end
47  bool Done();
48 
49  // Close the file handle owned by this IO object
50  bool Close();
51  };
52 
53  inline bool M2IO::Write( byte_type* anArray, size_t aCount )
54  {
55  size_t written = fwrite( anArray, sizeof(byte_type), aCount, fFile );
56  return (written == sizeof(byte_type) * aCount);
57  }
58  template< class XType >
59  inline bool M2IO::Write( XType* aDatum )
60  {
61  size_t written = fwrite( aDatum, sizeof(XType), 1, fFile );
62  return (written == 1);
63  }
64  template< class XType >
65  inline bool M2IO::Write( XType* aDatum, size_t aCount )
66  {
67  size_t written = fwrite( aDatum, sizeof(XType), aCount, fFile );
68  return (written == aCount);
69  }
70 
71  inline bool M2IO::Seek( long int aCount )
72  {
73  size_t success = fseek( fFile, aCount, SEEK_CUR );
74  return( success == 0 );
75  }
76 
77  inline bool M2IO::Read( byte_type* anArray, size_t aCount )
78  {
79  size_t read = fread( anArray, sizeof(byte_type), aCount, fFile );
80  return (read == sizeof(byte_type) * aCount);
81  }
82  template< class XType >
83  inline bool M2IO::Read( XType* aDatum )
84  {
85  size_t read = fread( aDatum, sizeof(XType), 1, fFile );
86  return (read == 1);
87  }
88  template< class XType >
89  inline bool M2IO::Read( XType* aDatum, size_t aCount )
90  {
91  size_t read = fread( aDatum, sizeof(XType), aCount, fFile );
92  return (read == aCount);
93  }
94 
95 }
96 
97 #endif
bool Close()
Definition: M2IO.cc:49
AccessModeType fMode
Definition: M2IO.hh:18
FILE * fFile
Definition: M2IO.hh:17
bool Seek(long int aCount)
Definition: M2IO.hh:71
uint32_t AccessModeType
Definition: M2Types.hh:30
bool Write(byte_type *anArray, size_t aCount)
Definition: M2IO.hh:53
bool Open(const std::string &aFilename)
Definition: M2IO.cc:20
bool Read(byte_type *anArray, size_t aCount)
Definition: M2IO.hh:77
bool Done()
Definition: M2IO.cc:37
M2IO(AccessModeType aMode)
Definition: M2IO.cc:6
uint8_t byte_type
Definition: M2Types.hh:10