2023-06-27 13:03:57 +00:00
|
|
|
#include "HGIni.h"
|
2022-05-31 08:17:29 +00:00
|
|
|
#include "HGInc.h"
|
2022-11-24 03:28:57 +00:00
|
|
|
#include "HGInfo.h"
|
2022-05-30 11:01:23 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
typedef std::vector<std::pair<std::string, std::string> > KeyList;
|
|
|
|
typedef std::vector<std::pair<std::string, KeyList> > SectionList;
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
static HGResult LoadIni(const char* file, SectionList& sectList)
|
|
|
|
{
|
|
|
|
sectList.clear();
|
2022-05-30 11:01:23 +00:00
|
|
|
|
|
|
|
FILE* fp = fopen(file, "r");
|
2023-03-29 01:35:08 +00:00
|
|
|
if (fp == NULL)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
return HGBASE_ERR_ACCESSDENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyList* pCurKeyList = NULL;
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
while (feof(fp) == 0)
|
|
|
|
{
|
2023-06-27 13:03:57 +00:00
|
|
|
char lineContent[2048] = { 0 };
|
|
|
|
if (NULL == fgets(lineContent, 2048, fp))
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
for (size_t i = 0; i < strlen(lineContent); ++i)
|
|
|
|
{
|
|
|
|
if (lineContent[i] == '\r' || lineContent[i] == '\n')
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
lineContent[i] = '\0';
|
|
|
|
break;
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
2023-03-29 01:35:08 +00:00
|
|
|
}
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
if (lineContent[0] == '[')
|
|
|
|
{
|
|
|
|
std::pair<std::string, KeyList> pr;
|
|
|
|
pr.first = lineContent;
|
|
|
|
sectList.push_back(pr);
|
|
|
|
pCurKeyList = §List[sectList.size() - 1].second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int pos = -1;
|
|
|
|
for (int i = 0; i < (int)strlen(lineContent); ++i)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
if (lineContent[i] == '=')
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
pos = i;
|
2022-05-30 11:01:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
if (NULL != pCurKeyList)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
std::pair<std::string, std::string> pr;
|
|
|
|
if (-1 != pos)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
pr.first.assign(lineContent, pos);
|
|
|
|
pr.second.assign(lineContent + pos + 1);
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
2023-03-29 01:35:08 +00:00
|
|
|
else
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
pr.first = lineContent;
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
2023-03-29 01:35:08 +00:00
|
|
|
|
|
|
|
pCurKeyList->push_back(pr);
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-29 01:35:08 +00:00
|
|
|
}
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
fclose(fp);
|
|
|
|
return HGBASE_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HGResult SaveIni(const char* file, const SectionList& sectList)
|
|
|
|
{
|
|
|
|
FILE* fp = fopen(file, "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
|
|
|
HGBase_WriteInfo(HGBASE_INFOTYPE_ERROR, "IniWriteValue: fopen fail %s errno=%d", file, errno);
|
|
|
|
return HGBASE_ERR_ACCESSDENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sectList.size(); ++i)
|
|
|
|
{
|
|
|
|
fputs(sectList[i].first.c_str(), fp);
|
|
|
|
fputs("\n", fp);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < sectList[i].second.size(); ++j)
|
|
|
|
{
|
|
|
|
fputs(sectList[i].second[j].first.c_str(), fp);
|
|
|
|
fputs("=", fp);
|
|
|
|
fputs(sectList[i].second[j].second.c_str(), fp);
|
|
|
|
fputs("\n", fp);
|
|
|
|
}
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
fclose(fp);
|
|
|
|
return HGBASE_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HGUInt IniWriteValue(const char* section, const char* key, const char* val, const char* file)
|
|
|
|
{
|
|
|
|
SectionList sectList;
|
|
|
|
LoadIni(file, sectList);
|
|
|
|
|
2022-05-30 11:01:23 +00:00
|
|
|
bool bFindSect = false;
|
|
|
|
for (size_t i = 0; i < sectList.size(); ++i)
|
|
|
|
{
|
|
|
|
if (strcmp(sectList[i].first.c_str(), section) == 0)
|
|
|
|
{
|
|
|
|
bool bFindKey = false;
|
|
|
|
for (size_t j = 0; j < sectList[i].second.size(); ++j)
|
|
|
|
{
|
|
|
|
if (strcmp(sectList[i].second[j].first.c_str(), key) == 0)
|
|
|
|
{
|
|
|
|
sectList[i].second[j].second = val;
|
|
|
|
bFindKey = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bFindKey)
|
|
|
|
{
|
|
|
|
std::pair<std::string, std::string> pr;
|
|
|
|
pr.first = key;
|
|
|
|
pr.second = val;
|
|
|
|
sectList[i].second.push_back(pr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bFindSect = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bFindSect)
|
|
|
|
{
|
|
|
|
std::pair<std::string, KeyList> pr;
|
|
|
|
pr.first = section;
|
|
|
|
std::pair<std::string, std::string> pr2;
|
|
|
|
pr2.first = key;
|
|
|
|
pr2.second = val;
|
|
|
|
pr.second.push_back(pr2);
|
|
|
|
sectList.push_back(pr);
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
return SaveIni(file, sectList);
|
|
|
|
}
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName,
|
|
|
|
const HGChar* keyName, HGInt value)
|
|
|
|
{
|
|
|
|
if (NULL == fileName || NULL == appName || NULL == keyName)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
return HGBASE_ERR_INVALIDARG;
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
char strValue[8];
|
|
|
|
sprintf(strValue, "%d", value);
|
|
|
|
char sect[256];
|
|
|
|
sprintf(sect, "[%s]", appName);
|
|
|
|
return IniWriteValue(sect, keyName, strValue, fileName);
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName,
|
|
|
|
const HGChar* keyName, const HGChar* value)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
if (NULL == fileName || NULL == appName || NULL == keyName || NULL == value)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2022-11-24 03:28:57 +00:00
|
|
|
return HGBASE_ERR_INVALIDARG;
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char sect[256];
|
2023-03-29 01:35:08 +00:00
|
|
|
sprintf(sect, "[%s]", appName);
|
|
|
|
return IniWriteValue(sect, keyName, value, fileName);
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
HGResult HGAPI HGBase_RemoveProfileSection(const HGChar* fileName, const HGChar* appName)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
if (NULL == fileName || NULL == appName)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
char sect[256];
|
|
|
|
sprintf(sect, "[%s]", appName);
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
SectionList sectList;
|
|
|
|
LoadIni(fileName, sectList);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sectList.size(); ++i)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
if (strcmp(sectList[i].first.c_str(), sect) == 0)
|
|
|
|
{
|
|
|
|
sectList.erase(sectList.begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
return SaveIni(fileName, sectList);
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
HGResult HGAPI HGBase_RenameProfileSection(const HGChar* fileName, const HGChar* oldAppName, const HGChar* newAppName)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
if (NULL == fileName || NULL == oldAppName || NULL == newAppName)
|
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
char sectOld[256];
|
|
|
|
sprintf(sectOld, "[%s]", oldAppName);
|
|
|
|
char sectNew[256];
|
|
|
|
sprintf(sectNew, "[%s]", newAppName);
|
2022-05-30 11:01:23 +00:00
|
|
|
|
|
|
|
SectionList sectList;
|
2023-03-29 01:35:08 +00:00
|
|
|
LoadIni(fileName, sectList);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sectList.size(); ++i)
|
2022-11-25 10:00:33 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
if (strcmp(sectList[i].first.c_str(), sectOld) == 0)
|
|
|
|
{
|
|
|
|
sectList[i].first = sectNew;
|
|
|
|
break;
|
|
|
|
}
|
2022-11-25 10:00:33 +00:00
|
|
|
}
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
return SaveIni(fileName, sectList);
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult HGAPI HGBase_RemoveProfileConfig(const HGChar* fileName, const HGChar* appName, const HGChar* keyName)
|
|
|
|
{
|
|
|
|
if (NULL == fileName || NULL == appName || NULL == keyName)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
char sect[256];
|
|
|
|
sprintf(sect, "[%s]", appName);
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
SectionList sectList;
|
|
|
|
LoadIni(fileName, sectList);
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
for (size_t i = 0; i < sectList.size(); ++i)
|
|
|
|
{
|
|
|
|
if (strcmp(sectList[i].first.c_str(), sect) == 0)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < sectList[i].second.size(); ++j)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
if (strcmp(sectList[i].second[j].first.c_str(), keyName) == 0)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
sectList[i].second.erase(sectList[i].second.begin() + j);
|
2022-05-30 11:01:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
break;
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
2023-03-29 01:35:08 +00:00
|
|
|
}
|
2022-05-30 11:01:23 +00:00
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
return SaveIni(fileName, sectList);
|
|
|
|
}
|
|
|
|
|
2023-06-26 10:23:09 +00:00
|
|
|
HGResult HGAPI HGBase_SearchProfileConfig(const HGChar* fileName, const HGChar* appName, const HGChar* keyName)
|
|
|
|
{
|
|
|
|
if (NULL == fileName || NULL == appName || NULL == keyName)
|
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult ret = HGBASE_ERR_FAIL;
|
|
|
|
|
|
|
|
char sect[256];
|
|
|
|
sprintf(sect, "[%s]", appName);
|
|
|
|
|
|
|
|
SectionList sectList;
|
|
|
|
LoadIni(fileName, sectList);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sectList.size(); ++i)
|
|
|
|
{
|
|
|
|
if (strcmp(sectList[i].first.c_str(), sect) == 0)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < sectList[i].second.size(); ++j)
|
|
|
|
{
|
|
|
|
if (strcmp(sectList[i].second[j].first.c_str(), keyName) == 0)
|
|
|
|
{
|
|
|
|
ret = HGBASE_ERR_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
static HGResult IniReadValue(const char* section, const char* key, char* val, unsigned int maxLen, const char* def, const char* file)
|
|
|
|
{
|
|
|
|
#if defined(HG_CMP_MSC)
|
|
|
|
if (0 != _access(file, 0))
|
|
|
|
#else
|
|
|
|
if (0 != access(file, 0))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
strcpy(val, def);
|
|
|
|
return HGBASE_ERR_FILENOTEXIST;
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
2023-03-29 01:35:08 +00:00
|
|
|
|
|
|
|
SectionList sectList;
|
|
|
|
HGResult ret = LoadIni(file, sectList);
|
|
|
|
if (HGBASE_ERR_OK != ret)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
|
|
|
strcpy(val, def);
|
2023-03-29 01:35:08 +00:00
|
|
|
return ret;
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool bGetVal = false;
|
|
|
|
for (size_t i = 0; i < sectList.size(); ++i)
|
|
|
|
{
|
|
|
|
if (strcmp(sectList[i].first.c_str(), section) == 0)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < sectList[i].second.size(); ++j)
|
|
|
|
{
|
|
|
|
if (strcmp(sectList[i].second[j].first.c_str(), key) == 0)
|
|
|
|
{
|
|
|
|
strcpy(val, sectList[i].second[j].second.c_str());
|
|
|
|
bGetVal = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bGetVal)
|
|
|
|
{
|
|
|
|
strcpy(val, def);
|
|
|
|
}
|
|
|
|
|
2022-11-24 03:28:57 +00:00
|
|
|
return HGBASE_ERR_OK;
|
2022-05-30 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName,
|
|
|
|
const HGChar* keyName, HGInt def, HGInt* value)
|
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
if (NULL == fileName || NULL == appName || NULL == keyName || NULL == value)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
char strValue[256] = { 0 };
|
2022-05-30 11:01:23 +00:00
|
|
|
char strDef[32];
|
|
|
|
sprintf(strDef, "%d", def);
|
2023-03-29 01:35:08 +00:00
|
|
|
char sect[256];
|
|
|
|
sprintf(sect, "[%s]", appName);
|
|
|
|
HGResult ret = IniReadValue(sect, keyName, strValue, 256, strDef, fileName);
|
2022-11-24 03:28:57 +00:00
|
|
|
if (HGBASE_ERR_OK != ret)
|
|
|
|
{
|
2022-11-24 11:09:59 +00:00
|
|
|
*value = def;
|
2022-11-24 03:28:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
*value = atoi(strValue);
|
2022-05-30 11:01:23 +00:00
|
|
|
return HGBASE_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName,
|
|
|
|
const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen)
|
|
|
|
{
|
2023-03-29 01:35:08 +00:00
|
|
|
if (NULL == fileName || NULL == appName || NULL == keyName
|
|
|
|
|| NULL == def || NULL == value || 0 == maxLen)
|
2022-05-30 11:01:23 +00:00
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:35:08 +00:00
|
|
|
char sect[256];
|
|
|
|
sprintf(sect, "[%s]", appName);
|
|
|
|
return IniReadValue(sect, keyName, value, maxLen, def, fileName);
|
2022-05-31 08:17:29 +00:00
|
|
|
}
|