/
muaimf.cpp
440 lines (359 loc) · 7.01 KB
/
muaimf.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
Copyright (C) 2016-2017 Claude SIMON (http://zeusw.org/epeios/contact.html).
This file is part of 'MMUAq' software.
'MMUAq' is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
'MMUAq' 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with 'MMUAq'. If not, see <http://www.gnu.org/licenses/>.
*/
#include "muaimf.h"
#include "stsfsm.h"
using namespace muaimf;
#define C( name ) case f##name : return #name; break
const char *muaimf::GetLabel( eField Field )
{
switch ( Field ) {
C( To );
C( From );
C( Subject );
C( MessageId );
C( Date );
default:
qRFwk();
break;
}
return NULL; // To avoid a warning.
}
#undef C
namespace {
stsfsm::wAutomat FieldLabelAutomat_;
void FillFieldLabelAutomat_( void )
{
FieldLabelAutomat_.Init();
stsfsm::Fill<eField>( FieldLabelAutomat_, f_amount, GetLabel );
}
}
eField muaimf::GetField( const str::dString &Label )
{
return stsfsm::GetId( Label, FieldLabelAutomat_, f_Undefined, f_amount, qRPU );
}
namespace {
typedef flw::sByte sByte_;
}
namespace {
inline void ConvertToWording_(
flw::rRFlow &IFlow,
flw::rWFlow &OFlow)
{
sByte_ Byte;
bso::sBool First = true;
while ( !IFlow.EndOfFlow() ) {
Byte = IFlow.Get();
if ( isupper( Byte ) )
if ( !First )
OFlow.Put( '-' );
First = false;
OFlow.Put( Byte );
}
}
inline void ConvertToWording_(
flw::rRFlow &IFlow,
str::dString &Wording )
{
qRH
flx::rStringOFlow OFlow;
qRB
OFlow.Init( Wording );
ConvertToWording_( IFlow, OFlow );
qRR
qRT
qRE
}
inline const char *ConvertToWording_( const char *Label )
{
static qCBUFFERh Buffer;
qRH
flx::sStringIFlow Flow;
str::wString
LabelAsString,
Wording;
qRB
LabelAsString.Init( Label );
Flow.Init( LabelAsString );
Wording.Init();
ConvertToWording_( Flow, Wording );
Wording.Convert( Buffer );
qRR
qRT
qRE
return Buffer;
}
}
const char *muaimf::GetWording( eField Field )
{
return ConvertToWording_( GetLabel( Field ) );
}
namespace {
stsfsm::wAutomat FieldWordingAutomat_;
// Not thhread-safe.
const char *GetUppercaseWording_( eField Field )
{
static qCBUFFERh Buffer;
qRH
str::wString Wording;
qRB
Wording.Init( GetWording( Field ) );
str::ToUpper( Wording );
Wording.Convert( Buffer );
qRR
qRT
qRE
return Buffer;
}
void FillFieldWordingAutomat_( void )
{
FieldWordingAutomat_.Init();
stsfsm::Fill<eField>( FieldWordingAutomat_, f_amount, GetUppercaseWording_ );
}
}
namespace {
eField Get_( const str::dString &Name )
{
eField Field = f_Undefined;
qRH
str::wString Upper;
qRB
Upper.Init( Name );
str::ToUpper( Upper );
Field = stsfsm::GetId( Upper, FieldWordingAutomat_, f_Undefined, f_amount, qRPU );
if ( Field == f_Undefined )
Field = f_Unknown;
qRR
qRT
qRE
return Field;
}
}
sFRow muaimf::dFields::Append(
const str::dString &Name,
const str::dString &Body )
{
sField_ Field;
Field.Init();
Field.Field = Get_( Name );
Field.Name = Append_( Name );
Field.Body = Append_( Body );
return Fields.Add( Field );
}
void muaimf::dFields::Search(
eField Field,
dFRows &Rows ) const
{
sFRow Row = First();
while ( Row != qNIL ) {
if ( Fields( Row ).Field == Field )
Rows.Append( Row );
Row = Next( Row );
}
}
sFRow muaimf::dFields::Search( eField Field ) const
{
sFRow Row = qNIL;
qRH
wFRows Rows;
qRB
Rows.Init();
Search( Field, Rows );
if ( Rows.Amount() != 0 )
Row = Rows( Rows.First() );
qRR
qRT
qRE
return Row;
}
namespace {
qCDEF( char, CR_, '\r' ) ;
qCDEF( char, LF_, '\n' ) ;
typedef flw::sByte sByte_;
inline bso::sBool IsWSP_( sByte_ Byte )
{
return ( Byte == ' ' ) || ( Byte == '\t' );
}
// If returns 'false', there is no more header.
bso::sBool GetField_(
flw::rRFlow &Flow,
str::dString &Name,
str::dString &Body )
{
bso::sBool Continue = true;
bso::sBool BodyPart = false;
bso::sBool TestFolding = false;
sByte_ Byte;
if ( Flow.EndOfFlow() )
return false;
else if ( Flow.View() == CR_ ) {
Flow.Skip();
if ( !Flow.EndOfFlow() )
if ( Flow.View() == LF_ )
Flow.Skip();
return false;
} else if ( Flow.View() == LF_ ) {
Flow.Skip();
return false;
}
while ( Continue ) {
Byte = Flow.Get();
if ( Byte == CR_ ) {
if ( Flow.EndOfFlow() )
Continue = false;
else
if ( Flow.View() == LF_ ) {
Flow.Skip();
TestFolding = true;
}
} else if ( Byte == LF_ ) {
TestFolding = true;
} else if ( Byte == ':' ) {
if ( BodyPart )
Body.Append( Byte );
else {
BodyPart = true;
switch ( Flow.Get() ) {
case ' ':
break;// Skipping the heading space.
case CR_:
if ( Flow.Get() != LF_ )
qRGnr();
if ( !IsWSP_(Flow.View()) ) {
BodyPart = false;
Continue = false;
}
break;
default:
qRGnr();
break;
}
}
} else {
if ( BodyPart )
Body.Append( Byte );
else
Name.Append( Byte );
}
if ( TestFolding ) {
if ( Flow.EndOfFlow() )
Continue = false;
else if ( IsWSP_(Flow.View() ) )
TestFolding = false;
else
Continue = false;
}
}
return true;
}
}
const str::dString &muaimf::GetField(
eField Field,
fdr::rRDriver &Driver,
str::dString &Body )
{
qRH
wFields Fields;
muaimf::sFRow Row = qNIL;
qRB
Fields.Init();
muaimf::Fill( Driver, Fields );
Row = Fields.Search( muaimf::fSubject );
if ( Row == qNIL )
qRGnr();
Fields.GetBody( Row, Body );
qRR
qRT
qRE
return Body;
}
void muaimf::Fill(
flw::rRFlow &Flow,
dFields &Fields )
{
qRH
str::wString Name, Body;
qRB
Name.Init();
Body.Init();
while ( GetField_( Flow, Name, Body ) ) {
Fields.Append( Name, Body );
Name.Init();
Body.Init();
}
qRR
qRT
qRE
}
void muaimf::Fill(
fdr::rRDriver &Driver,
dFields &Fields )
{
qRH
flw::rDressedRFlow<> Flow;
qRB
Flow.Init( Driver );
Fill( Flow, Fields );
qRR
qRT
qRE
}
void muaimf::Dump(
const dFields &Fields,
txf::rWFlow Flow )
{
qRH
sFRow Row = qNIL;
str::wString Entry;
qRB
Row = Fields.First();
while ( Row != qNIL ) {
if ( Fields.GetField( Row ) == f_Unknown ) {
Entry.Init();
Flow << Fields.GetName( Row, Entry );
} else
Flow << "- " << GetLabel( Fields.GetField( Row ) );
Flow << ": ";
Entry.Init();
Flow << Fields.GetBody( Row, Entry ) << txf::nl;
Row = Fields.Next( Row );
}
qRR
qRT
qRE
}
void muaimf::Dump(
const dFields &Fields,
fdr::rWDriver &Driver )
{
qRH
txf::rWFlow Flow;
qRB
Flow.Init( Driver );
Dump( Fields, Flow );
qRR
qRT
qRE
}
namespace {
void FillAutomats_( void )
{
FillFieldLabelAutomat_();
FillFieldWordingAutomat_();
}
}
qGCTOR( muaimf )
{
FillAutomats_();
}