1 /* 2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef __OSI_CONFIG_H__ 16 #define __OSI_CONFIG_H__ 17 18 // This module implements a configuration parser. Clients can query the 19 // contents of a configuration file through the interface provided here. 20 // The current implementation is read-only; mutations are only kept in 21 // memory. This parser supports the INI file format. 22 23 // Implementation notes: 24 // - Key/value pairs that are not within a section are assumed to be under 25 // the |CONFIG_DEFAULT_SECTION| section. 26 // - Multiple sections with the same name will be merged as if they were in 27 // a single section. 28 // - Empty sections with no key/value pairs will be treated as if they do 29 // not exist. In other words, |nvrec_config_has_section| will return false for 30 // empty sections. 31 // - Duplicate keys in a section will overwrite previous values. 32 33 #include <stdbool.h> 34 #include "list_ext.h" 35 // The default section name to use if a key/value pair is not defined within 36 // a section. 37 #define CONFIG_DEFAULT_SECTION "Global" 38 typedef struct { 39 char *key; 40 char *value; 41 } nvrec_entry_t; 42 43 typedef struct { 44 char *name; 45 list_t *entries; 46 } nvrec_section_t; 47 48 typedef struct nvrec_config { 49 list_t *sections; 50 }nvrec_config_t; 51 52 // Loads the specified file and returns a handle to the config file. If there 53 // was a problem loading the file or allocating memory, this function returns 54 // NULL. Clients must call |nvrec_config_free| on the returned handle when it is no 55 // longer required. |filename| must not be NULL and must point to a readable 56 // file on the filesystem. 57 nvrec_config_t *nvrec_config_new(const char *filename); 58 59 // Frees resources associated with the config file. No further operations may 60 // be performed on the |config| object after calling this function. |config| 61 // may be NULL. 62 void nvrec_config_free(nvrec_config_t *config); 63 64 // Returns true if the config file contains a section named |section|. If 65 // the section has no key/value pairs in it, this function will return false. 66 // |config| and |section| must not be NULL. 67 bool nvrec_config_has_section(const nvrec_config_t *config, const char *section); 68 69 // Returns true if the config file has a key named |key| under |section|. 70 // Returns false otherwise. |config|, |section|, and |key| must not be NULL. 71 bool nvrec_config_has_key(const nvrec_config_t *config, const char *section, const char *key); 72 73 // Returns the integral value for a given |key| in |section|. If |section| 74 // or |key| do not exist, or the value cannot be fully converted to an integer, 75 // this function returns |def_value|. |config|, |section|, and |key| must not 76 // be NULL. 77 int nvrec_config_get_int(const nvrec_config_t *config, const char *section, const char *key, int def_value); 78 79 // Returns the boolean value for a given |key| in |section|. If |section| 80 // or |key| do not exist, or the value cannot be converted to a boolean, this 81 // function returns |def_value|. |config|, |section|, and |key| must not be NULL. 82 bool nvrec_config_get_bool(const nvrec_config_t *config, const char *section, const char *key, bool def_value); 83 84 // Returns the string value for a given |key| in |section|. If |section| or 85 // |key| do not exist, this function returns |def_value|. The returned string 86 // is owned by the config module and must not be freed. |config|, |section|, 87 // and |key| must not be NULL. |def_value| may be NULL. 88 const char *nvrec_config_get_string(const nvrec_config_t *config, const char *section, const char *key, const char *def_value); 89 90 // Sets an integral value for the |key| in |section|. If |key| or |section| do 91 // not already exist, this function creates them. |config|, |section|, and |key| 92 // must not be NULL. 93 void nvrec_config_set_int(nvrec_config_t *config, const char *section, const char *key, int value); 94 95 // Sets a boolean value for the |key| in |section|. If |key| or |section| do 96 // not already exist, this function creates them. |config|, |section|, and |key| 97 // must not be NULL. 98 void nvrec_config_set_bool(nvrec_config_t *config, const char *section, const char *key, bool value); 99 100 // Sets a string value for the |key| in |section|. If |key| or |section| do 101 // not already exist, this function creates them. |config|, |section|, |key|, and 102 // |value| must not be NULL. 103 void nvrec_config_set_string(nvrec_config_t *config, const char *section, const char *key, const char *value); 104 nvrec_section_t *nvrec_section_find(const nvrec_config_t *config, const char *section); 105 void nvrec_entry_free(void *ptr); 106 #endif 107 108