Read INI configuration file

preface

. ini file is the abbreviation of Initialization File, i.e. Initialization File. It is the storage format adopted by the system configuration file of windows, which manages all configurations of windows. When developing programs, we often use ini files to configure software information. In particular, we need to display a large amount of information. It is impossible for us to write all strings and other information directly into the program. Putting them in the program will lead to difficult maintenance and messy programs. Therefore, we can directly edit the ini file for information configuration, which is convenient for program maintenance and program development.

I ini file example

[Setting]
UnitSize=1
TotalUnitNum=4096

[Item_0]
ItemUnitOffset=0
ItemUnitNum=8
ItemBitEnable=0
ItemDesc=Byte0-8: Namespace Size(NSZE)

[Item_1]
ItemUnitOffset=8
ItemUnitNum=8
ItemBitEnable=0
ItemDesc=Byte8-15: Namespace Capacity(NCAP)
...
[Item_7]
ItemUnitOffset=28
ItemUnitNum=1
ItemBitEnable=1
ItemDesc=Byte28: End-to-end Data Protection Capability(DPC)
BitItemNum=1,1,1,1,1,3,NULL
BitItemMethod=1,1,1,1,1,0,NULL
BitItem0_1=PI Type 1 supported
BitItem1_1=PI Type 2 Supported
BitItem2_1=PI Type 3 Supported
BitItem3_1=PI is in first 8 Bytes of metadata
BitItem4_1=Protection Information(PI) is in last 8 bytes of metadata
...

II Implementation method under Windows

Under Windows, Microsoft has encapsulated relevant methods for us, namely GetPrivateProfileInt() and GetPrivateProfileString() (winbase.h)

1. Read ini file

GetPrivateProfileInt() get integer:

UINT GetPrivateProfileInt(
  LPCTSTR lpAppName,  // section name
  LPCTSTR lpKeyName,  // key name
  INT nDefault,       // return value if key name not found
  LPCTSTR lpFileName  // initialization file name
);

Note: lpAppName and lpKeyName are not case sensitive. When integer < 0 is obtained, 0 is returned.

GetPrivateProfileString() get string:

DWORD GetPrivateProfileString(
  LPCTSTR lpAppName,        // section name
  LPCTSTR lpKeyName,        // key name
  LPCTSTR lpDefault,        // default string
  LPTSTR lpReturnedString,  // destination buffer
  DWORD nSize,              // size of destination buffer
  LPCTSTR lpFileName        // initialization file name
);

Note: lpAppName and lpKeyName are case insensitive. If lpAppName is NULL, the list of all sections of the ini file will be loaded in the lpReturnedString buffer. If lpKeyName=NULL, the list of all items of the specified section will be loaded in the lpReturnedString buffer.

2. Write ini file

WritePrivateProfileString function. If integer is not written, it can be converted to string and then written.

BOOL WritePrivateProfileString(
  LPCTSTR lpAppName,  // section name
  LPCTSTR lpKeyName,  // key name
  LPCTSTR lpString,   // string to add
  LPCTSTR lpFileName  // initialization file
);

The WritePrivateProfileSection function replaces the keys and values for the specified section in an initialization file. 

BOOL WritePrivateProfileSection(
  LPCTSTR lpAppName,  // section name
  LPCTSTR lpString,   // data
  LPCTSTR lpFileName  // file name
);

III Implementation method under Linux

linux environment has no special api similar to ini under Windows. It needs to be implemented by its own program.

1. Implementation method

char line[10240];
FILE* fp = fopen("xxx.ini", "rt");
while((fgets(line)) != NULL)
{
       // Remove the at the end of \ R \ n line and remove the space at the beginning
        Code slightly
        // If the first character is' [', it will be used as the title column
        if(line[0] == '[')
        {
              // Cut out the title name, and the code is omitted
              const char* sp = substring(line + 1, line.Length - 2)
                    ... Subsequent treatment
        }
        // Skip if comment line
        else if(line[0] == ';')
        {
        }
        else
        {
             // Search for first '='
             char* p = strchr(line, '=');
                // Content line if found
                if(p != NULL)
                {
                     *p ++;
                        char* key = line;
                        char* value = p;
                        ... Subsequent treatment
                }
        }
}
fclose(fp);

IV QT cross platform software implementation

Using QSettings class in Qt to read the configuration file of ini suffix is very simple.

1. QSettings reads ini file

	QString filePath;
    filePath = QCoreApplication::applicationDirPath();
    filePath += "myConfig.ini";
    QSettings *settings = new QSettings(filePath, QSettings::IniFormat);
	QString groupName = QString("Item_%1"). arg(i);
    settings->beginGroup(groupName);
    int unitOffset = settings->value("ItemUnitOffset").toInt();
    int unitNum = settings->value("ItemUnitNum").toInt();
    QString desc = settings->value("ItemDesc").toString();     
    settings->endGroup();   

Please refer to QSettings class for deleting and modifying ini files.

2. The problem of reading INI configuration file with comma by qsettings

When you use qstring desc = settings - > value ("ItemDesc") When toString() gets a string, if there is a comma in the string, you will find that the final value is empty.
Reason: Qt is automatically recognized as an array after being separated by commas.
Solution: so simply use value("ItemDesc") The value obtained by toString() is null. Tostrinlist() must be used to obtain it correctly!

Keywords: Linux Windows Qt ini

Added by stanleyg on Sat, 29 Jan 2022 11:44:54 +0200