-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeader.cc
91 lines (71 loc) · 2.42 KB
/
Leader.cc
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
#include "Leader.h"
#include <cstdio>
#include "StringUtil.h"
const size_t Leader::LEADER_LENGTH(24);
bool Leader::ParseLeader(const std::string &leader_string, Leader ** const leader, std::string * const err_msg) {
if (err_msg != NULL)
err_msg->clear();
if (leader == NULL) {
if (err_msg != NULL)
*err_msg = "\"leader\" argument to Leader::ParseLeader must point to something!";
return false;
}
if (leader_string.size() != LEADER_LENGTH) {
if (err_msg != NULL)
*err_msg = "Leader length must be " + std::to_string(LEADER_LENGTH) +
", found " + std::to_string(leader_string.size()) + "!";
return false;
}
unsigned record_length;
if (std::sscanf(leader_string.substr(0, 5).data(), "%5u", &record_length) != 1) {
if (err_msg != NULL)
*err_msg = "Can't parse record length!";
return false;
}
unsigned base_address_of_data;
if (std::sscanf(leader_string.substr(12, 5).data(), "%5u", &base_address_of_data) != 1) {
if (err_msg != NULL)
*err_msg = "Can't parse base address of data!";
return false;
}
//
// Validity checks:
//
// Check indicator count:
if (leader_string[10] != '2') {
if (err_msg != NULL)
*err_msg = "Invalid indicator count!";
return false;
}
// Check subfield code length:
if (leader_string[11] != '2') {
if (err_msg != NULL)
*err_msg = "Invalid subfield code length!";
return false;
}
// Check entry map:
if (leader_string.substr(20, 4) != "4500") {
if (err_msg != NULL)
*err_msg = "Invalid entry map!";
return false;
}
*leader = new Leader(leader_string, record_length, base_address_of_data);
return true;
}
bool Leader::setRecordLength(const unsigned new_record_length, std::string * const err_msg) {
if (err_msg != NULL)
err_msg->clear();
if (new_record_length > 99999) {
*err_msg = "new record length (" + std::to_string(new_record_length)
+ ") exceeds valid maximum (99999)!";
return false;
}
record_length_ = new_record_length;
raw_leader_ = StringUtil::PadLeading(std::to_string(record_length_), 5, '0') + raw_leader_.substr(5);
return true;
}
void Leader::setBaseAddressOfData(const unsigned new_base_address_of_data) {
base_address_of_data_ = new_base_address_of_data;
raw_leader_ = raw_leader_.substr(0, 12) + StringUtil::PadLeading(std::to_string(base_address_of_data_), 5, '0')
+ raw_leader_.substr(17);
}