From ac0923f38620d448b484c3170a50c8f8be20992f Mon Sep 17 00:00:00 2001 From: luoliangyi <87842688@qq.com> Date: Wed, 29 Mar 2023 09:35:08 +0800 Subject: [PATCH] =?UTF-8?q?HGIni=E6=A8=A1=E5=9D=97=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=88=A0=E9=99=A4Section=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/scanner/device_user.cpp | 2 - build/windows/HGBase/HGBase.def | 3 + modules/base/HGIni.cpp | 382 ++++++++++++++------------ modules/base/HGIni.h | 12 + modules/saneui/dialog_device_scan.h | 2 +- modules/saneui/dialog_device_select.h | 2 +- 6 files changed, 216 insertions(+), 187 deletions(-) diff --git a/app/scanner/device_user.cpp b/app/scanner/device_user.cpp index 41bfc7e9..6779947d 100644 --- a/app/scanner/device_user.cpp +++ b/app/scanner/device_user.cpp @@ -1,6 +1,4 @@ #include "device_user.h" -#include "dialog_device_select.h" -#include "dialog_device_scan.h" #include #ifdef HG_CMP_MSC diff --git a/build/windows/HGBase/HGBase.def b/build/windows/HGBase/HGBase.def index 08b0484f..d2d1de4b 100644 --- a/build/windows/HGBase/HGBase.def +++ b/build/windows/HGBase/HGBase.def @@ -80,6 +80,9 @@ HGBase_StandardiseFileName HGBase_SetProfileInt HGBase_SetProfileString +HGBase_RemoveProfileSection +HGBase_RenameProfileSection +HGBase_RemoveProfileConfig HGBase_GetProfileInt HGBase_GetProfileString diff --git a/modules/base/HGIni.cpp b/modules/base/HGIni.cpp index a98da2eb..e33e1872 100644 --- a/modules/base/HGIni.cpp +++ b/modules/base/HGIni.cpp @@ -4,80 +4,116 @@ #include #include -static HGUInt IniWriteValue(const char* section, const char* key, const char* val, const char* file) -{ - typedef std::vector > KeyList; - typedef std::vector > SectionList; +typedef std::vector > KeyList; +typedef std::vector > SectionList; - SectionList sectList; +static HGResult LoadIni(const char* file, SectionList& sectList) +{ + sectList.clear(); 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 }; - if (NULL == fgets(lineContent, 256, fp)) - { - continue; - } + continue; + } - if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n')) - { - continue; - } + if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n')) + { + 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 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] == '=') { - lineContent[i] = '\0'; + pos = i; break; } } - if (lineContent[0] == '[') + if (NULL != pCurKeyList) { - 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) + std::pair pr; + if (-1 != pos) { - if (lineContent[i] == '=') - { - pos = i; - break; - } + pr.first.assign(lineContent, pos); + pr.second.assign(lineContent + pos + 1); + } + else + { + pr.first = lineContent; } - 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); - } + 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; 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); } - 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); + return SaveIni(file, sectList); } 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; } - char str[8]; - sprintf(str, "%d", value); - return writeStringValue(appName, keyName, str, fileName); + 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; } - 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 > KeyList; - typedef std::vector > SectionList; + 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); +} + +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 @@ -195,76 +282,12 @@ static HGResult IniReadValue(const char* section, const char* key, char* val, co 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 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); - } - else + SectionList sectList; + HGResult ret = LoadIni(file, sectList); + if (HGBASE_ERR_OK != ret) { strcpy(val, def); - return HGBASE_ERR_ACCESSDENIED; + return ret; } bool bGetVal = false; @@ -294,47 +317,40 @@ static HGResult IniReadValue(const char* section, const char* key, char* val, co 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) + if (NULL == fileName || NULL == appName || NULL == keyName || NULL == value) { return HGBASE_ERR_INVALIDARG; } - char strRet[256] = { 0 }; + char strValue[256] = { 0 }; char strDef[32]; 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) { *value = def; return ret; } - *value = atoi(strRet); + *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 == appName || NULL == keyName || NULL == value) + if (NULL == fileName || NULL == appName || NULL == keyName + || NULL == def || NULL == value || 0 == maxLen) { 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); } diff --git a/modules/base/HGIni.h b/modules/base/HGIni.h index 82cc2c8e..66ac98b2 100644 --- a/modules/base/HGIni.h +++ b/modules/base/HGIni.h @@ -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, 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文件的值 */ HGEXPORT HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName, diff --git a/modules/saneui/dialog_device_scan.h b/modules/saneui/dialog_device_scan.h index f0b5d33c..ed9d27e7 100644 --- a/modules/saneui/dialog_device_scan.h +++ b/modules/saneui/dialog_device_scan.h @@ -2,7 +2,7 @@ #define DIALOG_DEVICE_SCAN_H #include "base/HGDef.h" -#include "sane.h" +#include "sane/sane_ex.h" #include namespace Ui { diff --git a/modules/saneui/dialog_device_select.h b/modules/saneui/dialog_device_select.h index 776afd90..58faab61 100644 --- a/modules/saneui/dialog_device_select.h +++ b/modules/saneui/dialog_device_select.h @@ -2,7 +2,7 @@ #define DIALOG_DEVICE_SELECT_H #include "base/HGDef.h" -#include "sane.h" +#include "sane/sane_ex.h" #include namespace Ui {