-
Notifications
You must be signed in to change notification settings - Fork 2
/
field.cpp
555 lines (464 loc) · 11.5 KB
/
field.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#include <cstdio>
#include <cstdlib> //for malloc(). TODO: Remove malloc()
#include <ctime>
#include <vector>
#include "parser.h"
using namespace std;
field::field(const string &str)
{
fill_default();
tag=0;
frm=new fldformat();
deletefrm=true;
firstfrm=frm;
data=str;
}
field::field(const fldformat *format, const string &str)
{
fill_default();
tag=0;
frm=format;
firstfrm=frm;
data=str;
}
field::field(const std::string &filename, const string &str)
{
fill_default();
tag=0;
frm=new fldformat(filename);
deletefrm=true;
firstfrm=frm;
data=str;
}
//copy constructor
field::field(const field &from):
data(from.data),
start(from.start),
blength(from.blength),
flength(from.flength),
frm(from.frm),
firstfrm(from.firstfrm),
deletefrm(from.deletefrm),
subfields(from.subfields),
altformat(from.altformat),
tagmap(from.tagmap),
tag(from.tag)
{
if(deletefrm)
{
firstfrm=new fldformat(*from.firstfrm);
frm=firstfrm;
for(const fldformat *tmpfrm=from.firstfrm; tmpfrm!=NULL; tmpfrm=tmpfrm->get_altformat(), frm=frm->get_altformat())
if(tmpfrm==from.frm)
break;
}
}
field::~field(void)
{
clear();
if(deletefrm)
delete frm;
}
void field::fill_default(void)
{
data.clear();
start=0;
blength=0;
flength=0;
frm=NULL;
subfields.clear();
altformat=0;
deletefrm=false;
firstfrm=frm;
tagmap.clear();
}
void field::clear(void)
{
const fldformat *tmpfrm=frm, *tmpfirstfrm=firstfrm;
unsigned int tmpaltformat=altformat;
bool delfrm=deletefrm;
fill_default();
deletefrm=delfrm;
firstfrm=tmpfirstfrm; //make formats immune to clear()
frm=tmpfrm;
altformat=tmpaltformat;
}
//forks the field. All data and subfields are also copied so that all pointers except frm will have new values to newly copied data but non-pointers will have same values
//If field is not empty, the format is retained
field& field::operator= (const field &from)
{
const fldformat *tmpfrm=frm, *tmpfirstfrm=firstfrm;
bool keepfrm=false;
if(this==&from)
return *this;
if(!empty())
keepfrm=true;
clear();
data=from.data;
start=from.start;
blength=from.blength;
flength=from.flength;
deletefrm=from.deletefrm;
frm=from.frm;
if(deletefrm)
{
firstfrm=new fldformat(*from.firstfrm);
frm=firstfrm;
for(const fldformat *tmpfrm=from.firstfrm; tmpfrm!=NULL; tmpfrm=tmpfrm->get_altformat(), frm=frm->get_altformat())
if(tmpfrm==from.frm)
break;
}
else
firstfrm=from.firstfrm;
subfields=from.subfields;
altformat=from.altformat;
if(keepfrm)
set_frm(tmpfirstfrm, tmpfrm);
tagmap=from.tagmap;
return *this;
}
//relink data from another field. The old field will become empty
void field::moveFrom(field &from)
{
if(this==&from)
return;
*this=from;
from.clear();
}
void field::swap(field &from)
{
if(this==&from)
return;
field tmpfld;
tmpfld.moveFrom(from);
from.moveFrom(*this);
moveFrom(tmpfld);
}
int field::compare (const field& other) const
{
if(empty() && other.empty())
return 0;
switch(frm->dataFormat)
{
case fldformat::fld_subfields:
case fldformat::fld_bcdsf:
case fldformat::fld_tlv:
break;
default:
return data.compare(other.data);
}
for(const_iterator i=begin(), j=other.begin(); i!=end() && j!=other.end(); ++i, ++j)
{
while((!i->second.frm || i->second.frm->dataFormat==fldformat::fld_isobitmap || i->second.frm->dataFormat==fldformat::fld_bitmap) && i!=end()) //skip subfields without format and bitmaps
++i;
while((!j->second.frm || j->second.frm->dataFormat==fldformat::fld_isobitmap || j->second.frm->dataFormat==fldformat::fld_bitmap) && j!=other.end())
++j;
if(i==end() && j==other.end())
break;
if(i==end())
return 1;
if(j==other.end())
return -1;
if(i->first<j->first)
return 1;
if(j->first<i->first)
return -1;
int r=i->second.compare(j->second);
if(r)
return r;
}
return 0;
}
void field::print_message(string numprefix) const
{
if(!numprefix.empty())
printf("%s ", numprefix.c_str());
printf("%s", frm->get_description().c_str());
if(!data.empty())
printf(" (%lu): \"%s\"\n", (unsigned long)flength, data.c_str());
else
printf(":\n");
switch(frm->dataFormat)
{
case fldformat::fld_subfields:
case fldformat::fld_bcdsf:
case fldformat::fld_tlv:
for(auto& i : *this)
if(!i.second.empty())
i.second.print_message(numprefix + to_string(i.first) + ".");
break;
default:
break;
}
}
bool field::empty(void) const
{
if(frm->empty())
return true;
switch(frm->dataFormat)
{
case fldformat::fld_subfields:
case fldformat::fld_bcdsf:
case fldformat::fld_tlv:
break;
default:
return data.empty();
}
if(subfields.empty())
return true;
for(auto& i : *this)
if(i.second.frm && i.second.frm->dataFormat!=fldformat::fld_isobitmap && i.second.frm->dataFormat!=fldformat::fld_bitmap && !i.second.empty())
return false;
return true;
}
// Switches to the next applicable altformat.
void field::switch_altformat(void)
{
const fldformat *frmtmp=frm;
for(unsigned int i=altformat+1; (frmtmp=frmtmp->get_altformat())!=NULL; i++)
{
try
{
change_format(frmtmp);
altformat=i;
return;
}
catch (const exception& e)
{
continue;
}
}
throw invalid_argument("No applicable altformats found after the current");
}
// Rewinds to the first applicable altformat.
void field::reset_altformat(void)
{
const fldformat *frmtmp=firstfrm;
for(unsigned int i=0; frmtmp!=NULL; frmtmp=frmtmp->get_altformat(), i++)
{
try
{
change_format(frmtmp);
altformat=i;
return;
}
catch (const exception& e)
{
continue;
}
}
throw invalid_argument("None formats are applicable");
}
// Assigns a new format to the field. Not to be used to switch to an altformat because it assumes the new format to be the root of altformat, so the information about the first altformat is lost and reset_altformat() would not reset to original altformat, use switch_altformat() instead.
// If frmaltnew is not null, it must be an altformat of frmnew
void field::set_frm(const fldformat *frmnew, const fldformat *frmaltnew)
{
const fldformat *frmtmp=frmnew;
for(unsigned int i=0; frmtmp!=NULL; frmtmp=frmtmp->get_altformat(), i++)
{
try
{
change_format(frmtmp);
altformat=i;
break;
}
catch (const exception& e)
{
continue;
}
}
if(!frmtmp)
throw invalid_argument("The new format is not applicable");
if(deletefrm)
{
delete firstfrm;
deletefrm=false;
}
firstfrm=frmnew;
if(frmaltnew)
{
frmtmp=frm;
for(unsigned int i=altformat; frmtmp!=NULL; frmtmp=frmtmp->get_altformat(), i++)
if(frmtmp==frmaltnew)
{
change_format(frmaltnew);
altformat=i;
return;
}
}
}
// Internal function to change current format, not to be called directly
// If the new format does not suit the already present field tree, the function will restore the original format. The function guarantees consistency of the resulting field format, however it is not guaranteed that all subfields will remain on the same altformats in case of failure.
void field::change_format(const fldformat *frmnew)
{
iterator i;
vector<const fldformat*> frmold(1, frm);
const bool sameformat(frm==frmnew);
if(!frmnew)
throw invalid_argument("No new format provided");
if(debug)
printf("trying to change sf %d format from %s to %s\n", tag, frm->get_description().c_str(), frmnew->get_description().c_str());
if(!data.empty() && !frmnew->data.empty() && data!=frmnew->data)
throw invalid_argument("data does not match mandatory data of the new format");
frm=frmnew;
try
{
for(i=begin(); i!=end(); ++i)
{
frmold.push_back(i->second.frm);
i->second.set_frm(&frmnew->sf(i->first));
}
}
catch (const exception& e)
{
if(debug)
printf("Error: Unable to change field format (%d). Reverting.\n", i->first);
try
{
frmold.pop_back();
if(i!=begin())
{
for(--i; i!=begin(); --i)
{
i->second.change_format(frmold.back());
frmold.pop_back();
}
i->second.change_format(frmold.back());
frmold.pop_back();
}
frm=frmold.back();
frmold.pop_back();
if(!frmold.empty())
throw runtime_error("field count mismatch");
}
catch (const exception& e1)
{
throw runtime_error("Unable to revert to the original format. The field is in undefined state.");
}
if(!sameformat)
throw invalid_argument("Fields don't fit in new format");
}
if(frm->hasBitmap!=-1 && !subfields.empty() && (--i)->first > frm->hasBitmap)
sf(frm->hasBitmap); //make sure to not skip bitmap field on serialize
}
//returns reference to subfield. If it does not exists, it will be added.
field& field::sf(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9)
{
if(n0 < 0)
throw out_of_range("Bad subfield number");
if(!sfexist(n0))
{
if(!frm->sfexist(n0))
throw out_of_range("No format for the subfield");
if(!subfields.count(n0))
{
if(frm->dataFormat!=fldformat::fld_tlv)
tagmap[n0]=n0;
else if(subfields.empty())
tagmap[0]=n0;
else
tagmap[tagmap.rbegin()->first+1]=n0;
}
if(subfields[n0].empty())
{
subfields[n0].set_frm(&frm->sf(n0));
subfields[n0].tag=n0;
}
if(frm->hasBitmap!=-1 && frm->dataFormat != fldformat::fld_tlv && n0 > frm->hasBitmap)
sf(frm->hasBitmap); //make sure to not skip bitmap field on serialize
}
if(n1<0)
return subfields[n0];
else
return subfields[n0].sf(n1, n2, n3, n4, n5, n6, n7, n8, n9);
}
bool field::sfexist(int n0, int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9) const
{
if(n0 < 0)
return false;
map<int,field>::const_iterator it = subfields.find(n0);
if(it == subfields.end())
return false;
if(n1<0)
return true;
else
return it->second.sfexist(n1, n2, n3, n4, n5, n6, n7, n8, n9);
}
int field::vsnprintf(size_t pos, size_t size, const char *format, va_list ap)
{
static char tmpstr[256];
char *strptr = size+1>sizeof(tmpstr)/sizeof(char) ? (char*)malloc((size+1)*sizeof(char)) : tmpstr; //TODO: replace malloc with new
int count=std::vsnprintf(strptr, size+1, format, ap);
if(count<0)
{
if(strptr!=tmpstr)
free(strptr);
return count;
}
data.replace(pos, string::npos, strptr, size<(size_t)count ? size : count);
if(strptr!=tmpstr)
free(strptr);
return count;
}
size_t field::strftime(size_t max, const char *format, const struct tm *tm)
{
static char tmpstr[256];
char *strptr = max+1>sizeof(tmpstr)/sizeof(char) ? (char*)malloc((max+1)*sizeof(char)) : tmpstr;
size_t count=std::strftime(strptr, max+1, format, tm);
if(count==0)
{
if(strptr!=tmpstr)
free(strptr);
return count;
}
data.assign(strptr, count);
if(strptr!=tmpstr)
free(strptr);
return count;
}
field::iterator field::begin(void)
{
return iterator(tagmap.begin(), subfields);
}
field::iterator field::end(void)
{
return iterator(tagmap.end(), subfields);
}
field::iterator field::find(int n)
{
map<int,field>::const_iterator i=subfields.find(n);
if(i==subfields.end())
return end();
return iterator(tagmap.find(i->second.tag), subfields);
}
field::const_iterator field::begin(void) const
{
return const_iterator(tagmap.begin(), subfields);
}
field::const_iterator field::end(void) const
{
return const_iterator(tagmap.end(), subfields);
}
field::const_iterator field::find(int n) const
{
map<int,field>::const_iterator i=subfields.find(n);
if(i==subfields.end())
return end();
return const_iterator(tagmap.find(i->second.tag), subfields);
}
field::reverse_iterator field::rbegin(void)
{
return reverse_iterator(end());
}
field::const_reverse_iterator field::rbegin(void) const
{
return const_reverse_iterator(end());
}
field::reverse_iterator field::rend(void)
{
return reverse_iterator(begin());
}
field::const_reverse_iterator field::rend(void) const
{
return const_reverse_iterator(begin());
}