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
|
/*
"Spam Filter"-Plugin for Miranda IM
Copyright 2003-2006 Heiko Herkenrath
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 ("SpamFilter-License.txt"); if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// -- Includes
#include "common.h"
// -----------------------------------------
/*
int SubStringExistsInStringCount(const TCHAR* pszStr, const TCHAR* pszSubString, BOOL bCaseSensitive)
{
int iSubStrLen;
char* pszOcc;
int iReturn;
if (!pszSubString) return 0;
pszOcc = pszStr;
iCount = 0;
iSubStrLen = lstrlen(pszSubString);
while ((pszOcc = bCaseSensitive?StrStr(pszOcc, pszOldSubStr):StrStrI(pszOcc, pszOldSubStr)) != NULL)
{
iCount++;
pszOcc = pszOcc+iSubStrLen;
}
return iCount;
}
*/
WCHAR* RemoveSubStr(WCHAR* pszStr, int iPos, int iSubStrLen)
{
if (!pszStr) return NULL;
if ((iPos > 0) && (iSubStrLen > 0) && (iPos < lstrlen(pszStr)))
MoveMemory(&pszStr[iPos], &pszStr[iPos+iSubStrLen], (lstrlen(&pszStr[iPos])+1)*sizeof(WCHAR));
return pszStr;
}
/*
BOOL HasSubStr(const TCHAR* pszStr, int iPos, const TCHAR* pszSubStr);
{
TCHAR* pszPos;
if (!pszSubStr || !pszStr || (iPos >= lstrlen(pszStr))) return FALSE;
// Test for suffix
if (iPos < 0)
return ((lstrlen(pszStr) <= lstrlen(pszSubStr)) || (StrCmp(pszStr+lstrlen(pszStr)-lstrlen(pszSubStr), pszSubStr) != 0));
else
return (StrCmpN(&pszStr[iPos], pszSubStr, lstrlen(pszSubStr)) != 0))
}
*/
WCHAR* InsertSubStr(WCHAR* pszStr, int cchMaxSize, int iPos, const WCHAR* pszSubStr)
{
int iSubStrLen;
if (!pszStr) return NULL;
if (!pszSubStr || (iPos < 0) || (iPos >= lstrlen(pszStr))) return pszStr;
iSubStrLen = lstrlen(pszSubStr);
if ((cchMaxSize > 0) && ((iSubStrLen+lstrlen(pszStr)+1) > cchMaxSize))
return pszStr;
MoveMemory(&pszStr[iPos+iSubStrLen], &pszStr[iPos], (lstrlen(&pszStr[iPos])+1)*sizeof(WCHAR));
CopyMemory(&pszStr[iPos], pszSubStr, iSubStrLen*sizeof(WCHAR)); // no terminating zero
return pszStr;
}
WCHAR* ReplaceSubStringWithString(const WCHAR* pszStr, const WCHAR* pszOldSubStr, const WCHAR* pszNewSubStr, BOOL bCaseSensitive, BOOL bAlsoSearchNewString, BOOL* pbSthReplaced)
{
// returned TCHAR* needs to be mir_free()'d
// returns NULL on error
int iOldSubStrLen, iNewSubStrLen;
int iPos;
WCHAR* pszOcc;
WCHAR* pszReturn;
WCHAR* pszReturnBuf;
if (pbSthReplaced) *pbSthReplaced = FALSE;
if (!pszStr) return NULL;
if (!pszOldSubStr) {
*pbSthReplaced = TRUE;
return mir_wstrdup(pszStr);
}
iOldSubStrLen = (int)lstrlen(pszOldSubStr);
iNewSubStrLen = pszNewSubStr?(int)lstrlen(pszNewSubStr):0;
// Make sure that no infinite loop occurs (on str in new one)
if (bAlsoSearchNewString && pszNewSubStr)
if (bCaseSensitive?StrStr(pszNewSubStr, pszOldSubStr):StrStrI(pszNewSubStr, pszOldSubStr))
bAlsoSearchNewString = FALSE;
pszReturn = mir_wstrdup(pszStr);
if (!pszReturn) return NULL;
pszOcc = pszReturn;
while ((pszOcc = bCaseSensitive?StrStr(pszOcc, pszOldSubStr):StrStrI(pszOcc, pszOldSubStr)) != NULL)
{
// Reallocate more memory (memory block might get moved)
if (iNewSubStrLen > iOldSubStrLen)
{
pszReturnBuf = pszReturn;
pszReturn = (WCHAR*)mir_realloc(pszReturnBuf, (lstrlen(pszReturn)+iNewSubStrLen-iOldSubStrLen+1)*sizeof(WCHAR));
if (!pszReturn) {
mir_free(pszReturnBuf);
return NULL;
}
}
iPos = pszOcc-pszReturn;
RemoveSubStr(pszReturn, iPos, iOldSubStrLen);
if (pszNewSubStr) InsertSubStr(pszReturn, 0, iPos, pszNewSubStr);
// Get new start position for search
if (!bAlsoSearchNewString)
pszOcc = &pszReturn[iPos+iNewSubStrLen];
if (pbSthReplaced) *pbSthReplaced = TRUE;
}
// Correct memory allocation (if less needed)
if (iNewSubStrLen < iOldSubStrLen)
{
pszReturnBuf = pszReturn;
pszReturn = (WCHAR*)mir_realloc(pszReturnBuf, (lstrlen(pszReturn)+1)*sizeof(WCHAR));
if (!pszReturn) pszReturn = pszReturnBuf;
}
return pszReturn;
}
BOOL ReplaceSubStringWithStringBuf(WCHAR* pszStr, int cchMaxSize, const WCHAR* pszOldSubStr, const WCHAR* pszNewSubStr, BOOL bCaseSensitive, BOOL bAlsoSearchNewString)
{
int iOldSubStrLen, iNewSubStrLen;
int iPos;
WCHAR* pszOcc;
BOOL bSthReplaced;
if (!pszStr) return FALSE;
if (!pszOldSubStr || (cchMaxSize <= 0)) return TRUE;
iOldSubStrLen = (int)lstrlen(pszOldSubStr);
iNewSubStrLen = pszNewSubStr?(int)lstrlen(pszNewSubStr):0;
// Make sure that no infinite loop occurs (on str in new one)
if (bAlsoSearchNewString && pszNewSubStr)
if (bCaseSensitive?StrStr(pszNewSubStr, pszOldSubStr):StrStrI(pszNewSubStr, pszOldSubStr))
bAlsoSearchNewString = FALSE;
pszOcc = pszStr;
bSthReplaced = FALSE;
while ((pszOcc = bCaseSensitive?StrStr(pszOcc, pszOldSubStr):StrStrI(pszOcc, pszOldSubStr)) != NULL)
{
if (cchMaxSize < lstrlen(pszStr)+iNewSubStrLen-iOldSubStrLen+1);
break;
iPos = pszOcc-pszStr;
RemoveSubStr(pszStr, iPos, iOldSubStrLen);
if (pszNewSubStr) InsertSubStr(pszStr, 0, iPos, pszNewSubStr);
// Get new start position for search
if (!bAlsoSearchNewString)
pszOcc = &pszStr[iPos+iNewSubStrLen];
bSthReplaced = TRUE;
}
return bSthReplaced;
}
WCHAR* ReplaceSubStringWithStringMultiple(const WCHAR* pszStr, STRINGLIST* pslFromTo, BOOL bCaseSensitive, BOOL bAlsoSearchNewString, BOOL* pbSthReplaced)
{
WCHAR* pszReplaced;
WCHAR* pszStrBuf;
int i;
BOOL bSthReplacedBuf;
if (pbSthReplaced) *pbSthReplaced = FALSE;
if (!pszStr) return NULL;
pszStrBuf = mir_wstrdup(pszStr);
if (pszStrBuf && pslFromTo)
for (i=SL_MIN_POS; i<SLGetMaxPos(pslFromTo); i+=2)
{
pszReplaced = ReplaceSubStringWithString(pszStrBuf, SLGetItem(pslFromTo, i), SLGetItem(pslFromTo, i+1), bCaseSensitive, bAlsoSearchNewString, &bSthReplacedBuf);
if (!pszReplaced) continue;
if (pszStrBuf) mir_free(pszStrBuf);
pszStrBuf = pszReplaced;
if (pbSthReplaced && bSthReplacedBuf) *pbSthReplaced = TRUE;
}
return pszStrBuf;
}
BOOL ReplaceSubStringWithStringMultipleBuf(WCHAR* pszStr, int cchMaxSize, STRINGLIST* pslFromTo, BOOL bCaseSensitive, BOOL bAlsoSearchNewString)
{
int i;
BOOL bSthReplaced;
if (!pszStr) return FALSE;
if (!pslFromTo) return TRUE;
bSthReplaced = FALSE;
for (i=SL_MIN_POS; i<SLGetMaxPos(pslFromTo); i+=2)
if (ReplaceSubStringWithStringBuf(pszStr, cchMaxSize, SLGetItem(pslFromTo, i), SLGetItem(pslFromTo, i+1), bCaseSensitive, bAlsoSearchNewString))
bSthReplaced = TRUE;
return bSthReplaced;
}
WCHAR* InsertSubStrEveryNChars(const WCHAR* pszStr, int iDist, const WCHAR* pszSubStr, BOOL bAlsoDoFirst, BOOL bAlsoDoLast)
{
// returned char needs to be mir_free()'d
// returns NULL on error
int iSubStrLen;
int iLoops;
WCHAR* pszReturn;
int i;
if (!pszStr) return NULL;
if (iDist <= 0) return mir_wstrdup(pszStr);
iSubStrLen = pszSubStr?lstrlen(pszSubStr):0;
iLoops = (int)(lstrlen(pszStr)/iDist);
if ((iLoops == (int)(lstrlen(pszStr)/iDist)) && !bAlsoDoLast) iLoops--;
if (bAlsoDoFirst) iLoops++;
pszReturn = (WCHAR*)mir_alloc((lstrlen(pszStr)+(iLoops*iSubStrLen)+1)*sizeof(WCHAR));
if (!pszReturn) return NULL;
mir_sntprintf(pszReturn, lstrlen(pszStr)+(iLoops*iSubStrLen)+1, _T("%s"), pszStr);
if (iSubStrLen > 0)
for (i=0; i<iLoops; i++) // FIX ME: use NextChar() instead
InsertSubStr(pszReturn, 0, (bAlsoDoFirst?0:iDist)+(i*(iSubStrLen+iDist)), pszSubStr);
return pszReturn;
}
BOOL RemoveWhiteSpaces(WCHAR* pszStr, BOOL bReplaceWithSpaces, BOOL bAlsoUnneededSpaces)
{
BOOL bReturn = FALSE;
if (!pszStr) return bReturn;
if (StrTrim(pszStr, _T("\r\n\t"))) bReturn = TRUE;
if (ReplaceSubStringWithStringBuf(pszStr, 0, _T("\r\n"), bReplaceWithSpaces?_T(" "):_T(""), TRUE, FALSE)) bReturn = TRUE;
if (ReplaceSubStringWithStringBuf(pszStr, 0, _T("\r"), bReplaceWithSpaces?_T(" "):_T(""), TRUE, FALSE)) bReturn = TRUE;
if (ReplaceSubStringWithStringBuf(pszStr, 0, _T("\n"), bReplaceWithSpaces?_T(" "):_T(""), TRUE, FALSE)) bReturn = TRUE;
if (ReplaceSubStringWithStringBuf(pszStr, 0, _T("\t"), bReplaceWithSpaces?_T(" "):_T(""), TRUE, FALSE)) bReturn = TRUE;
if (bAlsoUnneededSpaces)
{
if (StrTrim(pszStr, _T(" "))) bReturn = TRUE;
if (ReplaceSubStringWithStringBuf(pszStr, 0, _T(" "), _T(" "), TRUE, TRUE)) bReturn = TRUE;
}
return bReturn;
}
BOOL GetRandomString(WCHAR* pszStr, int cchSize)
{
static BOOL bRandInited = FALSE;
const WCHAR* pszChars = TranslateT("abcdefghkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ23456789"); // misunderstandable chars were removed (l=I=1, O=0=o, i=j)
int i;
if (!pszChars || !pszStr || (cchSize < 1)) return FALSE;
// Initialize random seed
if (!bRandInited)
{
srand((unsigned int)(GetTickCount()+time(NULL))); // very random
bRandInited = TRUE;
}
for (i=0; i<(cchSize-1); i++)
pszStr[i] = pszChars[(rand()*lstrlen(pszChars))/RAND_MAX]; // % operator is not a good choice
pszStr[cchSize-1] = _T('\0');
return TRUE;
}
|