C + + Open Source Library (i)----libconfig detailed

Source: Internet
Author: User
Tags parse error

Bloggers born with egg-ache disease, write Bo not easy, reprint annotated source http://www.cnblogs.com/liboBlog/, thank you!

In writing the program is an essential part of the Conf file parsing, but if you parse the words will be more cumbersome, because there are too many things to notice, such as: whether this conf file include other files, or in Nginx there are many key the same project, We should explain how to parse the different file types, such as different parsing methods and so on some tedious problems. We would like to have a "well-done" library to package for me, personally think Libconfig is such a very good library.

1. First we introduce the following libconfig,libconfig is a library for structured file reads, operations, writes, support for c,c++ two languages, the operating environment can be Linux,max OS, and also from support with vs2005 and later versions of Windows. The following is a detailed explanation of the Liconfig method.

2.setting and setting Type:

    • Setting: First we explain what a setting, the basic unit contained in the Libconfig file is setting, a simple understanding of setting is a set of key-value pairs, which records the properties of the Configure key and the value values.
    • Setting Value: Above we say setting value, incredibly libconfig so much, so libconfig support some what type of value?
      • quantifiable data: Includes int , 64bit int , float , double , The Boolean , string types. The
      • array type: is an array of quantifiable data, and of course their values must be equal.
      • Group Type: Unlike the above mentioned array, the basic composition of the group type is seting , which is a very common phenomenon in the configuration file, such as: We parse an XML file , <CHINA> <STATE> <name>libo </NAME> </STATE> </CHINA> such a nested pattern.
      • list: Contains a set of arbitrary types of data. For example, parse the structure: List = (("ABC", 123, True), 1.234, (/* An empty list */));
    • Setting says: Setting has a set of unique identifiers separated by a dot delimiter , and each name is a setting. If setting is an element of an array without name, you can use the subscript of the array to represent his name, for example:application.books.[1].title。
    • Name specification: The name in setting is strictly case-sensitive and can only consist of letters, numbers, dashes-, underscores _, asterisks *, and must begin with a letter and an asterisk . Feel naming specification a little bit of an egg ache, isn't it? So if we use the Libconfig to parse the URL, we should pay attention to, such as/usr/local/such a naming rule is wrong.
    • The parse rule for value: above we say the type of value allowed, then how is it parsed? if "" resolves to a string, if it is a simple integer, it resolves to int or int_64, and if present, the separation resolves to an array. This is the general rule.
    • Setting form: name=value or name:value Of course here we are writing c,c++, of course, will choose to use =.
    • The form of array and list in value: ARRAY=[VALUE1,VALU2,......] List (value1,value2,....)
    • The form of int in value: Int has two representations, a: a number consisting of 0-9 B: 16 binary numbers that start with the ox and of course the number is preceded by- + .
    • The form of int-64 in value: Due to version differences, there are two representations: A: Add an lafter a number, for example, 0L means a 64-bit 0 B:1.5 version will be automatically recognized, without adding l
    • The representation of a floating-point number in value: composed of numbers and. symbols, or E, with the same optional parameter +-
    • comments in text: A variety of forms, not only support the annotation in C + + method also supports the script annotation,/* Comment *//comment #注释
    • include: We mentioned above that Libconfig supports other file include in the Conf file, so here's the syntax: @include "filename"

3. After the above study we have almost understand the rules of the Conf file, below we look at the C + + API, note the official C and C + + API, here we mainly learn C + +, there is a need to jump value Libconfig official website to view. This part of the study we mainly through the Libconfig class to understand Libconfig.

    • Exception: As long as the program will be an exception, the need for type resolution of the program needs to pay considerable attention to the exception of the situation, libconfig the exception of the encapsulation is in place, below we for different anomalies to understand the Libconfig exception:
      • Value type Exception: We have already mentioned the type of value support and some parsing methods, and if you find that value exceeds the allowable type when parsing, it will throw settingtypeexception.
      • Setting not found exception: when we are extracting the data, if we give an incorrect setting name, we will throw the settingnotfoundexception.
      • Setting repeat exception: as we have mentioned above, setting is uniquely identified by a set of name, and settingnameexception exception occurs when we cannot find the unique setting by the combination of name.
      • Read Stream exception: throws a ParseException exception when a parse error occurs while reading a configuration from a stream .  
      • Of course, in addition to the above exception type, we should also focus on I/O exceptions because I am involved in I/O.
    • Conf class: Let's talk about the operation of the Conf class and related classes.
      • Constructor: Conf ()
      • Read and write:void read (File * stream);  void Write (File * stream); As you can see, there are two kinds of operations that are mentioned here, and we are not only able to read Conf files, but also write them. Notice that the exception is thrown. There are also readfile/writefile methods.
      • Set read/write mode: Method void setoptions (int options), allow the operation.
          • Optionautoconvert(0x01): Automatic value acquisition, note: This parsing method causes parsing errors, such as parsing int to float, which is not used by default.
          • Optionsemicolonseparators(0x02): semicolon-delimited, mainly when the configuration is written to add: This setting is optional, but is used by default.
          • Optioncolonassignmentforgroups(0x04): Intra-group Key/value delimiter, if not set between key and value =, if Open is ': ', mainly at the time of writing, by default is used.
          • Optioncolonassignmentfornongroups(0x08): Non-intra-group Key/value delimiter, if not set between key and value =, if Open is ': ', mainly at the time of writing, Default is not adopted.
          • Optionopenbraceonseparateline(0x10): If you add ' {' To the beginning of the line when writing, if not set, it will be added at the end of the previous row. Adopted by default.
      • Find Properties: Method: Setting & Lookup (const std::string &path) This method returns setting by entering path. Boollookupvalue (std::string path,bool $value) is also provided to determine if the value is present in the path directory.

Now believe that you have a certain understanding of libconfig, if you want to know more can see the official website, if you want to know how to use can download Libconfig, there are many example. Thank you!

C + + Open Source Library (i)----libconfig detailed

Related Article

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.