-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompMessage.cpp
231 lines (213 loc) · 5.53 KB
/
CompMessage.cpp
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
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include "StdAfx.h"
#include "CompMessage.h"
extern CTString _strStatsDetails;
CCompMessage::CCompMessage(void)
{
Clear();
}
void CCompMessage::Clear(void)
{
UnprepareMessage();
cm_fnmFileName.Clear();
cm_pcmiOriginal = NULL;
cm_bRead = FALSE;
}
// constructs message with a filename
void CCompMessage::SetMessage(CCompMessageID *pcmi)
{
cm_fnmFileName = pcmi->cmi_fnmFileName;
cm_bRead = pcmi->cmi_bRead;
cm_pcmiOriginal = pcmi;
}
// load a message from file
void CCompMessage::Load_t(void)
{
// if already loaded
if (cm_bLoaded) {
// do nothing
return;
}
// open file
CTFileStream strm;
strm.Open_t(cm_fnmFileName);
// read subject line
strm.ExpectKeyword_t("SUBJECT\r\n");
strm.GetLine_t(cm_strSubject);
// rea image type
strm.ExpectKeyword_t("IMAGE\r\n");
CTString strImage;
strm.GetLine_t(strImage);
if (strImage=="none") {
cm_itImage = IT_NONE;
} else if (strImage=="statistics") {
cm_itImage = IT_STATISTICS;
} else if (strImage=="picture") {
cm_itImage = IT_PICTURE;
cm_fnmPicture.ReadFromText_t(strm);
} else if (strImage=="model") {
cm_itImage = IT_MODEL;
cm_strModel.ReadFromText_t(strm, "");
} else {
throw TRANS("Unknown image type!");
}
// read text until end of file
strm.ExpectKeyword_t("TEXT\r\n");
cm_strText.ReadUntilEOF_t(strm);
cm_ctFormattedWidth = 0;
cm_ctFormattedLines = 0;
cm_strFormattedText = "";
cm_bLoaded = TRUE;
}
// format message for given line width
void CCompMessage::Format(INDEX ctCharsPerLine)
{
// if already formatted in needed size
if (cm_ctFormattedWidth == ctCharsPerLine) {
// do nothing
return;
}
// remember width
cm_ctFormattedWidth = ctCharsPerLine;
// get text
const char *strText = cm_strText;
if (strncmp(strText, "$STAT", 5)==0) {
strText = _strStatsDetails;
cm_strFormattedText = strText;
cm_ctFormattedLines = 1;
for (INDEX i=0; i<cm_strFormattedText.Length(); i++) {
if (cm_strFormattedText[i]=='\n') {
cm_ctFormattedLines++;
}
}
return;
}
// allocate overestimated buffer
SLONG slMaxBuffer = strlen(strText)*2;
char *pchBuffer = (char *)AllocMemory(slMaxBuffer);
// start at the beginning of text and buffer
const char *pchSrc = strText;
char *pchDst = pchBuffer;
cm_ctFormattedLines = 1;
INDEX ctChars = 0;
// while not end of text
while(*pchSrc!=0) {
// copy one char
char chLast = *pchDst++ = *pchSrc++;
// if it was line break
if (chLast=='\n') {
// new line
ctChars=0;
cm_ctFormattedLines++;
continue;
}
ctChars++;
// if out of row
if (ctChars>ctCharsPerLine) {
// start backtracking
const char *pchSrcBck = pchSrc-1;
char *pchDstBck = pchDst-1;
// while not start of row and not space
while (pchSrcBck>pchSrc-ctChars && *pchSrcBck!=' ') {
// go one char back
pchSrcBck--;
pchDstBck--;
}
// if start of row hit (cannot word-wrap)
if (pchSrcBck<pchSrc-ctChars) {
// just go to next line
pchSrc--;
pchDst--;
*pchDst++='\n';
ctChars=0;
cm_ctFormattedLines++;
continue;
}
// if can word-wrap, insert break before the last word
pchSrc = pchSrcBck+1;
pchDst = pchDstBck;
*pchDst++='\n';
ctChars=0;
cm_ctFormattedLines++;
}
}
// add end marker
*pchDst=0;
cm_strFormattedText = pchBuffer;
FreeMemory(pchBuffer);
}
// prepare message for using (load, format, etc.)
void CCompMessage::PrepareMessage(INDEX ctCharsPerLine)
{
// if not loaded
if (!cm_bLoaded) {
// try to
try {
// load it
Load_t();
// if failed
} catch (char *strError) {
// report warning
CPrintF("Cannot load message'%s': %s\n", (const CTString &)cm_fnmFileName, strError);
// do nothing else
return;
}
}
// format it for new width
Format(ctCharsPerLine);
}
// free memory used by message, but keep message filename
void CCompMessage::UnprepareMessage(void)
{
// clear everything except filename
cm_bLoaded = FALSE;
cm_strSubject.Clear();
cm_strText.Clear();
cm_strModel.Clear();
cm_fnmPicture.Clear();
cm_itImage = IT_NONE;
cm_strFormattedText.Clear();
cm_ctFormattedWidth = 0;
cm_ctFormattedLines = 0;
}
// mark message as read
void CCompMessage::MarkRead(void)
{
cm_bRead = TRUE;
cm_pcmiOriginal->cmi_bRead = TRUE;
}
// get one formatted line
CTString CCompMessage::GetLine(INDEX iLine)
{
const char *strText = cm_strFormattedText;
// find first line
INDEX i = 0;
while (i<iLine) {
strText = strchr(strText, '\n');
if (strText==NULL) {
return "";
} else {
i++;
strText++;
}
}
// find end of line
CTString strLine = strText;
char *pchEndOfLine = (char*)strchr(strLine, '\n');
// if found
if (pchEndOfLine!=NULL) {
// cut there
*pchEndOfLine = 0;
}
return strLine;
}