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