From 31f95aeec9e3fbfd8a14ed3c4482b8c7f95ecb00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 6 Jan 2009 15:02:34 +0000 Subject: [PATCH] tools: Added a folder where we can keep small usable utilities --- tools/errmsg.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tools/errmsg.cpp diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp new file mode 100644 index 000000000..905db9a41 --- /dev/null +++ b/tools/errmsg.cpp @@ -0,0 +1,62 @@ + +#include +#include +#include + +class Message +{ +private: + std::string _funcname; + std::string _msg; + std::string _par1; + +public: + Message(std::string funcname, std::string msg, std::string par1) + : _funcname(funcname), _msg(msg), _par1(par1) + { } + + std::string msg() const + { + std::string ret("\"" + _msg + "\""); + + if (! _par1.empty()) + { + std::string::size_type pos = 0; + while ((pos = ret.find("%1", pos)) != std::string::npos) + { + ret.erase(pos, 2); + ret.insert(pos, "\" + " + _par1 + " + \""); + } + } + + return ret; + } + + void generateCode() const + { + std::cout << " static std::string " << _funcname << "("; + if (! _par1.empty()) + std::cout << "const std::string &" << _par1; + std::cout << ") const\n"; + + std::cout << " { return " << msg() << "; }" << std::endl; + } + +}; + + + + +int main() +{ + // Error messages.. + std::list err; + err.push_back(Message("memleak", "Memory leak: %1", "varname")); + + // Generate code.. + for (std::list::const_iterator it = err.begin(); it != err.end(); ++it) + it->generateCode(); + + return 0; +} +