Debian Codegraph @

src/tools/file/input.h

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw: tools
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
18 *
19 *
20 * All Rights Reserved.
21 *
22 * For minor contributions see the git repository.
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
26 * in which case the provisions of the LGPLv2+ are applicable
27 * instead of those above.
28 */
29
30 #ifndef MWAW_FILE_H
31 # define MWAW_FILE_H
32 #include <stdio.h>
33
34 #include <vector>
35
36 namespace libmwaw_tools
37 {
38 class InputStream
39 {
40 protected:
41 InputStream()
42 : m_offset(0)
43 {
44 }
45 public:
46 virtual ~InputStream();
47
48 virtual long length() = 0;
49 enum SeekType { SK_SET, SK_CUR, SK_END };
50
51 virtual unsigned char const *read(unsigned long numBytes, unsigned long &numBytesRead) = 0;
52
53 unsigned char readU8();
54 unsigned short readU16();
55 unsigned int readU32();
56 char read8();
57 short read16();
58 int read32();
59 int seek(long offset, SeekType seekType);
60 long tell() const
61 {
62 return m_offset;
63 }
64
65 bool atEOS()
66 {
67 return m_offset >= length();
68 }
69
70 protected:
71 volatile long m_offset;
72 InputStream(const InputStream &) = delete;
73 InputStream &operator=(const InputStream &) = delete;
74 };
75
76 class StringStream final : public InputStream
77 {
78 public:
79 StringStream(unsigned char const *data, const unsigned long dataSize);
80 ~StringStream() final
81 {
82 }
83
84 unsigned char const *read(unsigned long numBytes, unsigned long &numBytesRead) final;
85 long length() final
86 {
87 return long(m_buffer.size());
88 }
89 private:
90 std::vector<unsigned char> m_buffer;
91 StringStream(const StringStream &) = delete;
92 StringStream &operator=(const StringStream &) = delete;
93 };
94
95 class FileStream final : public InputStream
96 {
97 public:
98 explicit FileStream(char const *path);
99 ~FileStream() final;
100 bool ok() const
101 {
102 return m_isOk;
103 }
104 unsigned char const *read(unsigned long numBytes, unsigned long &numBytesRead) final;
105 long length() final;
106 private:
107 FILE *m_file;
108 bool m_isOk;
109
110 std::vector<unsigned char> m_buffer;
111 long m_bufferPos;
112
113 FileStream(const FileStream &) = delete;
114 FileStream &operator=(const FileStream &) = delete;
115 };
116
117 }
118 #endif
119
120 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: