| 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 ([email protected]) |
| 11 | * Copyright (C) 2006, 2007 Andrew Ziem |
| 12 | * Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 13 | * Copyright (C) 2004 Marc Maurer ([email protected]) |
| 14 | * Copyright (C) 2003-2005 William Lachance ([email protected]) |
| 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 | : m_buffer(0) |
| 54 | , m_offset(0) |
| 55 | { |
| 56 | if (data && dataSize) |
| 57 | { |
| 58 | m_buffer.resize(dataSize); |
| 59 | std::memcpy(&m_buffer[0], data, dataSize); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | WPSStringStreamPrivate::~WPSStringStreamPrivate() |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | void WPSStringStreamPrivate::append(const unsigned char *data, unsigned dataSize) |
| 68 | { |
| 69 | if (!dataSize) return; |
| 70 | size_t actualSize=m_buffer.size(); |
| 71 | m_buffer.resize(actualSize+size_t(dataSize)); |
| 72 | std::memcpy(&m_buffer[actualSize], data, dataSize); |
| 73 | } |
| 74 | |
| 75 | WPSStringStream::WPSStringStream(const unsigned char *data, const unsigned int dataSize) : |
| 76 | librevenge::RVNGInputStream(), |
| 77 | m_data(new WPSStringStreamPrivate(data, dataSize)) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | WPSStringStream::~WPSStringStream() |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | void WPSStringStream::append(const unsigned char *data, const unsigned int dataSize) |
| 86 | { |
| 87 | if (m_data) m_data->append(data, dataSize); |
| 88 | } |
| 89 | |
| 90 | const unsigned char *WPSStringStream::read(unsigned long numBytes, unsigned long &numBytesRead) |
| 91 | { |
| 92 | numBytesRead = 0; |
| 93 | |
| 94 | if (numBytes == 0 || !m_data) |
| 95 | return nullptr; |
| 96 | |
| 97 | long numBytesToRead; |
| 98 | |
| 99 | if (static_cast<unsigned long>(m_data->m_offset)+numBytes < m_data->m_buffer.size()) |
| 100 | numBytesToRead = long(numBytes); |
| 101 | else |
| 102 | numBytesToRead = long(m_data->m_buffer.size()) - m_data->m_offset; |
| 103 | |
| 104 | numBytesRead = static_cast<unsigned long>(numBytesToRead); // about as paranoid as we can be.. |
| 105 | |
| 106 | if (numBytesToRead == 0) |
| 107 | return nullptr; |
| 108 | |
| 109 | long oldOffset = m_data->m_offset; |
| 110 | m_data->m_offset += numBytesToRead; |
| 111 | |
| 112 | return &m_data->m_buffer[size_t(oldOffset)]; |
| 113 | |
| 114 | } |
| 115 | |
| 116 | long WPSStringStream::tell() |
| 117 | { |
| 118 | return m_data ? m_data->m_offset : 0; |
| 119 | } |
| 120 | |
| 121 | int WPSStringStream::seek(long offset, librevenge::RVNG_SEEK_TYPE seekType) |
| 122 | { |
| 123 | if (!m_data) return -1; |
| 124 | if (seekType == librevenge::RVNG_SEEK_CUR) |
| 125 | m_data->m_offset += offset; |
| 126 | else if (seekType == librevenge::RVNG_SEEK_SET) |
| 127 | m_data->m_offset = offset; |
| 128 | else if (seekType == librevenge::RVNG_SEEK_END) |
| 129 | m_data->m_offset = offset+long(m_data->m_buffer.size()); |
| 130 | |
| 131 | if (m_data->m_offset < 0) |
| 132 | { |
| 133 | m_data->m_offset = 0; |
| 134 | return -1; |
| 135 | } |
| 136 | if (long(m_data->m_offset) > long(m_data->m_buffer.size())) |
| 137 | { |
| 138 | m_data->m_offset = long(m_data->m_buffer.size()); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | bool WPSStringStream::isEnd() |
| 146 | { |
| 147 | if (!m_data || long(m_data->m_offset) >= long(m_data->m_buffer.size())) |
| 148 | return true; |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | bool WPSStringStream::isStructured() |
| 154 | { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | unsigned WPSStringStream::subStreamCount() |
| 159 | { |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | const char *WPSStringStream::subStreamName(unsigned) |
| 164 | { |
| 165 | return nullptr; |
| 166 | } |
| 167 | |
| 168 | bool WPSStringStream::existsSubStream(const char *) |
| 169 | { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | librevenge::RVNGInputStream *WPSStringStream::getSubStreamById(unsigned) |
| 174 | { |
| 175 | return nullptr; |
| 176 | } |
| 177 | |
| 178 | librevenge::RVNGInputStream *WPSStringStream::getSubStreamByName(const char *) |
| 179 | { |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
| 183 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |