diff options
author | Davide Beatrici <git@davidebeatrici.dev> | 2020-10-16 07:31:53 +0300 |
---|---|---|
committer | Davide Beatrici <git@davidebeatrici.dev> | 2020-10-28 01:18:21 +0300 |
commit | 5debd105e33835d5e7f8681cecae5ccf225c19a7 (patch) | |
tree | c5b03c8160e8c7b00359ac603dfad0fe859babce /plugins | |
parent | 755a8092c085317f0779865bf2a2f13f27dab988 (diff) |
FEAT/REFAC(plugins): Add utf8ToUtf16(), move and rename readAll()
The new function is going to be used in a later commit.
readAll() is moved to mumble_plugin_utils.h and is renamed to readFile(), to make its purpose clearer.
This commit also moves the <math.h> include to mumble_plugin_utils.h, because that's where it's required.
Also, it's now included as <cmath>, as recommended for C++.
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/mumble_plugin_linux.h | 24 | ||||
-rw-r--r-- | plugins/mumble_plugin_utils.h | 25 |
2 files changed, 28 insertions, 21 deletions
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 <cstring> #include <elf.h> -#include <fstream> #include <iostream> -#include <math.h> #include <sstream> #include <stdio.h> #include <stdlib.h> #include <string> #include <sys/uio.h> -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 <cmath> #include <codecvt> +#include <fstream> #include <locale> #ifdef OS_LINUX @@ -14,6 +16,7 @@ #endif #ifdef _MSC_VER +# include <corecrt_math_defines.h> # include <intrin.h> #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) { |