From 07091c17cc8d302918f61e6be7e7f0366b901ecb Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 22 Apr 2017 12:20:26 +0200 Subject: [PATCH] more const, use relative filenames --- src/ebusd/main.cpp | 2 +- src/lib/ebus/message.cpp | 32 +++++++++++++++++--------------- src/lib/ebus/message.h | 24 ++++++++++++++++++------ 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/ebusd/main.cpp b/src/ebusd/main.cpp index 1ce9b0bc..ff066960 100644 --- a/src/ebusd/main.cpp +++ b/src/ebusd/main.cpp @@ -1076,7 +1076,7 @@ int main(int argc, char* argv[]) { setFacilitiesLogLevel(opt.logAreas, opt.logLevel); } - s_messageMap = new MessageMap(opt.checkConfig && opt.scanConfig && arg_index >= argc); + s_messageMap = new MessageMap(string(opt.configPath)+"/", opt.checkConfig && opt.scanConfig && arg_index >= argc); if (opt.checkConfig) { logNotice(lf_main, PACKAGE_STRING "." REVISION " performing configuration check..."); diff --git a/src/lib/ebus/message.cpp b/src/lib/ebus/message.cpp index a5391219..6b97e9d2 100644 --- a/src/lib/ebus/message.cpp +++ b/src/lib/ebus/message.cpp @@ -1676,13 +1676,6 @@ result_t LoadInstruction::execute(MessageMap* messages, ostringstream& log, Cond result_t temp; symbol_t address = (symbol_t)parseInt(m_defaults["zz"].c_str(), 16, 0, 0xff, temp); if (temp == RESULT_OK) { - size_t pos = m_filename.find_last_of('/'); - string filename; - if (pos == string::npos) { - filename = m_filename; - } else { - filename = m_filename.substr(pos+1); - } string comment; if (condition) { ostringstream out; @@ -1690,7 +1683,7 @@ result_t LoadInstruction::execute(MessageMap* messages, ostringstream& log, Cond comment = out.str(); log << " ("+comment+")"; } - messages->addLoadedFile(address, filename, comment); + messages->addLoadedFile(address, m_filename, comment); } } return result; @@ -1699,6 +1692,13 @@ result_t LoadInstruction::execute(MessageMap* messages, ostringstream& log, Cond vector MessageMap::s_noFiles; +const string MessageMap::getRelativePath(const string filename) const { + if (filename.length() >= m_configPath.length() && filename.substr(0, m_configPath.length()) == m_configPath) { + return filename.substr(m_configPath.length()); + } + return filename; +} + result_t MessageMap::add(Message* message, bool storeByName) { uint64_t key = message->getKey(); bool conditional = message->isConditional(); @@ -2105,9 +2105,10 @@ result_t MessageMap::readFromFile(const string filename, string& errorDescriptio } result_t result = MappedFileReader::readFromFile(filename, errorDescription, verbose, defaults, hash, size, time); if (result == RESULT_OK) { - m_loadedFileInfos[filename].m_hash = *hash; - m_loadedFileInfos[filename].m_size = *size; - m_loadedFileInfos[filename].m_time = *time; + const string file = getRelativePath(filename); + m_loadedFileInfos[file].m_hash = *hash; + m_loadedFileInfos[file].m_size = *size; + m_loadedFileInfos[file].m_time = *time; } return result; } @@ -2296,9 +2297,10 @@ result_t MessageMap::executeInstructions(ostringstream& log, void (*readMessageF return overallResult; } -void MessageMap::addLoadedFile(symbol_t address, string file, string comment) { - if (!file.empty()) { +void MessageMap::addLoadedFile(const symbol_t address, const string filename, string const comment) { + if (!filename.empty()) { vector& files = m_loadedFiles[address]; + const string file = getRelativePath(filename); files.push_back(file); if (!comment.empty()) { m_loadedFileInfos[file].m_comment = comment; @@ -2306,7 +2308,7 @@ void MessageMap::addLoadedFile(symbol_t address, string file, string comment) { } } -const vector& MessageMap::getLoadedFiles(symbol_t address) const { +const vector& MessageMap::getLoadedFiles(const symbol_t address) const { auto files = m_loadedFiles.find(address); if (files != m_loadedFiles.end()) { return files->second; @@ -2444,7 +2446,7 @@ deque MessageMap::findAll(const string& circuit, const string& name, c return ret; } -Message* MessageMap::find(MasterSymbolString& master, bool anyDestination, +Message* MessageMap::find(MasterSymbolString& master, const bool anyDestination, const bool withRead, const bool withWrite, const bool withPassive, const bool onlyAvailable) const { if (anyDestination && master.size() >= 5 && master[4] == 0 && master[2] == 0x07 && master[3] == 0x04) { return m_scanMessage; diff --git a/src/lib/ebus/message.h b/src/lib/ebus/message.h index c2933d03..b2cb9168 100644 --- a/src/lib/ebus/message.h +++ b/src/lib/ebus/message.h @@ -1212,11 +1212,13 @@ class MessageMap : public MappedFileReader { public: /** * Construct a new instance. + * @param configPath the path to the configuration files. * @param addAll whether to add all messages, even if duplicate. * @param preferLanguage the preferred language to use, or empty. */ - explicit MessageMap(const bool addAll = false, const string preferLanguage = "") + explicit MessageMap(const string configPath, const bool addAll = false, const string preferLanguage = "") : MappedFileReader::MappedFileReader(true), + m_configPath(configPath), m_addAll(addAll), m_additionalScanMessages(false), m_maxIdLength(0), m_maxBroadcastIdLength(0), m_messageCount(0), m_conditionalMessageCount(0), m_passiveMessageCount(0) { m_scanMessage = Message::createScanMessage(); @@ -1238,6 +1240,13 @@ class MessageMap : public MappedFileReader { } } + /** + * Return the relative file name of the given filename. + * @param filename the name of the configuration file (including relative path). + * @return the relative file name. + */ + const string getRelativePath(const string filename) const; + /** * Add a @a Message instance to this set. * @param message the @a Message instance to add. @@ -1319,17 +1328,17 @@ class MessageMap : public MappedFileReader { /** * Add a loaded file to a participant. * @param address the slave address. - * @param file the name of the file from which a configuration part was loaded for the participant. + * @param filename the name of the configuration file (including relative path). * @param comment an optional comment. */ - void addLoadedFile(symbol_t address, string file, string comment = ""); + void addLoadedFile(const symbol_t address, const string filename, const string comment = ""); /** * Get the loaded files for a participant. * @param address the slave address. * @return the loaded configuration files (list of file names with relative path). */ - const vector& getLoadedFiles(symbol_t address) const; + const vector& getLoadedFiles(const symbol_t address) const; /** * Get all loaded files. @@ -1408,7 +1417,7 @@ class MessageMap : public MappedFileReader { * @return the @a Message instance, or NULL. * Note: the caller may not free the returned instance. */ - Message* find(MasterSymbolString& master, bool anyDestination = false, const bool withRead = true, + Message* find(MasterSymbolString& master, const bool anyDestination = false, const bool withRead = true, const bool withWrite = true, const bool withPassive = true, const bool onlyAvailable = true) const; /** @@ -1493,6 +1502,9 @@ class MessageMap : public MappedFileReader { /** empty vector for @a getLoadedFiles(). */ static vector s_noFiles; + /** the path to the configuration files. */ + const string m_configPath; + /** whether to add all messages, even if duplicate. */ const bool m_addAll; @@ -1505,7 +1517,7 @@ class MessageMap : public MappedFileReader { /** whether additional scan @a Message instances are available. */ bool m_additionalScanMessages; - /** the loaded configuration files by slave address ((list of file names with relative path). */ + /** the loaded configuration files by slave address (list of file names with relative path). */ map> m_loadedFiles; /** the @a LoadedFileInfo by for load configuration files (by file name with relative path). */