• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 
3 // This module implements a configuration parser. Clients can query the
4 // contents of a configuration file through the interface provided here.
5 // The current implementation is read-only; mutations are only kept in
6 // memory. This parser supports the INI file format.
7 
8 // Implementation notes:
9 // - Key/value pairs that are not within a section are assumed to be under
10 //   the |CONFIG_DEFAULT_SECTION| section.
11 // - Multiple sections with the same name will be merged as if they were in
12 //   a single section.
13 // - Empty sections with no key/value pairs will be treated as if they do
14 //   not exist. In other words, |config_has_section| will return false for
15 //   empty sections.
16 // - Duplicate keys in a section will overwrite previous values.
17 // - All strings are case sensitive.
18 
19 #include <stdbool.h>
20 #include <list>
21 #include <memory>
22 #include <string>
23 
24 // The default section name to use if a key/value pair is not defined within
25 // a section.
26 #define CONFIG_DEFAULT_SECTION "Global"
27 
28 struct entry_t {
29   std::string key;
30   std::string value;
31 };
32 
33 struct section_t {
34   std::string name;
35   std::list<entry_t> entries;
36 };
37 
38 struct config_t {
39   std::list<section_t> sections;
40 };
41 
42 // Creates a new config object with no entries (i.e. not backed by a file).
43 // This function returns a unique pointer to config object.
44 std::unique_ptr<config_t> config_new_empty(void);
45 
46 // Loads the specified file and returns a handle to the config file. If there
47 // was a problem loading the file, this function returns
48 // NULL. |filename| must not be NULL and must point to a readable
49 // file on the filesystem.
50 std::unique_ptr<config_t> config_new(const char* filename);
51 
52 // Read the checksum from the |filename|
53 std::string checksum_read(const char* filename);
54 
55 // Clones |src|, including all of it's sections, keys, and values.
56 // Returns a new config which is a copy and separated from the original;
57 // changes to the new config are not reflected in any way in the original.
58 //
59 // This function will not return NULL.
60 std::unique_ptr<config_t> config_new_clone(const config_t& src);
61 
62 // Returns true if the config file contains a section named |section|. If
63 // the section has no key/value pairs in it, this function will return false.
64 bool config_has_section(const config_t& config, const std::string& section);
65 
66 // Returns true if the config file has a key named |key| under |section|.
67 // Returns false otherwise.
68 bool config_has_key(const config_t& config, const std::string& section,
69                     const std::string& key);
70 
71 // Returns the integral value for a given |key| in |section|. If |section|
72 // or |key| do not exist, or the value cannot be fully converted to an integer,
73 // this function returns |def_value|.
74 int config_get_int(const config_t& config, const std::string& section,
75                    const std::string& key, int def_value);
76 
77 // Returns the uint64_t value for a given |key| in |section|. If |section|
78 // or |key| do not exist, or the value cannot be fully converted to an integer,
79 // this function returns |def_value|.
80 uint64_t config_get_uint64(const config_t& config, const std::string& section,
81                            const std::string& key, uint64_t def_value);
82 
83 // Returns the boolean value for a given |key| in |section|. If |section|
84 // or |key| do not exist, or the value cannot be converted to a boolean, this
85 // function returns |def_value|.
86 bool config_get_bool(const config_t& config, const std::string& section,
87                      const std::string& key, bool def_value);
88 
89 // Returns the string value for a given |key| in |section|. If |section| or
90 // |key| do not exist, this function returns |def_value|. The returned string
91 // is owned by the config module and must not be freed or modified. |def_value|
92 // may be NULL.
93 const std::string* config_get_string(const config_t& config,
94                                      const std::string& section,
95                                      const std::string& key,
96                                      const std::string* def_value);
97 
98 // Sets an integral value for the |key| in |section|. If |key| or |section| do
99 // not already exist, this function creates them. |config| must not be NULL.
100 void config_set_int(config_t* config, const std::string& section,
101                     const std::string& key, int value);
102 
103 // Sets a uint64_t value for the |key| in |section|. If |key| or |section| do
104 // not already exist, this function creates them. |config| must not be NULL.
105 void config_set_uint64(config_t* config, const std::string& section,
106                        const std::string& key, uint64_t value);
107 
108 // Sets a boolean value for the |key| in |section|. If |key| or |section| do
109 // not already exist, this function creates them. |config| must not be NULL.
110 void config_set_bool(config_t* config, const std::string& section,
111                      const std::string& key, bool value);
112 
113 // Sets a string value for the |key| in |section|. If |key| or |section| do
114 // not already exist, this function creates them. |config| must not be NULL.
115 void config_set_string(config_t* config, const std::string& section,
116                        const std::string& key, const std::string& value);
117 
118 // Removes |section| from the |config| (and, as a result, all keys in the
119 // section).
120 // Returns true if |section| was found and removed from |config|, false
121 // otherwise.
122 // |config| may be NULL.
123 bool config_remove_section(config_t* config, const std::string& section);
124 
125 // Removes one specific |key| residing in |section| of the |config|. Returns
126 // true
127 // if the section and key were found and the key was removed, false otherwise.
128 // |config|may not be NULL.
129 bool config_remove_key(config_t* config, const std::string& section,
130                        const std::string& key);
131 
132 // Saves |config| to a file given by |filename|. Note that this could be a
133 // destructive operation: if |filename| already exists, it will be overwritten.
134 // The config module does not preserve comments or formatting so if a config
135 // file was opened with |config_new| and subsequently overwritten with
136 // |config_save|, all comments and special formatting in the original file will
137 // be lost. Neither |config| nor |filename| may be NULL.
138 bool config_save(const config_t& config, const std::string& filename);
139 
140 // Saves the encrypted |checksum| of config file to a given |filename| Note
141 // that this could be a destructive operation: if |filename| already exists,
142 // it will be overwritten.
143 bool checksum_save(const std::string& checksum, const std::string& filename);
144