summaryrefslogtreecommitdiff
path: root/plugins/Utils/mir_buffer.h
blob: 127892a965249c2903f89490ef97e549bf5c5f78 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
Copyright (C) 2005-2009 Ricardo Pescuma Domenecci

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

This 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/


#ifndef __MIR_BUFFER_H__
# define __MIR_BUFFER_H__

#include <windows.h>

#include <m_variables.h>

template<class T>
static inline size_t __blen(const T *str)
{
	return 0;
}

template<>
static inline size_t __blen<char>(const char *str)
{
	return mir_strlen(str);
}

template<>
static inline size_t __blen<wchar_t>(const wchar_t *str)
{
	return mir_wstrlen(str);
}

template<class T>
static inline T * __bTranslate(const T *str)
{
	return 0;
}

template<>
static inline char * __bTranslate<char>(const char *str)
{
	return Translate(str);
}

template<>
static inline wchar_t * __bTranslate<wchar_t>(const wchar_t *str)
{
	return TranslateW(str);
}


template<class O, class D>
static void __bcopy(D *dest, const O *orig, size_t len)
{
}

template<>
static void __bcopy(char *dest, const char *orig, size_t len)
{
	mir_strncpy(dest, orig, len);
}

template<>
static void __bcopy(WCHAR *dest, const WCHAR *orig, size_t len)
{
	wcsncpy(dest, orig, len);
}

template<>
static void __bcopy(WCHAR *dest, const char *orig, size_t len)
{
	MultiByteToWideChar(CallService("LangPack/GetCodePage", 0, 0), 0, orig, (int)len, dest, (int)len);
}

template<>
static void __bcopy(char *dest, const WCHAR *orig, size_t len)
{
	WideCharToMultiByte(CallService("LangPack/GetCodePage", 0, 0), 0, orig, (int)len, dest, (int)len, NULL, NULL);
}



template<class T>
class Buffer
{
	public:
		size_t len;
		T *str;

		Buffer() : str(NULL), size(0), len(0)
		{
			alloc(1);
			pack();
		}

		Buffer(T in) : str(NULL), size(0), len(0)
		{
			if (in == NULL)
			{
				alloc(1);
				pack();
			}
			else
			{
				str = in;
				size = len = __blen(str);
			}
		}

		~Buffer()
		{
			free();
		}

		void pack()
		{
			if (str != NULL)
				memset(&str[len], 0, sizeof(str[len]));
		}

		void alloc(size_t total)
		{
			if (total > size)
			{
				size = total + 256 - total % 256;
				if (str == NULL)
					str = (T *) mir_alloc(size * sizeof(T));
				else
					str = (T *) mir_realloc(str, size * sizeof(T));
			}
		}

		void free()
		{
			if (str != NULL)
			{
				mir_free(str);
				str = NULL;
				len = size = 0;
			}
		}

		void clear()
		{
			len = 0;
			pack();
		}

		void append(T app)
		{
			alloc(len + 1 + 1);

			str[len] = app;
			len++;
			pack();
		}

		void appendn(size_t n, T app)
		{
			alloc(len + n + 1);

			for (; n > 0; n--)
			{
				str[len] = app;
				len++;
			}
			pack();
		}

		void append(const char *app, size_t appLen = -1)
		{
			if (app == NULL)
				return;
			if (appLen == -1)
				appLen = __blen(app);

			size_t total = len + appLen + 1;
			alloc(total);

			__bcopy(&str[len], app, appLen);
			len += appLen;
			pack();
		}

		void append(const WCHAR *app, size_t appLen = -1)
		{
			if (app == NULL)
				return;
			if (appLen == -1)
				appLen = __blen(app);

			size_t total = len + appLen + 1;
			alloc(total);

			__bcopy(&str[len], app, appLen);
			len += appLen;
			pack();
		}

		void append(const Buffer<char> &app)
		{
			if (app.str == NULL)
				return;
			size_t appLen = app.len;

			size_t total = len + appLen + 1;
			alloc(total);

			__bcopy(&str[len], app.str, appLen);
			len += appLen;
			pack();
		}

		void append(const Buffer<WCHAR> &app)
		{
			size_t appLen = app.len;

			size_t total = len + appLen + 1;
			alloc(total);

			__bcopy(&str[len], app.str	, appLen);
			len += appLen;
			pack();
		}

		void appendPrintf(const T *app, ...)
		{
			size_t total = len + 512;
			alloc(total);

			va_list arg;
			va_start(arg, app);
			total = __bvsnprintf<T>(&str[len], size - len - 1, app, arg); //!!!!!!!!!!!!
			if (total < 0)
				total = size - len - 1;
			len += total;
			pack();
		}

		void insert(size_t pos, T *app, size_t appLen = -1)
		{
			if (pos > len)
				pos = len;
			if (pos < 0)
				pos = 0;

			if (appLen == -1)
				appLen = __blen(app);

			alloc(len + appLen + 1);

			if (pos < len)
				memmove(&str[pos + appLen], &str[pos], (len - pos) * sizeof(T));
			memmove(&str[pos], app, appLen * sizeof(T));

			len += appLen;
			pack();
		}

		void replace(size_t start, size_t end, T *app, size_t appLen = -1)
		{
			if (start > len)
				start = len;
			if (start < 0)
				start = 0;

			if (end > len)
				end = len;
			if (end < start)
				end = start;

			if (appLen == -1)
				appLen = __blen(app);

			size_t oldLen = end - start;
			if (oldLen < appLen)
				alloc(len + appLen - oldLen + 1);

			if (end < len && oldLen != appLen)
				memmove(&str[start + appLen], &str[end], (len - end) * sizeof(T));
			memmove(&str[start], app, appLen * sizeof(T));

			len += appLen - oldLen;
			pack();
		}

		void replaceAll(T find, T replace)
		{
			for(size_t i = 0; i < len; i++)
				if (str[len] == find)
					str[len] = replace;
			pack();
		}

		void translate()
		{
			if (str == NULL || len == 0)
				return;

			str[len] = 0;
			T *tmp = __bTranslate(str);
			len = __blen(tmp);
			alloc(len + 1);
			memmove(str, tmp, len * sizeof(T));
			pack();
		}

		void reverse()
		{
			for(size_t i = 0; i < len/2; i++)
			{
				T tmp = str[i];
				str[i] = str[len-i-1];
				str[len-i-1] = tmp;
			}
		}

		T *appender(size_t appLen)
		{
			alloc(len + appLen + 1);
			T *ret = &str[len];
			len += appLen;
			return ret;
		}

		T *lock(size_t maxSize)
		{
			alloc(len + maxSize + 1);
			return &str[len];
		}

		void release()
		{
			len += max(__blen(&str[len]), size - len - 1);
		}

		T *detach()
		{
			T *ret = str;
			str = NULL;
			len = 0;
			return ret;
		}

		void trimRight()
		{
			if (str == NULL)
				return;

			int e;
			for(e = len-1; e >= 0 && (str[e] == (T)' '
									  || str[e] == (T)'\t'
									  || str[e] == (T)'\r'
									  || str[e] == (T)'\n'); e--) ;
			len = e+1;
			pack();
		}

		void trimLeft()
		{
			if (str == NULL)
				return;

			int s;
			for(s = 0; str[s] == (T)' '
					   || str[s] == (T)'\t'
					   || str[s] == (T)'\r'
					   || str[s] == (T)'\n'; s++) ;
			if (s > 0)
			{
				memmove(str, &str[s], (len - s) * sizeof(T));
				len -= s;
			}
			pack();
		}

		void trim()
		{
			trimRight();
			trimLeft();
		}

		Buffer<T>& operator+=(const char *txt)
		{
			append(txt);
			return *this;
		}

		Buffer<T>& operator+=(const WCHAR *txt)
		{
			append(txt);
			return *this;
		}

		Buffer<T>& operator+=(const Buffer<T> &txt)
		{
			append(txt);
			return *this;
		}

		Buffer<T>& operator=(const char *txt)
		{
			clear();
			append(txt);
			return *this;
		}

		Buffer<T>& operator=(const WCHAR *txt)
		{
			clear();
			append(txt);
			return *this;
		}

		Buffer<T>& operator=(const Buffer<T> &txt)
		{
			clear();
			append(txt);
			return *this;
		}


	private:
		size_t size;
};


static void ReplaceVars(Buffer<TCHAR> *buffer, MCONTACT hContact, TCHAR **variables, int numVariables)
{
	if (buffer->len < 3)
		return;

	if (numVariables < 0)
		return;

	for(size_t i = buffer->len - 1; i > 0; i--)
	{
		if (buffer->str[i] == _T('%'))
		{
			// Find previous
			size_t j;
			for(j = i - 1; j > 0 && ((buffer->str[j] >= _T('a') && buffer->str[j] <= _T('z'))
								    || (buffer->str[j] >= _T('A') && buffer->str[j] <= _T('Z'))
									|| buffer->str[j] == _T('-')
									|| buffer->str[j] == _T('_')); j--) ;

			if (buffer->str[j] == _T('%'))
			{
				size_t foundLen = i - j + 1;
				if (foundLen == 9 && _tcsncmp(&buffer->str[j], _T("%contact%"), 9) == 0)
				{
					buffer->replace(j, i + 1, (TCHAR *) CallService(MS_CLIST_GETCONTACTDISPLAYNAME, hContact, GCDNF_TCHAR));
				}
				else if (foundLen == 6 && _tcsncmp(&buffer->str[j], _T("%date%"), 6) == 0)
				{
					TCHAR tmp[128];
					DBTIMETOSTRINGT tst = {0};
					tst.szFormat = _T("d s");
					tst.szDest = tmp;
					tst.cbDest = 128;
					CallService(MS_DB_TIME_TIMESTAMPTOSTRINGT, (WPARAM) time(NULL), (LPARAM) &tst);
					buffer->replace(j, i + 1, tmp);
				}
				else
				{
					for(int k = 0; k < numVariables; k += 2)
					{
						size_t len = mir_tstrlen(variables[k]);
						if (foundLen == len + 2 && _tcsncmp(&buffer->str[j]+1, variables[k], len) == 0)
						{
							buffer->replace(j, i + 1, variables[k + 1]);
							break;
						}
					}
				}
			}

			i = j;
			if (i == 0)
				break;
		}
		else if (buffer->str[i] == _T('\\') && i+1 <= buffer->len-1 && buffer->str[i+1] == _T('n'))
		{
			buffer->str[i] = _T('\r');
			buffer->str[i+1] = _T('\n');
		}
	}
}


static void ReplaceTemplate(Buffer<TCHAR> *out, MCONTACT hContact, TCHAR *templ, TCHAR **vars, int numVars)
{

	if (ServiceExists(MS_VARS_FORMATSTRING))
	{
		TCHAR *tmp = variables_parse_ex(templ, NULL, hContact, vars, numVars);
		if (tmp != NULL)
		{
			out->append(tmp);
			mir_free(tmp);
			out->pack();
			return;
		}
	}

	out->append(templ);
	ReplaceVars(out, hContact, vars, numVars);
	out->pack();
}


#endif // __MIR_BUFFER_H__