From d8f98565a8617658f610bc94a5d87266930beee4 Mon Sep 17 00:00:00 2001 From: danieldg Date: Wed, 21 Oct 2009 23:46:13 +0000 Subject: Use ConfigTagList as a faster access method for access to configuration git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11948 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/configreader.cpp | 86 ++++++++++++++++-------------------- src/logger.cpp | 15 +++---- src/modules.cpp | 28 ++++++++---- src/modules/m_alias.cpp | 7 ++- src/modules/m_helpop.cpp | 7 ++- src/modules/m_opermodes.cpp | 18 ++------ src/modules/m_restrictchans.cpp | 7 ++- src/modules/m_spanningtree/utils.cpp | 21 ++++----- src/modules/m_vhost.cpp | 7 ++- src/socket.cpp | 7 ++- src/stats.cpp | 9 ++-- 11 files changed, 97 insertions(+), 115 deletions(-) (limited to 'src') diff --git a/src/configreader.cpp b/src/configreader.cpp index 5d47e1931..20e244dcc 100644 --- a/src/configreader.cpp +++ b/src/configreader.cpp @@ -574,11 +574,10 @@ static void FindDNS(std::string& server) static void ReadXLine(ServerConfig* conf, const std::string& tag, const std::string& key, XLineFactory* make) { - for(int i=0;; ++i) + ConfigTagList tags = conf->ConfTags(tag); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* ctag = conf->ConfValue(tag, i); - if (!ctag) - break; + ConfigTag* ctag = i->second; std::string mask; if (!ctag->readString(key, mask)) throw CoreException("<"+tag+":"+key+"> missing at " + ctag->getTagLocation()); @@ -593,22 +592,19 @@ typedef std::map LocalIndex; void ServerConfig::CrossCheckOperClassType() { LocalIndex operclass; - for (int i = 0;; ++i) + ConfigTagList tags = ConfTags("class"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ConfValue("class", i); - if (!tag) - break; + ConfigTag* tag = i->second; std::string name = tag->getString("name"); if (name.empty()) throw CoreException(" missing from tag at " + tag->getTagLocation()); operclass[name] = tag; } - for (int i = 0;; ++i) + tags = ConfTags("type"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ConfValue("type", i); - if (!tag) - break; - + ConfigTag* tag = i->second; std::string name = tag->getString("name"); if (name.empty()) throw CoreException(" is missing from tag at " + tag->getTagLocation()); @@ -631,11 +627,10 @@ void ServerConfig::CrossCheckOperClassType() } } - for (int i = 0;; ++i) + tags = ConfTags("oper"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ConfValue("oper", i); - if (!tag) - break; + ConfigTag* tag = i->second; std::string name = tag->getString("name"); if (name.empty()) @@ -673,19 +668,18 @@ void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current) } ClassMap newBlocksByMask; + Classes.resize(config_data.count("type")); std::map names; bool try_again = true; for(int tries=0; try_again; tries++) { try_again = false; - for(unsigned int i=0;; i++) + ConfigTagList tags = ConfTags("type"); + int i=0; + for(ConfigIter it = tags.first; it != tags.second; ++it, ++i) { - ConfigTag* tag = ConfValue("connect", i); - if (!tag) - break; - if (Classes.size() <= i) - Classes.resize(i+1); + ConfigTag* tag = it->second; if (Classes[i]) continue; @@ -901,22 +895,20 @@ void ServerConfig::Fill() if (!sid.empty() && !ServerInstance->IsSID(sid)) throw CoreException(sid + " is not a valid server ID. A server ID must be 3 characters long, with the first character a digit and the next two characters a digit or letter."); - for (int i = 0;; ++i) + ConfigTagList tags = ConfTags("uline"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ConfValue("uline", i); - if (!tag) - break; + ConfigTag* tag = i->second; std::string server; if (!tag->readString("server", server)) throw CoreException(" tag missing server at " + tag->getTagLocation()); ulines[assign(server)] = tag->getBool("silent"); } - for(int i=0;; ++i) + tags = ConfTags("banlist"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ConfValue("banlist", i); - if (!tag) - break; + ConfigTag* tag = i->second; std::string chan; if (!tag->readString("chan", chan)) throw CoreException(" tag missing chan at " + tag->getTagLocation()); @@ -1002,11 +994,13 @@ void ServerConfig::Apply(ServerConfig* old, const std::string &useruid) std::string tag = Once[Index]; if (!ConfValue(tag)) throw CoreException("You have not defined a <"+tag+"> tag, this is required."); - if (ConfValue(tag, 1)) + ConfigTagList tags = ConfTags(tag); + if (tags.first != tags.second) { + tags.first++; errstr << "You have more than one <" << tag << "> tag.\n" - << "First occurrence at " << ConfValue(tag, 0)->getTagLocation() - << "; second occurrence at " << ConfValue(tag, 1)->getTagLocation() << std::endl; + << "First occurrence at " << ConfValue(tag)->getTagLocation() + << "; second occurrence at " << tags.first->second->getTagLocation() << std::endl; } } @@ -1135,11 +1129,10 @@ void ServerConfig::ApplyModules(User* user) std::vector added_modules; std::set removed_modules(v.begin(), v.end()); - for(int i=0; ; i++) + ConfigTagList tags = ConfTags("module"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ConfValue("module", i); - if (!tag) - break; + ConfigTag* tag = i->second; std::string name; if (tag->readString("name", name)) { @@ -1199,20 +1192,19 @@ bool ServerConfig::StartsWithWindowsDriveLetter(const std::string &path) return (path.length() > 2 && isalpha(path[0]) && path[1] == ':'); } -ConfigTag* ServerConfig::ConfValue(const std::string &tag, int offset) +ConfigTag* ServerConfig::ConfValue(const std::string &tag) { - ConfigDataHash::size_type pos = offset; - if (pos >= config_data.count(tag)) - return NULL; - ConfigDataHash::iterator iter = config_data.find(tag); - - for(int i = 0; i < offset; i++) - iter++; - + if (iter == config_data.end()) + return NULL; return iter->second; } +ConfigTagList ServerConfig::ConfTags(const std::string& tag) +{ + return config_data.equal_range(tag); +} + bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf) { if (!this) diff --git a/src/logger.cpp b/src/logger.cpp index f1bbaa7ea..e92762b46 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -86,12 +86,10 @@ void LogManager::OpenFileLogs() } ConfigReader Conf; std::map logmap; - std::map::iterator i; - for (int index = 0;; ++index) + ConfigTagList tags = ServerInstance->Config->ConfTags("log"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("log", index); - if (!tag) - break; + ConfigTag* tag = i->second; std::string method = tag->getString("method"); if (method != "file") { @@ -122,8 +120,9 @@ void LogManager::OpenFileLogs() loglevel = NONE; } FileWriter* fw; - std::string target = Conf.ReadValue("log", "target", index); - if ((i = logmap.find(target)) == logmap.end()) + std::string target = tag->getString("target"); + std::map::iterator fwi = logmap.find(target); + if (fwi == logmap.end()) { FILE* f = fopen(target.c_str(), "a"); fw = new FileWriter(f); @@ -131,7 +130,7 @@ void LogManager::OpenFileLogs() } else { - fw = i->second; + fw = fwi->second; } FileLogStream* fls = new FileLogStream(loglevel, fw); AddLogTypes(type, fls, true); diff --git a/src/modules.cpp b/src/modules.cpp index a4b9e05fa..f70c45bf2 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -588,11 +588,10 @@ void ModuleManager::LoadAll() printf("\n"); } - for(int count = 0;; count++) + ConfigTagList tags = ServerInstance->Config->ConfTags("module"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("module", count); - if (!tag) - break; + ConfigTag* tag = i->second; std::string name = tag->getString("name"); printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str()); @@ -825,12 +824,23 @@ ConfigReader::~ConfigReader() { } +static ConfigTag* SlowGetTag(const std::string &tag, int index) +{ + ConfigTagList tags = ServerInstance->Config->ConfTags(tag); + while (tags.first != tags.second) + { + if (!index) + return tags.first->second; + tags.first++; + index--; + } + return NULL; +} std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds) { - /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */ std::string result = default_value; - if (!ServerInstance->Config->ConfValue(tag, index)->readString(name, result, allow_linefeeds)) + if (!SlowGetTag(tag, index)->readString(name, result, allow_linefeeds)) { this->error = CONF_VALUE_NOT_FOUND; } @@ -845,7 +855,7 @@ std::string ConfigReader::ReadValue(const std::string &tag, const std::string &n bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index) { bool def = (default_value == "yes"); - return ServerInstance->Config->ConfValue(tag, index)->getBool(name, def); + return SlowGetTag(tag, index)->getBool(name, def); } bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index) @@ -857,7 +867,7 @@ bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive) { int v = atoi(default_value.c_str()); - int result = ServerInstance->Config->ConfValue(tag, index)->getInt(name, v); + int result = SlowGetTag(tag, index)->getInt(name, v); if ((need_positive) && (result < 0)) { @@ -885,7 +895,7 @@ int ConfigReader::Enumerate(const std::string &tag) ServerInstance->Logs->Log("MODULE", DEBUG, "Module is using ConfigReader::Enumerate on %s; this is slow!", tag.c_str()); int i=0; - while (ServerInstance->Config->ConfValue(tag, i)) i++; + while (SlowGetTag(tag, i)) i++; return i; } diff --git a/src/modules/m_alias.cpp b/src/modules/m_alias.cpp index 8a58e3dd4..a5f543508 100644 --- a/src/modules/m_alias.cpp +++ b/src/modules/m_alias.cpp @@ -73,11 +73,10 @@ class ModuleAlias : public Module fprefix = fpre.empty() ? '!' : fpre[0]; Aliases.clear(); - for (int i = 0;; i++) + ConfigTagList tags = ServerInstance->Config->ConfTags("alias"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("alias", i); - if (!tag) - break; + ConfigTag* tag = i->second; Alias a; a.AliasedCommand = tag->getString("text").c_str(); tag->readString("replace", a.ReplaceFormat, true); diff --git a/src/modules/m_helpop.cpp b/src/modules/m_helpop.cpp index 8f19bd72b..43da5b4ac 100644 --- a/src/modules/m_helpop.cpp +++ b/src/modules/m_helpop.cpp @@ -133,11 +133,10 @@ class ModuleHelpop : public Module helpop_map.clear(); - for (int i = 0;; i++) + ConfigTagList tags = ServerInstance->Config->ConfTags("helpop"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("helpop", i); - if (!tag) - break; + ConfigTag* tag = i->second; irc::string key = assign(tag->getString("key")); std::string value; tag->readString("value", value, true); /* Linefeeds allowed */ diff --git a/src/modules/m_opermodes.cpp b/src/modules/m_opermodes.cpp index 6b5ca10f9..94a9eefe9 100644 --- a/src/modules/m_opermodes.cpp +++ b/src/modules/m_opermodes.cpp @@ -49,22 +49,10 @@ class ModuleModesOnOper : public Module ApplyModes(user, ThisOpersModes); } - if (!opername.empty()) // if user is local .. + ThisOpersModes = user->oper->getConfig("modes"); + if (!ThisOpersModes.empty()) { - for (int i = 0;; i++) - { - ConfigTag* tag = ServerInstance->Config->ConfValue("oper", i); - if (!tag) - break; - if (tag->getString("name") != opername) - continue; - ThisOpersModes = tag->getString("modes"); - if (!ThisOpersModes.empty()) - { - ApplyModes(user, ThisOpersModes); - } - break; - } + ApplyModes(user, ThisOpersModes); } } diff --git a/src/modules/m_restrictchans.cpp b/src/modules/m_restrictchans.cpp index 8cc882d90..557515fe0 100644 --- a/src/modules/m_restrictchans.cpp +++ b/src/modules/m_restrictchans.cpp @@ -22,11 +22,10 @@ class ModuleRestrictChans : public Module void ReadConfig() { allowchans.clear(); - for (int i = 0;; i++) + ConfigTagList tags = ServerInstance->Config->ConfTags("allowchannel"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("allowchannel", i); - if (!tag) - return; + ConfigTag* tag = i->second; std::string txt = tag->getString("name"); allowchans.insert(txt.c_str()); } diff --git a/src/modules/m_spanningtree/utils.cpp b/src/modules/m_spanningtree/utils.cpp index b8560aaba..10a888833 100644 --- a/src/modules/m_spanningtree/utils.cpp +++ b/src/modules/m_spanningtree/utils.cpp @@ -368,11 +368,10 @@ void SpanningTreeUtilities::ReadConfiguration(bool rebind) if (rebind) { - for (int j = 0; ; j++) + ConfigTagList tags = ServerInstance->Config->ConfTags("bind"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("bind", j); - if (!tag) - break; + ConfigTag* tag = i->second; std::string Type = tag->getString("type"); std::string IP = tag->getString("address"); std::string Port = tag->getString("port"); @@ -417,11 +416,10 @@ void SpanningTreeUtilities::ReadConfiguration(bool rebind) AutoconnectBlocks.clear(); LinkBlocks.clear(); ValidIPs.clear(); - for (int j = 0;; ++j) + ConfigTagList tags = ServerInstance->Config->ConfTags("link"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("link", j); - if (!tag) - break; + ConfigTag* tag = i->second; reference L = new Link(tag); L->Name = tag->getString("name").c_str(); L->AllowMask = tag->getString("allowmask"); @@ -479,11 +477,10 @@ void SpanningTreeUtilities::ReadConfiguration(bool rebind) LinkBlocks.push_back(L); } - for (int j = 0;; ++j) + tags = ServerInstance->Config->ConfTags("autoconnect"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("autoconnect", j); - if (!tag) - break; + ConfigTag* tag = i->second; reference A = new Autoconnect(tag); A->Period = tag->getInt("period"); A->NextConnectTime = ServerInstance->Time() + A->Period; diff --git a/src/modules/m_vhost.cpp b/src/modules/m_vhost.cpp index 18c2a1eb0..1fac18514 100644 --- a/src/modules/m_vhost.cpp +++ b/src/modules/m_vhost.cpp @@ -27,11 +27,10 @@ class CommandVhost : public Command CmdResult Handle (const std::vector ¶meters, User *user) { - for (int index = 0;; index++) + ConfigTagList tags = ServerInstance->Config->ConfTags("vhost"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("vhost", index); - if (!tag) - break; + ConfigTag* tag = i->second; std::string mask = tag->getString("host"); std::string username = tag->getString("user"); std::string pass = tag->getString("pass"); diff --git a/src/socket.cpp b/src/socket.cpp index c4be23a60..1666e3e8b 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -108,11 +108,10 @@ int InspIRCd::BindPorts(FailedPortList &failed_ports) int bound = 0; std::vector old_ports(ports.begin(), ports.end()); - for (int count = 0;; count++) + ConfigTagList tags = ServerInstance->Config->ConfTags("bind"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = ServerInstance->Config->ConfValue("bind", count); - if (!tag) - break; + ConfigTag* tag = i->second; std::string porttag = tag->getString("port"); std::string Addr = tag->getString("address"); std::string Type = tag->getString("type"); diff --git a/src/stats.cpp b/src/stats.cpp index 588953f35..a469600af 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -234,14 +234,15 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results) /* stats o */ case 'o': - for (int i = 0;; i++) + { + ConfigTagList tags = ServerInstance->Config->ConfTags("oper"); + for(ConfigIter i = tags.first; i != tags.second; ++i) { - ConfigTag* tag = Config->ConfValue("oper", i); - if (!tag) - break; + ConfigTag* tag = i->second; results.push_back(sn+" 243 "+user->nick+" O "+tag->getString("host")+" * "+ tag->getString("name") + " " + tag->getString("type")+" 0"); } + } break; /* stats l (show user I/O stats) */ -- cgit v1.2.3