code_app/modules/base/HGUtility.cpp

676 lines
15 KiB
C++
Raw Normal View History

2022-05-03 10:25:52 +00:00
#include "HGUtility.h"
#include "HGInc.h"
#include "HGInfo.h"
#include "HGIni.h"
2022-05-03 10:25:52 +00:00
#if defined(HG_CMP_MSC)
#include <shlobj.h>
#include <atlstr.h>
#else
2022-05-23 11:36:52 +00:00
#include "uuid/uuid.h"
2022-05-03 10:25:52 +00:00
#endif
#include <string>
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};
2022-05-23 11:36:52 +00:00
strcpy(tmpPath, "/tmp/");
2022-05-03 10:25:52 +00:00
#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);
2022-05-03 10:25:52 +00:00
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);
2022-05-03 10:25:52 +00:00
ret = false;
}
}
}
if (!ret)
return HGBASE_ERR_ACCESSDENIED;
2022-05-03 10:25:52 +00:00
#else
2022-11-24 10:21:22 +00:00
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;
}
2022-05-03 10:25:52 +00:00
#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);
}
2022-05-03 10:25:52 +00:00
#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)
2022-05-03 10:25:52 +00:00
{
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);
}
2022-05-03 10:25:52 +00:00
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;
}
2022-05-03 10:25:52 +00:00
#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, "\\");
2022-05-17 02:29:40 +00:00
HGChar procName[64];
HGBase_GetProcessName(procName, 64);
strcat(cfgPath, procName);
strcat(cfgPath, "\\Cfg\\");
2022-05-03 10:25:52 +00:00
#else
char cfgPath[512] = { 0 };
struct passwd* pw = getpwuid(getuid());
strcpy(cfgPath, pw->pw_dir);
2022-05-03 10:25:52 +00:00
if (cfgPath[strlen(cfgPath) - 1] != '/')
strcat(cfgPath, "/");
2022-05-17 02:29:40 +00:00
strcat(cfgPath, ".");
HGChar procName[64];
HGBase_GetProcessName(procName, 64);
strcat(cfgPath, procName);
strcat(cfgPath, "/Cfg/");
2022-05-03 10:25:52 +00:00
#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;
}
2022-05-03 10:25:52 +00:00
#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, "\\");
2022-05-17 02:29:40 +00:00
HGChar procName[64];
HGBase_GetProcessName(procName, 64);
strcat(logPath, procName);
strcat(logPath, "\\Log\\");
2022-05-03 10:25:52 +00:00
#else
char logPath[512] = { 0 };
struct passwd* pw = getpwuid(getuid());
strcpy(logPath, pw->pw_dir);
2022-05-03 10:25:52 +00:00
if (logPath[strlen(logPath) - 1] != '/')
strcat(logPath, "/");
2022-05-17 02:29:40 +00:00
strcat(logPath, ".");
HGChar procName[64];
HGBase_GetProcessName(procName, 64);
strcat(logPath, procName);
strcat(logPath, "/Log/");
2022-05-03 10:25:52 +00:00
#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);
2022-05-03 10:25:52 +00:00
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, "/");
2022-05-03 10:25:52 +00:00
#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);
2022-06-11 06:18:33 +00:00
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);
2022-05-03 10:25:52 +00:00
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;
2022-06-11 06:18:33 +00:00
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)
2022-05-03 10:25:52 +00:00
return HGBASE_ERR_FAIL;
2022-06-11 06:18:33 +00:00
strcpy(name, procName.c_str());
2022-05-03 10:25:52 +00:00
#endif
return HGBASE_ERR_OK;
}
2022-06-11 06:18:33 +00:00
HGResult HGAPI HGBase_GetFilePath(const HGChar* fileName, HGChar* path, HGUInt maxLen)
2022-05-03 10:25:52 +00:00
{
2022-06-11 06:18:33 +00:00
if (NULL == fileName || NULL == path || 0 == maxLen)
2022-05-03 10:25:52 +00:00
{
return HGBASE_ERR_INVALIDARG;
}
#if defined(HG_CMP_MSC)
2022-06-11 06:18:33 +00:00
const char* pcName = strrchr(fileName, '\\');
2022-05-03 10:25:52 +00:00
if (NULL == pcName)
{
2022-06-11 06:18:33 +00:00
pcName = strrchr(fileName, '/');
2022-05-03 10:25:52 +00:00
if (NULL == pcName)
return HGBASE_ERR_FAIL;
}
#else
2022-06-11 06:18:33 +00:00
const char* pcName = strrchr(fileName, '/');
2022-05-03 10:25:52 +00:00
if (NULL == pcName)
return HGBASE_ERR_FAIL;
#endif
++pcName;
2022-06-11 06:18:33 +00:00
if (maxLen < (HGUInt)(pcName - fileName + 1))
2022-05-03 10:25:52 +00:00
return HGBASE_ERR_FAIL;
2022-06-11 06:18:33 +00:00
memcpy(path, fileName, pcName - fileName);
path[pcName - fileName] = 0;
2022-05-03 10:25:52 +00:00
return HGBASE_ERR_OK;
}
2022-06-11 06:18:33 +00:00
HGResult HGAPI HGBase_GetFileName(const HGChar* fileName, HGChar* name, HGUInt maxLen)
2022-05-11 09:19:50 +00:00
{
2022-06-11 06:18:33 +00:00
if (NULL == fileName || NULL == name || 0 == maxLen)
2022-05-11 09:19:50 +00:00
{
return HGBASE_ERR_INVALIDARG;
}
#if defined(HG_CMP_MSC)
2022-06-11 06:18:33 +00:00
const char* pcName = strrchr(fileName, '\\');
2022-05-11 09:19:50 +00:00
if (NULL == pcName)
{
2022-06-11 06:18:33 +00:00
pcName = strrchr(fileName, '/');
2022-05-11 09:19:50 +00:00
if (NULL == pcName)
return HGBASE_ERR_FAIL;
}
#else
2022-06-11 06:18:33 +00:00
const char* pcName = strrchr(fileName, '/');
2022-05-11 09:19:50 +00:00
if (NULL == pcName)
return HGBASE_ERR_FAIL;
#endif
++pcName;
2022-06-11 06:18:33 +00:00
if (maxLen < strlen(pcName) + 1)
2022-05-11 09:19:50 +00:00
return HGBASE_ERR_FAIL;
2022-06-11 06:18:33 +00:00
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);
2022-06-11 06:18:33 +00:00
#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());
2022-06-11 06:18:33 +00:00
#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
2022-05-11 09:19:50 +00:00
return HGBASE_ERR_OK;
}
2022-05-03 10:25:52 +00:00
HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar* result, HGUInt maxLen)
{
if (NULL == fileName || NULL == result || 0 == maxLen)
{
return HGBASE_ERR_INVALIDARG;
}
2022-05-17 02:29:40 +00:00
std::string stdFileName;
2022-05-03 10:25:52 +00:00
#if defined(HG_CMP_MSC)
bool separator = false;
for (const HGChar* p = fileName; *p != 0; ++p)
{
if (*p == '\\' || *p == '/')
{
if (!separator)
{
2022-05-17 02:29:40 +00:00
stdFileName.push_back('\\');
2022-05-03 10:25:52 +00:00
separator = true;
}
}
else
{
2022-05-17 02:29:40 +00:00
stdFileName.push_back(*p);
2022-05-03 10:25:52 +00:00
separator = false;
}
}
#else
2022-05-17 02:29:40 +00:00
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;
}
}
2022-05-03 10:25:52 +00:00
#endif
2022-05-17 02:29:40 +00:00
if (maxLen < (HGUInt)stdFileName.size() + 1)
return HGBASE_ERR_FAIL;
strcpy(result, stdFileName.c_str());
2022-05-03 10:25:52 +00:00
return HGBASE_ERR_OK;
}