- When many desktop software is opened, there are options such as remembering password, and it can also be read without networking. This is because it writes the configuration information locally, reads the local configuration information before opening each time, and then loads the relevant configuration. In this case, it can encapsulate a configuration class, which is convenient for us to reuse later.
- Take a look at the structure of the local configuration information
This is the Windows text, which is a random local configuration information
As you can see, the configuration text structure is mainly composed of [property name] and key = value
Then it is basically composed of. ini as the suffix. Based on this basic information, we can easily write our own configuration text classes
Next is the actual code
Header part:
#include <QFile> #include <QString> #include <QDebug> #include <QMutex> // Profile class class QIniConfig{ public: QIniConfig(); ~QIniConfig(); static QIniConfig* GetInstance(); bool Init(QString file_name); bool ConfigAttributeName(QString attr_name); //Configuration property name bool WriteToConfig(QString key_,QString val_); //Write profile bool IsConfigAttributeName();//Judge whether there is []; thus judge whether the configuration property name has been set QString ReadValue(QString key_,QString attr_=""); //Read the value of key / / if it is not found, it will return "" bool IsConfigStr(QString str_); // Determine whether it is a configuration character QString GetConfigName(QString str_); //Get property name [ii] from configuration name format get ii private: bool m_b_init_file; // Initialize file or not QFile* m_ini_file; //Handle to profile bool m_b_ConfigName; //Configure property name or not static QIniConfig* m_iniConfig; QString m_filename; //Profile name: };
- Implementation code:
#include "QIniConfig.h" QIniConfig::QIniConfig():m_b_init_file(false),m_ini_file(nullptr),m_b_ConfigName(false),m_filename("") { qDebug() << "Init Config"; } QIniConfig* QIniConfig::m_iniConfig = nullptr; QIniConfig* QIniConfig::GetInstance() { if(!m_iniConfig) { m_iniConfig = new QIniConfig(); return m_iniConfig; } return m_iniConfig; } QIniConfig::~QIniConfig() { qDebug() << "Destroy Config"; if(m_ini_file) { m_ini_file->close(); delete m_ini_file; m_ini_file = nullptr; } } bool QIniConfig::Init(QString file_name) { if(file_name.size()<1) { return false; } m_ini_file = new QFile(file_name); if(m_ini_file) { m_filename = file_name; m_b_init_file = true; // Description initialization successful m_ini_file->open(QIODevice::ReadWrite|QIODevice::Append); //Read write operation supported: write back IsConfigAttributeName(); return true; } return false; } // Configuration attribute name [attributeName] form bool QIniConfig::ConfigAttributeName(QString attr_name) { if(attr_name.size()<1) { return false; } if(!m_ini_file) { return false; } QString str_attribute = "[" + attr_name + "]" +"\n"; // Need to add line breaks for easy reading m_ini_file->write(str_attribute.toUtf8()); m_b_ConfigName = true; //Configuration of property name succeeded return true; } bool QIniConfig::WriteToConfig(QString key_, QString val_) { if(!m_ini_file) { return false; } if(!m_b_ConfigName) { return false; } QString str_key_val = key_ +"="+val_+"\n"; m_ini_file->write(str_key_val.toUtf8());//Write in return true; } bool QIniConfig::IsConfigAttributeName() { QFile file_(m_filename); file_.open(QIODevice::ReadOnly); // Read the first line to []; / / mainly judge whether there is [] QByteArray line = file_.readLine(); QString str(line); qDebug() << str; if(str.size() < 1) { m_b_ConfigName = false; } else{ if(str[0]=="[" && str[str.size()-2] == "]") { m_b_ConfigName = true; } } file_.close(); return m_b_ConfigName; } QString QIniConfig::ReadValue(QString key_, QString attr_) { QFile file_(m_filename); file_.open(QIODevice::ReadOnly); bool b_Found_Once = false; if(attr_ == "") { //If it is equal to null, it is easier to deal with. Just search by line directly while(!file_.atEnd()) { QByteArray array_ = file_.readLine(); QString str_(array_); if(str_.contains("=")) { // Conduct judgment processing str_.chop(1);//Remove spaces QStringList str_list = str_.split("="); if(str_list[0] == key_) { qDebug() << "value is " << str_list[1]; file_.close(); return str_list[1]; } } } file_.close(); return ""; } else{ while(!file_.atEnd()) { QByteArray array_ = file_.readLine(); QString str_(array_); // Property name here if(IsConfigStr(str_)) { if(b_Found_Once) { file_.close(); return ""; } QString str_configs = GetConfigName(str_); if(str_configs == attr_) { b_Found_Once = true; qDebug() << "find"; } } // Here is the form of key = value if(b_Found_Once) { if(str_.contains("=")) { // Conduct judgment processing str_.chop(1);//Remove spaces QStringList str_list = str_.split("="); if(str_list[0] == key_) { qDebug() << "value is " << str_list[1]; file_.close(); return str_list[1]; } } } } file_.close(); return ""; } file_.close(); return ""; } bool QIniConfig::IsConfigStr(QString str_) { if(str_.size() < 3) { return false; } if(str_[0]=="[" && str_[str_.size()-2] == "]") { return true; } return false; } QString QIniConfig::GetConfigName(QString str_) { str_.chop(1); QString strss = str_.replace("[",""); QString str_configName = strss.replace("]",""); return str_configName; }
- Since the code basically has comments, you can also talk about its calling methods directly
// Initialization profile: {filename} QIniConfig::GetInstance()->Init(ini_path); // Configuration property name section QIniConfig::GetInstance()->ConfigAttributeName("test"); //Write related configuration information QIniConfig::GetInstance()->WriteToConfig("keys","world"); //Read value QString value_ = QIniConfig::GetInstance()->ReadValue("keys","test");
It is convenient. There are two ways to read the value part: one is global reading, that is, it is not read according to the attribute name; the other is read according to the attribute name. You can call it flexibly for the corresponding use scenarios.