libopenraw  0.3.7
stream.hpp
1 /*
2  * libopenraw - stream.hpp
3  *
4  * Copyright (C) 2006-2020 Hubert Figuière
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #pragma once
23 
24 #include <sys/types.h>
25 #include <stddef.h>
26 #include <stdint.h>
27 
28 #include <memory>
29 #include <string>
30 
31 #include <libopenraw/consts.h>
32 
33 
34 namespace OpenRaw {
35 namespace IO {
36 
43 class Stream
44 {
45 public:
47  typedef std::shared_ptr<Stream> Ptr;
51  Stream(const char *filename);
52  virtual ~Stream();
53 
54  Stream(const Stream& f) = delete;
55  Stream & operator=(const Stream&) = delete;
56 
61 
62 // file APIs
64  virtual Error open() = 0;
66  virtual int close() = 0;
68  virtual int seek(off_t offset, int whence) = 0;
70  virtual int read(void *buf, size_t count) = 0;
72  virtual off_t filesize() = 0;
73 // virtual void *mmap(size_t l, off_t offset) = 0;
74 // virtual int munmap(void *addr, size_t l) = 0;
76  Error get_error() const
77  {
78  return m_error;
79  }
80 
82  const std::string &get_path() const
83  {
84  return m_fileName;
85  }
86 
88  uint8_t readByte() noexcept(false);
89 protected:
91  void set_error(Error error)
92  {
93  m_error = error;
94  }
95 
96 private:
98  std::string m_fileName;
99  Error m_error;
100 };
101 
103 }
104 }
105 
106 /*
107  Local Variables:
108  mode:c++
109  c-file-style:"stroustrup"
110  c-file-offsets:((innamespace . 0))
111  tab-width:2
112  c-basic-offset:2
113  indent-tabs-mode:nil
114  fill-column:80
115  End:
116 */
base virtual class for IO
Definition: stream.hpp:44
const std::string & get_path() const
Get the uri path of the file.
Definition: stream.hpp:82
virtual Error open()=0
open the file
virtual int close()=0
close the file
::or_error Error
Error type.
Definition: stream.hpp:60
Error get_error() const
Obtain the last error.
Definition: stream.hpp:76
Stream(const char *filename)
Construct the stream.
Definition: stream.cpp:30
virtual int read(void *buf, size_t count)=0
read in the file. Semantics are similar to POSIX read()
std::shared_ptr< Stream > Ptr
Definition: stream.hpp:47
virtual off_t filesize()=0
Return the filesize.
void set_error(Error error)
Set the error. This is calld by implementations.
Definition: stream.hpp:91
virtual int seek(off_t offset, int whence)=0
seek in the file. Semantics are similar to POSIX lseek()
uint8_t readByte() noexcept(false)
Read a single byte.
Definition: stream.cpp:40
or_error
Error codes returned by libopenraw.
Definition: consts.h:42
Global namespace for libopenraw.
Definition: arwfile.cpp:29