Debian Codegraph @

src/lib/MWAWStringStream.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
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 * Alternatively, the contents of this file may be used under the terms of
17 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
18 * in which case the provisions of the LGPLv2+ are applicable
19 * instead of those above.
20 */
21
22 #include <cstring>
23 #include <vector>
24
25 #include <librevenge-stream/librevenge-stream.h>
26
27 #include "MWAWStringStream.hxx"
28
29 //! internal data of a MWAWStringStream
30 class MWAWStringStreamPrivate
31 {
32 public:
33 //! constructor
34 MWAWStringStreamPrivate(const unsigned char *data, unsigned dataSize);
35 //! destructor
36 ~MWAWStringStreamPrivate();
37 //! append some data at the end of the actual stream
38 void append(const unsigned char *data, unsigned dataSize);
39 //! resize the stream to some size
40 void resize(unsigned long newSize);
41 //! the stream buffer
42 std::vector<unsigned char> m_buffer;
43 //! the stream offset
44 long m_offset;
45 private:
46 MWAWStringStreamPrivate(const MWAWStringStreamPrivate &) = delete;
47 MWAWStringStreamPrivate &operator=(const MWAWStringStreamPrivate &) = delete;
48 };
49
50 MWAWStringStreamPrivate::MWAWStringStreamPrivate(const unsigned char *data, unsigned dataSize)
51 : m_buffer(0)
52 , m_offset(0)
53 {
54 if (data && dataSize) {
55 m_buffer.resize(dataSize);
56 std::memcpy(&m_buffer[0], data, dataSize);
57 }
58 }
59
60 MWAWStringStreamPrivate::~MWAWStringStreamPrivate()
61 {
62 }
63
64 void MWAWStringStreamPrivate::resize(unsigned long newSize)
65 {
66 m_buffer.resize(newSize);
67 }
68
69 void MWAWStringStreamPrivate::append(const unsigned char *data, unsigned dataSize)
70 {
71 if (!dataSize) return;
72 size_t actualSize=m_buffer.size();
73 m_buffer.resize(actualSize+size_t(dataSize));
74 std::memcpy(&m_buffer[actualSize], data, dataSize);
75 }
76
77 MWAWStringStream::MWAWStringStream(const unsigned char *data, const unsigned int dataSize)
78 : librevenge::RVNGInputStream()
79 , m_data(new MWAWStringStreamPrivate(data, dataSize))
80 {
81 }
82
83 MWAWStringStream::~MWAWStringStream()
84 {
85 }
86
87 void MWAWStringStream::append(const unsigned char *data, const unsigned int dataSize)
88 {
89 if (m_data) m_data->append(data, dataSize);
90 }
91
92 void MWAWStringStream::resize(unsigned long newSize)
93 {
94 if (m_data) m_data->resize(newSize);
95 }
96
97 const unsigned char *MWAWStringStream::read(unsigned long numBytes, unsigned long &numBytesRead)
98 {
99 numBytesRead = 0;
100
101 if (numBytes == 0 || !m_data)
102 return nullptr;
103
104 long numBytesToRead;
105
106 if (static_cast<unsigned long>(m_data->m_offset)+numBytes < m_data->m_buffer.size())
107 numBytesToRead = long(numBytes);
108 else
109 numBytesToRead = long(m_data->m_buffer.size()) - m_data->m_offset;
110
111 numBytesRead = static_cast<unsigned long>(numBytesToRead); // about as paranoid as we can be..
112
113 if (numBytesToRead == 0)
114 return nullptr;
115
116 long oldOffset = m_data->m_offset;
117 m_data->m_offset += numBytesToRead;
118
119 return &m_data->m_buffer[size_t(oldOffset)];
120
121 }
122
123 long MWAWStringStream::tell()
124 {
125 return m_data ? m_data->m_offset : 0;
126 }
127
128 int MWAWStringStream::seek(long offset, librevenge::RVNG_SEEK_TYPE seekType)
129 {
130 if (!m_data) return -1;
131 if (seekType == librevenge::RVNG_SEEK_CUR)
132 m_data->m_offset += offset;
133 else if (seekType == librevenge::RVNG_SEEK_SET)
134 m_data->m_offset = offset;
135 else if (seekType == librevenge::RVNG_SEEK_END)
136 m_data->m_offset = offset+long(m_data->m_buffer.size());
137
138 if (m_data->m_offset < 0) {
139 m_data->m_offset = 0;
140 return -1;
141 }
142 if (long(m_data->m_offset) > long(m_data->m_buffer.size())) {
143 m_data->m_offset = long(m_data->m_buffer.size());
144 return -1;
145 }
146
147 return 0;
148 }
149
150 bool MWAWStringStream::isEnd()
151 {
152 if (!m_data || long(m_data->m_offset) >= long(m_data->m_buffer.size()))
153 return true;
154
155 return false;
156 }
157
158 bool MWAWStringStream::isStructured()
159 {
160 return false;
161 }
162
163 unsigned MWAWStringStream::subStreamCount()
164 {
165 return 0;
166 }
167
168 const char *MWAWStringStream::subStreamName(unsigned)
169 {
170 return nullptr;
171 }
172
173 bool MWAWStringStream::existsSubStream(const char *)
174 {
175 return false;
176 }
177
178 librevenge::RVNGInputStream *MWAWStringStream::getSubStreamById(unsigned)
179 {
180 return nullptr;
181 }
182
183 librevenge::RVNGInputStream *MWAWStringStream::getSubStreamByName(const char *)
184 {
185 return nullptr;
186 }
187
188 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: