aboutsummaryrefslogtreecommitdiff
path: root/src/old/flexbuf.cpp
blob: 6d55294511a7aea0ebdab69195684e974febb06f (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "flexbuf.h"
#include <string.h>

FlexBuf::FlexBuf()
{
	lpBuf = new char[1024];
	nLastChar = 0;
	nFirstChar = 0;
	nSize = 1024;
	nFill = 0;
	clearData();
}

FlexBuf::~FlexBuf()
{
	delete[] lpBuf;
}

bool FlexBuf::appendData( const char *lpData, int nDSize )
{
	int nStrLen;
	if( nDSize < 0 )
	{
		nStrLen = strlen( lpData );
	}
	else
	{
		nStrLen = nDSize;
	}

	if( nLastChar + nStrLen + 1 > nSize )
	{
		if( nFill + nStrLen + 1 < nSize )
		{
			memcpy( lpBuf, lpBuf+nFirstChar, nFill );
			nLastChar -= nFirstChar;
			nFirstChar = 0;
		}
		else
		{
			nSize += nStrLen+1;
			char *lpNewBuf = new char[nSize];
			memcpy( lpNewBuf, lpBuf+nFirstChar, nFill );
			delete[] lpBuf;
			lpBuf = lpNewBuf;
			nLastChar -= nFirstChar;
			nFirstChar = 0;
		}
	}

	memcpy( &lpBuf[nLastChar], lpData, nStrLen );
	nLastChar += nStrLen;
	nFill += nStrLen;
	lpBuf[nLastChar] = '\0';

	return true;
}

bool FlexBuf::appendData( const char lData )
{
	if( nLastChar + 2 > nSize )
	{
		if( nFill+2 < nSize )
		{
			memcpy( lpBuf, lpBuf+nFirstChar, nFill );
			nLastChar -= nFirstChar;
			nFirstChar = 0;
		}
		else
		{
			nSize += 1024;
			char *lpNewBuf = new char[nSize];
			memcpy( lpNewBuf, lpBuf+nFirstChar, nFill );
			delete[] lpBuf;
			lpBuf = lpNewBuf;
			nLastChar -= nFirstChar;
			nFirstChar = 0;
		}
	}

	lpBuf[nLastChar] = lData;
	nLastChar++;
	nFill++;
	lpBuf[nLastChar] = '\0';

	return true;
}

bool FlexBuf::appendData( const short lData )
{
	return appendData( (const char *)&lData, sizeof(short) );
}

bool FlexBuf::appendData( const int lData )
{
	return appendData( (const char *)&lData, sizeof(int) );
}

bool FlexBuf::appendData( const long lData )
{
	return appendData( (const char *)&lData, sizeof(long) );
}

bool FlexBuf::appendData( const float lData )
{
	return appendData( (const char *)&lData, sizeof(float) );
}

bool FlexBuf::appendData( const double lData )
{
	return appendData( (const char *)&lData, sizeof(double) );
}

bool FlexBuf::appendData( const unsigned char lData )
{
	return appendData( (const char)lData );
}

bool FlexBuf::appendData( const unsigned short lData )
{
	return appendData( (const char *)&lData, sizeof(short) );
}

bool FlexBuf::appendData( const unsigned long lData )
{
	return appendData( (const char *)&lData, sizeof(long) );
}

bool FlexBuf::appendData( const unsigned int lData )
{
	return appendData( (const char *)&lData, sizeof(int) );
}

bool FlexBuf::clearData()
{
	nFirstChar = nLastChar = nFill = 0;
	lpBuf[nLastChar] = '\0';

	return true;
}

const char *FlexBuf::getData()
{
	return (lpBuf+nFirstChar);
}

int FlexBuf::getLength()
{
	return nFill;
}

int FlexBuf::getCapacity()
{
	return nSize;
}

bool FlexBuf::usedData( int nAmount )
{
	// Remove from the end if negative
	if( nAmount < 0 )
	{
		if( nFill+nAmount < 0 )
		{
			nFill = nFirstChar = nLastChar = 0;
			return true;
		}
		nLastChar += nAmount;
		nFill += nAmount;
		return true;
	}
	if( nAmount > nFill )
	{
		nAmount = nSize;
//		return false;
	}

	//nLastChar -= nAmount;
	nFirstChar += nAmount;
	nFill -= nAmount;
	
	if( nFill == 0 )
	{
		nFirstChar = nLastChar = 0;
	}

	//if( nLastChar > 0 )
	//{
		//memmove( lpBuf, &lpBuf[nAmount], nLastChar );
	//}

	return true;
}

int FlexBuf::findChar( char cTarget )
{
	for( int j = nFirstChar; j < nLastChar; j++ )
	{
		if( lpBuf[j] == cTarget )
		{
			return j;
		}
	}

	return -1;
}

void FlexBuf::ensureCapacity( int nAmount )
{
	if( nLastChar + nAmount + 1 > nSize )
	{
		if( nFill + nAmount + 1 < nSize )
		{
			memcpy( lpBuf, lpBuf+nFirstChar, nFill );
			nLastChar -= nFirstChar;
			nFirstChar = 0;
		}
		else
		{
			nSize += nAmount+1;
			char *lpNewBuf = new char[nSize];
			memcpy( lpNewBuf, lpBuf+nFirstChar, nFill );
			delete[] lpBuf;
			lpBuf = lpNewBuf;
			nLastChar -= nFirstChar;
			nFirstChar = 0;
		}
	}
}