[General] Move printWithWidth and intToHexStr to components

This commit is contained in:
Koncord 2017-05-03 14:37:52 +08:00
parent 34e77c5dae
commit 47e6820f97
3 changed files with 30 additions and 26 deletions

View File

@ -44,18 +44,6 @@
using namespace std; using namespace std;
using namespace mwmp; using namespace mwmp;
void printWithWidth(ostringstream &sstr, string str, size_t width)
{
sstr << left << setw(width) << setfill(' ') << str;
}
string intToHexStr(unsigned val)
{
ostringstream sstr;
sstr << "0x" << setfill('0') << setw(8) << uppercase << hex << val;
return sstr.str();
}
string comparePlugins(PacketPreInit::PluginContainer checksums, PacketPreInit::PluginContainer checksumsResponse) string comparePlugins(PacketPreInit::PluginContainer checksums, PacketPreInit::PluginContainer checksumsResponse)
{ {
std::ostringstream sstr; std::ostringstream sstr;
@ -78,7 +66,7 @@ string comparePlugins(PacketPreInit::PluginContainer checksums, PacketPreInit::P
string plugin = checksums.at(i).first; string plugin = checksums.at(i).first;
sstr << plugin << " (" << intToHexStr(checksums.at(i).second[0]) << ")"; sstr << plugin << " (" << Utils::intToHexStr(checksums.at(i).second[0]) << ")";
lineLength = lineLength + plugin.size() + 13; lineLength = lineLength + plugin.size() + 13;
} }
@ -105,7 +93,7 @@ string comparePlugins(PacketPreInit::PluginContainer checksums, PacketPreInit::P
if (responseHashSize > 0) if (responseHashSize > 0)
{ {
sstr << intToHexStr(checksumsResponse.at(i).second[0]); sstr << Utils::intToHexStr(checksumsResponse.at(i).second[0]);
if (responseHashSize > 1) if (responseHashSize > 1)
sstr << " & " << (responseHashSize - 1) << " more"; sstr << " & " << (responseHashSize - 1) << " more";
@ -136,12 +124,12 @@ string comparePluginsMonospaced(PacketPreInit::PluginContainer checksums, Packet
pluginNameLen2 = checksumsResponse[i].first.size(); pluginNameLen2 = checksumsResponse[i].first.size();
sstr << "Your plugins or their load order don't match the server's.\n\n"; sstr << "Your plugins or their load order don't match the server's.\n\n";
printWithWidth(sstr, "Your current plugins are:", pluginNameLen1 + 16); Utils::printWithWidth(sstr, "Your current plugins are:", pluginNameLen1 + 16);
sstr << "To join this server, use:\n"; sstr << "To join this server, use:\n";
printWithWidth(sstr, "name", pluginNameLen1 + 2); Utils::printWithWidth(sstr, "name", pluginNameLen1 + 2);
printWithWidth(sstr, "hash", 14); Utils::printWithWidth(sstr, "hash", 14);
printWithWidth(sstr, "name", pluginNameLen2 + 2); Utils::printWithWidth(sstr, "name", pluginNameLen2 + 2);
sstr << "hash\n"; sstr << "hash\n";
for (size_t i = 0; i < checksums.size() || i < checksumsResponse.size(); i++) for (size_t i = 0; i < checksums.size() || i < checksumsResponse.size(); i++)
@ -154,24 +142,22 @@ string comparePluginsMonospaced(PacketPreInit::PluginContainer checksums, Packet
plugin = checksums.at(i).first; plugin = checksums.at(i).first;
val = checksums.at(i).second[0]; val = checksums.at(i).second[0];
printWithWidth(sstr, plugin, pluginNameLen1 + 2); Utils::printWithWidth(sstr, plugin, pluginNameLen1 + 2);
printWithWidth(sstr, intToHexStr(val), 14); Utils::printWithWidth(sstr, Utils::intToHexStr(val), 14);
} }
else else
{ Utils::printWithWidth(sstr, "", pluginNameLen1 + 16);
printWithWidth(sstr, "", pluginNameLen1 + 16);
}
if (i < checksumsResponse.size()) if (i < checksumsResponse.size())
{ {
printWithWidth(sstr, checksumsResponse[i].first, pluginNameLen2 + 2); Utils::printWithWidth(sstr, checksumsResponse[i].first, pluginNameLen2 + 2);
if (checksumsResponse[i].second.size() > 0) if (checksumsResponse[i].second.size() > 0)
{ {
if (full) if (full)
for (size_t j = 0; j < checksumsResponse[i].second.size(); j++) for (size_t j = 0; j < checksumsResponse[i].second.size(); j++)
printWithWidth(sstr, intToHexStr(checksumsResponse[i].second[j]), 14); Utils::printWithWidth(sstr, Utils::intToHexStr(checksumsResponse[i].second[j]), 14);
else else
sstr << intToHexStr(checksumsResponse[i].second[0]); sstr << Utils::intToHexStr(checksumsResponse[i].second[0]);
} }
else else
sstr << "any"; sstr << "any";

View File

@ -12,6 +12,7 @@
#include <sstream> #include <sstream>
#include <boost/crc.hpp> #include <boost/crc.hpp>
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
#include <iomanip>
using namespace std; using namespace std;
@ -147,3 +148,15 @@ unsigned int ::Utils::crc32checksum(const std::string &file)
} }
return crc32.checksum(); return crc32.checksum();
} }
void Utils::printWithWidth(ostringstream &sstr, string str, size_t width)
{
sstr << left << setw(width) << setfill(' ') << str;
}
string Utils::intToHexStr(unsigned val)
{
ostringstream sstr;
sstr << "0x" << setfill('0') << setw(8) << uppercase << hex << val;
return sstr.str();
}

View File

@ -6,6 +6,7 @@
#define UTILS_HPP #define UTILS_HPP
#include <string> #include <string>
#include <sstream>
#ifdef _WIN32 #ifdef _WIN32
int setenv(const char *name, const char *value, int overwrite); int setenv(const char *name, const char *value, int overwrite);
@ -30,5 +31,9 @@ namespace Utils
long int FileLength(const char *file); long int FileLength(const char *file);
unsigned int crc32checksum(const std::string &file); unsigned int crc32checksum(const std::string &file);
void printWithWidth(std::ostringstream &sstr, std::string str, size_t width);
std::string intToHexStr(unsigned val);
} }
#endif //UTILS_HPP #endif //UTILS_HPP