diff --git a/plugins/mumble_plugin_linux.h b/plugins/mumble_plugin_linux.h index e4f3b2316..6cdb1b2e8 100644 --- a/plugins/mumble_plugin_linux.h +++ b/plugins/mumble_plugin_linux.h @@ -10,35 +10,17 @@ # error "Include mumble_plugin_main.h instead of mumble_plugin_linux.h" #endif +#include "mumble_plugin_utils.h" + #include #include -#include #include -#include #include #include #include #include #include -static inline std::string readAll(const std::string &fn) { - std::ifstream ifs; - ifs.open(fn.c_str(), std::ifstream::binary); - - std::string content; - - char buf[256]; - while (ifs.good()) { - ifs.read(&buf[0], sizeof(buf)); - size_t nread = ifs.gcount(); - if (nread > 0) { - content.append(&buf[0], nread); - } - } - - return content; -} - // This function returns: // -1 in case of failure. // 0 if the process is 32-bit. @@ -100,7 +82,7 @@ static inline procptr_t getModuleAddr(const procid_t &pid, const wchar_t *modnam ss << static_cast< unsigned long >(pid); ss << std::string("/maps"); std::string mapsFn = ss.str(); - std::string maps = readAll(mapsFn); + std::string maps = readFile(mapsFn); if (maps.size() == 0) { return 0; diff --git a/plugins/mumble_plugin_utils.h b/plugins/mumble_plugin_utils.h index ce21fd426..4c690b169 100644 --- a/plugins/mumble_plugin_utils.h +++ b/plugins/mumble_plugin_utils.h @@ -6,7 +6,9 @@ #ifndef MUMBLE_MUMBLE_PLUGIN_UTILS_H_ #define MUMBLE_MUMBLE_PLUGIN_UTILS_H_ +#include #include +#include #include #ifdef OS_LINUX @@ -14,6 +16,7 @@ #endif #ifdef _MSC_VER +# include # include #endif @@ -30,6 +33,11 @@ static inline std::string utf16ToUtf8(const std::wstring &wstr) { return conv.to_bytes(wstr); } +static inline std::wstring utf8ToUtf16(const std::string &str) { + std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > conv; + return conv.from_bytes(str); +} + // escape lossily converts the given // string to ASCII, replacing any // character not within the printable @@ -71,6 +79,23 @@ static inline void escape(char *str, const size_t &size) { } } +/// Reads the file's content and returns it in a std::string. +static inline std::string readFile(const std::string &path) { + std::ifstream ifs(path, std::ifstream::binary); + std::string content; + char buf[256]; + + while (ifs.good()) { + ifs.read(&buf[0], sizeof(buf)); + size_t read = ifs.gcount(); + if (read > 0) { + content.append(&buf[0], read); + } + } + + return content; +} + /// Calculates sine and cosine of the specified value. /// On Linux the calculation is guaranteed to be simultaneous. static inline bool sinCos(const float value, float &outSin, float &outCos) {