File: FIFOBuffer.cpp

package info (click to toggle)
sbnc 1.2-26
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,216 kB
  • sloc: cpp: 17,556; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 284
file content (215 lines) | stat: -rw-r--r-- 5,382 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*******************************************************************************
 * shroudBNC - an object-oriented framework for IRC                            *
 * Copyright (C) 2005-2007 Gunnar Beutner                                      *
 *                                                                             *
 * 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 "StdAfx.h"

/**
 * CFIFOBuffer
 *
 * Constructs a new fifo buffer.
 */
CFIFOBuffer::CFIFOBuffer() {
	m_Buffer = NULL;
	m_BufferSize = 0;
	m_Offset = 0;
}

/**
 * ~CFIFOBuffer
 *
 * Destructs a fifo buffer.
 */
CFIFOBuffer::~CFIFOBuffer() {
	free(m_Buffer);
}

/**
 * ResizeBuffer
 *
 * Resizes a buffer. The new size of the buffer will be a multiple
 * of BLOCKSIZE. NULL is returned if the buffer could not be successfully
 * resized.
 *
 * @param Buffer the buffer which is to be resized
 * @param OldSize the old size of the buffer
 * @param NewSize the new size of the buffer
 */
void *CFIFOBuffer::ResizeBuffer(void *Buffer, size_t OldSize, size_t NewSize) {
	if (OldSize != 0) {
		OldSize += BLOCKSIZE - (OldSize % BLOCKSIZE);
	}

	size_t CeilNewSize = NewSize + BLOCKSIZE - (NewSize % BLOCKSIZE);

	size_t OldBlocks = OldSize / BLOCKSIZE;
	size_t NewBlocks = CeilNewSize / BLOCKSIZE;

	if (NewBlocks != OldBlocks) {
		if (NewSize == 0) {
			free(Buffer);

			return NULL;
		} else {
			return realloc(Buffer, NewBlocks * BLOCKSIZE);
		}
	} else {
		return Buffer;
	}
}

/**
 * Optimize
 *
 * Optimizes the memory usage of a buffer.
 */
void CFIFOBuffer::Optimize(void) {
	char *NewBuffer;

	if (m_Offset == 0 || m_Offset < m_BufferSize / 5) {
		return;
	}

	if (m_BufferSize - m_Offset <= 0) {
		free(m_Buffer);

		m_Buffer = NULL;
		m_BufferSize = 0;
		m_Offset = 0;

		return;
	}

	NewBuffer = (char *)ResizeBuffer(NULL, 0, m_BufferSize - m_Offset);

	CHECK_ALLOC_RESULT(NewBuffer, ResizeBuffer) {
		return;
	} CHECK_ALLOC_RESULT_END;

	memcpy(NewBuffer, m_Buffer + m_Offset, m_BufferSize - m_Offset);

	free(m_Buffer);
	m_Buffer = NewBuffer;
	m_BufferSize -= m_Offset;
	m_Offset = 0;
}

/**
 * GetSize
 *
 * Returns the size of the buffer.
 */
size_t CFIFOBuffer::GetSize(void) const {
	return m_BufferSize - m_Offset;
}

/**
 * Peek
 *
 * Returns a pointer to the buffer's data without advancing the read pointer (or
 * NULL if there is no data left in the buffer).
 */
char *CFIFOBuffer::Peek(void) const {
	if (GetSize() == 0) {
		return NULL;
	} else {
		return m_Buffer + m_Offset;
	}
}

/**
 * Reads and returns the specified amount of bytes from the buffer.
 *
 * @param Bytes the number of bytes which should be read from the buffer.
 *              If this value is greater than the size of the buffer,
 *              GetSize() bytes are read instead.
 */
char *CFIFOBuffer::Read(size_t Bytes) {
	char *ReturnValue;

	Optimize();

	ReturnValue = m_Buffer + m_Offset;

	if (Bytes > GetSize()) {
		m_Offset += GetSize();
	} else {
		m_Offset += Bytes;
	}

	return ReturnValue;
}

/**
 * Write
 *
 * Saves data in the buffer.
 *
 * @param Data a pointer to the data
 * @param Size the number of bytes which should be written
 */
RESULT<bool> CFIFOBuffer::Write(const char *Data, size_t Size) {
	char *tempBuffer;

	tempBuffer = (char *)ResizeBuffer(m_Buffer, m_BufferSize,
		m_BufferSize + Size);

	CHECK_ALLOC_RESULT(tempBuffer, ResizeBuffer) {
		THROW(bool, Generic_OutOfMemory, "ResizeBuffer() failed.");
	} CHECK_ALLOC_RESULT_END;

	m_Buffer = tempBuffer;
	memcpy(m_Buffer + m_BufferSize, Data, Size);
	m_BufferSize += Size;

	RETURN(bool, true);
}

/**
 * WriteUnformattedLine
 *
 * Writes a line into the buffer.
 *
 * @param Line the line
 */
RESULT<bool> CFIFOBuffer::WriteUnformattedLine(const char *Line) {
	size_t Length = strlen(Line);

	char *tempBuffer = (char *)ResizeBuffer(m_Buffer, m_BufferSize,
		m_BufferSize + Length + 2);

	CHECK_ALLOC_RESULT(tempBuffer, ResizeBuffer) {
		THROW(bool, Generic_OutOfMemory, "ResizeBuffer() failed.");
	} CHECK_ALLOC_RESULT_END;

	m_Buffer = tempBuffer;
	memcpy(m_Buffer + m_BufferSize, Line, Length);
	memcpy(m_Buffer + m_BufferSize + Length, "\r\n", 2);
	m_BufferSize += Length + 2;

	RETURN(bool, true);
}

/**
 * Flush
 *
 * Removes all data which is currently stored in the buffer.
 */
void CFIFOBuffer::Flush(void) {
	Read(GetSize());
}