summaryrefslogtreecommitdiff
path: root/plugins/CommonLibs/CString.cpp
blob: fb8760dc7483c961ccd484dc36f596c05895a869 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
	TCString.cpp - TCString class
	Copyright (c) 2005-2008 Chervov Dmitry

    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"
#include "CString.h"

#define STR_GROWBY 64

#define min(a,b) (((a) < (b)) ? (a) : (b))


template <class T>
void TString<T>::Empty()
{
	nBufSize = 1;
	SetAllocSize(STR_GROWBY);
	pBuf[0] = 0;
}


template <class T>
void TString<T>::Free()
{
//	HeapFree(GetProcessHeap(), 0, pBuf);
	free(pBuf);
	pBuf = NULL;
	nBufSize = 0;
	nAllocSize = 0;
}


template <class T>
T *TString<T>::GetBuffer(int nNewLen)
{
	if (nNewLen != -1)
	{
		SetBufSize(nNewLen + 1);
	}
	_ASSERT(pBuf);
	return pBuf;
}


template <class T>
void TString<T>::ReleaseBuffer(int nNewLen)
{
	if (nNewLen == -1)
	{
		nBufSize = My_lstrlen(pBuf) + 1;
	} else
	{
		nBufSize = nNewLen + 1;
		pBuf[nNewLen] = 0;
		_ASSERT(My_lstrlen(pBuf) == nNewLen);
	}
	_ASSERT(nBufSize <= nAllocSize); // prevent buffer overruns
}


template <class T>
void TString<T>::SetAllocSize(int nNewAllocSize)
{
	_ASSERT(nNewAllocSize > 0);
	T *pNewBuf = /*(char *)HeapAlloc(GetProcessHeap(), 0, sizeof(char) * nNewAllocSize);*/
(T *)malloc(sizeof(T) * nNewAllocSize);
	if (pBuf)
	{
		memcpy(pNewBuf, pBuf, sizeof(T) * min(nBufSize, nNewAllocSize));
		//HeapFree(GetProcessHeap(), 0, pBuf);
		free(pBuf);
	}
	pBuf = pNewBuf;
	nAllocSize = nNewAllocSize;
}


template <class T>
void TString<T>::SetBufSize(int nNewBufSize)
{
	_ASSERT(nNewBufSize >= 0);
	if (nNewBufSize < nBufSize)
	{
		_ASSERT(pBuf);
		pBuf[nNewBufSize - 1] = 0;
	}
	if ((unsigned)(nAllocSize - nNewBufSize) / STR_GROWBY)
	{
		SetAllocSize((nNewBufSize + STR_GROWBY - 1) - (nNewBufSize + STR_GROWBY - 1) % STR_GROWBY);
	}
	nBufSize = nNewBufSize;
}


template <class T>
TString<T>& TString<T>::Cat(const T *pStr)
{
	_ASSERT(pBuf && pStr);
	int StrLen = My_lstrlen(pStr);
	SetAllocSize(nBufSize + StrLen);
	My_lstrcpy(GetBuffer() + GetLen(), pStr);
	ReleaseBuffer(nBufSize + StrLen - 1);
	return *this;
}


template <class T>
TString<T>& TString<T>::Cat(const T c)
{
	_ASSERT(pBuf);
	SetAllocSize(nBufSize + 1);
	int CurLen = GetLen();
	T *p = GetBuffer();
	p[CurLen] = c;
	p[CurLen + 1] = '\0';
	ReleaseBuffer(nBufSize);
	return *this;
}


template <class T>
TString<T>& TString<T>::DiffCat(const T *pStart, const T *pEnd)
{
	_ASSERT(pBuf && pStart && pEnd);
	int StrLen = pEnd - pStart;
	SetAllocSize(nBufSize + StrLen);
	My_strncpy(GetBuffer() + GetLen(), pStart, StrLen);
	ReleaseBuffer(nBufSize + StrLen - 1);
	return *this;
}


template <class T>
TString<T>& TString<T>::Replace(const T *szFind, const T *szReplaceBy)
{
	if (!pBuf)
	{
		return *this;
	}
	T *pCurPos = pBuf;
	int FindLen = My_lstrlen(szFind);
	T *p;
	TString<T> Result;
	Result.GetBuffer(1)[0] = '\0';
	Result.ReleaseBuffer(0); // set the string to ""; we can't do it in a usual way (using a constructor or an assignment) because we don't know whether "" needs to be unicode or ansi
	while (p = ( T* )My_strstr(pCurPos, szFind))
	{
		Result.DiffCat(pCurPos, p);
		Result += szReplaceBy;
		pCurPos = p + FindLen;
	}
	Result += pCurPos;
	*this = Result;
	return *this;
}


template <class T>
TString<T>& TString<T>::Replace(int nIndex, int nCount, const T *szReplaceBy)
{
	if (!pBuf || !szReplaceBy || nIndex < 0 || nCount < 0)
	{
		return *this;
	}

	TString<T> Result;
	Result.GetBuffer(1)[0] = '\0';
	Result.ReleaseBuffer(0); // set the string to ""; we can't do it in a usual way (using a constructor or an assignment) because we don't know whether "" needs to be unicode or ansi
	if (nIndex > GetLen())
	{
		nIndex = GetLen();
	}
	if (nIndex + nCount > GetLen())
	{
		nCount = GetLen() - nIndex;
	}
	Result.DiffCat(pBuf, pBuf + nIndex);
	Result += szReplaceBy;
	Result += pBuf + nIndex + nCount;
	*this = Result;
	return *this;
}


template <class T>
TString<T> TString<T>::Left(int nCount) const
{
	_ASSERT(nCount >= 0);
	TString<T> Result(*this);
	Result.SetBufSize(nCount + 1);
	return Result;
}


template <class T>
TString<T> TString<T>::Right(int nCount) const
{
	_ASSERT(nCount >= 0);
	if (nCount < GetLen())
	{
		return &pBuf[GetLen() - nCount];
	} else
	{
		return *this;
	}
}


template <class T>
TString<T> TString<T>::SubStr(int nIndex, int nCount) const
{
	_ASSERT(nIndex >= 0 && nCount >= 0);
	TString<T> Result;
	if (nIndex < GetLen())
	{
		My_strncpy(Result.GetBuffer(nCount), &pBuf[nIndex], nCount);
		Result.ReleaseBuffer();
	} else
	{
		Result.GetBuffer(1)[0] = '\0';
		Result.ReleaseBuffer(0);
	}
	return Result;
}


template <class T>
TString<T> TString<T>::ToLower() const
{
	TString<T> Result(*this);
	if (!pBuf)
	{
		return Result; // return NULL string
	}
	My_strlwr((T*)Result);
	return Result;
}


template <class T>
TString<T>& TString<T>::operator = (const T *pStr)
{
	if (pStr)
	{
		int StrLen = My_lstrlen(pStr);
		SetBufSize(StrLen + 1);
		My_lstrcpy(GetBuffer(), pStr);
		ReleaseBuffer(StrLen);
	} else
	{
		Free();
	}
	return *this;
}


/*TCString& TCString::Format(char *pszFormat, ...)
{
	va_list argList;
	va_start(argList, pszFormat);
	int StrLen = _vscprintf(pszFormat, argList); // it's stupidity. in some versions of msvcrt.dll there's no _vscprintf function, so there's no any way to determine needed string length. so actually I can't use _vsnprintf too.
	_vsnprintf(GetBuffer(StrLen), StrLen, pszFormat, argList);
	ReleaseBuffer(StrLen);
	va_end(argList);
	return *this;
}
*/

template class TString<TCHAR>;
template class TString<char>;
template class TString<WCHAR>;


CString DBGetContactSettingString(HANDLE hContact, const char *szModule, const char *szSetting, const char *szDefaultValue)
{
	DBVARIANT dbv = {0};
	DBCONTACTGETSETTING dbcgs;
	dbcgs.szModule = szModule;
	dbcgs.pValue = &dbv;
	dbcgs.szSetting = szSetting;
	int iRes = CallService(MS_DB_CONTACT_GETSETTING, (WPARAM)hContact, (LPARAM)&dbcgs);
	CString Result;
	if (!iRes && dbv.type == DBVT_ASCIIZ)
	{
		Result = dbv.pszVal;
	} else
	{
		Result = szDefaultValue;
	}
	if (!iRes)
	{
		CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
	}
	return Result;
}



TCString DBGetContactSettingString(HANDLE hContact, const char *szModule, const char *szSetting, const TCHAR *szDefaultValue)
{
	DBVARIANT dbv = {0};
	DBCONTACTGETSETTING dbcgs;
	dbcgs.szModule = szModule;
	dbcgs.pValue = &dbv;
	dbcgs.szSetting = szSetting;
	dbv.type = DBVT_WCHAR;
	int iRes = CallService(MS_DB_CONTACT_GETSETTING_STR, (WPARAM)hContact, (LPARAM)&dbcgs);
	TCString Result;
	if (!iRes && dbv.type == DBVT_WCHAR)
	{
		Result = dbv.ptszVal;
	} else
	{
		Result = szDefaultValue;
	}
	if (!iRes)
	{
		CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
	}
	return Result;
}



int DBGetContactSettingString(HANDLE hContact, const char *szModule, const char *szSetting, DBVARIANT *dbv)
{
	return db_get_s(hContact, szModule, szSetting, dbv, DBVT_ASCIIZ);
}



TCString DBGetContactSettingAsString(HANDLE hContact, const char *szModule, const char *szSetting, const TCHAR *szDefaultValue)
{ // also converts numeric values to a string
	DBVARIANT dbv = {0};
	DBCONTACTGETSETTING dbcgs;
	dbcgs.szModule = szModule;
	dbcgs.pValue = &dbv;
	dbcgs.szSetting = szSetting;

	dbv.type = DBVT_WCHAR;
	int iRes = CallService(MS_DB_CONTACT_GETSETTING_STR, (WPARAM)hContact, (LPARAM)&dbcgs);

	TCString Result;
	if (!iRes && (dbv.type == DBVT_ASCIIZ || dbv.type == DBVT_WCHAR))
	{
		Result = dbv.ptszVal;
	} else if (dbv.type == DBVT_BYTE || dbv.type == DBVT_WORD || dbv.type == DBVT_DWORD)
	{
		long value = (dbv.type == DBVT_DWORD) ? dbv.dVal : (dbv.type == DBVT_WORD ? dbv.wVal : dbv.bVal);
		_ultot(value, Result.GetBuffer(64), 10);
		Result.ReleaseBuffer();
	} else
	{
		Result = szDefaultValue;
	}
	if (!iRes)
	{
		CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
	}
	return Result;
}