Inifile A lightweight INI file parsing library

Source: Internet
Author: User

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
    1. [Sys_table]
    2. Table_id=1
    3. max_column_id=11
    4. Table_type=1
    5. #rowkey is table name
    6. Rowkey_is_fixed_length=0
    7. column_info=table_name,varchar,128
    8. Column_info=table_id,int
    9. Column_info=table_type,int
    10. Column_info=rowkey_len,int
    11. 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 &section,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 &section,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 &section,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 &section,const string &key,vector<string> &value,vector<string> &comments);

  • Get Comments for section
    int getSectionComment(const string &section,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 &section,const string &key,const string &value,const string &comment="");
    • Delete Segment
      void deleteSection(const string &section);
    • Remove specific parameters for a specific segment
      void deleteKey(const string &section,const string &key);
    • Set up comments for section sections
      int setSectionComment(const string &section,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
    1. #this is commit
    2. ; This is commit
    3. [COMMON]
    4. DB = MySQL
    5. 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
  1. #include "inifile.h"
  2. Using namespace Inifile;
  3. filepath = "Test.ini";
  4. IniFile INI;
  5. Ini.open (filepath);
  6. Gets the value of the specified item for the specified segment
  7. String db_name = Ini.getvalue ("COMMON","db");
  8. Set new values and comments
  9. Ini.setvalue ("TEST","name","root","user name");
  10. Save to File
  11. Ini.save ();



Inifile A lightweight INI file parsing library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.