Qt- ini文件

#ifndef INIFILE_H
#define INIFILE_H
#include 
#include "UpdateSystem/UpdateSystem.h"

class IniFile
{
public:
    IniFile();
    void CreatExampleIniFile(QString path); //创建新文件
    void ReadIniFile(); // 返回数据
    bool CheckIni();
    QSettings *IniInfo;
    void DeleteIni(QString Group_data);
    void AddIni(QStringList data);
    void DeleteINI_Section(QString ObjSection,QString ObjPath);
    void DeleteINI_Key(QString ObjSection, QString ObjKey,QString ObjPath);
};

#endif // INIFILE_H
#include "inifile.h"
#include 
#include 
#include 
QString IniFilePath;

IniFile::IniFile()
{
    IniFilePath = Prj_Path.replace("exe","ini");
    QFile Inifile(IniFilePath);
    if(!Inifile.exists()){ //如果路径下不存在ini文件,新建一个
        if(!Inifile.open(QIODevice::WriteOnly|QIODevice::Text)){ //如果打开不成功
            qDebug()<beginGroup("0");  // 设置当前节名,代表以下的操作都是在这个节中
    IniInfo->setValue( "Product_example",  "1");
    IniInfo->setValue( "ProductType",  "1");
    IniInfo->setValue( "ChipType",  "ST");
    IniInfo->setValue( "Address",  "0x20000010~0x20010000:0x00000000\n0x00805000~0x0080A000:0x00020000");
    IniInfo->endGroup();                   // 结束当前节的操作
}

void IniFile::ReadIniFile()
{
    if(!CheckIni()){ // 校验不通过,备份原来的ini文件,并新建一个例程ini文件
        QMessageBox::about(NULL,"Error","程序目录下的配置文件有可能已损坏!!!\r\n程序将备份已损坏的配置文件,并自动生成一个示例配置文件!!!");
        int BackVersion = 0;
        QString BackName = "ini_back_";
        QString OldIniFilePath = IniFilePath;
        QFile OldIni(OldIniFilePath);
        OldIniFilePath.replace("ini",BackName);
        QString OldIniFileName = OldIniFilePath  + QString::number(BackVersion);
        while(!OldIni.rename(OldIniFileName)){ //备份ini文件
            BackVersion++;
            OldIniFileName = OldIniFilePath  + QString::number(BackVersion);
        }
        QFile NewIni(IniFilePath);
        if(!NewIni.exists()){ //如果路径下不存在ini文件,新建一个
            if(!NewIni.open(QIODevice::WriteOnly|QIODevice::Text)){ //如果打开不成功
                qDebug()<childGroups(); // 获取所有的节点
    for(int i = 0; i < tmp.size(); i++){ // 遍历节点下的所有键
        IniInfo->beginGroup(tmp.at(i));
        if((!IniInfo->contains("Product_example")) || (!IniInfo->contains("ProductType"))
                || (!IniInfo->contains("ChipType")) || (!IniInfo->contains("Address"))){
            ret = false; //如果不包含此四个键
        }
        else{
            if(IniInfo->value("Product_example").toString().isEmpty() || IniInfo->value("ProductType").toString().isEmpty()
                    || IniInfo->value("ChipType").toString().isEmpty() || IniInfo->value("Address").toString().isEmpty()){
                ret = false; //如果包含四个键中某个值不存在
            }
        }
        IniInfo->endGroup();                   // 结束当前节的操作
    }
    return ret;
}

void IniFile::DeleteIni(QString Group_data)
{
    DeleteINI_Section(Group_data, IniFilePath);
    DeleteINI_Key(Group_data, "Product_example", IniFilePath);
    DeleteINI_Key(Group_data, "ProductType", IniFilePath);
    DeleteINI_Key(Group_data, "ChipType", IniFilePath);
    DeleteINI_Key(Group_data, "Address", IniFilePath);
    IniInfo = new QSettings(IniFilePath, QSettings::IniFormat); // 检测ini是否合法
}

void IniFile::AddIni(QStringList data)
{
    IniInfo = new QSettings(IniFilePath, QSettings::IniFormat); // 检测ini是否合法
    QStringList tmp = IniInfo->childGroups();
    IniInfo->beginGroup(QString::number(tmp.size()));  // 设置当前节名,代表以下的操作都是在这个节中。默认0为示例,从1开始编号。
    IniInfo->setValue( "Product_example",  data.at(0));
    IniInfo->setValue( "ProductType",  data.at(1));
    IniInfo->setValue( "ChipType", data.at(2));
    IniInfo->setValue( "Address",  data.at(3));
    IniInfo->endGroup();                   // 结束当前节的操作
}

void IniFile::DeleteINI_Section(QString ObjSection,QString ObjPath)
{

    QString ExportINI;//用来存放导出的ini字符串
    QString ExportCopy;//用来存放导出的ini字符串备份
    QString SectionValue; //存放某一节点所有内容的字符串
    QString SectionValue1; //存放某一节点所有内容的字符串备份
    QString KeyValue; //存放某一键的键值的字符串
    QString FindValue;//想要查找的键值
    QFileInfo CheckFile(ObjPath);

    if (CheckFile.isFile()){ //若目标路径存在,则导入内容
        QFile ReadMyfile(ObjPath);
        ReadMyfile.open(QIODevice::ReadOnly);
        QTextStream infile(&ReadMyfile);
        ExportINI = infile.readAll(); //以文本方式读入路径下的ini文件,存入ExportINI字符串
         ExportINI.replace(QRegExp(" "), "");
         ExportCopy = ExportINI;
        ReadMyfile.close();
    }

    FindValue =  "[" + ObjSection + "]"; //以[]加节点名称用于搜索,防止如哈、哈哈等相似的匹配错误
    int SectionFindStart = ExportINI.indexOf(FindValue);//[节点]为起始点
    ExportINI.toLocal8Bit();//我也不知道这个转换用没有起到作用
    ExportINI.append("[--最后一行用来做标记的一串,写入文件时再删掉--]");
    int SectionFindEnd = ExportINI.indexOf("[", SectionFindStart+1);//下一个节点的"["为结束点
    SectionValue = ExportCopy.mid(SectionFindStart, SectionFindEnd-SectionFindStart);//获取节点内容
    SectionValue1 =  SectionValue;
    if (SectionFindStart != -1){
        ExportINI.replace(SectionValue,"");//替换ini内节的值
    }
    ExportINI.replace("[--最后一行用来做标记的一串,写入文件时再删掉--]", "");
    QFile myfile(ObjPath);
    myfile.open(QIODevice::WriteOnly);
    QTextStream infile(&myfile);
    infile << ExportINI; //相关的覆盖ini文件
    myfile.close();
}

void IniFile::DeleteINI_Key(QString ObjSection, QString ObjKey,QString ObjPath)
{
    QString ExportINI;//用来存放导出的ini字符串
    QString ExportCopy;//用来存放导出的ini字符串备份
    QString SectionValue; //存放某一节点所有内容的字符串
    QString SectionValue1; //存放某一节点所有内容的字符串备份
    QString KeyValue; //存放某一键的键值的字符串
    QString FindValue;//想要查找的键值
    QFileInfo CheckFile(ObjPath);
    if (CheckFile.isFile()){ //若目标路径存在,则导入内容
        QFile ReadMyfile(ObjPath);
        ReadMyfile.open(QIODevice::ReadOnly);
        QTextStream infile(&ReadMyfile);
        ExportINI = infile.readAll(); //以文本方式读入路径下的ini文件,存入ExportINI字符串
        ExportINI.replace(QRegExp(" "), "");
        ExportCopy = ExportINI;
        ReadMyfile.close();
    }
    FindValue =  "[" + ObjSection  + "]"; //以[]加节点名称用于搜索,防止如哈、哈哈等相似的匹配错误
    int SectionFindStart = ExportINI.indexOf(FindValue);//[节点]为起始点
    ExportINI.toLocal8Bit();//我也不知道这个转换用没有起到作用
    ExportINI.append("[--最后一行用来做标记的一串,写入文件时再删掉--]");
    int SectionFindEnd = ExportINI.indexOf("[", SectionFindStart + 1);//下一个节点的"["为结束点
    SectionValue = ExportCopy.mid(SectionFindStart,SectionFindEnd-SectionFindStart);//获取节点内容
    SectionValue1 =  SectionValue;
    int KeyFindStart =SectionValue.indexOf("\r\n"+ ObjKey +"=");//以"\r\n键值="作为条件匹配
    if (SectionFindStart != -1){
        if( KeyFindStart != -1){ //如果搜索到匹配的键值,则替换键值内容
            KeyFindStart =SectionValue.indexOf("=",KeyFindStart);//以等号作为起始点
            int KeyFindEnd = SectionValue.indexOf("\r\n", KeyFindStart);//以等号后的换行符作为结束点
            QString KeyValue = SectionValue.mid(KeyFindStart,KeyFindEnd - KeyFindStart-1);//截取键值
            KeyValue.replace(QRegExp("\r"), "");//替换掉换行符
            KeyValue.replace(QRegExp("\n"), "");//替换掉换行符
            SectionValue.replace(ObjKey+"="+KeyValue,"");//替换节内键值
            ExportINI.replace(SectionValue1,SectionValue);//替换ini内节的值
        }
    }
    ExportINI.replace("[--最后一行用来做标记的一串,写入文件时再删掉--]", "");
    QFile myfile(ObjPath);
    myfile.open(QIODevice::WriteOnly);
    QTextStream infile(&myfile);
    infile << ExportINI; //相关的覆盖ini文件
    myfile.close();
}

 

你可能感兴趣的:(Qt工程)