/src/libwps/src/lib/WPSStringStream.cpp
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
2 | | /* libwps |
3 | | * Version: MPL 2.0 / LGPLv2.1+ |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | * Major Contributor(s): |
10 | | * Copyright (C) 2009, 2011 Alonso Laurent (alonso@loria.fr) |
11 | | * Copyright (C) 2006, 2007 Andrew Ziem |
12 | | * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch) |
13 | | * Copyright (C) 2004 Marc Maurer (uwog@uwog.net) |
14 | | * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca) |
15 | | * |
16 | | * For minor contributions see the git repository. |
17 | | * |
18 | | * Alternatively, the contents of this file may be used under the terms |
19 | | * of the GNU Lesser General Public License Version 2.1 or later |
20 | | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
21 | | * applicable instead of those above. |
22 | | * |
23 | | * For further information visit http://libwps.sourceforge.net |
24 | | */ |
25 | | |
26 | | #include <cstring> |
27 | | #include <vector> |
28 | | |
29 | | #include <librevenge-stream/librevenge-stream.h> |
30 | | |
31 | | #include "WPSStringStream.h" |
32 | | |
33 | | //! internal data of a WPSStringStream |
34 | | class WPSStringStreamPrivate |
35 | | { |
36 | | public: |
37 | | //! constructor |
38 | | WPSStringStreamPrivate(const unsigned char *data, unsigned dataSize); |
39 | | //! destructor |
40 | | ~WPSStringStreamPrivate(); |
41 | | //! append some data at the end of the actual stream |
42 | | void append(const unsigned char *data, unsigned dataSize); |
43 | | //! the stream buffer |
44 | | std::vector<unsigned char> m_buffer; |
45 | | //! the stream offset |
46 | | long m_offset; |
47 | | private: |
48 | | WPSStringStreamPrivate(const WPSStringStreamPrivate &) = delete; |
49 | | WPSStringStreamPrivate &operator=(const WPSStringStreamPrivate &) = delete; |
50 | | }; |
51 | | |
52 | | WPSStringStreamPrivate::WPSStringStreamPrivate(const unsigned char *data, unsigned dataSize) |
53 | 14.3k | : m_buffer(0) |
54 | 14.3k | , m_offset(0) |
55 | 14.3k | { |
56 | 14.3k | if (data && dataSize) |
57 | 14.3k | { |
58 | 14.3k | m_buffer.resize(dataSize); |
59 | 14.3k | std::memcpy(&m_buffer[0], data, dataSize); |
60 | 14.3k | } |
61 | 14.3k | } |
62 | | |
63 | | WPSStringStreamPrivate::~WPSStringStreamPrivate() |
64 | 14.3k | { |
65 | 14.3k | } |
66 | | |
67 | | void WPSStringStreamPrivate::append(const unsigned char *data, unsigned dataSize) |
68 | 102 | { |
69 | 102 | if (!dataSize) return; |
70 | 102 | size_t actualSize=m_buffer.size(); |
71 | 102 | m_buffer.resize(actualSize+size_t(dataSize)); |
72 | 102 | std::memcpy(&m_buffer[actualSize], data, dataSize); |
73 | 102 | } |
74 | | |
75 | | WPSStringStream::WPSStringStream(const unsigned char *data, const unsigned int dataSize) : |
76 | 14.3k | librevenge::RVNGInputStream(), |
77 | 14.3k | m_data(new WPSStringStreamPrivate(data, dataSize)) |
78 | 14.3k | { |
79 | 14.3k | } |
80 | | |
81 | | WPSStringStream::~WPSStringStream() |
82 | 14.3k | { |
83 | 14.3k | } |
84 | | |
85 | | void WPSStringStream::append(const unsigned char *data, const unsigned int dataSize) |
86 | 102 | { |
87 | 102 | if (m_data) m_data->append(data, dataSize); |
88 | 102 | } |
89 | | |
90 | | const unsigned char *WPSStringStream::read(unsigned long numBytes, unsigned long &numBytesRead) |
91 | 54.4M | { |
92 | 54.4M | numBytesRead = 0; |
93 | | |
94 | 54.4M | if (numBytes == 0 || !m_data) |
95 | 0 | return nullptr; |
96 | | |
97 | 54.4M | long numBytesToRead; |
98 | | |
99 | 54.4M | if (static_cast<unsigned long>(m_data->m_offset)+numBytes < m_data->m_buffer.size()) |
100 | 54.4M | numBytesToRead = long(numBytes); |
101 | 6.06k | else |
102 | 6.06k | numBytesToRead = long(m_data->m_buffer.size()) - m_data->m_offset; |
103 | | |
104 | 54.4M | numBytesRead = static_cast<unsigned long>(numBytesToRead); // about as paranoid as we can be.. |
105 | | |
106 | 54.4M | if (numBytesToRead == 0) |
107 | 1.71k | return nullptr; |
108 | | |
109 | 54.4M | long oldOffset = m_data->m_offset; |
110 | 54.4M | m_data->m_offset += numBytesToRead; |
111 | | |
112 | 54.4M | return &m_data->m_buffer[size_t(oldOffset)]; |
113 | | |
114 | 54.4M | } |
115 | | |
116 | | long WPSStringStream::tell() |
117 | 14.2M | { |
118 | 14.2M | return m_data ? m_data->m_offset : 0; |
119 | 14.2M | } |
120 | | |
121 | | int WPSStringStream::seek(long offset, librevenge::RVNG_SEEK_TYPE seekType) |
122 | 10.2M | { |
123 | 10.2M | if (!m_data) return -1; |
124 | 10.2M | if (seekType == librevenge::RVNG_SEEK_CUR) |
125 | 136k | m_data->m_offset += offset; |
126 | 10.1M | else if (seekType == librevenge::RVNG_SEEK_SET) |
127 | 10.1M | m_data->m_offset = offset; |
128 | 8.00k | else if (seekType == librevenge::RVNG_SEEK_END) |
129 | 8.00k | m_data->m_offset = offset+long(m_data->m_buffer.size()); |
130 | | |
131 | 10.2M | if (m_data->m_offset < 0) |
132 | 0 | { |
133 | 0 | m_data->m_offset = 0; |
134 | 0 | return -1; |
135 | 0 | } |
136 | 10.2M | if (long(m_data->m_offset) > long(m_data->m_buffer.size())) |
137 | 120k | { |
138 | 120k | m_data->m_offset = long(m_data->m_buffer.size()); |
139 | 120k | return -1; |
140 | 120k | } |
141 | | |
142 | 10.1M | return 0; |
143 | 10.2M | } |
144 | | |
145 | | bool WPSStringStream::isEnd() |
146 | 948k | { |
147 | 948k | if (!m_data || long(m_data->m_offset) >= long(m_data->m_buffer.size())) |
148 | 1.06k | return true; |
149 | | |
150 | 947k | return false; |
151 | 948k | } |
152 | | |
153 | | bool WPSStringStream::isStructured() |
154 | 0 | { |
155 | 0 | return false; |
156 | 0 | } |
157 | | |
158 | | unsigned WPSStringStream::subStreamCount() |
159 | 0 | { |
160 | 0 | return 0; |
161 | 0 | } |
162 | | |
163 | | const char *WPSStringStream::subStreamName(unsigned) |
164 | 0 | { |
165 | 0 | return nullptr; |
166 | 0 | } |
167 | | |
168 | | bool WPSStringStream::existsSubStream(const char *) |
169 | 0 | { |
170 | 0 | return false; |
171 | 0 | } |
172 | | |
173 | | librevenge::RVNGInputStream *WPSStringStream::getSubStreamById(unsigned) |
174 | 0 | { |
175 | 0 | return nullptr; |
176 | 0 | } |
177 | | |
178 | | librevenge::RVNGInputStream *WPSStringStream::getSubStreamByName(const char *) |
179 | 0 | { |
180 | 0 | return nullptr; |
181 | 0 | } |
182 | | |
183 | | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |