From b8a4092aaa48d00d88765021a82d5f396ba08f0f Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 22 Jan 2022 11:04:16 -0500 Subject: Moved to c++20, reports full language name --- Makefile | 2 +- bible.cc | 7 ++++++- libbible.h | 5 +++++ mods.cc | 39 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 3189adc..4fe1ffc 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC=g++ LIBS=sword minizip -CFLAGS=-c -Wall -fPIC -std=c++17 +CFLAGS=-c -Wall -fPIC -std=c++20 LDFLAGS=-lstdc++fs SOURCES=libbible.cc mods.cc settings.cc OBJECTS=$(SOURCES:.cc=.o) diff --git a/bible.cc b/bible.cc index 943d6d0..fe2c828 100644 --- a/bible.cc +++ b/bible.cc @@ -72,11 +72,16 @@ void listChapters(string modname, string book) { void listInstallable(string language) { map> installable = libbible::downloadModsAvailable(); + map languages = libbible::getLanguageNames(); for(auto pair : installable) { if(!language.empty() && language != pair.first) { continue; } - printf("For language %s:\n", pair.first.c_str()); + printf("For language %s:", pair.first.c_str()); + if(!languages[pair.first].empty()) { + printf(" (%s)", languages[pair.first].c_str()); + } + printf("\n"); for(string name : pair.second) { printf(" %s\n", name.c_str()); } diff --git a/libbible.h b/libbible.h index 82a15ab..f77dc8c 100644 --- a/libbible.h +++ b/libbible.h @@ -64,6 +64,11 @@ namespace libbible { */ std::map> downloadModsAvailable(); + /** + * @return A mapping from language abbreviations to full language names + */ + std::map getLanguageNames(); + /** * Cancel an in-progress download */ diff --git a/mods.cc b/mods.cc index 30b24f8..708f3f1 100644 --- a/mods.cc +++ b/mods.cc @@ -42,6 +42,7 @@ void myStatusReporter::update(unsigned long totalBytes, unsigned long completedB string basedir = (getenv("HOME")) + string("/.sword/"); sword::InstallMgr *installMgr = new sword::InstallMgr((basedir + std::string("InstallMgr")).c_str(), nullptr); map>> installSources; +map languageNames; // maps abbreviation to full name void libbible::setStatusReporter(libbible::Status& status) { myStatusReporter *msr = new myStatusReporter(&status); @@ -52,6 +53,7 @@ void libbible::setStatusReporter(libbible::Status& status) { map> libbible::downloadModsAvailable() { installSources.clear(); + languageNames.clear(); mkdir((basedir + std::string("mods.d/")).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); mkdir((basedir + std::string("modules/")).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); installMgr->setUserDisclaimerConfirmed(true); @@ -71,6 +73,7 @@ map> libbible::downloadModsAvailable() { } installMgr->readInstallConf(); map> modsAvailable; + map> languagesToFull; //printf("Getting langs...\n"); for(auto src : installMgr->sources) { if(src.second->getMgr()->Modules.empty()) { @@ -82,7 +85,19 @@ map> libbible::downloadModsAvailable() { string type(curMod->getType()); if(type == "Biblical Texts") { string language(curMod->getLanguage()); - //printf("Got language %s\n", language.c_str()); + string fullLang; + if(curMod->getConfigEntry("LCSH")) { + // Split on periods, last field, strip + fullLang = string(curMod->getConfigEntry("LCSH")); + // If ends with ., remove + if(fullLang.ends_with('.')) fullLang = fullLang.substr(0, fullLang.size()-1); + if(fullLang.find('.') != string::npos) fullLang = fullLang.substr(fullLang.find_last_of('.')+1); + while(fullLang.starts_with(' ')) fullLang = fullLang.substr(1); + while(fullLang.ends_with(' ')) fullLang = fullLang.substr(0, fullLang.size()-1); + } + vector newLangs; + languagesToFull.emplace(language, newLangs); + languagesToFull[language].push_back(fullLang); vector newMods; vector> newSources; // emplace only adds if key is unique @@ -94,9 +109,31 @@ map> libbible::downloadModsAvailable() { } } } + // Now use majority voting to move languagesToFull -> languageNames + for(const auto& [abbrev, fulls] : languagesToFull) { + std::map majVote; + for(auto full : fulls) { + majVote.try_emplace(full, 0); + majVote[full]++; + } + string selected = fulls[0]; + for(auto full : fulls) { + if(majVote[full] > majVote[selected] or (majVote[full] == majVote[selected] and full.size() < selected.size())) { + selected = full; + } + } + languageNames[abbrev] = selected; + } return modsAvailable; } +std::map libbible::getLanguageNames() { + if(languageNames.empty()) { + downloadModsAvailable(); + } + return languageNames; +} + void libbible::terminateDownload() { installMgr->terminate(); } -- cgit v1.2.3