HGIni模块增加删除Section的功能

This commit is contained in:
luoliangyi 2023-03-29 09:35:08 +08:00
parent 529f01d9b2
commit ac0923f386
6 changed files with 216 additions and 187 deletions

View File

@ -1,6 +1,4 @@
#include "device_user.h" #include "device_user.h"
#include "dialog_device_select.h"
#include "dialog_device_scan.h"
#include <QMessageBox> #include <QMessageBox>
#ifdef HG_CMP_MSC #ifdef HG_CMP_MSC

View File

@ -80,6 +80,9 @@ HGBase_StandardiseFileName
HGBase_SetProfileInt HGBase_SetProfileInt
HGBase_SetProfileString HGBase_SetProfileString
HGBase_RemoveProfileSection
HGBase_RenameProfileSection
HGBase_RemoveProfileConfig
HGBase_GetProfileInt HGBase_GetProfileInt
HGBase_GetProfileString HGBase_GetProfileString

View File

@ -4,80 +4,116 @@
#include <vector> #include <vector>
#include <string> #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;
typedef std::vector<std::pair<std::string, std::string> > KeyList;
typedef std::vector<std::pair<std::string, KeyList> > SectionList;
SectionList sectList; static HGResult LoadIni(const char* file, SectionList& sectList)
{
sectList.clear();
FILE* fp = fopen(file, "r"); FILE* fp = fopen(file, "r");
if (fp != NULL) if (fp == NULL)
{ {
KeyList* pCurKeyList = NULL; return HGBASE_ERR_ACCESSDENIED;
}
while (feof(fp) == 0) KeyList* pCurKeyList = NULL;
while (feof(fp) == 0)
{
char lineContent[256] = { 0 };
if (NULL == fgets(lineContent, 256, fp))
{ {
char lineContent[256] = { 0 }; continue;
if (NULL == fgets(lineContent, 256, fp)) }
{
continue;
}
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n')) if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))
{ {
continue; continue;
} }
for (size_t i = 0; i < strlen(lineContent); ++i) for (size_t i = 0; i < strlen(lineContent); ++i)
{
if (lineContent[i] == '\r' || lineContent[i] == '\n')
{ {
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] == '=')
{ {
lineContent[i] = '\0'; pos = i;
break; break;
} }
} }
if (lineContent[0] == '[') if (NULL != pCurKeyList)
{ {
std::pair<std::string, KeyList> pr; std::pair<std::string, std::string> pr;
pr.first = lineContent; if (-1 != pos)
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] == '=') pr.first.assign(lineContent, pos);
{ pr.second.assign(lineContent + pos + 1);
pos = i; }
break; else
} {
pr.first = lineContent;
} }
if (NULL != pCurKeyList) pCurKeyList->push_back(pr);
{
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);
} }
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; bool bFindSect = false;
for (size_t i = 0; i < sectList.size(); ++i) for (size_t i = 0; i < sectList.size(); ++i)
{ {
@ -118,41 +154,7 @@ static HGUInt IniWriteValue(const char* section, const char* key, const char* va
sectList.push_back(pr); sectList.push_back(pr);
} }
fp = fopen(file, "w"); return SaveIni(file, sectList);
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, HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName,
@ -163,28 +165,113 @@ HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appNam
return HGBASE_ERR_INVALIDARG; return HGBASE_ERR_INVALIDARG;
} }
char str[8]; char strValue[8];
sprintf(str, "%d", value); sprintf(strValue, "%d", value);
return writeStringValue(appName, keyName, str, fileName); char sect[256];
sprintf(sect, "[%s]", appName);
return IniWriteValue(sect, keyName, strValue, fileName);
} }
HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName, HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* value) 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) if (NULL == fileName || NULL == appName || NULL == keyName)
{ {
return HGBASE_ERR_INVALIDARG; return HGBASE_ERR_INVALIDARG;
} }
return writeStringValue(appName, keyName, value, fileName); char sect[256];
} sprintf(sect, "[%s]", appName);
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; 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);
}
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 defined(HG_CMP_MSC)
if (0 != _access(file, 0)) if (0 != _access(file, 0))
#else #else
@ -195,76 +282,12 @@ static HGResult IniReadValue(const char* section, const char* key, char* val, co
return HGBASE_ERR_FILENOTEXIST; return HGBASE_ERR_FILENOTEXIST;
} }
FILE* fp = fopen(file, "r"); SectionList sectList;
if (fp != NULL) HGResult ret = LoadIni(file, sectList);
{ if (HGBASE_ERR_OK != ret)
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); strcpy(val, def);
return HGBASE_ERR_ACCESSDENIED; return ret;
} }
bool bGetVal = false; bool bGetVal = false;
@ -294,47 +317,40 @@ static HGResult IniReadValue(const char* section, const char* key, char* val, co
return HGBASE_ERR_OK; 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, HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, HGInt def, HGInt* value) const HGChar* keyName, HGInt def, HGInt* value)
{ {
if (NULL == appName || NULL == keyName || NULL == value) if (NULL == fileName || NULL == appName || NULL == keyName || NULL == value)
{ {
return HGBASE_ERR_INVALIDARG; return HGBASE_ERR_INVALIDARG;
} }
char strRet[256] = { 0 }; char strValue[256] = { 0 };
char strDef[32]; char strDef[32];
sprintf(strDef, "%d", def); sprintf(strDef, "%d", def);
HGResult ret = readStringValue(appName, keyName, strRet, strDef, fileName); char sect[256];
sprintf(sect, "[%s]", appName);
HGResult ret = IniReadValue(sect, keyName, strValue, 256, strDef, fileName);
if (HGBASE_ERR_OK != ret) if (HGBASE_ERR_OK != ret)
{ {
*value = def; *value = def;
return ret; return ret;
} }
*value = atoi(strRet); *value = atoi(strValue);
return HGBASE_ERR_OK; return HGBASE_ERR_OK;
} }
HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName, HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen) const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen)
{ {
if (NULL == appName || NULL == keyName || NULL == value) if (NULL == fileName || NULL == appName || NULL == keyName
|| NULL == def || NULL == value || 0 == maxLen)
{ {
return HGBASE_ERR_INVALIDARG; return HGBASE_ERR_INVALIDARG;
} }
return readStringValue(appName, keyName, value, def, fileName); char sect[256];
sprintf(sect, "[%s]", appName);
return IniReadValue(sect, keyName, value, maxLen, def, fileName);
} }

View File

@ -14,6 +14,18 @@ HGEXPORT HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGCha
HGEXPORT HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName, HGEXPORT HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* value); const HGChar* keyName, const HGChar* value);
/* 删除ini文件的section
*/
HGEXPORT HGResult HGAPI HGBase_RemoveProfileSection(const HGChar* fileName, const HGChar* appName);
/* 重命名ini文件的section
*/
HGEXPORT HGResult HGAPI HGBase_RenameProfileSection(const HGChar* fileName, const HGChar* oldAppName, const HGChar* newAppName);
/* 删除ini文件的config
*/
HGEXPORT HGResult HGAPI HGBase_RemoveProfileConfig(const HGChar* fileName, const HGChar* appName, const HGChar* keyName);
/* 获取ini文件的值 /* 获取ini文件的值
*/ */
HGEXPORT HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName, HGEXPORT HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName,

View File

@ -2,7 +2,7 @@
#define DIALOG_DEVICE_SCAN_H #define DIALOG_DEVICE_SCAN_H
#include "base/HGDef.h" #include "base/HGDef.h"
#include "sane.h" #include "sane/sane_ex.h"
#include <QDialog> #include <QDialog>
namespace Ui { namespace Ui {

View File

@ -2,7 +2,7 @@
#define DIALOG_DEVICE_SELECT_H #define DIALOG_DEVICE_SELECT_H
#include "base/HGDef.h" #include "base/HGDef.h"
#include "sane.h" #include "sane/sane_ex.h"
#include <QDialog> #include <QDialog>
namespace Ui { namespace Ui {