finish linux related code writting. remove all offensive content.
- REMOVE all offensive content in README and etc. This project respect to every contributors. - However, for commercial reason, we limit the contribution from Dassault. - Change license. - Finish linux platform code.
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
#include "CKMinContext.hpp"
|
||||
#include "CKObjects.hpp"
|
||||
#include "CKManagers.hpp"
|
||||
#include <cstdarg>
|
||||
|
||||
namespace LibCmo::CK2 {
|
||||
|
||||
|
@ -72,9 +72,21 @@ namespace LibCmo::EncodingHelper {
|
||||
#else
|
||||
|
||||
static constexpr const size_t IconvInc = 16;
|
||||
static const iconv_t InvalidIconvDescriptor = reinterpret_cast<iconv_t>(-1);
|
||||
|
||||
bool DoIconv(const char* enc_from, const char* enc_to, std::string& str_from, std::string& str_to) {
|
||||
iconv_t cd;
|
||||
bool CreateIconvDescriptor(const char* enc_from, const char* enc_to, iconv_t& val) {
|
||||
val = iconv_open(enc_to, enc_from);
|
||||
return val != InvalidIconvDescriptor;
|
||||
}
|
||||
void DestroyIconvDescriptor(iconv_t& val) {
|
||||
if (val == InvalidIconvDescriptor) return;
|
||||
|
||||
iconv_close(val);
|
||||
val = InvalidIconvDescriptor;
|
||||
}
|
||||
|
||||
// Reference: https://stackoverflow.com/questions/13297458/simple-utf8-utf16-string-conversion-with-iconv
|
||||
bool DoIconv(iconv_t& cd, const std::string& str_from, std::string& str_to) {
|
||||
char *inbuf = nullptr, *outbuf = nullptr;
|
||||
size_t inbytesleft, outbytesleft, nchars, result_len;
|
||||
|
||||
@ -84,10 +96,9 @@ namespace LibCmo::EncodingHelper {
|
||||
return true;
|
||||
}
|
||||
|
||||
// create iconv descriptor
|
||||
cd = iconv_open(enc_to, enc_from);
|
||||
if (cd == (iconv_t) -1) {
|
||||
// fail to create iconv descriptor
|
||||
// check iconv descriptor
|
||||
if (cd == InvalidIconvDescriptor) {
|
||||
// invalid iconv descriptor
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -95,9 +106,9 @@ namespace LibCmo::EncodingHelper {
|
||||
str_to.resize(str_from.size() + IconvInc);
|
||||
// setup some variables
|
||||
inbytesleft = str_from.size();
|
||||
inbuf = str_from.data();
|
||||
inbuf = const_cast<char*>(str_from.c_str());
|
||||
|
||||
outbytesleft = str_to_size();
|
||||
outbytesleft = str_to.size();
|
||||
outbuf = str_to.data();
|
||||
|
||||
result_len = str_to.size();
|
||||
@ -115,13 +126,13 @@ namespace LibCmo::EncodingHelper {
|
||||
// resize for container
|
||||
str_to.resize(result_len);
|
||||
|
||||
// assign new outbuf from failed position
|
||||
// assign new outbuf from failed position
|
||||
outbuf = str_to.data() + len;
|
||||
nchars = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
|
||||
}
|
||||
|
||||
// close iconv descriptor
|
||||
iconv_close(cd);
|
||||
// restore descriptor initial state
|
||||
iconv(cd, nullptr, nullptr, nullptr, nullptr);
|
||||
|
||||
// check error
|
||||
if (nchars == (size_t)-1) {
|
||||
@ -185,45 +196,45 @@ namespace LibCmo::EncodingHelper {
|
||||
|
||||
#else
|
||||
|
||||
IconvPair::IconvPair() :
|
||||
FromUtf8(InvalidIconvDescriptor), ToUtf8(InvalidIconvDescriptor) {
|
||||
}
|
||||
|
||||
IconvPair::~IconvPair() {
|
||||
DestroyIconvDescriptor(this->FromUtf8);
|
||||
DestroyIconvDescriptor(this->ToUtf8);
|
||||
}
|
||||
|
||||
|
||||
static constexpr const char UTF8_SYMBOL[] = "UTF-8";
|
||||
|
||||
ENCODING_TOKEN CreateEncodingToken(std::string& token_string) {
|
||||
ENCODING_TOKEN token = new(std::nothrow) char[token_string.size() + 1];
|
||||
if (token == nullptr) return ENCODING_TOKEN_DFAULT;
|
||||
ENCODING_TOKEN CreateEncodingToken(const std::string& token_string) {
|
||||
ENCODING_TOKEN token = new(std::nothrow) IconvPair();
|
||||
if (token == nullptr) return ENCODING_TOKEN_DEFAULT;
|
||||
|
||||
if (!CreateIconvDescriptor(UTF8_SYMBOL, token_string.c_str(), token->FromUtf8) ||
|
||||
!CreateIconvDescriptor(token_string.c_str(), UTF8_SYMBOL, token->ToUtf8)) {
|
||||
delete token;
|
||||
return ENCODING_TOKEN_DEFAULT;
|
||||
}
|
||||
|
||||
std::memcpy(token, token_string.c_str(), token_string.size() + 1);
|
||||
return token;
|
||||
}
|
||||
|
||||
void DestroyEncodingToken(ENCODING_TOKEN token) {
|
||||
void DestroyEncodingToken(const ENCODING_TOKEN& token) {
|
||||
if (token != ENCODING_TOKEN_DEFAULT) {
|
||||
delete[] token;
|
||||
delete token;
|
||||
}
|
||||
}
|
||||
|
||||
void GetUtf8VirtoolsName(std::string& native_name, std::string& u8_name, ENCODING_TOKEN token) {
|
||||
if (token == ENCODING_TOKEN_DEFAULT) {
|
||||
u8_name = native_name.c_str();
|
||||
return;
|
||||
}
|
||||
|
||||
// convert with fallback
|
||||
if (!DoIconv(token, UTF8_SYMBOL, native_name, u8_name)) {
|
||||
u8_name = native_name.c_str();
|
||||
}
|
||||
bool GetUtf8VirtoolsName(const std::string& native_name, std::string& u8_name, const ENCODING_TOKEN& token) {
|
||||
if (token == ENCODING_TOKEN_DEFAULT) return false;
|
||||
return DoIconv(token->ToUtf8, native_name, u8_name);
|
||||
}
|
||||
|
||||
void GetNativeVirtoolsName(std::string& u8_name, std::string& native_name, ENCODING_TOKEN token) {
|
||||
|
||||
if (token == ENCODING_TOKEN_DEFAULT) {
|
||||
native_name = u8_name.c_str();
|
||||
return;
|
||||
}
|
||||
|
||||
// convert with fallback
|
||||
if (!DoIconv(UTF8_SYMBOL, token, u8_name, native_name)) {
|
||||
native_name = u8_name.c_str();
|
||||
}
|
||||
bool GetNativeVirtoolsName(const std::string& u8_name, std::string& native_name, const ENCODING_TOKEN& token) {
|
||||
if (token == ENCODING_TOKEN_DEFAULT) return false;
|
||||
return DoIconv(token->FromUtf8, u8_name, native_name);
|
||||
}
|
||||
|
||||
void SetStdPathFromU8Path(std::filesystem::path& stdpath, const char* u8_path) {
|
||||
|
@ -30,7 +30,10 @@ namespace LibCmo::EncodingHelper {
|
||||
|
||||
#else
|
||||
|
||||
bool DoIconv(const char* enc_from, const char* enc_to, std::string& str_from, std::string& str_to);
|
||||
bool CreateIconvDescriptor(const char* enc_from, const char* enc_to, iconv_t& val);
|
||||
void DestroyIconvDescriptor(iconv_t& val);
|
||||
|
||||
bool DoIconv(iconv_t& cd, const std::string& str_from, std::string& str_to);
|
||||
|
||||
#endif
|
||||
|
||||
@ -47,7 +50,17 @@ namespace LibCmo::EncodingHelper {
|
||||
|
||||
#else
|
||||
|
||||
using ENCODING_TOKEN = char*;
|
||||
class IconvPair {
|
||||
public:
|
||||
IconvPair();
|
||||
IconvPair(const IconvPair&) = delete;
|
||||
IconvPair& operator=(const IconvPair&) = delete;
|
||||
~IconvPair();
|
||||
|
||||
iconv_t FromUtf8;
|
||||
iconv_t ToUtf8;
|
||||
};
|
||||
using ENCODING_TOKEN = IconvPair*;
|
||||
constexpr const ENCODING_TOKEN ENCODING_TOKEN_DEFAULT = nullptr;
|
||||
|
||||
#endif
|
||||
|
@ -9,7 +9,7 @@ namespace LibCmo::VxMath {
|
||||
m_hFile(NULL), m_hFileMapping(NULL), m_hFileMapView(NULL),
|
||||
m_dwFileSizeLow(0), m_dwFileSizeHigh(0),
|
||||
#else
|
||||
#error NO IMPLEMENTATION FOR LINUX MMAP!
|
||||
m_hFile(-1), m_offFileSize(0), m_pFileAddr((void*)-1),
|
||||
#endif
|
||||
m_szFilePath(),
|
||||
m_bIsValid(false), m_pMemoryMappedFileBase(nullptr), m_cbFile(0u) {
|
||||
@ -75,9 +75,43 @@ namespace LibCmo::VxMath {
|
||||
m_cbFile = static_cast<size_t>(static_cast<uint64_t>(m_dwFileSizeLow) | (static_cast<uint64_t>(m_dwFileSizeHigh) << 32));
|
||||
|
||||
#else
|
||||
#error NO IMPLEMENTATION FOR LINUX MMAP!
|
||||
#endif
|
||||
// create file
|
||||
// we do not need provide mode_t, because is served for new created file.
|
||||
// we are opening a existed file.
|
||||
this->m_hFile = open(m_szFilePath.string().c_str(), O_RDONLY);
|
||||
if (m_hFile == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// calculate file size
|
||||
struct stat sb;
|
||||
int err = fstat(m_hFile, &sb);
|
||||
// if failed or not a regular file, exit
|
||||
if (err == -1 || (sb.st_mode & S_IFMT) != S_IFREG) {
|
||||
close(m_hFile);
|
||||
return;
|
||||
}
|
||||
// setup size
|
||||
this->m_offFileSize = sb.st_size;
|
||||
|
||||
// map file
|
||||
this->m_pFileAddr = mmap(
|
||||
nullptr, // request linux distribute one
|
||||
this->m_offFileSize, // map the full file
|
||||
PROT_READ, // only for reading
|
||||
MAP_PRIVATE,
|
||||
this->m_hFile,
|
||||
0 // no offset
|
||||
);
|
||||
if (this->m_pFileAddr == MAP_FAILED) {
|
||||
close(m_hFile);
|
||||
return;
|
||||
}
|
||||
|
||||
// write member data
|
||||
m_pMemoryMappedFileBase = m_pFileAddr;
|
||||
m_cbFile = static_cast<size_t>(this->m_offFileSize);
|
||||
#endif
|
||||
|
||||
// set valid
|
||||
this->m_bIsValid = true;
|
||||
@ -95,7 +129,8 @@ namespace LibCmo::VxMath {
|
||||
CloseHandle(m_hFileMapping);
|
||||
CloseHandle(m_hFile);
|
||||
#else
|
||||
#error NO IMPLEMENTATION FOR LINUX MMAP!
|
||||
munmap(this->m_pFileAddr, this->m_offFileSize);
|
||||
close(m_hFile);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,10 @@
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
@ -15,15 +19,15 @@ namespace LibCmo::VxMath {
|
||||
class VxMemoryMappedFile {
|
||||
private:
|
||||
|
||||
|
||||
#if defined(LIBCMO_OS_WIN32)
|
||||
HANDLE m_hFile;
|
||||
DWORD m_dwFileSizeLow, m_dwFileSizeHigh;
|
||||
HANDLE m_hFileMapping;
|
||||
LPVOID m_hFileMapView;
|
||||
#else
|
||||
|
||||
#error NO IMPLEMENTATION FOR LINUX MMAP!
|
||||
int m_hFile;
|
||||
off_t m_offFileSize;
|
||||
void* m_pFileAddr;
|
||||
#endif
|
||||
|
||||
std::filesystem::path m_szFilePath;
|
||||
|
Reference in New Issue
Block a user