-
Notifications
You must be signed in to change notification settings - Fork 2
/
module_helper.cpp
218 lines (170 loc) · 6.8 KB
/
module_helper.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
/* This file is part of node-sword-interface.
Copyright (C) 2019 - 2024 Tobias Klein <contact@tklein.info>
node-sword-interface is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
node-sword-interface 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with node-sword-interface. See the file COPYING.
If not, see <http://www.gnu.org/licenses/>. */
// Std includes
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
// Sword includes
#include <swmodule.h>
#include <versekey.h>
#include <localemgr.h>
#include "module_helper.hpp"
using namespace std;
using namespace sword;
bool ModuleHelper::moduleHasGlobalOption(SWModule* module, string globalOption)
{
return this->moduleHasKeyValuePair(module, "GlobalOptionFilter", globalOption);
}
bool ModuleHelper::moduleHasFeature(sword::SWModule* module, std::string feature)
{
return this->moduleHasKeyValuePair(module, "Feature", feature);
}
bool ModuleHelper::moduleHasStrongsKeys(sword::SWModule* module)
{
bool hasHebrewStrongsKeys = this->moduleHasFeature(module, "HebrewDef");
bool hasGreekStrongsKeys = this->moduleHasFeature(module, "GreekDef");
return hasHebrewStrongsKeys || hasGreekStrongsKeys;
}
bool ModuleHelper::moduleHasKeyValuePair(sword::SWModule* module, std::string key, std::string value)
{
bool hasKeyValuePair = false;
ConfigEntMap::const_iterator it = module->getConfig().lower_bound(key.c_str());
ConfigEntMap::const_iterator end = module->getConfig().upper_bound(key.c_str());
for(; it !=end; ++it) {
string currentValue = string(it->second.c_str());
if (currentValue.find(value) != string::npos) {
hasKeyValuePair = true;
break;
}
}
return hasKeyValuePair;
}
bool ModuleHelper::moduleHasBook(sword::SWModule* module, std::string bookCode)
{
bool hasBook = true;
stringstream key;
key << bookCode;
key << " 1:1";
module->setKey(key.str().c_str());
string moduleKeyText = string(module->getKey()->getShortText());
/* In case of apocryphal books the hasEntry method below is not enough.
Once we have set the key we need to compare the actual module key with the key we wanted to set.
For apocryphal books we may get "Rev 1:1" as actual module key and this will mismatch with the set key
and then indicate that the book is not existing. */
if (moduleKeyText != key.str()) {
hasBook = false;
} else {
hasBook = module->hasEntry(module->getKey());
}
return hasBook;
}
vector<string> ModuleHelper::getBookList(string moduleName)
{
string currentBookName = "";
vector<string> bookList;
SWModule* module = this->_moduleStore.getLocalModule(moduleName);
if (module == 0) {
cerr << "getLocalModule returned zero pointer for " << moduleName << endl;
} else {
VerseKey *vk = (VerseKey *)module->getKey();
for ((*vk) = TOP; !vk->popError(); vk->setBook(vk->getBook()+1)) {
if (module->hasEntry(vk)) {
currentBookName = vk->getOSISBookName();
bookList.push_back(currentBookName);
}
}
}
return bookList;
}
int ModuleHelper::getBookChapterCount(std::string moduleName, std::string bookCode)
{
SWModule* module = this->_moduleStore.getLocalModule(moduleName);
int bookChapterCount = -1;
if (module == 0) {
cerr << "getLocalModule returned zero pointer for " << moduleName << endl;
} else {
stringstream key;
key << bookCode;
key << " 1:1";
module->setKey(key.str().c_str());
VerseKey currentVerseKey(module->getKey());
bookChapterCount = currentVerseKey.getChapterMax();
}
return bookChapterCount;
}
int ModuleHelper::getChapterVerseCount(std::string moduleName, std::string bookCode, int chapter)
{
SWModule* module = this->_moduleStore.getLocalModule(moduleName);
int chapterVerseCount = -1;
if (module == 0) {
cerr << "getLocalModule returned zero pointer for " << moduleName << endl;
} else {
stringstream key;
key << bookCode << " " << chapter << ":1";
module->setKey(key.str().c_str());
VerseKey currentVerseKey(module->getKey());
chapterVerseCount = currentVerseKey.getVerseMax();
}
return chapterVerseCount;
}
map<string, int> ModuleHelper::getAbsoluteVerseNumberMap(SWModule* module, vector<string> bookList)
{
string lastKey = "";
int currentAbsoluteVerseNumber = 1;
string lastBookName = "";
string currentBookName = "";
std::map<std::string, int> absoluteVerseNumbers;
if (bookList.size() == 0) {
bookList = this->getBookList(string(module->getName()));
}
for (unsigned int i = 0; i < bookList.size(); i++) {
string currentBook = bookList[i];
stringstream initialKey;
initialKey << currentBook << " " << "1:1";
currentAbsoluteVerseNumber = 1;
module->setKey(initialKey.str().c_str());
VerseKey currentVerseKey(module->getKey());
currentBookName = currentVerseKey.getBookAbbrev();
lastBookName = currentBookName;
for (;;) {
currentVerseKey = module->getKey();
currentBookName = currentVerseKey.getBookAbbrev();
string currentKey(currentVerseKey.getShortText());
// Stop, once the newly read key is the same as the previously read key
if (currentKey == lastKey) { break; }
// Stop once we finished the book
if ((currentAbsoluteVerseNumber > 0) && (currentBookName != lastBookName)) { break; }
absoluteVerseNumbers[currentKey] = currentAbsoluteVerseNumber;
module->increment();
currentAbsoluteVerseNumber++;
lastKey = currentKey;
lastBookName = currentBookName;
}
}
return absoluteVerseNumbers;
}
bool ModuleHelper::isBrokenMarkupModule(std::string moduleName)
{
return std::find(this->_brokenMarkupModules.begin(),
this->_brokenMarkupModules.end(),
moduleName) != this->_brokenMarkupModules.end();
}
bool ModuleHelper::isInconsistentClosingEndDivModule(std::string moduleName)
{
return std::find(this->_inconsistentClosingEndDivModules.begin(),
this->_inconsistentClosingEndDivModules.end(),
moduleName) != this->_inconsistentClosingEndDivModules.end();
}