diff --git a/include/of_str.h b/include/of_str.h index fba8eb0..0be43eb 100644 --- a/include/of_str.h +++ b/include/of_str.h @@ -15,10 +15,18 @@ public: static ofString replace(const ofString& str, const ofString& from, const ofString& to); static std::vector split(const ofString& input, const ofString& delimiter); static ofString trim(const ofString& input); + /// @brief 获取一个添加当前日期后缀的字符串(到秒,如 some.txt => some_20150115151221.txt)。 - /// @param name - /// @return + /// @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 diff --git a/src/of_path.cpp b/src/of_path.cpp index c25a969..00a556b 100644 --- a/src/of_path.cpp +++ b/src/of_path.cpp @@ -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 diff --git a/src/of_str.cpp b/src/of_str.cpp index 63e6fb5..b36d15a 100644 --- a/src/of_str.cpp +++ b/src/of_str.cpp @@ -4,6 +4,7 @@ #include #include #include +#include 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 diff --git a/src/of_util.cpp b/src/of_util.cpp index 19d26ec..d96a8fa 100644 --- a/src/of_util.cpp +++ b/src/of_util.cpp @@ -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 {