code_app/modules/base/HGIni.cpp

341 lines
7.0 KiB
C++

#include "HGIni.h"
#include "HGInc.h"
#include "HGInfo.h"
#include <vector>
#include <string>
static HGUInt 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 = &sectList[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)
{
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);
}
}
fclose(fp);
return HGBASE_ERR_OK;
}
static HGResult writeStringValue(const char* section, const char* key, const char* val, const char* file)
{
if (section == NULL || key == NULL || val == NULL || file == NULL)
{
return HGBASE_ERR_INVALIDARG;
}
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);
return writeStringValue(appName, keyName, str, fileName);
}
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;
}
return writeStringValue(appName, keyName, value, fileName);
}
static HGResult 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;
#if defined(HG_CMP_MSC)
if (0 != _access(file, 0))
#else
if (0 != access(file, 0))
#endif
{
strcpy(val, def);
return HGBASE_ERR_FILENOTEXIST;
}
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 = &sectList[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 HGBASE_ERR_ACCESSDENIED;
}
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 HGBASE_ERR_OK;
}
static HGResult 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 HGBASE_ERR_INVALIDARG;
}
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);
HGResult ret = readStringValue(appName, keyName, strRet, strDef, fileName);
if (HGBASE_ERR_OK != ret)
{
*value = def;
return ret;
}
*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;
}
return readStringValue(appName, keyName, value, def, fileName);
}