From 51734065a38cf6db48fe9fb4b16461ce424cad90 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 21 Nov 2020 14:33:33 -0500 Subject: Updated API to be more explicit with references --- bible.cc | 2 +- libbible.cc | 132 +++++++++++++++++++++++++++++--------------------------- libbible.h | 20 ++++++--- mods.cc | 1 + testLibbible.cc | 31 +++++++++---- 5 files changed, 106 insertions(+), 80 deletions(-) diff --git a/bible.cc b/bible.cc index 0bcd9f1..2a282c7 100644 --- a/bible.cc +++ b/bible.cc @@ -181,7 +181,7 @@ int main(int argc, char* argv[]) { return 0; } - auto text = libbible::getText(libbible::passage{.modName = modname, .reference = reference}); + auto text = libbible::getText(libbible::getPassage(modname, reference)); int chapter = 0; int verse = 0; const char* indent = " "; diff --git a/libbible.cc b/libbible.cc index 1bbf5c2..1401208 100644 --- a/libbible.cc +++ b/libbible.cc @@ -61,7 +61,6 @@ vector libbible::getPassages(string modName, string book) { VerseKey *key = (VerseKey *) target->getKey(); libbible::passage pass; pass.modName = modName; - pass.reference = ref; pass.book = string(key->getBookName()); pass.bookShort = string(key->getBookAbbrev()); pass.chapterStart = chapter; @@ -82,62 +81,65 @@ libbible::text getEmptyText(VerseKey *key) { return t; } -void inferMissing(libbible::passage *pass, SWModule *target) { +libbible::passage libbible::getPassage(string modName, string reference) { + libbible::passage pass; + pass.modName = modName; + SWModule *target = library.getModule(pass.modName.c_str()); + if(target == nullptr || reference.empty()) { + // Bad input + return pass; + } vector validBooks = getBooks(target); //printf("Hey, I'm inferring missing parts!\n"); - if(! pass->reference.empty()) { - // Let's use the target to help us - target->setKey(pass->reference.c_str()); - VerseKey *key = (VerseKey *) target->getKey(); - pass->book = string(key->getBookName()); - // Hold on a moment, is this book even legal? - if(find(validBooks.begin(), validBooks.end(), pass->book) == validBooks.end()) { - key->setBookName(validBooks[0].c_str()); - pass->book = string(key->getBookName()); + // Let's use the target to help us + target->setKey(reference.c_str()); + VerseKey *key = (VerseKey *) target->getKey(); + pass.book = string(key->getBookName()); + // Hold on a moment, is this book even legal? + if(find(validBooks.begin(), validBooks.end(), pass.book) == validBooks.end()) { + key->setBookName(validBooks[0].c_str()); + pass.book = string(key->getBookName()); + } + pass.bookShort = string(key->getBookAbbrev()); + pass.chapterStart = key->getChapter(); + pass.verseStart = key->getVerse(); + //printf("Results so far: book: %s; chapterStart: %d; verseStart: %d\n", pass.book.c_str(), pass.chapterStart, pass.verseStart); + // And now we just need chapterEnd and verseEnd. Yippee. + string ref = string(reference); + ref.erase(remove(ref.begin(), ref.end(), ' '), ref.end()); + if(ref.find('-') == string::npos) { + // There's no range! + if(ref.find(':') == string::npos) { + // It's a full chapter reference + pass.chapterEnd = pass.chapterStart; + pass.verseEnd = key->getVerseMax(); + } else { + // It's a single verse reference + pass.chapterEnd = pass.chapterStart; + pass.verseEnd = pass.verseStart; + //printf("Hey, it's a single verse reference!\n"); } - pass->bookShort = string(key->getBookAbbrev()); - pass->chapterStart = key->getChapter(); - pass->verseStart = key->getVerse(); - //printf("Results so far: book: %s; chapterStart: %d; verseStart: %d\n", pass->book.c_str(), pass->chapterStart, pass->verseStart); - // And now we just need chapterEnd and verseEnd. Yippee. - string ref = string(pass->reference); - ref.erase(remove(ref.begin(), ref.end(), ' '), ref.end()); - if(ref.find('-') == string::npos) { - // There's no range! - if(ref.find(':') == string::npos) { - // It's a full chapter reference - pass->chapterEnd = pass->chapterStart; - pass->verseEnd = key->getVerseMax(); - } else { - // It's a single verse reference - pass->chapterEnd = pass->chapterStart; - pass->verseEnd = pass->verseStart; - //printf("Hey, it's a single verse reference!\n"); - } + } else { + if(ref.find(':') == string::npos) { + // It's a multi-full-chapter reference + pass.chapterEnd = stoi(ref.substr(ref.find_last_of('-')+1)); + key->setChapter(pass.chapterEnd); + pass.verseEnd = key->getVerseMax(); } else { - if(ref.find(':') == string::npos) { - // It's a multi-full-chapter reference - pass->chapterEnd = stoi(ref.substr(ref.find_last_of('-')+1)); - key->setChapter(pass->chapterEnd); - pass->verseEnd = key->getVerseMax(); + // It falls in categories c:v-v or c:v-c:v (or, technically, c-c:v) + string rangeEnd = ref.substr(ref.find_last_of('-')+1); + if(rangeEnd.find(':') == string::npos) { + // It's c:v-v + pass.verseEnd = stoi(rangeEnd); + pass.chapterEnd = pass.chapterStart; } else { - // It falls in categories c:v-v or c:v-c:v (or, technically, c-c:v) - string rangeEnd = ref.substr(ref.find_last_of('-')+1); - if(rangeEnd.find(':') == string::npos) { - // It's c:v-v - pass->verseEnd = stoi(rangeEnd); - pass->chapterEnd = pass->chapterStart; - } else { - // It's c:v-c:v (or c-c:v, but code is the same) - pass->chapterEnd = stoi(rangeEnd.substr(0, rangeEnd.find(':'))); - pass->verseEnd = stoi(rangeEnd.substr(rangeEnd.find(':')+1)); - } + // It's c:v-c:v (or c-c:v, but code is the same) + pass.chapterEnd = stoi(rangeEnd.substr(0, rangeEnd.find(':'))); + pass.verseEnd = stoi(rangeEnd.substr(rangeEnd.find(':')+1)); } } } - if(pass->book.empty()) { - pass->book = pass->bookShort; - } + return pass; } vector libbible::getText(libbible::passage pass) { @@ -147,7 +149,9 @@ vector libbible::getText(libbible::passage pass) { // Module doesn't exist return texts; } - inferMissing(&pass, target); + if(pass.book.empty()) { + pass.book = pass.bookShort; + } target->setKey((pass.book + " " + to_string(pass.chapterStart) + ":" + to_string(pass.verseStart)).c_str()); @@ -164,14 +168,14 @@ vector libbible::getText(libbible::passage pass) { // Handle ¶ symbol if at beginning of line /*if(text.find("¶") == 0) { - text.erase(0, 1); - // Append \n to text in previous texts (if applicable) - if(! texts.empty()) { - texts.back().text += '\n'; - } - endOfParagraph = true; + text.erase(0, 1); + // Append \n to text in previous texts (if applicable) + if(! texts.empty()) { + texts.back().text += '\n'; + } + endOfParagraph = true; }*/ - + texts.push_back(getEmptyText(key)); /* Things to enforce: @@ -180,13 +184,13 @@ vector libbible::getText(libbible::passage pass) { // Find and replace everything in "subs" map /*const map subs = {{"¶", "\n\t"}}; - for(auto const &repl : subs) { - string::size_type location; - while((location = text.find(repl.first)) != string::npos) { - text.replace(location, repl.first.size(), repl.second); - } - }*/ - + for(auto const &repl : subs) { + string::size_type location; + while((location = text.find(repl.first)) != string::npos) { + text.replace(location, repl.first.size(), repl.second); + } + }*/ + if(key->getVerse() == 1 || endOfParagraph) { if(find(texts.back().modifiers.begin(), texts.back().modifiers.end(), "paragraph") == texts.back().modifiers.end()) { texts.back().modifiers.push_back("paragraph"); diff --git a/libbible.h b/libbible.h index 85ea9eb..c1058e7 100644 --- a/libbible.h +++ b/libbible.h @@ -3,7 +3,7 @@ #include namespace libbible { - + struct text { int chapter; int verse; @@ -17,7 +17,6 @@ namespace libbible { std::string modName; std::string book; std::string bookShort; - std::string reference; int chapterStart; int verseStart; int chapterEnd; @@ -34,14 +33,21 @@ namespace libbible { */ std::vector getPassages(std::string modName, std::string book); + /* + * @param modName the module to use for determining the passage + * @param reference a human-readable reference, e.g., "gen 1:26-27" + * @return the passage matching the reference + */ + passage getPassage(std::string modName, std::string reference); + /* * @return Text for a passage */ std::vector getText(struct passage pass); /************************** - * Methods dealing with mods - ***************************/ + * Methods dealing with mods + ***************************/ class Status { public: @@ -82,9 +88,9 @@ namespace libbible { void uninstallMod(std::string modname); /****************************** - * Methods dealing with settings - *******************************/ - + * Methods dealing with settings + *******************************/ + /* * From already established code, valid and useful values are: * int fontsize: the last used size of the font diff --git a/mods.cc b/mods.cc index 903c91f..9c9483e 100644 --- a/mods.cc +++ b/mods.cc @@ -45,6 +45,7 @@ void libbible::setStatusReporter(libbible::Status& status) { myStatusReporter *msr = new myStatusReporter(&status); free(installMgr); installMgr = new sword::InstallMgr((basedir + std::string("InstallMgr")).c_str(), msr); + installMgr->setUserDisclaimerConfirmed(true); } map> libbible::downloadModsAvailable() { diff --git a/testLibbible.cc b/testLibbible.cc index 8a7f468..b8f93b3 100644 --- a/testLibbible.cc +++ b/testLibbible.cc @@ -60,6 +60,18 @@ void StatusTester::update(unsigned long totalBytes, unsigned long completedBytes //----------------------------------------------------------------------------- +class CancelTester : public libbible::Status +{ + public: + virtual void update(unsigned long totalBytes, unsigned long completedBytes, string message); +}; + +void CancelTester::update(unsigned long totalBytes, unsigned long completedBytes, string message) { + libbible::terminateDownload(); +} + +//----------------------------------------------------------------------------- + void TestLibbible::testGetModules(void) { map> mods = libbible::getModules(); for(auto pair : mods) { @@ -141,7 +153,7 @@ void TestLibbible::testGetText(void) { //printf("Text is: `%s`\n", allText.c_str()); CPPUNIT_ASSERT(allText == "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life. "); - text = libbible::getText(libbible::passage{.modName = "KJV", .reference="John 3:3"}); + text = libbible::getText(libbible::getPassage("KJV", "John 3:3")); allText.clear(); for(auto tex : text) { allText += tex.text; @@ -149,17 +161,17 @@ void TestLibbible::testGetText(void) { //printf("Text is: `%s`\n", allText.c_str()); CPPUNIT_ASSERT(allText == "Jesus answered and said unto him, Verily, verily, I say unto thee, Except a man be born again, he cannot see the kingdom of God. "); - text = libbible::getText(libbible::passage{.modName = "KJV", .reference="Gal 5:22-23"}); + text = libbible::getText(libbible::getPassage("KJV", "Gal 5:22-23")); chapVerses = getChapVerses(text); shouldContain = vector>({pair(5, 22), pair(5, 23)}); CPPUNIT_ASSERT(chapVerses == shouldContain); - text = libbible::getText(libbible::passage{.modName = "KJV", .reference="1 cor 1:31-2:1"}); + text = libbible::getText(libbible::getPassage("KJV", "1 cor 1:31-2:1")); chapVerses = getChapVerses(text); shouldContain = vector>({pair(1, 31), pair(2, 1)}); CPPUNIT_ASSERT(chapVerses == shouldContain); - text = libbible::getText(libbible::passage{.modName = "KJV", .reference="ps 14-15"}); + text = libbible::getText(libbible::getPassage("KJV", "ps 14-15")); chapVerses = getChapVerses(text); shouldContain = vector>({pair(14, 1), pair(14, 2), @@ -188,11 +200,7 @@ void TestLibbible::testSettings(void) { } void TestLibbible::testDownload(void) { - StatusTester status; - libbible::setStatusReporter(status); map> modsAvailable = libbible::downloadModsAvailable(); - //CPPUNIT_ASSERT(status.hasBeenUpdated); - status.hasBeenUpdated = false; // We try installing the first available one string language; string name; @@ -204,8 +212,15 @@ void TestLibbible::testDownload(void) { CPPUNIT_ASSERT(!language.empty() && !name.empty()); // Try uninstalling it (shouldn't crash or have nasty side effects!) libbible::uninstallMod(name); + // Try installing it with cancel. Shoudn't work because it gets cancelled! + CancelTester cancel; + libbible::setStatusReporter(cancel); + libbible::installModFromInternet(language, name); auto mods = libbible::getModules(); CPPUNIT_ASSERT(mods.find(name) == mods.end()); + // Now we try with normal status + StatusTester status; + libbible::setStatusReporter(status); libbible::installModFromInternet(language, name); mods = libbible::getModules(); CPPUNIT_ASSERT(mods.find(name) != mods.end()); -- cgit v1.2.3