diff options
author | Attila Molnar <attilamolnar@hush.com> | 2013-05-20 11:05:05 -0700 |
---|---|---|
committer | Attila Molnar <attilamolnar@hush.com> | 2013-05-20 11:05:05 -0700 |
commit | 991a6a429bd6abc70c0eac33bcca124b0600cc0f (patch) | |
tree | 73961818b96646cfbe0a0ff945a23db04edc4823 | |
parent | 3ce33ee0e6bc0b8fbe2a564a37e02d2e56510b06 (diff) | |
parent | 4e40ee49bba3f7bab05a58516e2039351fb33069 (diff) |
Merge pull request #542 from ShutterQuick/inspircd+printfcleanup
Replaced all the individual vsnprintfing with a macro
-rw-r--r-- | include/inspircd.h | 3 | ||||
-rw-r--r-- | include/inspstring.h | 9 | ||||
-rw-r--r-- | src/channels.cpp | 47 | ||||
-rw-r--r-- | src/helperfuncs.cpp | 23 | ||||
-rw-r--r-- | src/logger.cpp | 13 | ||||
-rw-r--r-- | src/modules/m_spanningtree/main.cpp | 8 | ||||
-rw-r--r-- | src/snomasks.cpp | 22 | ||||
-rw-r--r-- | src/usermanager.cpp | 10 | ||||
-rw-r--r-- | src/users.cpp | 103 |
9 files changed, 77 insertions, 161 deletions
diff --git a/include/inspircd.h b/include/inspircd.h index 9a9583499..de574b6f4 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -543,7 +543,8 @@ class CoreExport InspIRCd * @param ... * @return The formatted string */ - static const char* Format(const char* formatString, ...); + static const char* Format(const char* formatString, ...) CUSTOM_PRINTF(1, 2); + static const char* Format(const va_list &vaList, const char* formatString) CUSTOM_PRINTF(2, 0); static void QuickExit(int status); diff --git a/include/inspstring.h b/include/inspstring.h index eb7b7218f..ccc77da66 100644 --- a/include/inspstring.h +++ b/include/inspstring.h @@ -24,6 +24,15 @@ #include "config.h" #include <cstring> +/** Sets ret to the formated string. last is the last parameter before ..., and format is the format in printf-style */ +#define VAFORMAT(ret, last, format) \ + do { \ + va_list _vaList; \ + va_start(_vaList, last); \ + ret = InspIRCd::Format(_vaList, format); \ + va_end(_vaList); \ + } while (false); + /** Compose a hex string from raw data. * @param raw The raw data to compose hex from (can be NULL if rawsize is 0) * @param rawsize The size of the raw data buffer diff --git a/src/channels.cpp b/src/channels.cpp index 0cc007464..1c8e9e856 100644 --- a/src/channels.cpp +++ b/src/channels.cpp @@ -595,17 +595,12 @@ void Channel::KickUser(User *src, User *user, const std::string& reason) void Channel::WriteChannel(User* user, const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - if (!user || !text) return; - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - this->WriteChannel(user, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + this->WriteChannel(user, textbuffer); } void Channel::WriteChannel(User* user, const std::string &text) @@ -624,17 +619,12 @@ void Channel::WriteChannel(User* user, const std::string &text) void Channel::WriteChannelWithServ(const std::string& ServName, const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - if (!text) return; - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - this->WriteChannelWithServ(ServName, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + this->WriteChannelWithServ(ServName, textbuffer); } void Channel::WriteChannelWithServ(const std::string& ServName, const std::string &text) @@ -652,34 +642,23 @@ void Channel::WriteChannelWithServ(const std::string& ServName, const std::strin * for the sender (for privmsg etc) */ void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - if (!text) return; - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + this->WriteAllExceptSender(user, serversource, status, textbuffer); } void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - if (!text) return; - int offset = snprintf(textbuffer,MAXBUF,":%s ", serversource ? ServerInstance->Config->ServerName.c_str() : user->GetFullHost().c_str()); - - va_start(argsPtr, text); - vsnprintf(textbuffer + offset, MAXBUF - offset, text, argsPtr); - va_end(argsPtr); - - this->RawWriteAllExcept(user, serversource, status, except_list, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + textbuffer = ":" + (serversource ? ServerInstance->Config->ServerName : user->GetFullHost()) + " " + textbuffer; + this->RawWriteAllExcept(user, serversource, status, except_list, textbuffer); } void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text) diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index d11a13aa0..0673efedf 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -372,13 +372,9 @@ void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const std::str void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - va_start (argsPtr, format); - vsnprintf(textbuffer, MAXBUF, format, argsPtr); - va_end(argsPtr); - - this->SendWhoisLine(user, dest, numeric, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, format, format) + this->SendWhoisLine(user, dest, numeric, textbuffer); } /** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array @@ -424,19 +420,22 @@ unsigned long InspIRCd::Duration(const std::string &str) return total + subtotal; } -const char* InspIRCd::Format(const char* formatString, ...) +const char* InspIRCd::Format(const va_list &vaList, const char* formatString) { static std::vector<char> formatBuffer(1024); int vsnret = 0; - - va_list vaList; - va_start(vaList, formatString); while ((vsnret = vsnprintf(&formatBuffer[0], formatBuffer.size(), formatString, vaList)) < 0 || static_cast<unsigned int>(vsnret) >= formatBuffer.size()) formatBuffer.resize(formatBuffer.size() * 2); - va_end(vaList); return &formatBuffer[0]; } +const char* InspIRCd::Format(const char* formatString, ...) +{ + const char* ret; + VAFORMAT(ret, formatString, formatString); + return ret; +} + bool InspIRCd::ULine(const std::string& sserver) { if (sserver.empty()) diff --git a/src/logger.cpp b/src/logger.cpp index b33f65ac4..2b0b623f6 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -296,18 +296,11 @@ bool LogManager::DelLogType(const std::string &type, LogStream *l) void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...) { if (Logging) - { return; - } - - va_list a; - static char buf[65536]; - - va_start(a, fmt); - vsnprintf(buf, 65536, fmt, a); - va_end(a); - this->Log(type, loglevel, std::string(buf)); + std::string buf; + VAFORMAT(buf, fmt, fmt); + this->Log(type, loglevel, buf); } void LogManager::Log(const std::string &type, int loglevel, const std::string &msg) diff --git a/src/modules/m_spanningtree/main.cpp b/src/modules/m_spanningtree/main.cpp index 92d274679..244180b2f 100644 --- a/src/modules/m_spanningtree/main.cpp +++ b/src/modules/m_spanningtree/main.cpp @@ -380,12 +380,8 @@ ModResult ModuleSpanningTree::HandleVersion(const std::vector<std::string>& para */ void ModuleSpanningTree::RemoteMessage(User* user, const char* format, ...) { - char text[MAXBUF]; - va_list argsPtr; - - va_start(argsPtr, format); - vsnprintf(text, MAXBUF, format, argsPtr); - va_end(argsPtr); + std::string text; + VAFORMAT(text, format, format); if (IS_LOCAL(user)) user->WriteNotice(text); diff --git a/src/snomasks.cpp b/src/snomasks.cpp index f018a0d1a..820f65e4b 100644 --- a/src/snomasks.cpp +++ b/src/snomasks.cpp @@ -52,26 +52,16 @@ void SnomaskManager::WriteGlobalSno(char letter, const std::string& text) void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - this->WriteToSnoMask(letter, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + this->WriteToSnoMask(letter, textbuffer); } void SnomaskManager::WriteGlobalSno(char letter, const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - this->WriteGlobalSno(letter, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + this->WriteGlobalSno(letter, textbuffer); } SnomaskManager::SnomaskManager() diff --git a/src/usermanager.cpp b/src/usermanager.cpp index aa0be2f3b..09c9a5bde 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -322,13 +322,9 @@ unsigned int UserManager::LocalUserCount() void UserManager::ServerNoticeAll(const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - va_start (argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - const std::string message = "NOTICE $" + ServerInstance->Config->ServerName + " :" + textbuffer; + std::string message; + VAFORMAT(message, text, text); + message = "NOTICE $" + ServerInstance->Config->ServerName + " :" + message; for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++) { diff --git a/src/users.cpp b/src/users.cpp index f7dafeb86..7e1df61fe 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -1016,14 +1016,9 @@ void LocalUser::Write(const std::string& text) */ void LocalUser::Write(const char *text, ...) { - va_list argsPtr; - char textbuffer[MAXBUF]; - - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - this->Write(std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + this->Write(textbuffer); } void User::WriteServ(const std::string& text) @@ -1036,14 +1031,9 @@ void User::WriteServ(const std::string& text) */ void User::WriteServ(const char* text, ...) { - va_list argsPtr; - char textbuffer[MAXBUF]; - - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - this->WriteServ(std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + this->WriteServ(textbuffer); } void User::WriteNotice(const std::string& text) @@ -1053,14 +1043,9 @@ void User::WriteNotice(const std::string& text) void User::WriteNumeric(unsigned int numeric, const char* text, ...) { - va_list argsPtr; - char textbuffer[MAXBUF]; - - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - this->WriteNumeric(numeric, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + this->WriteNumeric(numeric, textbuffer); } void User::WriteNumeric(unsigned int numeric, const std::string &text) @@ -1088,14 +1073,9 @@ void User::WriteFrom(User *user, const std::string &text) void User::WriteFrom(User *user, const char* text, ...) { - va_list argsPtr; - char textbuffer[MAXBUF]; - - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - - this->WriteFrom(user, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + this->WriteFrom(user, textbuffer); } @@ -1103,14 +1083,9 @@ void User::WriteFrom(User *user, const char* text, ...) void User::WriteTo(User *dest, const char *data, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - - va_start(argsPtr, data); - vsnprintf(textbuffer, MAXBUF, data, argsPtr); - va_end(argsPtr); - - this->WriteTo(dest, std::string(textbuffer)); + std::string textbuffer; + VAFORMAT(textbuffer, data, data); + this->WriteTo(dest, textbuffer); } void User::WriteTo(User *dest, const std::string &data) @@ -1120,36 +1095,24 @@ void User::WriteTo(User *dest, const std::string &data) void User::WriteCommon(const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - if (this->registered != REG_ALL || quitting) return; - int len = snprintf(textbuffer,MAXBUF,":%s ",this->GetFullHost().c_str()); - - va_start(argsPtr, text); - vsnprintf(textbuffer + len, MAXBUF - len, text, argsPtr); - va_end(argsPtr); - - this->WriteCommonRaw(std::string(textbuffer), true); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + textbuffer = ":" + this->GetFullHost() + " " + textbuffer; + this->WriteCommonRaw(textbuffer, true); } void User::WriteCommonExcept(const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - if (this->registered != REG_ALL || quitting) return; - int len = snprintf(textbuffer,MAXBUF,":%s ",this->GetFullHost().c_str()); - - va_start(argsPtr, text); - vsnprintf(textbuffer + len, MAXBUF - len, text, argsPtr); - va_end(argsPtr); - - this->WriteCommonRaw(std::string(textbuffer), false); + std::string textbuffer; + VAFORMAT(textbuffer, text, text); + textbuffer = ":" + this->GetFullHost() + " " + textbuffer; + this->WriteCommonRaw(textbuffer, false); } void User::WriteCommonRaw(const std::string &line, bool include_self) @@ -1248,14 +1211,9 @@ void FakeUser::SendText(const std::string& line) void User::SendText(const char *text, ...) { - va_list argsPtr; - char line[MAXBUF]; - - va_start(argsPtr, text); - vsnprintf(line, MAXBUF, text, argsPtr); - va_end(argsPtr); - - SendText(std::string(line)); + std::string line; + VAFORMAT(line, text, text); + SendText(line); } void User::SendText(const std::string &LinePrefix, std::stringstream &TextStream) @@ -1445,13 +1403,8 @@ bool User::ChangeIdent(const char* newident) void User::SendAll(const char* command, const char* text, ...) { - char textbuffer[MAXBUF]; - va_list argsPtr; - - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - + std::string textbuffer; + VAFORMAT(textbuffer, text, text); const std::string message = ":" + this->GetFullHost() + " " + command + " $* :" + textbuffer; for (LocalUserList::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); i++) |