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 std::vector<ofString> split(const ofString& input, const ofString& delimiter);
|
||||
static ofString trim(const ofString& input);
|
||||
|
||||
/// @brief 获取一个添加当前日期后缀的字符串(到秒,如 some.txt => some_20150115151221.txt)。
|
||||
/// @param name
|
||||
/// @return
|
||||
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
|
||||
|
||||
|
@ -63,15 +63,26 @@ ofString COfPath::get_full_path()
|
||||
return ofT("");
|
||||
}
|
||||
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
|
||||
ofChar buffer[PATH_MAX];
|
||||
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
|
||||
if (len == -1) {
|
||||
return ofT("");
|
||||
}
|
||||
buffer[len] = ofT('\0'); // 确保字符串以null终止
|
||||
buffer[len] = ofT('\0');
|
||||
return ofString(buffer);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
ofString COfPath::get_home()
|
||||
@ -106,9 +117,13 @@ ofString COfPath::get_home()
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
ofChar* homedir = getenv("HOME");
|
||||
char* homedir = getenv("HOME");
|
||||
if (homedir) {
|
||||
#ifdef UNICODE_OFSTR
|
||||
return COfStr::u8_convert(std::string(homedir));
|
||||
#else
|
||||
return ofString(homedir);
|
||||
#endif
|
||||
}
|
||||
return ofT("");
|
||||
#endif
|
||||
@ -152,7 +167,12 @@ bool COfPath::write(const ofString& path, const char* data, int len)
|
||||
{
|
||||
FILE* f = nullptr;
|
||||
#ifdef UNICODE_OFSTR
|
||||
#ifdef _WIN32
|
||||
f = _wfopen(path.c_str(), ofT("wb"));
|
||||
#else
|
||||
std::string tp = COfStr::u8_convert(path);
|
||||
f = fopen(tp.c_str(), "wb");
|
||||
#endif
|
||||
#else
|
||||
f = fopen(path.c_str(), ofT("wb"));
|
||||
#endif
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utf8.h>
|
||||
|
||||
namespace ofen {
|
||||
COfStr::COfStr()
|
||||
@ -68,4 +69,24 @@ ofString COfStr::get_ofile_name(const ofString& name)
|
||||
ofString extension = name.substr(dot_pos);
|
||||
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
|
||||
|
@ -99,7 +99,12 @@ ofString OfUtil::get_file_size(long long bytes)
|
||||
|
||||
long long kb = bytes / 1024;
|
||||
|
||||
#if defined(UNICODE_OFSTR)
|
||||
std::wostringstream oss;
|
||||
#else
|
||||
std::ostringstream oss;
|
||||
#endif
|
||||
|
||||
if (gb > 0) {
|
||||
oss << gb << ofT("G_");
|
||||
}
|
||||
@ -218,10 +223,11 @@ void CThreadSleep::contiune()
|
||||
}
|
||||
void CThreadSleep::set_timeout(int milsec)
|
||||
{
|
||||
if (milsec <= 0)
|
||||
if (milsec <= 0) {
|
||||
timeout_ = default_timeout_;
|
||||
else
|
||||
} else {
|
||||
timeout_ = milsec;
|
||||
}
|
||||
}
|
||||
bool CThreadSleep::get_status() const
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user