2022-05-30 11:01:23 +00:00
|
|
|
#include "HGIni.h"
|
2022-05-31 08:17:29 +00:00
|
|
|
#include "HGInc.h"
|
2022-05-30 11:01:23 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
static int IniWriteValue(const char* section, const char* key, const char* val, const char* file)
|
|
|
|
{
|
|
|
|
typedef std::vector<std::pair<std::string, std::string> > KeyList;
|
|
|
|
typedef std::vector<std::pair<std::string, KeyList> > SectionList;
|
|
|
|
|
|
|
|
SectionList sectList;
|
|
|
|
|
|
|
|
FILE* fp = fopen(file, "r");
|
|
|
|
if (fp != NULL)
|
|
|
|
{
|
|
|
|
KeyList* pCurKeyList = NULL;
|
|
|
|
|
|
|
|
while (feof(fp) == 0)
|
|
|
|
{
|
|
|
|
char lineContent[256] = { 0 };
|
|
|
|
if (NULL == fgets(lineContent, 256, fp))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < strlen(lineContent); ++i)
|
|
|
|
{
|
|
|
|
if (lineContent[i] == '\r' || lineContent[i] == '\n')
|
|
|
|
{
|
|
|
|
lineContent[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (lineContent[i] == '=')
|
|
|
|
{
|
|
|
|
pos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != pCurKeyList)
|
|
|
|
{
|
|
|
|
std::pair<std::string, std::string> pr;
|
|
|
|
if (-1 != pos)
|
|
|
|
{
|
|
|
|
pr.first.assign(lineContent, pos);
|
|
|
|
pr.second.assign(lineContent + pos + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pr.first = lineContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
pCurKeyList->push_back(pr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(file, "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int writeStringValue(const char* section, const char* key, const char* val, const char* file)
|
|
|
|
{
|
|
|
|
if (section == NULL || key == NULL || val == NULL || file == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char sect[256];
|
|
|
|
sprintf(sect, "[%s]", section);
|
|
|
|
return IniWriteValue(sect, key, val, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName,
|
|
|
|
const HGChar* keyName, HGInt value)
|
|
|
|
{
|
|
|
|
if (NULL == fileName || NULL == appName || NULL == keyName)
|
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
char str[8];
|
|
|
|
sprintf(str, "%d", value);
|
|
|
|
if (0 != writeStringValue(appName, keyName, str, fileName))
|
|
|
|
return HGBASE_ERR_FAIL;
|
|
|
|
return HGBASE_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName,
|
|
|
|
const HGChar* keyName, const HGChar* value)
|
|
|
|
{
|
|
|
|
if (NULL == fileName || NULL == appName || NULL == keyName)
|
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != writeStringValue(appName, keyName, value, fileName))
|
|
|
|
return HGBASE_ERR_FAIL;
|
|
|
|
return HGBASE_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int IniReadValue(const char* section, const char* key, char* val, const char* def, const char* file)
|
|
|
|
{
|
|
|
|
typedef std::vector<std::pair<std::string, std::string> > KeyList;
|
|
|
|
typedef std::vector<std::pair<std::string, KeyList> > SectionList;
|
|
|
|
|
|
|
|
SectionList sectList;
|
|
|
|
|
|
|
|
FILE* fp = fopen(file, "r");
|
|
|
|
if (fp != NULL)
|
|
|
|
{
|
|
|
|
KeyList* pCurKeyList = NULL;
|
|
|
|
|
|
|
|
while (feof(fp) == 0)
|
|
|
|
{
|
|
|
|
char lineContent[256] = { 0 };
|
|
|
|
if (NULL == fgets(lineContent, 256, fp))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < strlen(lineContent); ++i)
|
|
|
|
{
|
|
|
|
if (lineContent[i] == '\r' || lineContent[i] == '\n')
|
|
|
|
{
|
|
|
|
lineContent[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (lineContent[i] == '=')
|
|
|
|
{
|
|
|
|
pos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != pCurKeyList)
|
|
|
|
{
|
|
|
|
std::pair<std::string, std::string> pr;
|
|
|
|
if (-1 != pos)
|
|
|
|
{
|
|
|
|
pr.first.assign(lineContent, pos);
|
|
|
|
pr.second.assign(lineContent + pos + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pr.first = lineContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
pCurKeyList->push_back(pr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(val, def);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int readStringValue(const char* section, const char* key, char* val, const char* def, const char* file)
|
|
|
|
{
|
|
|
|
if (section == NULL || key == NULL || val == NULL || def == NULL || file == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char sect[256];
|
|
|
|
sprintf(sect, "[%s]", section);
|
|
|
|
return IniReadValue(sect, key, val, def, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName,
|
|
|
|
const HGChar* keyName, HGInt def, HGInt* value)
|
|
|
|
{
|
|
|
|
if (NULL == appName || NULL == keyName || NULL == value)
|
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
char strRet[256] = { 0 };
|
|
|
|
char strDef[32];
|
|
|
|
sprintf(strDef, "%d", def);
|
|
|
|
readStringValue(appName, keyName, strRet, strDef, fileName);
|
|
|
|
*value = atoi(strRet);
|
|
|
|
return HGBASE_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName,
|
|
|
|
const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen)
|
|
|
|
{
|
|
|
|
if (NULL == appName || NULL == keyName || NULL == value)
|
|
|
|
{
|
|
|
|
return HGBASE_ERR_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
readStringValue(appName, keyName, value, def, fileName);
|
|
|
|
return HGBASE_ERR_OK;
|
2022-05-31 08:17:29 +00:00
|
|
|
}
|