diff options
author | Your Name <you@example.com> | 2020-11-21 14:33:33 -0500 |
---|---|---|
committer | Your Name <you@example.com> | 2020-11-21 14:33:33 -0500 |
commit | 51734065a38cf6db48fe9fb4b16461ce424cad90 (patch) | |
tree | 9abea3db7c02d7eb957256062f8a71d6a7672e35 /libbible.cc | |
parent | dcfac3432f981191da68f14f21f5010fc4da51c0 (diff) | |
download | libbible-51734065a38cf6db48fe9fb4b16461ce424cad90.tar.gz libbible-51734065a38cf6db48fe9fb4b16461ce424cad90.tar.bz2 libbible-51734065a38cf6db48fe9fb4b16461ce424cad90.zip |
Updated API to be more explicit with references
Diffstat (limited to 'libbible.cc')
-rw-r--r-- | libbible.cc | 132 |
1 files changed, 68 insertions, 64 deletions
diff --git a/libbible.cc b/libbible.cc index 1bbf5c2..1401208 100644 --- a/libbible.cc +++ b/libbible.cc @@ -61,7 +61,6 @@ vector<libbible::passage> 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<string> 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::text> libbible::getText(libbible::passage pass) { @@ -147,7 +149,9 @@ vector<libbible::text> 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::text> 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::text> libbible::getText(libbible::passage pass) { // Find and replace everything in "subs" map /*const map<string, string> 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"); |