summaryrefslogtreecommitdiff
path: root/plugins/SmileyAdd/bkstring.cpp
blob: 2973783abc1b271698720a8e3e6dbbd2c89b7cb3 (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
/*
Miranda SmileyAdd Plugin
Copyright (C) 2008 - 2011 Boris Krasnovskiy All Rights Reserved

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 version 2
of the License.

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, see <http://www.gnu.org/licenses/>.
*/

#ifndef __GNUC__
#	ifdef _DEBUG
#		define _CRTDBG_MAP_ALLOC
#		include <stdlib.h>
#		include <crtdbg.h>
#  else
#		include <stdlib.h>
#  endif
#endif

#include <stdio.h>
#include <stdarg.h>

#include "bkstring.h"


bkstring::~bkstring() { if (sizeAlloced) free(buf); }

void bkstring::reserve(size_type len)
{
	if (len >= sizeAlloced || sizeAlloced == 0)
	{
		if (sizeAlloced == 0) buf = NULL;
		buf = (value_type*)realloc(buf, (len+1) * sizeof(value_type));
		if (sizeAlloced == 0) buf[0] = 0;
		sizeAlloced = len+1;
	}
}

void bkstring::appendfmt(const value_type *fmt, ...) 
{
	areserve(_tcslen(fmt)*2);

	va_list vararg;
	va_start(vararg, fmt);
	for (;;) 
	{
		int len = _vsntprintf(buf + lenBuf, sizeAlloced - lenBuf - 1, fmt, vararg);
		if (len < 0)
			reserve(sizeAlloced + 256);
		else
		{
			lenBuf += len;
			buf[lenBuf] = 0;
			break;
		}
	}
	va_end(vararg);
}

bkstring& bkstring::append(const value_type* _Ptr)
{
	size_type len = _tcslen(_Ptr);
	areserve(len);
	memcpy(buf+lenBuf, _Ptr, (len+1)*sizeof(value_type));
	lenBuf += len;
	return *this;
}

bkstring& bkstring::append(const value_type* _Ptr, size_type _Count)
{
	size_type len = min(_tcslen(_Ptr), _Count);
	areserve(len);
	memcpy(buf+lenBuf, _Ptr, len*sizeof(value_type));
	lenBuf += len;
	buf[lenBuf] = 0;
	return *this;
}

bkstring& bkstring::append(const bkstring& _Str, size_type _Off, size_type _Count)
{
	size_type len = min(_Count, _Str.size() - _Off);
	areserve(len);
	memcpy(buf+lenBuf, _Str.c_str()+_Off, len*sizeof(value_type));
	lenBuf += len;
	buf[lenBuf] = 0;
	return *this;
}

bkstring& bkstring::append(const bkstring& _Str)
{
	size_type len = _Str.size();
	areserve(len);
	memcpy(buf+lenBuf, _Str.c_str(), len*sizeof(value_type));
	lenBuf += len;
	buf[lenBuf] = 0;
	return *this;
}

bkstring& bkstring::append(size_type _Count, value_type _Ch)
{
	areserve(_Count);
	for(size_type i=0; i<_Count; ++i) buf[lenBuf+i] = _Ch;
	lenBuf += _Count;
	buf[lenBuf] = 0;
	return *this;
}


bkstring& bkstring::assign(const value_type* _Ptr, size_type _Count)
{
	if (_Count == 0 && sizeAlloced == 0)
	{
		buf = (TCHAR*)_T("");
	}
	else
	{
		reserve(_Count);
		memcpy(buf, _Ptr, _Count*sizeof(value_type));
		buf[_Count] = 0;
		lenBuf = _Count;
	}
	return *this;
}

bkstring& bkstring::assign(const bkstring& _Str, size_type _Off, size_type _Count)
{
	size_type len = min(_Count, _Str.size() - _Off);
	if (len == 0 && sizeAlloced == 0)
	{
		buf = (TCHAR*)_T("");
	}
	else
	{
		reserve(len);
		memcpy(buf, _Str.c_str() + _Off, len*sizeof(value_type));
		lenBuf = len;
		buf[len] = 0;
	}
	return *this;
}

bkstring& bkstring::assign(size_type _Count, value_type _Ch)
{
	reserve(_Count);
	for(size_type i=0; i<_Count; ++i) buf[i] = _Ch;
	buf[_Count] = 0;
	lenBuf = _Count;
	return *this;
}

bkstring::size_type bkstring::find(value_type _Ch, size_type _Off) const
{
	for (size_type i=_Off; i<=lenBuf; ++i)
		if (buf[i] == _Ch) return i;
	return (size_type)npos;
}

bkstring::size_type bkstring::find(const value_type* _Ptr, size_type _Off) const
{
	if (_Off > lenBuf) return (size_type)npos;

	value_type* pstr = _tcsstr(buf+_Off, _Ptr);
	return pstr ? pstr - buf : npos;
}

bkstring::size_type bkstring::find_last_of(value_type _Ch, size_type _Off) const
{
	for (size_type i=(_Off == npos ? lenBuf : _Off); i--;)
		if (buf[i] == _Ch) return i;
	return (size_type)npos;
}

bkstring& bkstring::insert(size_type _P0, const value_type* _Ptr, size_type _Count)
{
	size_type len = _tcslen(_Ptr);
	if (_Count < len) len = _Count;
	areserve(len);
	value_type *p = buf + _P0;
	memmove(p+len, p, (lenBuf-_P0+1)*sizeof(value_type));
	memcpy(p, _Ptr, _Count*sizeof(value_type));
	lenBuf += len;
	return *this;
}

bkstring& bkstring::insert(size_type _P0, size_type _Count, value_type _Ch)
{
	areserve(_Count);
	value_type *p = buf + _P0;
	memmove(p+_Count, p, (lenBuf-_P0+1)*sizeof(value_type));
	for(size_type i=0; i<_Count; ++i) p[i] = _Ch;
	lenBuf += _Count;
	return *this;
}

bkstring& bkstring::erase(size_type _Pos, size_type _Count)
{
	if (_Pos < lenBuf)
	{
		const size_type len = min(lenBuf - _Pos, _Count);
		value_type *p = buf + _Pos;
		lenBuf -= len;
		memmove(p, p+len, (lenBuf - _Pos)*sizeof(value_type));
		buf[lenBuf] = 0;
	}
	return *this;
}