fix:修正UNICODE下编译
This commit is contained in:
parent
71948320b7
commit
cf634e27da
@ -15,10 +15,18 @@ public:
|
|||||||
static ofString replace(const ofString& str, const ofString& from, const ofString& to);
|
static ofString replace(const ofString& str, const ofString& from, const ofString& to);
|
||||||
static std::vector<ofString> split(const ofString& input, const ofString& delimiter);
|
static std::vector<ofString> split(const ofString& input, const ofString& delimiter);
|
||||||
static ofString trim(const ofString& input);
|
static ofString trim(const ofString& input);
|
||||||
|
|
||||||
/// @brief 获取一个添加当前日期后缀的字符串(到秒,如 some.txt => some_20150115151221.txt)。
|
/// @brief 获取一个添加当前日期后缀的字符串(到秒,如 some.txt => some_20150115151221.txt)。
|
||||||
/// @param name
|
/// @param name
|
||||||
/// @return
|
/// @return
|
||||||
static ofString get_ofile_name(const ofString& name);
|
static ofString get_ofile_name(const ofString& name);
|
||||||
|
|
||||||
|
static std::string u8_convert(const std::wstring& source);
|
||||||
|
|
||||||
|
/// @brief
|
||||||
|
/// @param source 需要内容为UTF-8格式。
|
||||||
|
/// @return
|
||||||
|
static std::wstring u8_convert(const std::string& source);
|
||||||
};
|
};
|
||||||
}; // namespace ofen
|
}; // namespace ofen
|
||||||
|
|
||||||
|
@ -63,15 +63,26 @@ ofString COfPath::get_full_path()
|
|||||||
return ofT("");
|
return ofT("");
|
||||||
}
|
}
|
||||||
return ofString(buffer.data());
|
return ofString(buffer.data());
|
||||||
|
#else
|
||||||
|
#if defined(UNICODE_OFSTR)
|
||||||
|
char buffer[PATH_MAX];
|
||||||
|
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
|
||||||
|
if (len == -1) {
|
||||||
|
return ofT("");
|
||||||
|
}
|
||||||
|
buffer[len] = ofT('\0');
|
||||||
|
std::wstring wpath = std::wstring(buffer, buffer + len);
|
||||||
|
return ofString(wpath);
|
||||||
#else
|
#else
|
||||||
ofChar buffer[PATH_MAX];
|
ofChar buffer[PATH_MAX];
|
||||||
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
|
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
|
||||||
if (len == -1) {
|
if (len == -1) {
|
||||||
return ofT("");
|
return ofT("");
|
||||||
}
|
}
|
||||||
buffer[len] = ofT('\0'); // 确保字符串以null终止
|
buffer[len] = ofT('\0');
|
||||||
return ofString(buffer);
|
return ofString(buffer);
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
ofString COfPath::get_home()
|
ofString COfPath::get_home()
|
||||||
@ -106,9 +117,13 @@ ofString COfPath::get_home()
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
ofChar* homedir = getenv("HOME");
|
char* homedir = getenv("HOME");
|
||||||
if (homedir) {
|
if (homedir) {
|
||||||
|
#ifdef UNICODE_OFSTR
|
||||||
|
return COfStr::u8_convert(std::string(homedir));
|
||||||
|
#else
|
||||||
return ofString(homedir);
|
return ofString(homedir);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
return ofT("");
|
return ofT("");
|
||||||
#endif
|
#endif
|
||||||
@ -152,7 +167,12 @@ bool COfPath::write(const ofString& path, const char* data, int len)
|
|||||||
{
|
{
|
||||||
FILE* f = nullptr;
|
FILE* f = nullptr;
|
||||||
#ifdef UNICODE_OFSTR
|
#ifdef UNICODE_OFSTR
|
||||||
|
#ifdef _WIN32
|
||||||
f = _wfopen(path.c_str(), ofT("wb"));
|
f = _wfopen(path.c_str(), ofT("wb"));
|
||||||
|
#else
|
||||||
|
std::string tp = COfStr::u8_convert(path);
|
||||||
|
f = fopen(tp.c_str(), "wb");
|
||||||
|
#endif
|
||||||
#else
|
#else
|
||||||
f = fopen(path.c_str(), ofT("wb"));
|
f = fopen(path.c_str(), ofT("wb"));
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utf8.h>
|
||||||
|
|
||||||
namespace ofen {
|
namespace ofen {
|
||||||
COfStr::COfStr()
|
COfStr::COfStr()
|
||||||
@ -68,4 +69,24 @@ ofString COfStr::get_ofile_name(const ofString& name)
|
|||||||
ofString extension = name.substr(dot_pos);
|
ofString extension = name.substr(dot_pos);
|
||||||
return base_name + ofT("_") + timestamp + extension;
|
return base_name + ofT("_") + timestamp + extension;
|
||||||
}
|
}
|
||||||
|
std::string COfStr::u8_convert(const std::wstring& source)
|
||||||
|
{
|
||||||
|
std::string utf8_result;
|
||||||
|
try {
|
||||||
|
utf8::utf16to8(source.begin(), source.end(), std::back_inserter(utf8_result));
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return utf8_result;
|
||||||
|
}
|
||||||
|
std::wstring COfStr::u8_convert(const std::string& source)
|
||||||
|
{
|
||||||
|
std::wstring wide_result;
|
||||||
|
try {
|
||||||
|
utf8::utf8to16(source.begin(), source.end(), std::back_inserter(wide_result));
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
return L"";
|
||||||
|
}
|
||||||
|
return wide_result;
|
||||||
|
}
|
||||||
} // namespace ofen
|
} // namespace ofen
|
||||||
|
@ -99,7 +99,12 @@ ofString OfUtil::get_file_size(long long bytes)
|
|||||||
|
|
||||||
long long kb = bytes / 1024;
|
long long kb = bytes / 1024;
|
||||||
|
|
||||||
|
#if defined(UNICODE_OFSTR)
|
||||||
|
std::wostringstream oss;
|
||||||
|
#else
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (gb > 0) {
|
if (gb > 0) {
|
||||||
oss << gb << ofT("G_");
|
oss << gb << ofT("G_");
|
||||||
}
|
}
|
||||||
@ -218,10 +223,11 @@ void CThreadSleep::contiune()
|
|||||||
}
|
}
|
||||||
void CThreadSleep::set_timeout(int milsec)
|
void CThreadSleep::set_timeout(int milsec)
|
||||||
{
|
{
|
||||||
if (milsec <= 0)
|
if (milsec <= 0) {
|
||||||
timeout_ = default_timeout_;
|
timeout_ = default_timeout_;
|
||||||
else
|
} else {
|
||||||
timeout_ = milsec;
|
timeout_ = milsec;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
bool CThreadSleep::get_status() const
|
bool CThreadSleep::get_status() const
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user