File: dbuffer.cpp

package info (click to toggle)
ufoai 2.5-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 82,128 kB
  • sloc: cpp: 225,227; python: 5,111; ansic: 4,133; php: 2,209; perl: 1,931; sh: 1,517; xml: 1,115; makefile: 401; sed: 11
file content (141 lines) | stat: -rw-r--r-- 3,503 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
 * @file
 */

/*
Copyright (C) 2002-2013 UFO: Alien Invasion.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "common.h"

dbuffer::dbuffer (int reserve) : _length(0)
{
	_data.reserve(reserve);
}

dbuffer::dbuffer (const dbuffer &other)
{
	_data = other._data;
	_length = other._length;
}

dbuffer::~dbuffer ()
{
}

void dbuffer::add (const char* data, size_t len)
{
	_data.insert(_data.begin() + _length, data, data + len);
	_length += len;
}

/**
 * @brief Read data from a dbuffer
 * @param[out] data pointer to where the data should be copied
 * @param[in] len maximum number of bytes to copy
 * @return number of bytes copied
 *
 * @par
 * Copies up to @c len bytes into @c data
 *
 * @par
 * If the buffer does not contain at least @c len bytes, then as many
 * bytes as are present will be copied.
 */
size_t dbuffer::get (char* data, size_t len) const
{
	if (len > _length) {
		len = _length;
	}
	std::vector<char>::const_iterator copyEnd = _data.begin() + len;
	std::copy(_data.begin(), copyEnd, data);

	return len;
}

/**
 * @brief Read data from a dbuffer
 * @param[in] offset the offset in the source buffer where data should be copied from
 * @param[out] data pointer to where the data should be copied
 * @param[in] len maximum number of bytes to copy
 * @return number of bytes copied
 *
 * @par
 * Copies up to @c len bytes into @c data
 *
 * @par
 * If the buffer does not contain at least @c len bytes after offset,
 * then as many bytes as are present will be copied.
 */
size_t dbuffer::getAt (size_t offset, char* data, size_t len) const
{
	if (offset > _length)
		return 0;

	std::vector<char>::const_iterator copyBegin = _data.begin() + offset;
	len = std::min(len, _length - offset);
	std::vector<char>::const_iterator copyEnd = copyBegin + len;
	std::copy(copyBegin, copyEnd, data);

	return len;
}

/**
 * @brief Deletes data from a dbuffer
 * @param[in] len number of bytes to delete
 * Deletes the given number of bytes from the start of the dbuffer
 */
size_t dbuffer::remove (size_t len)
{
	if (len <= 0) {
		return 0;
	}

	if (len > _length) {
		len = _length;
	}
	std::vector<char>::iterator eraseEnd = _data.begin() + len;
	_data.erase(_data.begin(), eraseEnd);
	_length -= len;
	return len;
}

/**
 * @brief Read and delete data from a dbuffer
 * @param[out] data pointer to where the data should be copied
 * @param[in] len maximum number of bytes to copy
 * @return number of bytes copied
 *
 * @par
 * Copies up to @c len bytes into @c data, and removes them from the dbuffer
 *
 * @par
 * If the buffer does not contain at least @c len bytes, then as many
 * bytes as are present will be copied.
 *
 * @par
 * However many bytes are copied, exactly that many will be removed
 * from the start of the dbuffer.
 */
size_t dbuffer::extract (char* data, size_t len)
{
	len = get(data, len);
	remove(len);
	return len;
}