编程知识 cdmana.com

On the format of configuration file

There are several popular configuration file formats , Each has its own advantages . Find the best format for you !

There are thousands of configuration files on the computer . You may never deal directly with most of these documents , But they are scattered in your  /etc  as well as  ~/.config~/.local/usr  In the folder . Some may be in  /var, even to the extent that  /opt  In the folder . If you accidentally opened or changed them , You may have questions : Why do some configuration files look like a certain format , Others look completely different ?

Storage configuration is a very flexible task , Because as long as developers know how their code stores data in files , They can easily write code to extract data as needed . However , The technology industry is very fond of standardized things with detailed documentation , Therefore, over the years, several common formats have emerged to simplify configuration tasks .

Why do we need configuration files

Configuration files are important for modern computing . They enable you to customize the way you interact with your application , Or customize how the application interacts with other programs in the system . With the configuration file , Whenever you start an application , It will have “ memory ”, Record how you like to use the program .

The structure of the configuration file can be very simple , And it's usually really simple . for example , If you want to write an application , The only thing a program needs to know is its user's preferred name , Then its unique configuration file can contain only one word : user name . It looks like this :

Tux

But often applications need to track more than just a piece of information , Therefore, configuration files usually have a key and a value :

NAME='Tux'
SPECIES='Penguin'

Even without programming experience , You can also imagine how the code parses this data . Here are two simple examples , A use  , Another use  . Both examples focus only on including  NAME “ key ” The line of , And return what appears in the equal sign (=) After that “ value ”:

$ awk -F'=' '/NAME/ { print $2; }' myconfig.ini
'Tux'
$ grep NAME fake.txt | cut -d'=' -f2
'Tux'

The same principle applies to any programming language and any configuration file . As long as you have a unified data structure , You can write simple code to extract and parse it when needed .

Select Format

To ensure universal effectiveness , The most important thing about configuration files is that they are consistent and predictable . You would never want to do such a thing : In the name of saving user preferences , Store information in files at will , Then spend a few days Reverse Engineering , To find the random information that eventually appears in the file .

There are several popular configuration file formats , Each format has its own advantages .

INI

INI The file adopts the format of key value pairs :

[example]
name=Tux
style=widgety,fidgety
enabled=1

This simple configuration style is intuitive , As long as you don't choose to use bad key names ( For example, use  unampref  Such a mysterious key name instead of  name) Just fine . These key value pairs are easy to parse and edit .

Except for keys and values ,INI The format can also be divided into   section section. In the following example code ,[example]  and  [demo]  These are the two sections in the configuration file :

[example]
name=Tux
style=widgety,fidgety
enabled=1

[demo]
name=Beastie
fullscreen=1

These configuration statements are a little complicated to parse , Because there are two  name  key . Imagine , A careless programmer queries in this configuration file  name, The result always returns  Beastie, Because this is the document for  name  The last defined value of . When parsing such a file , Developers must be more careful to search for keys in sections , It can be tricky , Depending on the language used to parse the file . However , It is still a very popular format , Most languages have a ready-made library to help programmers parse INI file .

YAML

  It's a structured list , Can contain values or key value pairs :

---
Example:
  Name: 'Tux'
  Style:
   - 'widgety'
    - 'fidgety'
  Enabled: 1

YAML Format is very popular , Part of the reason is that it looks neat . Data should be placed in a specific position relative to its upper data , There are not many other grammars . However , For some people, this feature , It may be a problem in the eyes of others . Many developers are reluctant to use YAML, It is precisely because it attaches great importance to the essence of   non-existent   Things that are . If you are in the YAML Indentation error in ,YAML The parser may treat your file as invalid , Even if it is not considered invalid , The returned data may also be wrong .

Most languages have YAML Parser , And it has good open source YAML linters( Verify the syntax of the application ) To help you make sure YAML Integrity of documents .

JSON

JSON The file is technically YAML Subset , So its data structure is the same , Although its syntax is completely different :

{
  "Example": {
    "Name": [
      "Tux"
    ],
    "Style": [
      "widgety",
      "fidgety"
    ],
    "Enabled": 1
  }
}

JSON stay JavaScript It's popular among programmers , That's not surprising , because JSON Its full name is JavaScript Object symbols JavaScript Object Notation. Because of and Web Development is closely related ,JSON yes Web API Common output formats . Most programming languages have parsing JSON The library of .

XML

XML Use labels as keys , Surround the configuration values :

<example>
  <name>Tux</name>
  <style priority="user">widgety</style>
  <style priority="fallback">fidgety</style>
  <enabled>1</enabled>
</example>

XML Often be Java Programmers use ,Java There is a rich set of XML Parser . although XML Known for being very strict , But it's also very flexible . With a series of specific labels HTML Different ,XML You can invent your own label at will . As long as you always adhere to the same build rules , And have a good library to parse it , You can extract data accurately and easily .

There are some good open source linter Can help you verify XML file , And most programming languages provide for parsing XML The library of .

Binary format

Linux Proud of plain text configuration . The advantage of this is that you can use    And other basic tools to view configuration data , You can even use    To edit the configuration .

however , Some applications use binary format configuration , This means that the data is encoded in an unnatural language format . These files usually require a special application ( It's usually the application they want to configure ) To explain their data . You can't view these files , At least not in any meaningful way , And they cannot be edited outside their host application . Some reasons for choosing binary format are as follows :

  • Speed :  Programmers can use custom symbols to register specific information bits at certain points in the binary configuration file . Extracting data does not involve searching , Because everything is indexed .
  • size :  The text file may become larger , If you select compressed text file , It's actually converting it to binary format . Binary files can be made smaller by coding techniques ( The same is true for text files , But sometimes , Your optimization will obscure the data , So that the file becomes a binary file ).
  • obscure :  Some programmers don't even want people to view their configuration files , So encode them as binary data . This usually only frustrates users , Not a good reason to use binary format .

If it must be configured in binary format , Please use a format that already exists as an open standard , for example  .

Found a valid configuration format

The configuration format helps developers store the data needed by the application , And help users store preferences for how they want the application to operate . For the question of what format should be used , There may be no wrong answer , As long as you think the language you use can support it well . When developing an application , View available formats , Model with some sample data , Review and evaluate the libraries and utilities provided by your programming language , Then choose the format you think is most appropriate .


via: 

author :  Topic selection :  translator :  proofreading :

This paper is written by    Original compilation ,  Honor roll out


版权声明
本文为[Linux China]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002150150618A.html

Scroll to Top