Beatmup
input_stream.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program 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
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "../basic_types.h"
21 #include <fstream>
22 
23 namespace Beatmup {
24  /**
25  Minimal input stream interface
26  */
27  class InputStream {
28  public:
29  /**
30  Reads a given number of bytes into a specific memory location.
31  \param[in] buffer The address to store the data to
32  \param[in] bytes Number of bytes to read
33  \return `true` on success.
34  */
35  virtual bool operator()(void * buffer, msize bytes) = 0;
36 
37  /**
38  Moves the read pointer to a given position in the stream.
39  \param pos The position in bytes from the beginning of the stream
40  \return `true` on success.
41  */
42  virtual bool seek(msize pos) = 0;
43 
44  /**
45  Returns `true`, if the end of the stream is reached (i.e., all the data is read or the stream is empty).
46  */
47  virtual bool eof() const = 0;
48  };
49 
50 
51  /**
52  InputStream reading from a file
53  */
54  class FileInputStream : public InputStream {
55  private:
56  std::ifstream stream;
57  public:
59  inline FileInputStream(const char* filename) { open(filename); }
60 
61  bool isOpen() const;
62  void open(const char* filename);
63  void close();
64  void clear();
65 
66  bool operator()(void * buffer, msize bytes);
67  bool seek(msize pos);
68  bool eof() const;
69  };
70 
71 
72  /**
73  InputStream reading from memory
74  */
75  class MemoryInputStream : public InputStream {
76  private:
77  const char* data;
78  const msize size;
80  public:
81  inline MemoryInputStream(const void* data, msize size): data(static_cast<const char*>(data)), size(size), ptr(0) {}
82 
83  bool operator()(void * buffer, msize bytes);
84  bool seek(msize pos);
85  bool eof() const;
86  };
87 }
InputStream reading from a file.
Definition: input_stream.h:54
bool eof() const
Returns true, if the end of the stream is reached (i.e., all the data is read or the stream is empty)...
void open(const char *filename)
FileInputStream(const char *filename)
Definition: input_stream.h:59
bool operator()(void *buffer, msize bytes)
Reads a given number of bytes into a specific memory location.
bool seek(msize pos)
Moves the read pointer to a given position in the stream.
Minimal input stream interface.
Definition: input_stream.h:27
virtual bool seek(msize pos)=0
Moves the read pointer to a given position in the stream.
virtual bool operator()(void *buffer, msize bytes)=0
Reads a given number of bytes into a specific memory location.
virtual bool eof() const =0
Returns true, if the end of the stream is reached (i.e., all the data is read or the stream is empty)...
InputStream reading from memory.
Definition: input_stream.h:75
MemoryInputStream(const void *data, msize size)
Definition: input_stream.h:81
bool eof() const
Returns true, if the end of the stream is reached (i.e., all the data is read or the stream is empty)...
bool seek(msize pos)
Moves the read pointer to a given position in the stream.
bool operator()(void *buffer, msize bytes)
Reads a given number of bytes into a specific memory location.
uint32_t msize
memory size
Definition: basic_types.h:30
JNIEnv jlong jstring filename