199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #ifndef __INCLUDE_RTE_CFGFILE_H__ 699a2dd95SBruce Richardson #define __INCLUDE_RTE_CFGFILE_H__ 799a2dd95SBruce Richardson 899a2dd95SBruce Richardson #include <stddef.h> 999a2dd95SBruce Richardson 1099a2dd95SBruce Richardson #ifdef __cplusplus 1199a2dd95SBruce Richardson extern "C" { 1299a2dd95SBruce Richardson #endif 1399a2dd95SBruce Richardson 1499a2dd95SBruce Richardson /** 1599a2dd95SBruce Richardson * @file 16*d5d13ef9SThomas Monjalon * Configuration File management. 1799a2dd95SBruce Richardson * 18*d5d13ef9SThomas Monjalon * This library allows reading application defined parameters 19*d5d13ef9SThomas Monjalon * from standard format configuration file. 203e4c5be9SThomas Monjalon */ 2199a2dd95SBruce Richardson 2299a2dd95SBruce Richardson #ifndef CFG_NAME_LEN 2399a2dd95SBruce Richardson #define CFG_NAME_LEN 64 2499a2dd95SBruce Richardson #endif 2599a2dd95SBruce Richardson 2699a2dd95SBruce Richardson #ifndef CFG_VALUE_LEN 2799a2dd95SBruce Richardson #define CFG_VALUE_LEN 256 2899a2dd95SBruce Richardson #endif 2999a2dd95SBruce Richardson 3099a2dd95SBruce Richardson /** Configuration file */ 3199a2dd95SBruce Richardson struct rte_cfgfile; 3299a2dd95SBruce Richardson 3399a2dd95SBruce Richardson /** Configuration file entry */ 3499a2dd95SBruce Richardson struct rte_cfgfile_entry { 3599a2dd95SBruce Richardson char name[CFG_NAME_LEN]; /**< Name */ 3699a2dd95SBruce Richardson char value[CFG_VALUE_LEN]; /**< Value */ 3799a2dd95SBruce Richardson }; 3899a2dd95SBruce Richardson 3999a2dd95SBruce Richardson /** Configuration file operation optional arguments */ 4099a2dd95SBruce Richardson struct rte_cfgfile_parameters { 4199a2dd95SBruce Richardson /** Config file comment character; one of '!', '#', '%', ';', '@' */ 4299a2dd95SBruce Richardson char comment_character; 4399a2dd95SBruce Richardson }; 4499a2dd95SBruce Richardson 4599a2dd95SBruce Richardson /**@{ cfgfile load operation flags */ 4699a2dd95SBruce Richardson enum { 4799a2dd95SBruce Richardson /** 4899a2dd95SBruce Richardson * Indicates that the file supports key value entries before the first 4999a2dd95SBruce Richardson * defined section. These entries can be accessed in the "GLOBAL" 5099a2dd95SBruce Richardson * section. 5199a2dd95SBruce Richardson */ 5299a2dd95SBruce Richardson CFG_FLAG_GLOBAL_SECTION = 1, 5399a2dd95SBruce Richardson 5499a2dd95SBruce Richardson /** 5599a2dd95SBruce Richardson * Indicates that file supports key value entries where the value can 5699a2dd95SBruce Richardson * be zero length (e.g., "key="). 5799a2dd95SBruce Richardson */ 5899a2dd95SBruce Richardson CFG_FLAG_EMPTY_VALUES = 2, 5999a2dd95SBruce Richardson }; 6099a2dd95SBruce Richardson /**@} */ 6199a2dd95SBruce Richardson 6299a2dd95SBruce Richardson /** Defines the default comment character used for parsing config files. */ 6399a2dd95SBruce Richardson #define CFG_DEFAULT_COMMENT_CHARACTER ';' 6499a2dd95SBruce Richardson 6599a2dd95SBruce Richardson /** 66*d5d13ef9SThomas Monjalon * Open config file. 6799a2dd95SBruce Richardson * 6899a2dd95SBruce Richardson * @param filename 69*d5d13ef9SThomas Monjalon * Config file name. 7099a2dd95SBruce Richardson * @param flags 71*d5d13ef9SThomas Monjalon * Config file flags. 7299a2dd95SBruce Richardson * @return 73*d5d13ef9SThomas Monjalon * Handle to configuration file on success, NULL otherwise. 7499a2dd95SBruce Richardson */ 7599a2dd95SBruce Richardson struct rte_cfgfile *rte_cfgfile_load(const char *filename, int flags); 7699a2dd95SBruce Richardson 7799a2dd95SBruce Richardson /** 7899a2dd95SBruce Richardson * Open config file with specified optional parameters. 7999a2dd95SBruce Richardson * 8099a2dd95SBruce Richardson * @param filename 8199a2dd95SBruce Richardson * Config file name 8299a2dd95SBruce Richardson * @param flags 8399a2dd95SBruce Richardson * Config file flags 8499a2dd95SBruce Richardson * @param params 8599a2dd95SBruce Richardson * Additional configuration attributes. Must be configured with desired 8699a2dd95SBruce Richardson * values prior to invoking this API. 8799a2dd95SBruce Richardson * @return 8899a2dd95SBruce Richardson * Handle to configuration file on success, NULL otherwise 8999a2dd95SBruce Richardson */ 9099a2dd95SBruce Richardson struct rte_cfgfile *rte_cfgfile_load_with_params(const char *filename, 9199a2dd95SBruce Richardson int flags, const struct rte_cfgfile_parameters *params); 9299a2dd95SBruce Richardson 9399a2dd95SBruce Richardson /** 9499a2dd95SBruce Richardson * Create new cfgfile instance with empty sections and entries 9599a2dd95SBruce Richardson * 9699a2dd95SBruce Richardson * @param flags 9799a2dd95SBruce Richardson * - CFG_FLAG_GLOBAL_SECTION 9899a2dd95SBruce Richardson * Indicates that the file supports key value entries before the first 9999a2dd95SBruce Richardson * defined section. These entries can be accessed in the "GLOBAL" 10099a2dd95SBruce Richardson * section. 10199a2dd95SBruce Richardson * - CFG_FLAG_EMPTY_VALUES 10299a2dd95SBruce Richardson * Indicates that file supports key value entries where the value can 10399a2dd95SBruce Richardson * be zero length (e.g., "key="). 10499a2dd95SBruce Richardson * @return 10599a2dd95SBruce Richardson * Handle to cfgfile instance on success, NULL otherwise 10699a2dd95SBruce Richardson */ 10799a2dd95SBruce Richardson struct rte_cfgfile *rte_cfgfile_create(int flags); 10899a2dd95SBruce Richardson 10999a2dd95SBruce Richardson /** 11099a2dd95SBruce Richardson * Add section in cfgfile instance. 11199a2dd95SBruce Richardson * 11299a2dd95SBruce Richardson * @param cfg 11399a2dd95SBruce Richardson * Pointer to the cfgfile structure. 11499a2dd95SBruce Richardson * @param sectionname 11599a2dd95SBruce Richardson * Section name which will be add to cfgfile. 11699a2dd95SBruce Richardson * @return 11799a2dd95SBruce Richardson * 0 on success, -ENOMEM if can't add section 11899a2dd95SBruce Richardson */ 11999a2dd95SBruce Richardson int 12099a2dd95SBruce Richardson rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname); 12199a2dd95SBruce Richardson 12299a2dd95SBruce Richardson /** 12399a2dd95SBruce Richardson * Add entry to specified section in cfgfile instance. 12499a2dd95SBruce Richardson * 12599a2dd95SBruce Richardson * @param cfg 12699a2dd95SBruce Richardson * Pointer to the cfgfile structure. 12799a2dd95SBruce Richardson * @param sectionname 12899a2dd95SBruce Richardson * Given section name to add an entry. 12999a2dd95SBruce Richardson * @param entryname 13099a2dd95SBruce Richardson * Entry name to add. 13199a2dd95SBruce Richardson * @param entryvalue 13299a2dd95SBruce Richardson * Entry value to add. 13399a2dd95SBruce Richardson * @return 13499a2dd95SBruce Richardson * 0 on success, -EEXIST if entry already exist, -EINVAL if bad argument 13599a2dd95SBruce Richardson */ 13699a2dd95SBruce Richardson int rte_cfgfile_add_entry(struct rte_cfgfile *cfg, 13799a2dd95SBruce Richardson const char *sectionname, const char *entryname, 13899a2dd95SBruce Richardson const char *entryvalue); 13999a2dd95SBruce Richardson 14099a2dd95SBruce Richardson /** 14199a2dd95SBruce Richardson * Update value of specified entry name in given section in config file 14299a2dd95SBruce Richardson * 14399a2dd95SBruce Richardson * @param cfg 14499a2dd95SBruce Richardson * Config file 14599a2dd95SBruce Richardson * @param sectionname 14699a2dd95SBruce Richardson * Section name 14799a2dd95SBruce Richardson * @param entryname 14899a2dd95SBruce Richardson * Entry name to look for the value change 14999a2dd95SBruce Richardson * @param entryvalue 15099a2dd95SBruce Richardson * New entry value. Can be also an empty string if CFG_FLAG_EMPTY_VALUES = 1 15199a2dd95SBruce Richardson * @return 15299a2dd95SBruce Richardson * 0 on success, -EINVAL if bad argument 15399a2dd95SBruce Richardson */ 15499a2dd95SBruce Richardson int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname, 15599a2dd95SBruce Richardson const char *entryname, const char *entryvalue); 15699a2dd95SBruce Richardson 15799a2dd95SBruce Richardson /** 15899a2dd95SBruce Richardson * Save object cfgfile to file on disc 15999a2dd95SBruce Richardson * 16099a2dd95SBruce Richardson * @param cfg 16199a2dd95SBruce Richardson * Config file structure 16299a2dd95SBruce Richardson * @param filename 16399a2dd95SBruce Richardson * File name to save data 16499a2dd95SBruce Richardson * @return 16599a2dd95SBruce Richardson * 0 on success, errno otherwise 16699a2dd95SBruce Richardson */ 16799a2dd95SBruce Richardson int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename); 16899a2dd95SBruce Richardson 16999a2dd95SBruce Richardson /** 170*d5d13ef9SThomas Monjalon * Get number of sections in config file. 17199a2dd95SBruce Richardson * 17299a2dd95SBruce Richardson * @param cfg 173*d5d13ef9SThomas Monjalon * Config file. 17499a2dd95SBruce Richardson * @param sec_name 175*d5d13ef9SThomas Monjalon * Section name. 17699a2dd95SBruce Richardson * @param length 177*d5d13ef9SThomas Monjalon * Maximum section name length. 17899a2dd95SBruce Richardson * @return 179*d5d13ef9SThomas Monjalon * Number of sections. 18099a2dd95SBruce Richardson */ 18199a2dd95SBruce Richardson int rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sec_name, 18299a2dd95SBruce Richardson size_t length); 18399a2dd95SBruce Richardson 18499a2dd95SBruce Richardson /** 18599a2dd95SBruce Richardson * Get name of all config file sections. 18699a2dd95SBruce Richardson * 18799a2dd95SBruce Richardson * Fills in the array sections with the name of all the sections in the file 18899a2dd95SBruce Richardson * (up to the number of max_sections sections). 18999a2dd95SBruce Richardson * 19099a2dd95SBruce Richardson * @param cfg 191*d5d13ef9SThomas Monjalon * Config file. 19299a2dd95SBruce Richardson * @param sections 193*d5d13ef9SThomas Monjalon * Array containing section names after successful invocation. 194*d5d13ef9SThomas Monjalon * Each element of this array should be preallocated by the user 195*d5d13ef9SThomas Monjalon * with at least CFG_NAME_LEN characters. 19699a2dd95SBruce Richardson * @param max_sections 197*d5d13ef9SThomas Monjalon * Maximum number of section names to be stored in sections array. 19899a2dd95SBruce Richardson * @return 199*d5d13ef9SThomas Monjalon * Number of populated sections names. 20099a2dd95SBruce Richardson */ 20199a2dd95SBruce Richardson int rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[], 20299a2dd95SBruce Richardson int max_sections); 20399a2dd95SBruce Richardson 20499a2dd95SBruce Richardson /** 205*d5d13ef9SThomas Monjalon * Check if given section exists in config file. 20699a2dd95SBruce Richardson * 20799a2dd95SBruce Richardson * @param cfg 208*d5d13ef9SThomas Monjalon * Config file. 20999a2dd95SBruce Richardson * @param sectionname 210*d5d13ef9SThomas Monjalon * Section name. 21199a2dd95SBruce Richardson * @return 212*d5d13ef9SThomas Monjalon * TRUE (value different than 0) if section exists, FALSE (value 0) otherwise. 21399a2dd95SBruce Richardson */ 21499a2dd95SBruce Richardson int rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname); 21599a2dd95SBruce Richardson 21699a2dd95SBruce Richardson /** 217*d5d13ef9SThomas Monjalon * Get number of entries in given config file section. 21899a2dd95SBruce Richardson * 219*d5d13ef9SThomas Monjalon * If multiple sections have the given name, 220*d5d13ef9SThomas Monjalon * this function operates on the first one. 22199a2dd95SBruce Richardson * 22299a2dd95SBruce Richardson * @param cfg 223*d5d13ef9SThomas Monjalon * Config file. 22499a2dd95SBruce Richardson * @param sectionname 225*d5d13ef9SThomas Monjalon * Section name. 22699a2dd95SBruce Richardson * @return 227*d5d13ef9SThomas Monjalon * Number of entries in section on success, -1 otherwise. 22899a2dd95SBruce Richardson */ 22999a2dd95SBruce Richardson int rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg, 23099a2dd95SBruce Richardson const char *sectionname); 23199a2dd95SBruce Richardson 23299a2dd95SBruce Richardson /** 233*d5d13ef9SThomas Monjalon * Get number of entries in given config file section. 23499a2dd95SBruce Richardson * 235*d5d13ef9SThomas Monjalon * The index of a section is the same as the index of its name 236*d5d13ef9SThomas Monjalon * in the result of rte_cfgfile_sections. 237*d5d13ef9SThomas Monjalon * This API can be used when there are multiple sections with the same name. 23899a2dd95SBruce Richardson * 23999a2dd95SBruce Richardson * @param cfg 240*d5d13ef9SThomas Monjalon * Config file. 24199a2dd95SBruce Richardson * @param sectionname 242*d5d13ef9SThomas Monjalon * Section name. 24399a2dd95SBruce Richardson * @param index 244*d5d13ef9SThomas Monjalon * Section index. 24599a2dd95SBruce Richardson * @return 246*d5d13ef9SThomas Monjalon * Number of entries in section on success, -1 otherwise. 24799a2dd95SBruce Richardson */ 24899a2dd95SBruce Richardson int rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg, 24999a2dd95SBruce Richardson char *sectionname, 25099a2dd95SBruce Richardson int index); 25199a2dd95SBruce Richardson 25299a2dd95SBruce Richardson /** 253*d5d13ef9SThomas Monjalon * Get section entries as key-value pairs. 25499a2dd95SBruce Richardson * 255*d5d13ef9SThomas Monjalon * If multiple sections have the given name, 256*d5d13ef9SThomas Monjalon * this function operates on the first one. 25799a2dd95SBruce Richardson * 25899a2dd95SBruce Richardson * @param cfg 259*d5d13ef9SThomas Monjalon * Config file. 26099a2dd95SBruce Richardson * @param sectionname 261*d5d13ef9SThomas Monjalon * Section name. 26299a2dd95SBruce Richardson * @param entries 26399a2dd95SBruce Richardson * Pre-allocated array of at least max_entries entries where the section 264*d5d13ef9SThomas Monjalon * entries are stored as key-value pair after successful invocation. 26599a2dd95SBruce Richardson * @param max_entries 266*d5d13ef9SThomas Monjalon * Maximum number of section entries to be stored in entries array. 26799a2dd95SBruce Richardson * @return 268*d5d13ef9SThomas Monjalon * Number of entries populated on success, -1 otherwise. 26999a2dd95SBruce Richardson */ 27099a2dd95SBruce Richardson int rte_cfgfile_section_entries(struct rte_cfgfile *cfg, 27199a2dd95SBruce Richardson const char *sectionname, 27299a2dd95SBruce Richardson struct rte_cfgfile_entry *entries, 27399a2dd95SBruce Richardson int max_entries); 27499a2dd95SBruce Richardson 27599a2dd95SBruce Richardson /** 276*d5d13ef9SThomas Monjalon * Get section entries as key-value pairs. 27799a2dd95SBruce Richardson * 278*d5d13ef9SThomas Monjalon * The index of a section is the same as the index of its name 279*d5d13ef9SThomas Monjalon * in the result of rte_cfgfile_sections. 280*d5d13ef9SThomas Monjalon * This API can be used when there are multiple sections with the same name. 28199a2dd95SBruce Richardson * 28299a2dd95SBruce Richardson * @param cfg 283*d5d13ef9SThomas Monjalon * Config file. 28499a2dd95SBruce Richardson * @param index 285*d5d13ef9SThomas Monjalon * Section index. 28699a2dd95SBruce Richardson * @param sectionname 287*d5d13ef9SThomas Monjalon * Pre-allocated string of at least CFG_NAME_LEN characters 288*d5d13ef9SThomas Monjalon * where the section name is stored after successful invocation. 28999a2dd95SBruce Richardson * @param entries 29099a2dd95SBruce Richardson * Pre-allocated array of at least max_entries entries where the section 291*d5d13ef9SThomas Monjalon * entries are stored as key-value pair after successful invocation. 29299a2dd95SBruce Richardson * @param max_entries 293*d5d13ef9SThomas Monjalon * Maximum number of section entries to be stored in entries array. 29499a2dd95SBruce Richardson * @return 295*d5d13ef9SThomas Monjalon * Number of entries populated on success, -1 otherwise. 29699a2dd95SBruce Richardson */ 29799a2dd95SBruce Richardson int rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, 29899a2dd95SBruce Richardson int index, 29999a2dd95SBruce Richardson char *sectionname, 30099a2dd95SBruce Richardson struct rte_cfgfile_entry *entries, 30199a2dd95SBruce Richardson int max_entries); 30299a2dd95SBruce Richardson 30399a2dd95SBruce Richardson /** 304*d5d13ef9SThomas Monjalon * Get value of the named entry in named config file section. 30599a2dd95SBruce Richardson * 306*d5d13ef9SThomas Monjalon * If multiple sections have the given name, 307*d5d13ef9SThomas Monjalon * this function operates on the first one. 30899a2dd95SBruce Richardson * 30999a2dd95SBruce Richardson * @param cfg 310*d5d13ef9SThomas Monjalon * Config file. 31199a2dd95SBruce Richardson * @param sectionname 312*d5d13ef9SThomas Monjalon * Section name. 31399a2dd95SBruce Richardson * @param entryname 314*d5d13ef9SThomas Monjalon * Entry name. 31599a2dd95SBruce Richardson * @return 316*d5d13ef9SThomas Monjalon * Entry value on success, NULL otherwise. 31799a2dd95SBruce Richardson */ 31899a2dd95SBruce Richardson const char *rte_cfgfile_get_entry(struct rte_cfgfile *cfg, 31999a2dd95SBruce Richardson const char *sectionname, 32099a2dd95SBruce Richardson const char *entryname); 32199a2dd95SBruce Richardson 32299a2dd95SBruce Richardson /** 323*d5d13ef9SThomas Monjalon * Check if given entry exists in named config file section. 32499a2dd95SBruce Richardson * 325*d5d13ef9SThomas Monjalon * If multiple sections have the given name, 326*d5d13ef9SThomas Monjalon * this function operates on the first one. 32799a2dd95SBruce Richardson * 32899a2dd95SBruce Richardson * @param cfg 329*d5d13ef9SThomas Monjalon * Config file. 33099a2dd95SBruce Richardson * @param sectionname 331*d5d13ef9SThomas Monjalon * Section name. 33299a2dd95SBruce Richardson * @param entryname 333*d5d13ef9SThomas Monjalon * Entry name. 33499a2dd95SBruce Richardson * @return 335*d5d13ef9SThomas Monjalon * TRUE (value different than 0) if entry exists, FALSE (value 0) otherwise. 33699a2dd95SBruce Richardson */ 33799a2dd95SBruce Richardson int rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname, 33899a2dd95SBruce Richardson const char *entryname); 33999a2dd95SBruce Richardson 34099a2dd95SBruce Richardson /** 341*d5d13ef9SThomas Monjalon * Close config file. 34299a2dd95SBruce Richardson * 34399a2dd95SBruce Richardson * @param cfg 344*d5d13ef9SThomas Monjalon * Config file. 34599a2dd95SBruce Richardson * @return 346*d5d13ef9SThomas Monjalon * 0 on success, -1 otherwise. 34799a2dd95SBruce Richardson */ 34899a2dd95SBruce Richardson int rte_cfgfile_close(struct rte_cfgfile *cfg); 34999a2dd95SBruce Richardson 35099a2dd95SBruce Richardson #ifdef __cplusplus 35199a2dd95SBruce Richardson } 35299a2dd95SBruce Richardson #endif 35399a2dd95SBruce Richardson 35499a2dd95SBruce Richardson #endif 355