-
Notifications
You must be signed in to change notification settings - Fork 4
/
TUN_EmbedHTML.cpp
executable file
·380 lines (289 loc) · 10.1 KB
/
TUN_EmbedHTML.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
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
// TUN_EmbedHTML.cpp: Implementation of class/functions for EmbedHTML TUN
//
// (C)opyright in 2009 by Mark Henning, Germany
//
// Contact: See contact form at www.mark-henning.de
//
// You may use this code for free. If you find an error or make some
// interesting changes, please let me know.
//
//////////////////////////////////////////////////////////////////////
#include <sstream>
#include "TUN_EmbedHTML.h"
namespace TUN
{
//////////////////////////////////////////////////////////////////////
// class CTokenPos
//////////////////////////////////////////////////////////////////////
CTokenPos::CTokenPos(CSingleScale::eSection section,
CSingleScale::eKey key) :
m_section(section),
m_key(key),
m_pos(0)
{
}
std::string CTokenPos::GetToken() const
{
return "@@@"
+ CSingleScale::GetSections().at(m_section)
+ ":"
+ CSingleScale::GetKeys().at(m_key)
+ "@@@";
}
bool CTokenPos::FindNext(const std::string & str, std::string::size_type pos)
{
m_pos = str.find(GetToken(), pos);
return ( m_pos != std::string::npos );
}
bool CTokenPos::operator< (const CTokenPos & rhs) const
{
return m_pos > rhs.m_pos;
}
void CTokenPos::ReplaceToken(std::string & str,
CSingleScale & ss,
long lVersionFrom /* = 0 */,
long lVersionTo /* = 200 */,
std::string strListItemSeparator /* = std::string("<br />") */) const
{
// Erase the token
str.erase(m_pos, GetToken().size());
// Retrieve the token value
std::string strValue;
switch ( m_section )
{
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
case CSingleScale::SEC_ScaleBegin:
switch ( m_key )
{
case CSingleScale::KEY_Format:
strValue = ss.Format();
break;
case CSingleScale::KEY_FormatVersion:
strValue = ss.FormatVersionAsStr();
break;
case CSingleScale::KEY_FormatSpecs:
strValue = ss.FormatSpecs();
break;
default:
assert(false); // key not implemented
} // switch ( m_key )
break;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
case CSingleScale::SEC_Info:
switch ( m_key )
{
case CSingleScale::KEY_Name:
strValue = HTMLEntities(ss.m_strName);
break;
case CSingleScale::KEY_ID:
strValue = HTMLEntities(ss.m_strID);
break;
case CSingleScale::KEY_Filename:
strValue = HTMLEntities(ss.m_strFilename);
break;
case CSingleScale::KEY_Author:
strValue = HTMLEntities(ss.m_strAuthor);
break;
case CSingleScale::KEY_Location:
strValue = HTMLEntities(ss.m_strLocation);
break;
case CSingleScale::KEY_Contact:
strValue = HTMLEntities(ss.m_strContact);
break;
case CSingleScale::KEY_Date:
strValue = HTMLEntities(ss.GetDate());
break;
case CSingleScale::KEY_Editor:
strValue = HTMLEntities(ss.m_strEditor);
break;
case CSingleScale::KEY_EditorSpecs:
strValue = HTMLEntities(ss.m_strEditorSpecs);
break;
case CSingleScale::KEY_Description:
strValue = HTMLEntities(ss.m_strDescription);
break;
case CSingleScale::KEY_Keyword:
strValue = StringList2String(ss.m_lstrKeywords, strListItemSeparator);
break;
case CSingleScale::KEY_History:
strValue = HTMLEntities(ss.m_strHistory);
break;
case CSingleScale::KEY_Geography:
strValue = HTMLEntities(ss.m_strGeography);
break;
case CSingleScale::KEY_Instrument:
strValue = HTMLEntities(ss.m_strInstrument);
break;
case CSingleScale::KEY_Composition:
strValue = StringList2String(ss.m_lstrCompositions, strListItemSeparator);
break;
case CSingleScale::KEY_Comments:
strValue = HTMLEntities(ss.m_strComments);
break;
default:
assert(false); // key not implemented
} // switch ( m_key )
break;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
case CSingleScale::SEC_DataSet:
switch ( m_key )
{
case CSingleScale::KEY_AllData:
{
std::stringstream strstr;
ss.Write(strstr, lVersionFrom, lVersionTo);
strValue = strstr.str() + "__FakeKey = ";
}
break;
default:
assert(false); // key not implemented
} // switch ( m_key )
break;
default:
assert(false); // section not implemented
} // switch ( m_section )
// Insert the token value
str.insert(m_pos, strValue);
}
//////////////////////////////////////////////////////////////////////
// Tool functions
//////////////////////////////////////////////////////////////////////
void BuildListOfSupportedTokens(std::list<CTokenPos> & ltpTokens)
{
ltpTokens.clear();
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_ScaleBegin, CSingleScale::KEY_Format));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_ScaleBegin, CSingleScale::KEY_FormatVersion));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_ScaleBegin, CSingleScale::KEY_FormatSpecs));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Name));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_ID));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Filename));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Author));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Location));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Contact));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Date));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Editor));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_EditorSpecs));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Description));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Keyword));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_History));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Geography));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Instrument));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Composition));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_Info, CSingleScale::KEY_Comments));
ltpTokens.push_back(CTokenPos(CSingleScale::SEC_DataSet, CSingleScale::KEY_AllData));
}
void WriteListOfSupportedTokens(std::string strFile)
{
std::list<CTokenPos> ltpTokens;
BuildListOfSupportedTokens(ltpTokens);
std::ofstream ofs(strFile.c_str(), std::ios_base::out | std::ios_base::trunc);
std::list<CTokenPos>::iterator itTokens;
for ( itTokens = ltpTokens.begin() ; itTokens != ltpTokens.end() ; ++itTokens )
ofs << itTokens->GetToken().c_str() << std::endl;
ofs.close();
}
void BuildListOfFoundTokens(const std::string & str,
std::list<CTokenPos> & ltpTokens,
std::list<CTokenPos> & ltpPositions)
{
std::list<CTokenPos>::iterator itTokens;
for ( itTokens = ltpTokens.begin() ; itTokens != ltpTokens.end() ; ++itTokens )
{
itTokens->m_pos = -1;
while ( itTokens->FindNext(str, ++itTokens->m_pos) )
ltpPositions.push_back(*itTokens);
}
ltpPositions.sort();
}
void ReplaceListOfFoundTokens(std::string & str,
CSingleScale & ss,
const std::list<CTokenPos> & ltpPositions,
long lVersionFrom /* = 0 */,
long lVersionTo /* = 200 */)
{
std::list<CTokenPos>::const_iterator itPos;
for ( itPos = ltpPositions.begin() ; itPos != ltpPositions.end() ; ++itPos )
itPos->ReplaceToken(str, ss, lVersionFrom, lVersionTo);
}
std::string StringList2String(const std::list<std::string> & lstr,
std::string strListItemSeparator)
{
std::string strResult;
std::list<std::string>::const_iterator it;
for ( it = lstr.begin() ; it != lstr.end() ; ++it )
{
if ( !strResult.empty() )
strResult += strListItemSeparator;
strResult += HTMLEntities(*it);
}
return strResult;
}
std::string HTMLEntities(const std::string & str)
{
std::string strResult;
strResult.reserve(str.size());
for ( long l = 0 ; l < str.size() ; ++l )
switch ( str.at(l) )
{
case '\0': strResult += "<br />"; strResult += '\0'; break;
case '\n': strResult += "\n<br />"; break;
case '\"': strResult += """; break;
case '&': strResult += "&"; break;
case '<': strResult += "<"; break;
case '>': strResult += ">"; break;
default: strResult += str.at(l);
}
return strResult;
}
//////////////////////////////////////////////////////////////////////
// class CEmbedHTML
//////////////////////////////////////////////////////////////////////
CEmbedHTML::CEmbedHTML(std::string strTemplate, bool bTemplateIsFile /* = true */)
{
// Retrieves Template content
if ( bTemplateIsFile )
{
std::ifstream ifstr(strTemplate.c_str(), std::ios_base::in | std::ios_base::binary);
std::stringstream strstr;
strstr << ifstr.rdbuf();
m_strTemplate = strstr.str(); // strstrstrstrstrstr... :-)
ifstr.close();
}
else
m_strTemplate = strTemplate;
// Build the list of tokens
BuildListOfSupportedTokens(m_ltpTokens);
// Search for the tokens in the template
BuildListOfFoundTokens(m_strTemplate, m_ltpTokens, m_ltpPositions);
}
CEmbedHTML::~CEmbedHTML()
{
}
bool CEmbedHTML::WriteFile(std::string strScaleFile, std::string strEmbeddedFile,
long lVersionFrom /* = 0 */, long lVersionTo /* = 200 */)
{
// Load file
CSingleScale ss;
if ( !ss.Read(strScaleFile.c_str()) )
{
m_err = ss.Err();
return false;
}
return WriteFile(ss, strEmbeddedFile, lVersionFrom, lVersionTo);
}
bool CEmbedHTML::WriteFile(CSingleScale & ssScale, std::string strEmbeddedFile,
long lVersionFrom /* = 0 */, long lVersionTo /* = 200 */)
{
// Embed data
std::string strEmbeddedData = m_strTemplate;
ReplaceListOfFoundTokens(strEmbeddedData, ssScale, m_ltpPositions,
lVersionFrom, lVersionTo);
// Write result
std::ofstream ofs(strEmbeddedFile.c_str(), std::ios_base::out | std::ios_base::trunc);
ofs << strEmbeddedData;
if ( !ofs )
return m_err.SetError("Error writing the file.");
ofs.close();
return m_err.SetOK();
}
} // namespace TUN