-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurse.cpp
More file actions
191 lines (155 loc) · 4.67 KB
/
Curse.cpp
File metadata and controls
191 lines (155 loc) · 4.67 KB
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
// Curse.cpp: implementation of the CCurse class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Curse.h"
#include <string.h>
#include <stdio.h>
#define MAX_CURSE_STRING 7000
#define HUGE_NUMBER MAX_CURSE_STRING
static char curse_string[MAX_CURSE_STRING];
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCurse::CCurse()
{
}
CCurse::~CCurse()
{
}
void CCurse::LoadCurse(const char *filename)
{
static char comment_chars[] = { "$;\n" };
char buf[128];
char *ptr = curse_string;
FILE *file;
if (file = fopen(filename, "r"), !file) return;
while (fgets(buf, sizeof(buf), file)) {
if (strchr(comment_chars, buf[0])) continue;
ptr += sprintf(ptr, buf);
if (ptr - curse_string >= MAX_CURSE_STRING) {
curse_string[MAX_CURSE_STRING - 1] = 0;
break;
}
}
fclose(file);
return;
}
void CCurse::filterChar(const char *src, char *dst, const char *chars)
{
char *srcp = (char *) src;
char *dstp = dst;
while (*srcp) {
if (strchr(chars, *srcp) == NULL) {
*dstp = *srcp; dstp += 1;
} else {
int msb, lsb;
msb = *(unsigned char *)(srcp-1);
lsb = *(unsigned char *)srcp;
if (srcp > src && (
(msb == 0x82 && lsb > 0x9e && lsb < 0xf2) // hirakana
|| (msb == 0x83 && lsb > 0x3f && lsb < 0x97) // katakana
|| (msb >= 0x88 && msb <= 0x9f && msb != 0x7f // kanji 1 group
&& lsb > 0x3f && lsb < 0xfd)
|| (msb >= 0xe0 && msb <= 0xfc && msb != 0x7f // kanji 2 group
&& lsb > 0x3f && lsb < 0xfd))) {
*dstp = *srcp; dstp += 1;
}
}
srcp += 1;
}
*dstp = 0;
}
char* CCurse::getField(const char *buf, int delim, char *field)
{
unsigned char *src = (unsigned char *)buf;
while ((int)*src == delim && *src != 0) src++;
while ((int)*src != delim && *src != 0) *field ++ = *src ++;
*field = '\0';
return (char *)src;
}
bool CCurse::IsCurse(const char * str)
{
char string[HUGE_NUMBER], line[HUGE_NUMBER], word[HUGE_NUMBER];
char *ptr = curse_string;
const char *sub, *subs, *subw;
if (!str) return false;
// filter characters to keep users from
// speaking curses mixed with white spaces
// or punctuaion mark
filterChar(str, string, "\t _-.:^");
// optimized for multibyte code set.
while (ptr = getField(ptr, '\n', line), line[0]) {
getField(line, '\t', word);
sub = string;
while (*sub && *(sub + 1)) {
subs = sub; // current comparison pointer in string
subw = word; // current comparison pointer in word
while (*subs && (*subs == *subw)) {
subs++; subw++;
if (*subw == 0) {
return true; // reach to null, curse
}
}
sub += *sub < 0 ? 2 : 1;
}
}
return false;
}
char* CCurse::ConvertString(char * str, int max_len)
{
char result[MAX_CURSE_STRING], string[MAX_CURSE_STRING];
char line[128], curse[128], replace[128];
char *ptr = curse_string, *lptr, *cptr;
if (!str) return NULL;
if (!max_len || max_len >= sizeof(result)) return str;
// skip converting, if no curse is found.
// in fact, 'isCurse' and 'convert' are redundant.
// but it can be ignored and more efficient
// because users speak a normal sentence
// much more than a curse
if (!IsCurse(str)) return str;
// filter characters to keep users from
// speaking curses mixed with white spaces
// or punctuaion mark
filterChar(str, string, "\t _-.:^");
while (ptr = getField(ptr, '\n', line), line[0])
{
lptr = line;
lptr = getField(lptr, '\t', curse); // get the first field
lptr = getField(lptr, '\t', replace); // get the second field
cptr = string;
strcpy(result, string);
while (cptr = strstr(string, curse), cptr)
{
// if there is no matching replacement of curse word
if (!replace[0])
{
str[0] = NULL;
return str;
}
if ( (max_len-1) < (int)( (cptr - string) + strlen(replace) + strlen(cptr + strlen(curse) ) ) )
{
// string too long, remove the curses that linger around
strncpy(result, string, cptr - string);
strcpy(result + (cptr - string), cptr + strlen(curse));
}
else
{
strncpy(result, string, cptr - string);
strcpy(result + (cptr - string), replace);
strcpy(result + (cptr - string + strlen(replace)), cptr + strlen(curse));
}
// refresh intermediate result
strcpy(string, result);
}
}
result[max_len] = 0; // prevent overflow
strcpy(str, result);
return str;
}