INI file is a common configuration file. It is composed of simple text and simple structure. INI file will have a different extension, such as ". Ini.", ". Cfg", ". conf" and so on.
INI file format
INI file consists of 3 important parts: parameters (Parameters), segments (sections), and annotations (comments). The format is as follows:
- Segment (sections)
[section]
- Parameters (Parameters)
name=value
- Note (comments)
;comments
Each segment includes a segment name, a comment, and a certain parameter pair, and the segment name is not repeatable. The parameter pairs in the section are ordered and repeatable .
Comments usually begin with a semicolon " ;
", followed by a semicolon, until the end of the line is all annotations. However, there are also many configuration file comments that begin with " #
".
Important Notes
In general, the names of paragraphs and paragraphs cannot be duplicated. The parameter pairs in the segment are ordered and repeatable. Although this is a relatively small number of repetitive parameters, some places are still used. The schema configuration file for the Oceanbase database is as follows.
[Plain]View Plaincopy
- [Sys_table]
- Table_id=1
- max_column_id=11
- Table_type=1
- #rowkey is table name
- Rowkey_is_fixed_length=0
- column_info=table_name,varchar,128
- Column_info=table_id,int
- Column_info=table_type,int
- Column_info=rowkey_len,int
- Column_info=max_column_id,int
INI file does not necessarily have a segment name, a default segment name is usually added to it at this time.
The example has a segment name, a segment name sys_table
, no other segments in the configuration file, sys_table
and multiple parameters in that segment, column_info
each representing sys_table
information about one of the fields. Parameters rowkey_is_fixed_length
are also annotated.
Annotations are an important way to improve the readability of your configuration files. In the process of parsing, it is generally ignored. But if you're going to add a file-saving feature to your parsing library, consider the possibility that a user might annotate each parameter or segment.
Interface design of Inifile
The interface design must ensure sufficient stability and ease of use. The INI profile is usually read at startup, and once the program is started, the configuration file is rarely modified halfway. So a simple parsing library, the primary goal is to resolve the INI file, and then to modify the content in memory, and the memory modified content to save to the INI file.
The complete parsing must include at least the following features:
Open and parse an INI file named FName
int open(const string &fname);
gets the value of the first key of the section segment and assigns the value to
int getValue(const string §ion,const string &key,string &value);
Gets the value of the first key of the section segment, assigns the value to value, and assigns the comment to the comment
int getValue(const string §ion,const string &key,string &value,string &comment);
gets the value of all keys for the section segment and assigns the value to the vector of values
int getValues(const string §ion,const string &key,vector<string> &values);
Gets the value of all key keys for the section segment and assigns the value to the vector of values, assigning annotations to the comments vector
int getValues(const string §ion,const string &key,vector<string> &value,vector<string> &comments);
Get Comments for section
int getSectionComment(const string §ion,string & comment);
Get a list of comment markers
void getCommentFlags(vector<string> &flags);
- set up a list of comment markers
void setCommentFlags(const vector<string> &flags);
If you want to modify the value of the INI file setting in your program and save the modified content to the original INI file or another file, you need to include the following features:
- Save content to the current file
int save();
- save content to a file named FName
int saveas(const string &fname);
- setting values and annotations at the same time
int setValue(const string §ion,const string &key,const string &value,const string &comment="");
- Delete Segment
void deleteSection(const string §ion);
- Remove specific parameters for a specific segment
void deleteKey(const string §ion,const string &key);
- Set up comments for section sections
int setSectionComment(const string §ion,const string & comment);
Features of Inifile
- Support Parsing INI file
- Support for modifying and saving INI files
- Support for setting multiple annotation characters, default to "#" and ";"
- Support for duplicate name of parameter
The Inifile library contains both the INI file parsing and the ability to modify and save the INI file.
If no segment name is specified in the INI file during parsing, an empty string is specified as the segment name, the segment is map
saved, and the parameters in the segment are vector
saved, so the parameter names are supported for repetition.
Inifile in addition to suitable for use in the general background program to read the configuration file, but also to help GUI interface program to provide parameter configuration, modification, preservation and so on.
Use of Inifile
Use very simple to generate an INI file Test.ini
cat > test.ini
[Plain]View Plaincopy
- #this is commit
- ; This is commit
- [COMMON]
- DB = MySQL
- Passwd=root
First specify the header file and namespace and then use the Open function to open the INI file GetValue to get the value of the specified item for the specified segment
[CPP]View Plaincopy
- #include "inifile.h"
- Using namespace Inifile;
- filepath = "Test.ini";
- IniFile INI;
- Ini.open (filepath);
- Gets the value of the specified item for the specified segment
- String db_name = Ini.getvalue ("COMMON","db");
- Set new values and comments
- Ini.setvalue ("TEST","name","root","user name");
- Save to File
- Ini.save ();
Inifile A lightweight INI file parsing library