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 21 #include <list> 22 #include <memory> 23 #include <string> 24 25 // The default section name to use if a key/value pair is not defined within 26 // a section. 27 #define CONFIG_DEFAULT_SECTION "Global" 28 29 struct entry_t { 30 std::string key; 31 std::string value; 32 }; 33 34 struct section_t { 35 std::string name; 36 std::list<entry_t> entries; 37 void Set(std::string key, std::string value); 38 std::list<entry_t>::iterator Find(const std::string& key); 39 bool Has(const std::string& key); 40 }; 41 42 struct config_t { 43 std::list<section_t> sections; 44 std::list<section_t>::iterator Find(const std::string& section); 45 bool Has(const std::string& section); 46 }; 47 48 // Creates a new config object with no entries (i.e. not backed by a file). 49 // This function returns a unique pointer to config object. 50 std::unique_ptr<config_t> config_new_empty(void); 51 52 // Loads the specified file and returns a handle to the config file. If there 53 // was a problem loading the file, this function returns 54 // NULL. |filename| must not be NULL and must point to a readable 55 // file on the filesystem. 56 std::unique_ptr<config_t> config_new(const char* filename); 57 58 // Read the checksum from the |filename| 59 std::string checksum_read(const char* filename); 60 61 // Clones |src|, including all of it's sections, keys, and values. 62 // Returns a new config which is a copy and separated from the original; 63 // changes to the new config are not reflected in any way in the original. 64 // 65 // This function will not return NULL. 66 std::unique_ptr<config_t> config_new_clone(const config_t& src); 67 68 // Returns true if the config file contains a section named |section|. If 69 // the section has no key/value pairs in it, this function will return false. 70 bool config_has_section(const config_t& config, const std::string& section); 71 72 // Returns true if the config file has a key named |key| under |section|. 73 // Returns false otherwise. 74 bool config_has_key(const config_t& config, const std::string& section, const std::string& key); 75 76 // Returns the integral value for a given |key| in |section|. If |section| 77 // or |key| do not exist, or the value cannot be fully converted to an integer, 78 // this function returns |def_value|. 79 int config_get_int(const config_t& config, const std::string& section, const std::string& key, 80 int def_value); 81 82 // Returns the uint64_t value for a given |key| in |section|. If |section| 83 // or |key| do not exist, or the value cannot be fully converted to an integer, 84 // this function returns |def_value|. 85 uint64_t config_get_uint64(const config_t& config, const std::string& section, 86 const std::string& key, uint64_t def_value); 87 88 // Returns the boolean value for a given |key| in |section|. If |section| 89 // or |key| do not exist, or the value cannot be converted to a boolean, this 90 // function returns |def_value|. 91 bool config_get_bool(const config_t& config, const std::string& section, const std::string& key, 92 bool def_value); 93 94 // Returns the string value for a given |key| in |section|. If |section| or 95 // |key| do not exist, this function returns |def_value|. The returned string 96 // is owned by the config module and must not be freed or modified. |def_value| 97 // may be NULL. 98 const std::string* config_get_string(const config_t& config, const std::string& section, 99 const std::string& key, const std::string* def_value); 100 101 // Sets an integral value for the |key| in |section|. If |key| or |section| do 102 // not already exist, this function creates them. |config| must not be NULL. 103 void config_set_int(config_t* config, const std::string& section, const std::string& key, 104 int value); 105 106 // Sets a uint64_t value for the |key| in |section|. If |key| or |section| do 107 // not already exist, this function creates them. |config| must not be NULL. 108 void config_set_uint64(config_t* config, const std::string& section, const std::string& key, 109 uint64_t value); 110 111 // Sets a boolean value for the |key| in |section|. If |key| or |section| do 112 // not already exist, this function creates them. |config| must not be NULL. 113 void config_set_bool(config_t* config, const std::string& section, const std::string& key, 114 bool value); 115 116 // Sets a string value for the |key| in |section|. If |key| or |section| do 117 // not already exist, this function creates them. |config| must not be NULL. 118 void config_set_string(config_t* config, const std::string& section, const std::string& key, 119 const std::string& value); 120 121 // Removes |section| from the |config| (and, as a result, all keys in the 122 // section). 123 // Returns true if |section| was found and removed from |config|, false 124 // otherwise. 125 // |config| may be NULL. 126 bool config_remove_section(config_t* config, const std::string& section); 127 128 // Removes one specific |key| residing in |section| of the |config|. Returns 129 // true 130 // if the section and key were found and the key was removed, false otherwise. 131 // |config|may not be NULL. 132 bool config_remove_key(config_t* config, const std::string& section, const std::string& key); 133 134 // Saves |config| to a file given by |filename|. Note that this could be a 135 // destructive operation: if |filename| already exists, it will be overwritten. 136 // The config module does not preserve comments or formatting so if a config 137 // file was opened with |config_new| and subsequently overwritten with 138 // |config_save|, all comments and special formatting in the original file will 139 // be lost. Neither |config| nor |filename| may be NULL. 140 bool config_save(const config_t& config, const std::string& filename); 141 142 // Saves the encrypted |checksum| of config file to a given |filename| Note 143 // that this could be a destructive operation: if |filename| already exists, 144 // it will be overwritten. 145 bool checksum_save(const std::string& checksum, const std::string& filename); 146