HARDT - The Ham Radio DSP Toolkit
hfilereader.h
1 #ifndef __HFILEREADER_H
2 #define __HFILEREADER_H
3 
7 template <class T>
8 class HFileReader : public HReader<T>
9 {
10  private:
11 
12  std::ifstream _stream;
13  const std::string _filename;
14  HProbe<T>* _probe;
15 
16  public:
17 
19  HFileReader(const char* filename, HProbe<T>* probe = nullptr):
20  _filename(std::string(filename)),
21  _probe(probe)
22  {}
23 
25  HFileReader(const std::string filename, HProbe<T>* probe = nullptr):
26  _filename(filename),
27  _probe(probe)
28  {}
29 
31  virtual int Read(T* dest, size_t blocksize)
32  {
33  // Read next chunk
34  _stream.read((char*) dest, blocksize * sizeof(T));
35 
36  // Probe output ?
37  if( _probe != nullptr ) {
38  _probe->Write(dest, blocksize * sizeof(T));
39  }
40 
41  // Check for eof
42  if( _stream.eof() )
43  {
44  HLog("At eof. Returning zero read");
45  return 0;
46  }
47 
48  return blocksize;
49  }
50 
52  bool Start()
53  {
54  HLog("Trying to open stream for %s", _filename.c_str());
55  _stream.open(_filename.c_str(), std::ios::binary);
56  if( !_stream.is_open())
57  {
58  HError("Failed to open file %s", _filename.c_str());
59  return false;
60  }
61  HLog("Stream is open");
62  return true;
63  }
64 
66  bool Stop()
67  {
68  HLog("Closing stream");
69  _stream.close();
70  return true;
71  }
72 
74  void Seek(int bytes)
75  {
76  _stream.seekg(bytes, std::ios::beg);
77  }
78 
80  bool Command(HCommand* command) {
81  // No further propagation of commands
82  return true;
83  }
84 };
85 
86 #endif
HFileReader::HFileReader
HFileReader(const std::string filename, HProbe< T > *probe=nullptr)
Definition: hfilereader.h:25
HFileReader::Stop
bool Stop()
Definition: hfilereader.h:66
HFileReader::Read
virtual int Read(T *dest, size_t blocksize)
Definition: hfilereader.h:31
HReader
Definition: hreader.h:24
HFileReader
Definition: hfilereader.h:8
HProbe
Definition: hprobe.h:10
HFileReader::Command
bool Command(HCommand *command)
Definition: hfilereader.h:80
HFileReader::Seek
void Seek(int bytes)
Definition: hfilereader.h:74
HFileReader::HFileReader
HFileReader(const char *filename, HProbe< T > *probe=nullptr)
Definition: hfilereader.h:19
HCommand
Definition: hcommand.h:81
HProbe::Write
int Write(T *src, size_t blocksize)
Definition: hprobe.h:40
HFileReader::Start
bool Start()
Definition: hfilereader.h:52