UFO: Alien Invasion
Loading...
Searching...
No Matches
dbuffer.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23*/
24
25#include "common.h"
26
27dbuffer::dbuffer (int reserve) : _length(0)
28{
29 _data.reserve(reserve);
30}
31
33{
34 _data = other._data;
35 _length = other._length;
36}
37
41
42void dbuffer::add (const char* data, size_t len)
43{
44 _data.insert(_data.begin() + _length, data, data + len);
45 _length += len;
46}
47
61size_t dbuffer::get (char* data, size_t len) const
62{
63 if (len > _length) {
64 len = _length;
65 }
66 std::vector<char>::const_iterator copyEnd = _data.begin() + len;
67 std::copy(_data.begin(), copyEnd, data);
68
69 return len;
70}
71
86size_t dbuffer::getAt (size_t offset, char* data, size_t len) const
87{
88 if (offset > _length)
89 return 0;
90
91 std::vector<char>::const_iterator copyBegin = _data.begin() + offset;
92 len = std::min(len, _length - offset);
93 std::vector<char>::const_iterator copyEnd = copyBegin + len;
94 std::copy(copyBegin, copyEnd, data);
95
96 return len;
97}
98
104size_t dbuffer::remove (size_t len)
105{
106 if (len <= 0) {
107 return 0;
108 }
109
110 if (len > _length) {
111 len = _length;
112 }
113 std::vector<char>::iterator eraseEnd = _data.begin() + len;
114 _data.erase(_data.begin(), eraseEnd);
115 _length -= len;
116 return len;
117}
118
136size_t dbuffer::extract (char* data, size_t len)
137{
138 len = get(data, len);
139 remove(len);
140 return len;
141}
void add(const char *, size_t)
Definition dbuffer.cpp:42
dbuffer(int reserve=512)
Definition dbuffer.cpp:27
size_t extract(char *, size_t)
Read and delete data from a dbuffer.
Definition dbuffer.cpp:136
std::vector< char > _data
Definition dbuffer.h:25
size_t remove(size_t)
Deletes data from a dbuffer.
Definition dbuffer.cpp:104
size_t getAt(size_t, char *, size_t) const
Read data from a dbuffer.
Definition dbuffer.cpp:86
virtual ~dbuffer()
Definition dbuffer.cpp:38
size_t _length
Definition dbuffer.h:24
size_t get(char *, size_t) const
Read data from a dbuffer.
Definition dbuffer.cpp:61
definitions common between client and server, but not game lib
voidpf uLong offset
Definition ioapi.h:45
QGL_EXTERN GLuint GLchar GLuint * len
Definition r_gl.h:99
QGL_EXTERN GLsizei const GLvoid * data
Definition r_gl.h:89