From 386f0dafd55897723356c75299cd02ed0b882f5b Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Mon, 3 Jul 2023 17:26:40 +0100 Subject: Replace FileReader with something more sensible. --- src/configreader.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/configreader.cpp') diff --git a/src/configreader.cpp b/src/configreader.cpp index 363bc16c4..1c418eae7 100644 --- a/src/configreader.cpp +++ b/src/configreader.cpp @@ -40,6 +40,12 @@ #include "configparser.h" #include "exitcodes.h" +ServerConfig::ReadResult::ReadResult(const std::string& c, const std::string& e) + : contents(c) + , error(e) +{ +} + ServerConfig::ServerLimits::ServerLimits(const std::shared_ptr& tag) : MaxLine(tag->getNum("maxline", 512, 512)) , MaxNick(tag->getNum("maxnick", 30, 1, MaxLine)) @@ -88,6 +94,45 @@ ServerConfig::ServerConfig() { } +ServerConfig::ReadResult ServerConfig::ReadFile(const std::string& file, bool invalidate) +{ + auto contents = filecontents.find(file); + if (invalidate) + filecontents.erase(contents); + else if (contents != filecontents.end()) + return ReadResult(contents->second, {}); + + bool executable = false; + std::string name = file; + std::string path = file; + + // If the caller specified a short name (e.g. ) then look it up. + auto source = filesources.find(file); + if (source != filesources.end()) + { + name = source->first; + path = source->second.second; + executable = source->second.second; + } + + // Try to open the file and error out if it fails. + auto fh = ParseStack::DoOpenFile(path, executable); + if (!fh) + return ReadResult({}, strerror(errno)); + + std::stringstream datastream; + char databuf[4096]; + while (fgets(databuf, sizeof(databuf), fh.get())) + { + size_t len = strlen(databuf); + if (len) + datastream.write(databuf, len); + } + + filecontents[name] = datastream.str(); + return ReadResult(filecontents[name], {}); +} + void ServerConfig::CrossCheckOperBlocks() { std::unordered_map> operclass; -- cgit v1.3.1-10-gc9f91