From 8545667bbde8579e596ff1d302ab673504b4175b Mon Sep 17 00:00:00 2001 From: eduard Date: Fri, 28 Dec 2012 17:54:56 +0100 Subject: [PATCH] string compare and tolower --- apps/openmw/mwdialogue/dialoguemanagerimp.cpp | 78 +++++++------------ apps/openmw/mwgui/inventorywindow.cpp | 15 +--- apps/openmw/mwscript/containerextensions.cpp | 17 +--- apps/openmw/mwworld/containerstore.cpp | 18 ++--- apps/openmw/mwworld/worldimp.cpp | 16 +--- components/esm_store/reclists.hpp | 41 ++++------ components/misc/stringops.cpp | 37 +++++++++ components/misc/stringops.hpp | 9 +++ 8 files changed, 101 insertions(+), 130 deletions(-) diff --git a/apps/openmw/mwdialogue/dialoguemanagerimp.cpp b/apps/openmw/mwdialogue/dialoguemanagerimp.cpp index 62f7df679b..2c30ebf06a 100644 --- a/apps/openmw/mwdialogue/dialoguemanagerimp.cpp +++ b/apps/openmw/mwdialogue/dialoguemanagerimp.cpp @@ -41,30 +41,6 @@ namespace { - std::string toLower (const std::string& name) - { - std::string lowerCase; - - std::transform (name.begin(), name.end(), std::back_inserter (lowerCase), - (int(*)(int)) std::tolower); - - return lowerCase; - } - - bool stringCompareNoCase (std::string first, std::string second) - { - unsigned int i=0; - while ( (itolower(second[i])) return false; - ++i; - } - if (first.length() bool selectCompare (char comp, T1 value1, T2 value2) @@ -72,7 +48,7 @@ namespace switch (comp) { case '0': return value1==value2; - case '1': return value1!=value2; +// case '1': return value1!=value2; case '2': return value1>value2; case '3': return value1>=value2; case '4': return value1first; - if(PCstats.getFactionRanks().find(toLower(NPCFaction)) != PCstats.getFactionRanks().end()) sameFaction = 1; + if(PCstats.getFactionRanks().find(Misc::toLower(NPCFaction)) != PCstats.getFactionRanks().end()) sameFaction = 1; } if(!selectCompare(comp,sameFaction,select.mI)) return false; } @@ -307,12 +283,12 @@ namespace MWDialogue if (select.mType==ESM::VT_Short || select.mType==ESM::VT_Int || select.mType==ESM::VT_Long) { - if (!checkGlobal (comp, toLower (name), select.mI)) + if (!checkGlobal (comp, Misc::toLower (name), select.mI)) return false; } else if (select.mType==ESM::VT_Float) { - if (!checkGlobal (comp, toLower (name), select.mF)) + if (!checkGlobal (comp, Misc::toLower (name), select.mF)) return false; } else @@ -326,13 +302,13 @@ namespace MWDialogue if (select.mType==ESM::VT_Short || select.mType==ESM::VT_Int || select.mType==ESM::VT_Long) { - if (!checkLocal (comp, toLower (name), select.mI, actor, + if (!checkLocal (comp, Misc::toLower (name), select.mI, actor, MWBase::Environment::get().getWorld()->getStore())) return false; } else if (select.mType==ESM::VT_Float) { - if (!checkLocal (comp, toLower (name), select.mF, actor, + if (!checkLocal (comp, Misc::toLower (name), select.mF, actor, MWBase::Environment::get().getWorld()->getStore())) return false; } @@ -345,7 +321,7 @@ namespace MWDialogue case '4'://journal if(select.mType==ESM::VT_Int) { - if(!selectCompare(comp,MWBase::Environment::get().getJournal()->getJournalIndex(toLower(name)),select.mI)) return false; + if(!selectCompare(comp,MWBase::Environment::get().getJournal()->getJournalIndex(Misc::toLower(name)),select.mI)) return false; } else throw std::runtime_error ( @@ -361,7 +337,7 @@ namespace MWDialogue int sum = 0; for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end(); ++iter) - if (toLower(iter->getCellRef().mRefID) == toLower(name)) + if (Misc::toLower(iter->getCellRef().mRefID) == Misc::toLower(name)) sum += iter->getRefData().getCount(); if(!selectCompare(comp,sum,select.mI)) return false; } @@ -375,7 +351,7 @@ namespace MWDialogue case '7':// not ID if(select.mType==ESM::VT_String ||select.mType==ESM::VT_Int)//bug in morrowind here? it's not a short, it's a string { - int isID = int(toLower(name)==toLower(MWWorld::Class::get (actor).getId (actor))); + int isID = int(Misc::toLower(name)==Misc::toLower(MWWorld::Class::get (actor).getId (actor))); if (selectCompare(comp,!isID,select.mI)) return false; } else @@ -391,7 +367,7 @@ namespace MWDialogue if(select.mType==ESM::VT_Int) { MWWorld::LiveCellRef* npc = actor.get(); - int isFaction = int(toLower(npc->base->mFaction) == toLower(name)); + int isFaction = int(Misc::toLower(npc->base->mFaction) == Misc::toLower(name)); if(selectCompare(comp,!isFaction,select.mI)) return false; } @@ -408,7 +384,7 @@ namespace MWDialogue if(select.mType==ESM::VT_Int) { MWWorld::LiveCellRef* npc = actor.get(); - int isClass = int(toLower(npc->base->mClass) == toLower(name)); + int isClass = int(Misc::toLower(npc->base->mClass) == Misc::toLower(name)); if(selectCompare(comp,!isClass,select.mI)) return false; } @@ -425,7 +401,7 @@ namespace MWDialogue if(select.mType==ESM::VT_Int) { MWWorld::LiveCellRef* npc = actor.get(); - int isRace = int(toLower(npc->base->mRace) == toLower(name)); + int isRace = int(Misc::toLower(npc->base->mRace) == Misc::toLower(name)); if(selectCompare(comp,!isRace,select.mI)) return false; } @@ -438,7 +414,7 @@ namespace MWDialogue case 'B'://not Cell if(select.mType==ESM::VT_Int) { - int isCell = int(toLower(actor.getCell()->cell->mName) == toLower(name)); + int isCell = int(Misc::toLower(actor.getCell()->cell->mName) == Misc::toLower(name)); if(selectCompare(comp,!isCell,select.mI)) return false; } @@ -451,13 +427,13 @@ namespace MWDialogue if (select.mType==ESM::VT_Short || select.mType==ESM::VT_Int || select.mType==ESM::VT_Long) { - if (checkLocal (comp, toLower (name), select.mI, actor, + if (checkLocal (comp, Misc::toLower (name), select.mI, actor, MWBase::Environment::get().getWorld()->getStore())) return false; } else if (select.mType==ESM::VT_Float) { - if (checkLocal (comp, toLower (name), select.mF, actor, + if (checkLocal (comp, Misc::toLower (name), select.mF, actor, MWBase::Environment::get().getWorld()->getStore())) return false; } @@ -482,7 +458,7 @@ namespace MWDialogue // actor id if (!info.mActor.empty()) - if (toLower (info.mActor)!=MWWorld::Class::get (actor).getId (actor)) + if (Misc::toLower (info.mActor)!=MWWorld::Class::get (actor).getId (actor)) return false; //NPC race @@ -496,7 +472,7 @@ namespace MWDialogue if (!cellRef) return false; - if (toLower (info.mRace)!=toLower (cellRef->base->mRace)) + if (Misc::toLower (info.mRace)!=Misc::toLower (cellRef->base->mRace)) return false; } @@ -511,7 +487,7 @@ namespace MWDialogue if (!cellRef) return false; - if (toLower (info.mClass)!=toLower (cellRef->base->mClass)) + if (Misc::toLower (info.mClass)!=Misc::toLower (cellRef->base->mClass)) return false; } @@ -523,7 +499,7 @@ namespace MWDialogue //MWWorld::Class npcClass = MWWorld::Class::get(actor); MWMechanics::NpcStats stats = MWWorld::Class::get(actor).getNpcStats(actor); - std::map::iterator it = stats.getFactionRanks().find(toLower(info.mNpcFaction)); + std::map::iterator it = stats.getFactionRanks().find(Misc::toLower(info.mNpcFaction)); if(it!=stats.getFactionRanks().end()) { //check rank @@ -540,7 +516,7 @@ namespace MWDialogue if(!info.mPcFaction.empty()) { MWMechanics::NpcStats stats = MWWorld::Class::get(MWBase::Environment::get().getWorld()->getPlayer().getPlayer()).getNpcStats(MWBase::Environment::get().getWorld()->getPlayer().getPlayer()); - std::map::iterator it = stats.getFactionRanks().find(toLower(info.mPcFaction)); + std::map::iterator it = stats.getFactionRanks().find(Misc::toLower(info.mPcFaction)); if(it!=stats.getFactionRanks().end()) { //check rank @@ -593,13 +569,13 @@ namespace MWDialogue ESMS::RecListCaseT::MapType dialogueList = MWBase::Environment::get().getWorld()->getStore().dialogs.list; for(ESMS::RecListCaseT::MapType::iterator it = dialogueList.begin(); it!=dialogueList.end();it++) { - mDialogueMap[toLower(it->first)] = it->second; + mDialogueMap[Misc::toLower(it->first)] = it->second; } } void DialogueManager::addTopic (const std::string& topic) { - mKnownTopics[toLower(topic)] = true; + mKnownTopics[Misc::toLower(topic)] = true; } void DialogueManager::parseText (std::string text) @@ -753,9 +729,9 @@ namespace MWDialogue { if (isMatching (mActor, *iter) && functionFilter(mActor,*iter,true)) { - mActorKnownTopics.push_back(toLower(it->first)); + mActorKnownTopics.push_back(Misc::toLower(it->first)); //does the player know the topic? - if(mKnownTopics.find(toLower(it->first)) != mKnownTopics.end()) + if(mKnownTopics.find(Misc::toLower(it->first)) != mKnownTopics.end()) { keywordList.push_back(it->first); break; @@ -813,7 +789,7 @@ namespace MWDialogue win->setServices (windowServices); // sort again, because the previous sort was case-sensitive - keywordList.sort(stringCompareNoCase); + keywordList.sort(Misc::stringCompareNoCase); win->setKeywords(keywordList); mChoice = choice; @@ -907,7 +883,7 @@ namespace MWDialogue { MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow(); win->askQuestion(question); - mChoiceMap[toLower(question)] = choice; + mChoiceMap[Misc::toLower(question)] = choice; mIsInChoice = true; } diff --git a/apps/openmw/mwgui/inventorywindow.cpp b/apps/openmw/mwgui/inventorywindow.cpp index bb3dc67e6d..1ac5b2fc68 100644 --- a/apps/openmw/mwgui/inventorywindow.cpp +++ b/apps/openmw/mwgui/inventorywindow.cpp @@ -24,19 +24,6 @@ #include "scrollwindow.hpp" #include "spellwindow.hpp" -namespace -{ - std::string toLower (const std::string& name) - { - std::string lowerCase; - - std::transform (name.begin(), name.end(), std::back_inserter (lowerCase), - (int(*)(int)) std::tolower); - - return lowerCase; - } -} - namespace MWGui { @@ -284,7 +271,7 @@ namespace MWGui for (MWWorld::ContainerStoreIterator it = invStore.begin(); it != invStore.end(); ++it) { - if (toLower(it->getCellRef().mRefID) == "gold_001") + if (Misc::toLower(it->getCellRef().mRefID) == "gold_001") return it->getRefData().getCount(); } return 0; diff --git a/apps/openmw/mwscript/containerextensions.cpp b/apps/openmw/mwscript/containerextensions.cpp index 5e9746b2f1..725885e415 100644 --- a/apps/openmw/mwscript/containerextensions.cpp +++ b/apps/openmw/mwscript/containerextensions.cpp @@ -18,19 +18,6 @@ #include "interpretercontext.hpp" #include "ref.hpp" -namespace -{ - std::string toLower (const std::string& name) - { - std::string lowerCase; - - std::transform (name.begin(), name.end(), std::back_inserter (lowerCase), - (int(*)(int)) std::tolower); - - return lowerCase; - } -} - namespace MWScript { namespace Container @@ -78,7 +65,7 @@ namespace MWScript Interpreter::Type_Integer sum = 0; for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end(); ++iter) - if (toLower(iter->getCellRef().mRefID) == toLower(item)) + if (Misc::toLower(iter->getCellRef().mRefID) == Misc::toLower(item)) sum += iter->getRefData().getCount(); runtime.push (sum); @@ -108,7 +95,7 @@ namespace MWScript for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end() && count; ++iter) { - if (toLower(iter->getCellRef().mRefID) == toLower(item)) + if (Misc::toLower(iter->getCellRef().mRefID) == Misc::toLower(item)) { if (iter->getRefData().getCount()<=count) { diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index 5c4dd03a45..f736a52537 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -34,12 +34,6 @@ namespace return sum; } - - bool compare_string_ci(std::string str1, std::string str2) - { - boost::algorithm::to_lower(str1); - return str1 == str2; - } } MWWorld::ContainerStore::ContainerStore() : mStateId (0), mCachedWeight (0), mWeightUpToDate (false) {} @@ -81,11 +75,11 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add (const Ptr& ptr) MWWorld::LiveCellRef *gold = ptr.get(); - if (compare_string_ci(gold->ref.mRefID, "gold_001") - || compare_string_ci(gold->ref.mRefID, "gold_005") - || compare_string_ci(gold->ref.mRefID, "gold_010") - || compare_string_ci(gold->ref.mRefID, "gold_025") - || compare_string_ci(gold->ref.mRefID, "gold_100")) + if (Misc::compare_string_ci(gold->ref.mRefID, "gold_001") + || Misc::compare_string_ci(gold->ref.mRefID, "gold_005") + || Misc::compare_string_ci(gold->ref.mRefID, "gold_010") + || Misc::compare_string_ci(gold->ref.mRefID, "gold_025") + || Misc::compare_string_ci(gold->ref.mRefID, "gold_100")) { MWWorld::ManualRef ref(MWBase::Environment::get().getWorld()->getStore(), "Gold_001"); @@ -93,7 +87,7 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add (const Ptr& ptr) ref.getPtr().getRefData().setCount(count); for (MWWorld::ContainerStoreIterator iter (begin(type)); iter!=end(); ++iter) { - if (compare_string_ci((*iter).get()->ref.mRefID, "gold_001")) + if (Misc::compare_string_ci((*iter).get()->ref.mRefID, "gold_001")) { (*iter).getRefData().setCount( (*iter).getRefData().getCount() + count); flagAsModified(); diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index c7f0de245e..fe8aeb90bc 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -228,12 +228,12 @@ namespace MWWorld return cell; // didn't work -> now check for regions - std::string cellName2 = ESMS::RecListT::toLower (cellName); + std::string cellName2 = Misc::toLower (cellName); for (ESMS::RecListT::MapType::const_iterator iter (mStore.regions.list.begin()); iter!=mStore.regions.list.end(); ++iter) { - if (ESMS::RecListT::toLower (iter->second.mName)==cellName2) + if (Misc::toLower (iter->second.mName)==cellName2) { if (const ESM::Cell *cell = mStore.cells.searchExtByRegion (iter->first)) return cell; @@ -563,16 +563,6 @@ namespace MWWorld } } - std::string toLower (const std::string& name) - { - std::string lowerCase; - - std::transform (name.begin(), name.end(), std::back_inserter (lowerCase), - (int(*)(int)) std::tolower); - - return lowerCase; - } - void World::moveObject(const Ptr &ptr, CellStore &newCell, float x, float y, float z) { ESM::Position &pos = ptr.getRefData().getPosition(); @@ -585,7 +575,7 @@ namespace MWWorld if (*currCell != newCell) { if (isPlayer) { if (!newCell.isExterior()) { - changeToInteriorCell(toLower(newCell.cell->mName), pos); + changeToInteriorCell(Misc::toLower(newCell.cell->mName), pos); } else { int cellX = newCell.cell->mData.mX; int cellY = newCell.cell->mData.mY; diff --git a/components/esm_store/reclists.hpp b/components/esm_store/reclists.hpp index 0b265a5666..f9f74244b6 100644 --- a/components/esm_store/reclists.hpp +++ b/components/esm_store/reclists.hpp @@ -28,15 +28,6 @@ namespace ESMS virtual int getSize() = 0; virtual void listIdentifier (std::vector& identifier) const = 0; - static std::string toLower (const std::string& name) - { - std::string lowerCase; - - std::transform (name.begin(), name.end(), std::back_inserter (lowerCase), - (int(*)(int)) std::tolower); - - return lowerCase; - } }; typedef std::map RecListList; @@ -53,14 +44,14 @@ namespace ESMS // Load one object of this type void load(ESMReader &esm, const std::string &id) { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); list[id2].load(esm); } // Find the given object ID, or return NULL if not found. const X* search(const std::string &id) const { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); typename MapType::const_iterator iter = list.find (id2); @@ -104,7 +95,7 @@ namespace ESMS // Load one object of this type void load(ESMReader &esm, const std::string &id) { - //std::string id2 = toLower (id); + //std::string id2 = Misc::toLower (id); list[id].load(esm); } @@ -112,12 +103,12 @@ namespace ESMS // Find the given object ID, or return NULL if not found. const X* search(const std::string &id) const { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); for (typename MapType::const_iterator iter = list.begin(); iter != list.end(); ++iter) { - if (toLower(iter->first) == id2) + if (Misc::toLower(iter->first) == id2) return &iter->second; } @@ -127,12 +118,12 @@ namespace ESMS // non-const version X* search(const std::string &id) { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); for (typename MapType::iterator iter = list.begin(); iter != list.end(); ++iter) { - if (toLower(iter->first) == id2) + if (Misc::toLower(iter->first) == id2) return &iter->second; } @@ -172,7 +163,7 @@ namespace ESMS // Load one object of this type void load(ESMReader &esm, const std::string &id) { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); list[id2].mId = id2; list[id2].load(esm); } @@ -180,7 +171,7 @@ namespace ESMS // Find the given object ID, or return NULL if not found. const X* search(const std::string &id) const { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); typename MapType::const_iterator iter = list.find (id2); @@ -223,7 +214,7 @@ namespace ESMS void load(ESMReader &esm, const std::string &id) { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); X& ref = list[id2]; ref.mId = id; @@ -233,7 +224,7 @@ namespace ESMS // Find the given object ID, or return NULL if not found. const X* search(const std::string &id) const { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); typename MapType::const_iterator iter = list.find (id2); @@ -440,7 +431,7 @@ namespace ESMS { for (ExtCells::const_iterator iter = extCells.begin(); iter!=extCells.end(); ++iter) { - if (toLower (iter->second->mName) == toLower (id)) + if (Misc::toLower (iter->second->mName) == Misc::toLower (id)) return iter->second; } @@ -449,10 +440,10 @@ namespace ESMS const ESM::Cell *searchExtByRegion (const std::string& id) const { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); for (ExtCells::const_iterator iter = extCells.begin(); iter!=extCells.end(); ++iter) - if (toLower (iter->second->mRegion)==id) + if (Misc::toLower (iter->second->mRegion)==id) return iter->second; return 0; @@ -586,7 +577,7 @@ namespace ESMS X ref; ref.load (esm); - std::string realId = toLower (ref.mData.mName.toString()); + std::string realId = Misc::toLower (ref.mData.mName.toString()); std::swap (list[realId], ref); } @@ -594,7 +585,7 @@ namespace ESMS // Find the given object ID, or return NULL if not found. const X* search(const std::string &id) const { - std::string id2 = toLower (id); + std::string id2 = Misc::toLower (id); typename MapType::const_iterator iter = list.find (id2); diff --git a/components/misc/stringops.cpp b/components/misc/stringops.cpp index 53eed1fdce..f209bf46b3 100644 --- a/components/misc/stringops.cpp +++ b/components/misc/stringops.cpp @@ -1,7 +1,14 @@ #include "stringops.hpp" +#include +#include +#include + #include #include +#include + + namespace Misc { @@ -61,4 +68,34 @@ bool iends(const char* str1, const char* str2) return strcasecmp(str2, str1+len1-len2) == 0; } +std::string toLower (const std::string& name) +{ + std::string lowerCase; + + std::transform (name.begin(), name.end(), std::back_inserter (lowerCase), + (int(*)(int)) std::tolower); + + return lowerCase; +} + +bool stringCompareNoCase (std::string first, std::string second) +{ + unsigned int i=0; + while ( (itolower(second[i])) return false; + ++i; + } + if (first.length() + namespace Misc { @@ -16,6 +18,13 @@ bool ibegins(const char* str1, const char* str2); /// Case insensitive, returns true if str1 ends with substring str2 bool iends(const char* str1, const char* str2); + +std::string toLower (const std::string& name); + +bool stringCompareNoCase (std::string first, std::string second); + +bool compare_string_ci (std::string first, std::string second); + } #endif