UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dbuffer.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #include "common.h"
26 
27 dbuffer::dbuffer (int reserve) : _length(0)
28 {
29  _data.reserve(reserve);
30 }
31 
32 dbuffer::dbuffer (const dbuffer& other)
33 {
34  _data = other._data;
35  _length = other._length;
36 }
37 
39 {
40 }
41 
42 void dbuffer::add (const char* data, size_t len)
43 {
44  _data.insert(_data.begin() + _length, data, data + len);
45  _length += len;
46 }
47 
61 size_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 
86 size_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 
104 size_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 
136 size_t dbuffer::extract (char* data, size_t len)
137 {
138  len = get(data, len);
139  remove(len);
140  return len;
141 }
dbuffer(int reserve=512)
Definition: dbuffer.cpp:27
size_t getAt(size_t, char *, size_t) const
Read data from a dbuffer.
Definition: dbuffer.cpp:86
std::vector< char > _data
Definition: dbuffer.h:25
void add(const char *, size_t)
Definition: dbuffer.cpp:42
size_t _length
Definition: dbuffer.h:24
size_t extract(char *, size_t)
Read and delete data from a dbuffer.
Definition: dbuffer.cpp:136
virtual ~dbuffer()
Definition: dbuffer.cpp:38
size_t get(char *, size_t) const
Read data from a dbuffer.
Definition: dbuffer.cpp:61
QGL_EXTERN GLuint GLchar GLuint * len
Definition: r_gl.h:99
size_t remove(size_t)
Deletes data from a dbuffer.
Definition: dbuffer.cpp:104
definitions common between client and server, but not game lib
GLsizei const GLvoid * data
Definition: r_gl.h:152
voidpf uLong offset
Definition: ioapi.h:45