[6] | 1 | // Copyright (c) 2013 by István Váradi
|
---|
| 2 |
|
---|
| 3 | // This file is part of VSCPL, a simple cross-platform utility library
|
---|
| 4 |
|
---|
| 5 | // Redistribution and use in source and binary forms, with or without
|
---|
| 6 | // modification, are permitted provided that the following conditions are met:
|
---|
| 7 |
|
---|
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
| 9 | // list of conditions and the following disclaimer.
|
---|
| 10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
| 11 | // this list of conditions and the following disclaimer in the documentation
|
---|
| 12 | // and/or other materials provided with the distribution.
|
---|
| 13 |
|
---|
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
| 15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
| 16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
| 17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
| 18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
| 19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
| 20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 24 |
|
---|
| 25 | // The views and conclusions contained in the software and documentation are those
|
---|
| 26 | // of the authors and should not be interpreted as representing official policies,
|
---|
| 27 | // either expressed or implied, of the FreeBSD Project.
|
---|
| 28 |
|
---|
| 29 | #ifndef HU_VARADIISTVAN_SCPL_IO_BUFFER_H
|
---|
| 30 | #define HU_VARADIISTVAN_SCPL_IO_BUFFER_H
|
---|
| 31 | //------------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | #include <algorithm>
|
---|
| 34 |
|
---|
| 35 | #include <cstdlib>
|
---|
| 36 | #include <cstring>
|
---|
| 37 |
|
---|
| 38 | //------------------------------------------------------------------------------
|
---|
| 39 |
|
---|
| 40 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
| 41 |
|
---|
| 42 | //------------------------------------------------------------------------------
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Base class for buffers. It maintains a memory area of a certain
|
---|
| 46 | * capacity and the length of the data in the buffer.
|
---|
| 47 | */
|
---|
| 48 | class Buffer
|
---|
| 49 | {
|
---|
| 50 | private:
|
---|
| 51 | /**
|
---|
| 52 | * The data itself.
|
---|
| 53 | */
|
---|
| 54 | unsigned char* data;
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * The capacity of the buffer.
|
---|
| 58 | */
|
---|
| 59 | size_t capacity;
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * The amount of data in the buffer.
|
---|
| 63 | */
|
---|
| 64 | size_t length;
|
---|
| 65 |
|
---|
| 66 | public:
|
---|
| 67 | /**
|
---|
| 68 | * Construct the buffer with the given capacity.
|
---|
| 69 | */
|
---|
| 70 | Buffer(size_t capacity);
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Destroy the buffer.
|
---|
| 74 | */
|
---|
| 75 | ~Buffer();
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Get the capacity of the buffer.
|
---|
| 79 | */
|
---|
| 80 | size_t getCapacity() const;
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Get the pointer to the data.
|
---|
| 84 | */
|
---|
| 85 | const unsigned char* getData() const;
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Get the pointer to the data for modification.
|
---|
| 89 | */
|
---|
| 90 | unsigned char* getData();
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * Get the length of the data.
|
---|
| 94 | */
|
---|
| 95 | size_t getLength() const;
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Get whether the buffer is empty or not.
|
---|
| 99 | */
|
---|
| 100 | bool isEmpty() const;
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Get the available capacity in the buffer.
|
---|
| 104 | */
|
---|
| 105 | size_t getAvailable() const;
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Reset the buffer.
|
---|
| 109 | */
|
---|
| 110 | void reset();
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * Add the given amount to the length. Should not exceed the
|
---|
| 114 | * capacity!
|
---|
| 115 | *
|
---|
| 116 | * @return the actual amount added.
|
---|
| 117 | */
|
---|
| 118 | size_t addLength(size_t n);
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Append the given data to the contents of the given buffer.
|
---|
| 122 | *
|
---|
| 123 | * @return the number of bytes actually appended
|
---|
| 124 | */
|
---|
| 125 | size_t append(const void* src, size_t size);
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Extract data into the given buffer from the given offset.
|
---|
| 129 | *
|
---|
| 130 | * @return the number of bytes actually extracted.
|
---|
| 131 | */
|
---|
| 132 | size_t extract(void* dest, size_t size, size_t offset = 0) const;
|
---|
| 133 | };
|
---|
| 134 |
|
---|
| 135 | //------------------------------------------------------------------------------
|
---|
| 136 | // Inline definitions
|
---|
| 137 | //------------------------------------------------------------------------------
|
---|
| 138 |
|
---|
| 139 | inline Buffer::Buffer(size_t capacity) :
|
---|
| 140 | data(new unsigned char[capacity]),
|
---|
| 141 | capacity(capacity),
|
---|
| 142 | length(0)
|
---|
| 143 | {
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | //------------------------------------------------------------------------------
|
---|
| 147 |
|
---|
| 148 | inline Buffer::~Buffer()
|
---|
| 149 | {
|
---|
| 150 | delete[] data;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | //------------------------------------------------------------------------------
|
---|
| 154 |
|
---|
| 155 | inline size_t Buffer::getCapacity() const
|
---|
| 156 | {
|
---|
| 157 | return capacity;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | //------------------------------------------------------------------------------
|
---|
| 161 |
|
---|
| 162 | inline const unsigned char* Buffer::getData() const
|
---|
| 163 | {
|
---|
| 164 | return data;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | //------------------------------------------------------------------------------
|
---|
| 168 |
|
---|
| 169 | inline unsigned char* Buffer::getData()
|
---|
| 170 | {
|
---|
| 171 | return data;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | //------------------------------------------------------------------------------
|
---|
| 175 |
|
---|
| 176 | inline size_t Buffer::getLength() const
|
---|
| 177 | {
|
---|
| 178 | return length;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | //------------------------------------------------------------------------------
|
---|
| 182 |
|
---|
| 183 | inline bool Buffer::isEmpty() const
|
---|
| 184 | {
|
---|
| 185 | return length==0;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | //------------------------------------------------------------------------------
|
---|
| 189 |
|
---|
| 190 | inline size_t Buffer::getAvailable() const
|
---|
| 191 | {
|
---|
| 192 | return capacity - length;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | //------------------------------------------------------------------------------
|
---|
| 196 |
|
---|
| 197 | inline void Buffer::reset()
|
---|
| 198 | {
|
---|
| 199 | length = 0;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | //------------------------------------------------------------------------------
|
---|
| 203 |
|
---|
| 204 | inline size_t Buffer::addLength(size_t n)
|
---|
| 205 | {
|
---|
| 206 | size_t toAdd = std::min(n, getAvailable());
|
---|
| 207 | length += toAdd;
|
---|
| 208 | return toAdd;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | //------------------------------------------------------------------------------
|
---|
| 212 |
|
---|
| 213 | inline size_t Buffer::append(const void* src, size_t size)
|
---|
| 214 | {
|
---|
| 215 | size_t toCopy = std::min(size, getAvailable());
|
---|
| 216 | memcpy(data + length, src, toCopy);
|
---|
| 217 | length += toCopy;
|
---|
| 218 | return toCopy;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | //------------------------------------------------------------------------------
|
---|
| 222 |
|
---|
| 223 | inline size_t Buffer::extract(void* dest, size_t size, size_t offset) const
|
---|
| 224 | {
|
---|
| 225 | size_t toCopy = (offset>=length) ? 0 : std::min(size, length - offset);
|
---|
| 226 | memcpy(dest, data + offset, toCopy);
|
---|
| 227 | return toCopy;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | //------------------------------------------------------------------------------
|
---|
| 231 |
|
---|
| 232 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
| 233 |
|
---|
| 234 | //------------------------------------------------------------------------------
|
---|
| 235 | #endif // HU_VARADIISTVAN_SCPL_IO_BUFFER_H
|
---|
| 236 |
|
---|
| 237 | // Local Variables:
|
---|
| 238 | // mode: C++
|
---|
| 239 | // c-basic-offset: 4
|
---|
| 240 | // indent-tabs-mode: nil
|
---|
| 241 | // End:
|
---|