mirror of
https://github.com/mumble-voip/mumble.git
synced 2025-10-26 11:19:16 +00:00
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++.
This commit is contained in:
parent
755a8092c0
commit
5debd105e3
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user