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 --- mods.cc | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'mods.cc') 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