#include "HGIni.h" #include "HGInc.h" #include "HGInfo.h" #include #include typedef std::vector > KeyList; typedef std::vector > SectionList; static HGResult LoadIni(const char* file, SectionList& sectList) { sectList.clear(); FILE* fp = fopen(file, "r"); if (fp == NULL) { return HGBASE_ERR_ACCESSDENIED; } KeyList* pCurKeyList = NULL; while (feof(fp) == 0) { char lineContent[2048] = { 0 }; if (NULL == fgets(lineContent, 2048, 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 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 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); 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); } } 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); 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 pr; pr.first = key; pr.second = val; sectList[i].second.push_back(pr); } bFindSect = true; break; } } if (!bFindSect) { std::pair pr; pr.first = section; std::pair pr2; pr2.first = key; pr2.second = val; pr.second.push_back(pr2); sectList.push_back(pr); } return SaveIni(file, sectList); } 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 strValue[8]; sprintf(strValue, "%d", value); char sect[256]; sprintf(sect, "[%s]", appName); return IniWriteValue(sect, keyName, strValue, fileName); } HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName, const HGChar* keyName, const HGChar* value) { if (NULL == fileName || NULL == appName || NULL == keyName || NULL == value) { return HGBASE_ERR_INVALIDARG; } char sect[256]; sprintf(sect, "[%s]", appName); return IniWriteValue(sect, keyName, value, fileName); } HGResult HGAPI HGBase_RemoveProfileSection(const HGChar* fileName, const HGChar* appName) { if (NULL == fileName || NULL == appName) { return HGBASE_ERR_INVALIDARG; } 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) { sectList.erase(sectList.begin() + i); break; } } return SaveIni(fileName, sectList); } HGResult HGAPI HGBase_RenameProfileSection(const HGChar* fileName, const HGChar* oldAppName, const HGChar* newAppName) { 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); SectionList sectList; LoadIni(fileName, sectList); for (size_t i = 0; i < sectList.size(); ++i) { if (strcmp(sectList[i].first.c_str(), sectOld) == 0) { sectList[i].first = sectNew; break; } } return SaveIni(fileName, sectList); } HGResult HGAPI HGBase_RemoveProfileConfig(const HGChar* fileName, const HGChar* appName, const HGChar* keyName) { if (NULL == fileName || NULL == appName || NULL == keyName) { return HGBASE_ERR_INVALIDARG; } 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) { sectList[i].second.erase(sectList[i].second.begin() + j); break; } } break; } } return SaveIni(fileName, sectList); } 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; } 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; } SectionList sectList; HGResult ret = LoadIni(file, sectList); if (HGBASE_ERR_OK != ret) { strcpy(val, def); return ret; } 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; } HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName, const HGChar* keyName, HGInt def, HGInt* value) { if (NULL == fileName || NULL == appName || NULL == keyName || NULL == value) { return HGBASE_ERR_INVALIDARG; } char strValue[256] = { 0 }; char strDef[32]; sprintf(strDef, "%d", def); char sect[256]; sprintf(sect, "[%s]", appName); HGResult ret = IniReadValue(sect, keyName, strValue, 256, strDef, fileName); if (HGBASE_ERR_OK != ret) { *value = def; return ret; } *value = atoi(strValue); 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 == fileName || NULL == appName || NULL == keyName || NULL == def || NULL == value || 0 == maxLen) { return HGBASE_ERR_INVALIDARG; } char sect[256]; sprintf(sect, "[%s]", appName); return IniReadValue(sect, keyName, value, maxLen, def, fileName); }