blob: f25f33b70c34d1aadf87a2775caacf4d40c76f6e (
plain)
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
|
#include "rapidxml.hpp"
#include <cstring>
inline bool is_content_root(const char* name, std::size_t len) {
return (len == 8 && std::memcmp(name, "abstract", 8) == 0) ||
(len == 4 && std::memcmp(name, "body", 4) == 0);
}
size_t sum_text(const rapidxml::xml_node<>* node) {
size_t total = 0;
for (auto* child = node->first_node(); child; child = child->next_sibling()) {
if (child->type() == rapidxml::node_type::node_data) {
total += child->value_size();
} else if (child->type() == rapidxml::node_type::node_element) {
total += sum_text(child);
}
}
return total;
}
size_t walk(const rapidxml::xml_node<>* node) {
size_t total = 0;
for (auto* child = node->first_node(); child; child = child->next_sibling()) {
if (child->type() != rapidxml::node_type::node_element) continue;
if (is_content_root(child->name(), child->name_size())) {
total += sum_text(child);
} else {
total += walk(child);
}
}
return total;
}
size_t pubmed_len(char* buffer, size_t /*len*/) {
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_no_entity_translation |
rapidxml::parse_no_string_terminators>(buffer);
return walk(&doc);
}
size_t pubmed_len_with_entities(char* buffer, size_t /*len*/) {
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_no_string_terminators>(buffer);
return walk(&doc);
}
|