#include "HGUtility.h" #include "HGInc.h" #include "HGInfo.h" #include "HGIni.h" #if defined(HG_CMP_MSC) #include #include #else #include "uuid/uuid.h" #endif #include HGResult HGAPI HGBase_GetTmpPath(HGChar* path, HGUInt maxLen) { if (NULL == path || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if !defined(HG_CMP_MSC) char tmpPath[512] = {0}; strcpy(tmpPath, "/tmp/"); #else CHAR tmpPath[MAX_PATH] = { 0 }; DWORD len = GetTempPathA(MAX_PATH, tmpPath); if (0 == len) return HGBASE_ERR_FAIL; if (tmpPath[strlen(tmpPath) - 1] != '\\') strcat(tmpPath, "\\"); #endif if (maxLen < strlen(tmpPath) + 1) return HGBASE_ERR_FAIL; strcpy(path, tmpPath); return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetCurrentDir(HGChar* dir, HGUInt maxLen) { if (NULL == dir || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if !defined(HG_CMP_MSC) char buffer[512] = { 0 }; char* p = getcwd(buffer, 512); if (NULL == p) return HGBASE_ERR_FAIL; if (buffer[strlen(buffer) - 1] != '/') strcat(buffer, "/"); #else CHAR buffer[MAX_PATH] = { 0 }; DWORD len = GetCurrentDirectoryA(MAX_PATH, buffer); if (0 == len) return HGBASE_ERR_FAIL; if (buffer[strlen(buffer) - 1] != '\\') strcat(buffer, "\\"); #endif if (maxLen < strlen(buffer) + 1) return HGBASE_ERR_FAIL; strcpy(dir, buffer); return HGBASE_ERR_OK; } HGResult HGAPI HGBase_SetCurrentDir(const HGChar* dir) { if (NULL == dir) { return HGBASE_ERR_INVALIDARG; } #if !defined(HG_CMP_MSC) if (0 != chdir(dir)) return HGBASE_ERR_FAIL; #else if (!SetCurrentDirectoryA(dir)) return HGBASE_ERR_FAIL; #endif return HGBASE_ERR_OK; } HGResult HGAPI HGBase_CreateDir(const HGChar* dir) { if (NULL == dir) { return HGBASE_ERR_INVALIDARG; } #if !defined(HG_CMP_MSC) bool ret = true; char muldir[512] = { 0 }; strcpy(muldir, dir); for (size_t i = 0; i < strlen(muldir); ++i) { if ('/' == muldir[i]) { muldir[i] = '\0'; if ('\0' != *muldir && 0 != access(muldir, 0)) { if (0 != mkdir(muldir, 0777)) { HGBase_WriteInfo(HGBASE_INFOTYPE_ERROR, "HGBase_CreateDir: mkdir fail, %s errno=%d", dir, errno); ret = false; break; } } muldir[i] = '/'; } } if (ret) { if ('\0' != *muldir && 0 != access(muldir, 0)) { if (0 != mkdir(muldir, 0777)) { HGBase_WriteInfo(HGBASE_INFOTYPE_ERROR, "HGBase_CreateDir: mkdir fail, %s errno=%d", dir, errno); ret = false; } } } if (!ret) return HGBASE_ERR_ACCESSDENIED; #else int ret = SHCreateDirectoryExA(NULL, dir, NULL); if (ERROR_SUCCESS != ret && ERROR_ALREADY_EXISTS != ret) { HGBase_WriteInfo(HGBASE_INFOTYPE_ERROR, "HGBase_CreateDir: SHCreateDirectoryExA fail, %s GetLastError=%u", dir, GetLastError()); return HGBASE_ERR_ACCESSDENIED; } #endif return HGBASE_ERR_OK; } HGResult HGAPI HGBase_DeleteDir(const HGChar* dir) { if (NULL == dir) { return HGBASE_ERR_INVALIDARG; } #if !defined(HG_CMP_MSC) if (0 != rmdir(dir)) return HGBASE_ERR_FAIL; #else if (!RemoveDirectoryA(dir)) return HGBASE_ERR_FAIL; #endif return HGBASE_ERR_OK; } HGResult HGAPI HGBase_DeleteFile(const HGChar* fileName) { if (NULL == fileName) { return HGBASE_ERR_INVALIDARG; } #if !defined(HG_CMP_MSC) if (0 != unlink(fileName)) return HGBASE_ERR_FAIL; #else if (!DeleteFileA(fileName)) return HGBASE_ERR_FAIL; #endif return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetModuleName(HGPointer addr, HGChar* name, HGUInt maxLen) { if (NULL == name || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if !defined(HG_CMP_MSC) if (NULL == addr) { char dir[PATH_MAX] = {0}; if (0 == readlink("/proc/self/exe", dir, PATH_MAX)) return HGBASE_ERR_FAIL; if (maxLen < strlen(dir) + 1) return HGBASE_ERR_FAIL; strcpy(name, dir); } else { Dl_info dlinfo; if (0 == dladdr(addr, &dlinfo)) return HGBASE_ERR_FAIL; if (maxLen < strlen(dlinfo.dli_fname) + 1) return HGBASE_ERR_FAIL; strcpy(name, dlinfo.dli_fname); } #else HMODULE hModule = NULL; GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)addr, &hModule); CHAR moduleName[MAX_PATH] = { 0 }; DWORD len = GetModuleFileNameA(hModule, moduleName, MAX_PATH); if (0 == len || maxLen < strlen(moduleName) + 1) return HGBASE_ERR_FAIL; strcpy(name, moduleName); #endif return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetUuid(HGChar* uuid, HGUInt maxLen) { if (NULL == uuid || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if !defined(HG_CMP_MSC) uuid_t id; uuid_generate(id); char str[128] = {0}; uuid_unparse(id, str); if (maxLen < strlen(str) + 1) return HGBASE_ERR_FAIL; strcpy(uuid, str); #else GUID guid; if (S_OK != CoCreateGuid(&guid)) return HGBASE_ERR_FAIL; char str[128] = {0}; sprintf(str, "%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); if (maxLen < strlen(str) + 1) return HGBASE_ERR_FAIL; strcpy(uuid, str); #endif return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetTmpFileName(const HGChar* suffix, HGChar* fileName, HGUInt maxLen) { if (NULL == fileName || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } HGChar path[256] = { 0 }, uuid[128] = {0}; HGBase_GetTmpPath(path, 256); HGBase_GetUuid(uuid, 128); strcat(path, uuid); if (NULL != suffix && 0 != *suffix) { strcat(path, "."); strcat(path, suffix); } if (maxLen < strlen(path) + 1) return HGBASE_ERR_FAIL; strcpy(fileName, path); return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetConfigPath(HGChar* configPath, HGUInt maxLen) { if (NULL == configPath || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } HGChar moduleName[256]; HGBase_GetModuleName(NULL, moduleName, 256); HGChar modulePath[256]; HGBase_GetFilePath(moduleName, modulePath, 256); strcat(modulePath, "first.cfg"); HGChar dataPath[256] = {0}; HGBase_GetProfileString(modulePath, "constraints", "local_data_path", "", dataPath, 256); if ('\0' != *dataPath) { #if defined(HG_CMP_MSC) if (dataPath[strlen(dataPath) - 1] != '\\') strcat(dataPath, "\\"); strcat(dataPath, "Cfg\\"); #else if (dataPath[strlen(dataPath) - 1] != '/') strcat(dataPath, "/"); strcat(dataPath, "Cfg/"); #endif if (maxLen < strlen(dataPath) + 1) return HGBASE_ERR_FAIL; strcpy(configPath, dataPath); return HGBASE_ERR_OK; } #if defined(HG_CMP_MSC) CHAR cfgPath[MAX_PATH] = { 0 }; BOOL ret = SHGetSpecialFolderPathA(NULL, cfgPath, CSIDL_APPDATA, FALSE); if (!ret) return HGBASE_ERR_FAIL; if (cfgPath[strlen(cfgPath) - 1] != '\\') strcat(cfgPath, "\\"); HGChar procName[64]; HGBase_GetProcessName(procName, 64); strcat(cfgPath, procName); strcat(cfgPath, "\\Cfg\\"); #else char cfgPath[512] = { 0 }; struct passwd* pw = getpwuid(getuid()); strcpy(cfgPath, pw->pw_dir); if (cfgPath[strlen(cfgPath) - 1] != '/') strcat(cfgPath, "/"); strcat(cfgPath, "."); HGChar procName[64]; HGBase_GetProcessName(procName, 64); strcat(cfgPath, procName); strcat(cfgPath, "/Cfg/"); #endif if (maxLen < strlen(cfgPath) + 1) return HGBASE_ERR_FAIL; strcpy(configPath, cfgPath); return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetLogFilePath(HGChar* logFilePath, HGUInt maxLen) { if (NULL == logFilePath || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } HGChar moduleName[256]; HGBase_GetModuleName(NULL, moduleName, 256); HGChar modulePath[256]; HGBase_GetFilePath(moduleName, modulePath, 256); strcat(modulePath, "first.cfg"); HGChar dataPath[256] = { 0 }; HGBase_GetProfileString(modulePath, "constraints", "local_data_path", "", dataPath, 256); if ('\0' != *dataPath) { #if defined(HG_CMP_MSC) if (dataPath[strlen(dataPath) - 1] != '\\') strcat(dataPath, "\\"); strcat(dataPath, "Cfg\\"); #else if (dataPath[strlen(dataPath) - 1] != '/') strcat(dataPath, "/"); strcat(dataPath, "Cfg/"); #endif if (maxLen < strlen(dataPath) + 1) return HGBASE_ERR_FAIL; strcpy(logFilePath, dataPath); return HGBASE_ERR_OK; } #if defined(HG_CMP_MSC) CHAR logPath[MAX_PATH] = { 0 }; BOOL ret = SHGetSpecialFolderPathA(NULL, logPath, CSIDL_APPDATA, FALSE); if (!ret) return HGBASE_ERR_FAIL; if (logPath[strlen(logPath) - 1] != '\\') strcat(logPath, "\\"); HGChar procName[64]; HGBase_GetProcessName(procName, 64); strcat(logPath, procName); strcat(logPath, "\\Log\\"); #else char logPath[512] = { 0 }; struct passwd* pw = getpwuid(getuid()); strcpy(logPath, pw->pw_dir); if (logPath[strlen(logPath) - 1] != '/') strcat(logPath, "/"); strcat(logPath, "."); HGChar procName[64]; HGBase_GetProcessName(procName, 64); strcat(logPath, procName); strcat(logPath, "/Log/"); #endif if (maxLen < strlen(logPath) + 1) return HGBASE_ERR_FAIL; strcpy(logFilePath, logPath); return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetDocumentsPath(HGChar* documentsPath, HGUInt maxLen) { if (NULL == documentsPath || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if defined(HG_CMP_MSC) CHAR docPath[MAX_PATH] = { 0 }; BOOL ret = SHGetSpecialFolderPathA(NULL, docPath, CSIDL_MYDOCUMENTS, FALSE); if (!ret) return HGBASE_ERR_FAIL; if (docPath[strlen(docPath) - 1] != '\\') strcat(docPath, "\\"); #else char docPath[512] = { 0 }; struct passwd* pw = getpwuid(getuid()); strcpy(docPath, pw->pw_dir); if (docPath[strlen(docPath) - 1] != '/') strcat(docPath, "/"); std::string docName; FILE* file = popen("cat $HOME/.config/user-dirs.dirs | grep DOCUMENTS | tail -1 | cut -d '=' -f 2 | sed 's/\"//g'", "r"); if (NULL != file) { char data[1024] = { 0 }; if (NULL != fgets(data, 1024, file)) { char* start = strchr(data, '/'); if (NULL != start && 0 != *(start + 1)) { char* end = strchr(start + 1, '\n'); if (NULL != end) *end = 0; docName = start + 1; } } pclose(file); } if (docName.empty()) docName = "Documents"; strcat(docPath, docName.c_str()); strcat(docPath, "/"); #endif if (maxLen < strlen(docPath) + 1) return HGBASE_ERR_FAIL; strcpy(documentsPath, docPath); return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetProcessName(HGChar* name, HGUInt maxLen) { if (NULL == name || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if defined(HG_CMP_MSC) CHAR moduleName[MAX_PATH] = { 0 }; DWORD len = GetModuleFileNameA(NULL, moduleName, MAX_PATH); if (0 == len) return HGBASE_ERR_FAIL; CStringA strModuleName(moduleName); int pos = strModuleName.ReverseFind('\\'); if (-1 == pos) return HGBASE_ERR_FAIL; strModuleName = strModuleName.Right(strModuleName.GetLength() - pos - 1); pos = strModuleName.ReverseFind('.'); if (-1 != pos) strModuleName = strModuleName.Left(pos); if (maxLen < (HGUInt)strModuleName.GetLength() + 1) return HGBASE_ERR_FAIL; strcpy(name, strModuleName); #else char aucPathBuf[1024] = { 0 }; if (readlink("/proc/self/exe", aucPathBuf, 1024) <= 0) return HGBASE_ERR_FAIL; const char* pcName = strrchr(aucPathBuf, '/'); if (NULL == pcName) return HGBASE_ERR_FAIL; ++pcName; std::string procName(pcName); const char* pcSuffix = strrchr(procName.c_str(), '.'); if (NULL != pcSuffix) procName = procName.substr(0, pcSuffix - procName.c_str()); if (maxLen < (HGUInt)procName.size() + 1) return HGBASE_ERR_FAIL; strcpy(name, procName.c_str()); #endif return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetFilePath(const HGChar* fileName, HGChar* path, HGUInt maxLen) { if (NULL == fileName || NULL == path || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if defined(HG_CMP_MSC) const char* pcName = strrchr(fileName, '\\'); if (NULL == pcName) { pcName = strrchr(fileName, '/'); if (NULL == pcName) return HGBASE_ERR_FAIL; } #else const char* pcName = strrchr(fileName, '/'); if (NULL == pcName) return HGBASE_ERR_FAIL; #endif ++pcName; if (maxLen < (HGUInt)(pcName - fileName + 1)) return HGBASE_ERR_FAIL; memcpy(path, fileName, pcName - fileName); path[pcName - fileName] = 0; return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetFileName(const HGChar* fileName, HGChar* name, HGUInt maxLen) { if (NULL == fileName || NULL == name || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if defined(HG_CMP_MSC) const char* pcName = strrchr(fileName, '\\'); if (NULL == pcName) { pcName = strrchr(fileName, '/'); if (NULL == pcName) return HGBASE_ERR_FAIL; } #else const char* pcName = strrchr(fileName, '/'); if (NULL == pcName) return HGBASE_ERR_FAIL; #endif ++pcName; if (maxLen < strlen(pcName) + 1) return HGBASE_ERR_FAIL; strcpy(name, pcName); return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetFilePrefix(const HGChar* fileName, HGChar* prefix, HGUInt maxLen) { if (NULL == fileName || NULL == prefix || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if defined(HG_CMP_MSC) CStringA strPrefix; CStringA strFileName(fileName); int pos = strFileName.ReverseFind('.'); if (-1 == pos) strPrefix = strFileName; else { CStringA strSuffix = strFileName.Right(strFileName.GetLength() - pos - 1); if (-1 != strSuffix.Find('\\') || -1 != strSuffix.Find('/')) strPrefix = strFileName; else strPrefix = strFileName.Left(pos); } if (maxLen < (HGUInt)strPrefix.GetLength() + 1) return HGBASE_ERR_FAIL; strcpy(prefix, strPrefix); #else std::string strPrefix; const char* pcSuffix = strrchr(fileName, '.'); if (NULL == pcSuffix) strPrefix = fileName; else { if (NULL != strchr(pcSuffix + 1, '/')) strPrefix = fileName; else strPrefix = std::string(fileName, pcSuffix - fileName); } if (maxLen < (HGUInt)strPrefix.size() + 1) return HGBASE_ERR_FAIL; strcpy(prefix, strPrefix.c_str()); #endif return HGBASE_ERR_OK; } HGResult HGAPI HGBase_GetFileSuffix(const HGChar* fileName, HGChar* suffix, HGUInt maxLen) { if (NULL == fileName || NULL == suffix || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } #if defined(HG_CMP_MSC) CStringA strFileName(fileName); int pos = strFileName.ReverseFind('.'); if (-1 == pos) return HGBASE_ERR_FAIL; CStringA strSuffix = strFileName.Right(strFileName.GetLength() - pos - 1); if (-1 != strSuffix.Find('\\') || -1 != strSuffix.Find('/')) return HGBASE_ERR_FAIL; if (maxLen < (HGUInt)strSuffix.GetLength() + 1) return HGBASE_ERR_FAIL; strcpy(suffix, strSuffix); #else const char* pcSuffix = strrchr(fileName, '.'); if (NULL == pcSuffix) return HGBASE_ERR_FAIL; ++pcSuffix; if (NULL != strchr(pcSuffix, '/')) return HGBASE_ERR_FAIL; if (maxLen < (HGUInt)strlen(pcSuffix) + 1) return HGBASE_ERR_FAIL; strcpy(suffix, pcSuffix); #endif return HGBASE_ERR_OK; } HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar* result, HGUInt maxLen) { if (NULL == fileName || NULL == result || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } std::string stdFileName; #if defined(HG_CMP_MSC) bool separator = false; for (const HGChar* p = fileName; *p != 0; ++p) { if (*p == '\\' || *p == '/') { if (!separator) { stdFileName.push_back('\\'); separator = true; } } else { stdFileName.push_back(*p); separator = false; } } #else bool separator = false; for (const HGChar* p = fileName; *p != 0; ++p) { if (*p == '/') { if (!separator) { stdFileName.push_back('/'); separator = true; } } else { stdFileName.push_back(*p); separator = false; } } #endif if (maxLen < (HGUInt)stdFileName.size() + 1) return HGBASE_ERR_FAIL; strcpy(result, stdFileName.c_str()); return HGBASE_ERR_OK; }