diff --git a/include/of_path.h b/include/of_path.h index 85eabed..9249c7f 100644 --- a/include/of_path.h +++ b/include/of_path.h @@ -16,6 +16,10 @@ public: static ofString replaceStr(const ofString& str, const ofString& from, const ofString& to); static ofString getFullRunPath(); static ofString getHome(); + static ofString getConfigDir(const ofString& sub_dir, bool create = false); + static ofString getFull(const ofString& path, const ofString& sub_file_path); + static bool isExist(const ofString& path); + static bool writeBin(const ofString& path, const char* data, int len); }; }; // namespace ofen #endif \ No newline at end of file diff --git a/src/of_path.cpp b/src/of_path.cpp index b37c61b..a23dd34 100644 --- a/src/of_path.cpp +++ b/src/of_path.cpp @@ -1,5 +1,6 @@ #include "of_path.h" #include +#include #ifdef _WIN32 #include @@ -64,7 +65,7 @@ ofString COfPath::getFullRunPath() #elif defined(__clang__) && defined(__APPLE__) uint32_t size = 0; _NSGetExecutablePath(NULL, &size); // 获取路径缓冲区的大小 - std::vector buffer(size); // 创建缓冲区 + std::vector buffer(size); // 创建缓冲区 if (_NSGetExecutablePath(buffer.data(), &size) != 0) { return ofT(""); } @@ -85,17 +86,20 @@ ofString COfPath::getHome() #if defined(_WIN32) ofChar* value = nullptr; std::size_t len = 0; +#ifdef UNICODE_OFSTR + auto err = _wdupenv_s(&value, &len, ofT("USERPROFILE")); +#else auto err = _dupenv_s(&value, &len, "USERPROFILE"); +#endif if (err == 0 && value != nullptr) { ofString ret(value); free(value); return ret; - } - else { + } else { return ofT(""); } #else - ofChar *homedir = getenv("HOME"); + ofChar* homedir = getenv("HOME"); if (homedir) { return ofString(homedir); } @@ -103,4 +107,49 @@ ofString COfPath::getHome() #endif } +ofString COfPath::getConfigDir(const ofString& sub_dir, bool create) +{ + fs::path userHome = fs::path(getHome()); + userHome.append(".config"); + userHome.append(sub_dir); + if (create) { + if (!fs::exists(userHome)) { + fs::create_directories(userHome); + } + } +#ifdef UNICODE_OFSTR + return ofString(userHome.wstring()); +#else + return ofString(userHome.string()); +#endif +} + +ofString COfPath::getFull(const ofString& path, const ofString& sub_file_path) +{ + fs::path p(path); + p.append(sub_file_path); +#ifdef UNICODE_OFSTR + return ofString(p.wstring()); +#else + return ofString(p.string()); +#endif +} + +bool COfPath::isExist(const ofString& path) +{ + fs::path p(path); + return fs::exists(p); +} + +bool COfPath::writeBin(const ofString& path, const char* data, int len) +{ + std::ofstream file(path, std::ios::binary); + if (!file.is_open()) { + return false; + } + file.write(data, len); + file.close(); + return true; +} + } // namespace ofen diff --git a/src/of_win.cpp b/src/of_win.cpp index 569a586..988d6ae 100644 --- a/src/of_win.cpp +++ b/src/of_win.cpp @@ -24,18 +24,18 @@ OfStatus COfWin::appendPathVariable(const ofString& path) return STA_CANOT_OPEN_REG; } DWORD bufferSize = 0; - if (RegQueryValueEx(hKey, "Path", nullptr, nullptr, nullptr, &bufferSize) != ERROR_SUCCESS) { + if (RegQueryValueEx(hKey, ofT("Path"), nullptr, nullptr, nullptr, &bufferSize) != ERROR_SUCCESS) { RegCloseKey(hKey); return STA_CANOT_QUERY_REG; } ofString currentPath(bufferSize, ofT('\0')); - if (RegQueryValueEx(hKey, "Path", nullptr, nullptr, reinterpret_cast(¤tPath[0]), &bufferSize) != ERROR_SUCCESS) { + if (RegQueryValueEx(hKey, ofT("Path"), nullptr, nullptr, reinterpret_cast(¤tPath[0]), &bufferSize) != ERROR_SUCCESS) { RegCloseKey(hKey); return STA_CANOT_QUERY_REG; } currentPath.resize(bufferSize - 1); ofString updatedPath = currentPath + ofT(";") + path; - if (RegSetValueEx(hKey, "Path", 0, REG_EXPAND_SZ, reinterpret_cast(updatedPath.c_str()), updatedPath.size() + 1) != ERROR_SUCCESS) { + if (RegSetValueEx(hKey, ofT("Path"), 0, REG_EXPAND_SZ, reinterpret_cast(updatedPath.c_str()), updatedPath.size() + 1) != ERROR_SUCCESS) { RegCloseKey(hKey); return STA_CANOT_SET_REG; }