Debian Codegraph @

src/tools/zip/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 #include <vector>
34
35 namespace libmwaw_zip
36 {
37 /** virtual class used to define an Input Stream */
38 class InputStream
39 {
40 protected:
41 //! constructor
42 InputStream()
43 : m_offset(0)
44 {
45 }
46 public:
47 //! destructor
48 virtual ~InputStream();
49 //! returns the file length
50 virtual long length() = 0;
51 //! enum to define the seek type: SK_SET, SK_CUR, SK_END
52 enum SeekType { SK_SET, SK_CUR, SK_END };
53 //! try to read numBytes
54 virtual unsigned char const *read(unsigned long numBytes, unsigned long &numBytesRead) = 0;
55 //! read a unsigned char
56 unsigned char readU8();
57 //! read a unsigned short (big endian)
58 unsigned short readU16();
59 //! read a unsigned int (big endian)
60 unsigned int readU32();
61 //! read a char
62 char read8();
63 //! read a short (big endian)
64 short read16();
65 //! read a unsigned int (big endian)
66 int read32();
67 //! try to go to a position. Returns 0 if ok
68 int seek(long offset, SeekType seekType);
69 //! return the actual position
70 long tell() const
71 {
72 return m_offset;
73 }
74 //! return true if we are at the end of the file
75 bool atEOS()
76 {
77 return m_offset >= length();
78 }
79
80 protected:
81 //! the actual position in the file
82 volatile long m_offset;
83 private:
84 InputStream(const InputStream &) = delete;
85 InputStream &operator=(const InputStream &) = delete;
86 };
87
88 class StringStream final : public InputStream
89 {
90 public:
91 StringStream(unsigned char const *data, const unsigned long dataSize);
92 ~StringStream() final { }
93
94 unsigned char const *read(unsigned long numBytes, unsigned long &numBytesRead) final;
95 long length() final
96 {
97 return long(m_buffer.size());
98 }
99 private:
100 std::vector<unsigned char> m_buffer;
101 StringStream(const StringStream &) = delete;
102 StringStream &operator=(const StringStream &) = delete;
103 };
104
105 class FileStream final : public InputStream
106 {
107 public:
108 explicit FileStream(char const *path);
109 ~FileStream() final;
110 bool ok() const
111 {
112 return m_isOk;
113 }
114 unsigned char const *read(unsigned long numBytes, unsigned long &numBytesRead) final;
115 long length() final;
116 private:
117 FILE *m_file;
118 bool m_isOk;
119 std::vector<unsigned char> m_buffer;
120 long m_bufferPos;
121
122 FileStream(const FileStream &) = delete;
123 FileStream &operator=(const FileStream &) = delete;
124 };
125
126 }
127 #endif
128
129 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: