Commit 799080b0f9ae55aa675b4c935a702054e11cade4
1 parent
8a6b3582
In FUTURE, refactor `BufferInputSource` to use is::OffsetBuffer.
Showing
2 changed files
with
91 additions
and
1 deletions
include/qpdf/BufferInputSource.hh
| @@ -23,6 +23,8 @@ | @@ -23,6 +23,8 @@ | ||
| 23 | #include <qpdf/Buffer.hh> | 23 | #include <qpdf/Buffer.hh> |
| 24 | #include <qpdf/InputSource.hh> | 24 | #include <qpdf/InputSource.hh> |
| 25 | 25 | ||
| 26 | +#include <memory> | ||
| 27 | + | ||
| 26 | class QPDF_DLL_CLASS BufferInputSource: public InputSource | 28 | class QPDF_DLL_CLASS BufferInputSource: public InputSource |
| 27 | { | 29 | { |
| 28 | public: | 30 | public: |
| @@ -52,11 +54,17 @@ class QPDF_DLL_CLASS BufferInputSource: public InputSource | @@ -52,11 +54,17 @@ class QPDF_DLL_CLASS BufferInputSource: public InputSource | ||
| 52 | void unreadCh(char ch) override; | 54 | void unreadCh(char ch) override; |
| 53 | 55 | ||
| 54 | private: | 56 | private: |
| 57 | +#ifndef QPDF_FUTURE | ||
| 55 | bool own_memory; | 58 | bool own_memory; |
| 56 | std::string description; | 59 | std::string description; |
| 57 | Buffer* buf; | 60 | Buffer* buf; |
| 58 | qpdf_offset_t cur_offset; | 61 | qpdf_offset_t cur_offset; |
| 59 | qpdf_offset_t max_offset; | 62 | qpdf_offset_t max_offset; |
| 63 | +#else | ||
| 64 | + class Members; | ||
| 65 | + | ||
| 66 | + std::unique_ptr<Members> m; | ||
| 67 | +#endif | ||
| 60 | }; | 68 | }; |
| 61 | 69 | ||
| 62 | #endif // QPDF_BUFFERINPUTSOURCE_HH | 70 | #endif // QPDF_BUFFERINPUTSOURCE_HH |
libqpdf/BufferInputSource.cc
| @@ -10,6 +10,8 @@ | @@ -10,6 +10,8 @@ | ||
| 10 | 10 | ||
| 11 | using namespace qpdf; | 11 | using namespace qpdf; |
| 12 | 12 | ||
| 13 | +#ifndef QPDF_FUTURE | ||
| 14 | + | ||
| 13 | BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) : | 15 | BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) : |
| 14 | own_memory(own_memory), | 16 | own_memory(own_memory), |
| 15 | description(description), | 17 | description(description), |
| @@ -145,6 +147,86 @@ BufferInputSource::unreadCh(char ch) | @@ -145,6 +147,86 @@ BufferInputSource::unreadCh(char ch) | ||
| 145 | } | 147 | } |
| 146 | } | 148 | } |
| 147 | 149 | ||
| 150 | +#else | ||
| 151 | + | ||
| 152 | +class BufferInputSource::Members | ||
| 153 | +{ | ||
| 154 | + public: | ||
| 155 | + Members(std::string const& description, Buffer* buf, bool own_memory) : | ||
| 156 | + buf(own_memory ? buf : nullptr), | ||
| 157 | + is(description, | ||
| 158 | + buf && buf->getSize() > 0 | ||
| 159 | + ? std::string_view(reinterpret_cast<const char*>(buf->getBuffer()), buf->getSize()) | ||
| 160 | + : std::string_view()) | ||
| 161 | + { | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + Members(std::string const& description, std::string const& str) : | ||
| 165 | + content(str), | ||
| 166 | + is(description, content) | ||
| 167 | + { | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + ~Members() = default; | ||
| 171 | + | ||
| 172 | + std::unique_ptr<Buffer> buf{nullptr}; | ||
| 173 | + std::string content; | ||
| 174 | + is::OffsetBuffer is; | ||
| 175 | +}; | ||
| 176 | + | ||
| 177 | +BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) : | ||
| 178 | + m(std::make_unique<Members>(description, buf, own_memory)) | ||
| 179 | +{ | ||
| 180 | +} | ||
| 181 | + | ||
| 182 | +BufferInputSource::BufferInputSource(std::string const& description, std::string const& contents) : | ||
| 183 | + m(std::make_unique<Members>(description, contents)) | ||
| 184 | +{ | ||
| 185 | +} | ||
| 186 | +BufferInputSource::~BufferInputSource() = default; | ||
| 187 | + | ||
| 188 | +qpdf_offset_t | ||
| 189 | +BufferInputSource::findAndSkipNextEOL() | ||
| 190 | +{ | ||
| 191 | + auto result = m->is.findAndSkipNextEOL(); | ||
| 192 | + last_offset = m->is.getLastOffset(); | ||
| 193 | + return result; | ||
| 194 | +} | ||
| 195 | +std::string const& | ||
| 196 | +BufferInputSource::getName() const | ||
| 197 | +{ | ||
| 198 | + return m->is.getName(); | ||
| 199 | +} | ||
| 200 | +qpdf_offset_t | ||
| 201 | +BufferInputSource::tell() | ||
| 202 | +{ | ||
| 203 | + return m->is.tell(); | ||
| 204 | +} | ||
| 205 | +void | ||
| 206 | +BufferInputSource::seek(qpdf_offset_t offset, int whence) | ||
| 207 | +{ | ||
| 208 | + m->is.seek(offset, whence); | ||
| 209 | +} | ||
| 210 | +void | ||
| 211 | +BufferInputSource::rewind() | ||
| 212 | +{ | ||
| 213 | + m->is.rewind(); | ||
| 214 | +} | ||
| 215 | +size_t | ||
| 216 | +BufferInputSource::read(char* buffer, size_t length) | ||
| 217 | +{ | ||
| 218 | + auto result = m->is.read(buffer, length); | ||
| 219 | + last_offset = m->is.getLastOffset(); | ||
| 220 | + return result; | ||
| 221 | +} | ||
| 222 | +void | ||
| 223 | +BufferInputSource::unreadCh(char ch) | ||
| 224 | +{ | ||
| 225 | + m->is.unreadCh(ch); | ||
| 226 | +} | ||
| 227 | + | ||
| 228 | +#endif // QPDF_FUTURE | ||
| 229 | + | ||
| 148 | qpdf_offset_t | 230 | qpdf_offset_t |
| 149 | is::OffsetBuffer::findAndSkipNextEOL() | 231 | is::OffsetBuffer::findAndSkipNextEOL() |
| 150 | { | 232 | { |
| @@ -161,7 +243,7 @@ is::OffsetBuffer::findAndSkipNextEOL() | @@ -161,7 +243,7 @@ is::OffsetBuffer::findAndSkipNextEOL() | ||
| 161 | qpdf_offset_t result = 0; | 243 | qpdf_offset_t result = 0; |
| 162 | auto buffer = view_.begin(); | 244 | auto buffer = view_.begin(); |
| 163 | auto end = view_.end(); | 245 | auto end = view_.end(); |
| 164 | - auto p = buffer + pos; | 246 | + auto p = buffer + static_cast<std::ptrdiff_t>(pos); |
| 165 | 247 | ||
| 166 | while (p < end && !(*p == '\r' || *p == '\n')) { | 248 | while (p < end && !(*p == '\r' || *p == '\n')) { |
| 167 | ++p; | 249 | ++p; |