TCHAR QuoteText TCHAR text int charsPerLine int removeExistingQuotes i

 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
TCHAR *QuoteText(TCHAR *text, int charsPerLine, int removeExistingQuotes)
{
int inChar, outChar, lineChar;
int justDoneLineBreak, bufSize;
TCHAR *strout;
#ifdef _UNICODE
bufSize = lstrlenW(text) + 23;
#else
bufSize = strlen(text) + 23;
#endif
strout = (TCHAR*)malloc(bufSize * sizeof(TCHAR));
inChar = 0;
justDoneLineBreak = 1;
for (outChar = 0, lineChar = 0;text[inChar];) {
if (outChar >= bufSize - 8) {
bufSize += 20;
strout = (TCHAR *)realloc(strout, bufSize * sizeof(TCHAR));
}
if (justDoneLineBreak && text[inChar] != '\r' && text[inChar] != '\n') {
if (removeExistingQuotes)
if (text[inChar] == '>') {
while (text[++inChar] != '\n');
inChar++;
continue;
}
strout[outChar++] = '>';
strout[outChar++] = ' ';
lineChar = 2;
}
if (lineChar == charsPerLine && text[inChar] != '\r' && text[inChar] != '\n') {
int decreasedBy;
for (decreasedBy = 0;lineChar > 10;lineChar--, inChar--, outChar--, decreasedBy++)
if (strout[outChar] == ' ' || strout[outChar] == '\t' || strout[outChar] == '-') break;
if (lineChar <= 10) {
lineChar += decreasedBy;
inChar += decreasedBy;
outChar += decreasedBy;
} else inChar++;
strout[outChar++] = '\r';
strout[outChar++] = '\n';
justDoneLineBreak = 1;
continue;
}
strout[outChar++] = text[inChar];
lineChar++;
if (text[inChar] == '\n' || text[inChar] == '\r') {
if (text[inChar] == '\r' && text[inChar+1] != '\n')
strout[outChar++] = '\n';
justDoneLineBreak = 1;
lineChar = 0;
} else justDoneLineBreak = 0;
inChar++;
}
strout[outChar++] = '\r';
strout[outChar++] = '\n';
strout[outChar] = 0;
return strout;
}