00001 /************************************************************************************************** 00002 Software License Agreement (BSD License) 00003 00004 Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt 00005 All rights reserved. 00006 00007 Redistribution and use in source and binary forms, with or without modification, are permitted 00008 provided that the following conditions are met: 00009 00010 *Redistributions of source code must retain the above copyright notice, this list of 00011 conditions and the following disclaimer. 00012 *Redistributions in binary form must reproduce the above copyright notice, this list of 00013 conditions and the following disclaimer in the documentation and/or other materials provided 00014 with the distribution. 00015 *Neither the name of the University of Aveiro nor the names of its contributors may be used to 00016 endorse or promote products derived from this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 00019 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00021 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00024 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00025 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 ***************************************************************************************************/ 00033 #ifndef _CONFIG_H 00034 #define _CONFIG_H 00035 00036 #include <stdio.h> 00037 00038 #ifdef __cplusplus 00039 extern "C" { 00040 #endif 00041 00042 typedef struct _Config Config; 00043 00044 /* Parses a file and returns a handle to the contents. The file should 00045 * already be opened and passed in as f. filename is used merely for 00046 * printing error messages if the need should arise. */ 00047 Config * 00048 config_parse_file (FILE * f, char * filename); 00049 00050 /* Parses the default DGC configuration file (config/master.cfg) and 00051 * returns a handle to it. If the environment variable DGC_CONFIG_PATH 00052 * is set, that path is used instead. */ 00053 Config * 00054 config_parse_default (void); 00055 00063 int config_get_default_src (char *buf, int buflen); 00064 00065 /* Frees a configuration handle */ 00066 void 00067 config_free (Config * conf); 00068 00069 /* Prints the contents of a parsed configuration file. */ 00070 int 00071 config_print (Config * conf); 00072 00076 int config_has_key (Config *conf, const char *key); 00077 00078 /* Return number of top-level keys in container named by containerKey. 00079 * Return -1 if container key not found. 00080 */ 00081 int 00082 config_get_num_subkeys (Config * conf, const char * containerKey); 00083 00084 /* Fetch all top-level keys in container named by containerKey. 00085 * 00086 * Returns a newly allocated, NULL-terminated, array of strings. This array 00087 * should be freed by the caller (e.g. with g_strfreev) 00088 */ 00089 char ** 00090 config_get_subkeys (Config * conf, const char * containerKey); 00091 00092 00093 00094 /* These four functions search for a key (i.e. "sick.front.pos") and fetch 00095 * the value associated with that key into val, converting it to the specified 00096 * type. If the conversion is not possible or the key is not found, an error 00097 * is returned. If the key contains an array of multiple values, the first 00098 * value is used. Return 0 on success, -1 on failure. */ 00099 int 00100 config_get_int (Config * conf, const char * key, int * val); 00101 int 00102 config_get_boolean (Config * conf, const char * key, int * val); 00103 int 00104 config_get_double (Config * conf, const char * key, double * val); 00105 int 00106 config_get_str (Config * conf, const char * key, char ** val); 00107 00108 double config_get_double_or_fail (Config *conf, const char *key); 00109 00110 /* These four functions do the same thing as the previous four, except they 00111 * return the value associated with the key. In case of error, the default 00112 * value def is returned instead. */ 00113 int 00114 config_get_int_or_default (Config * conf, const char * key, int def); 00115 int 00116 config_get_boolean_or_default (Config * conf, const char * key, int def); 00117 double 00118 config_get_double_or_default (Config * conf, const char * key, double def); 00119 char * 00120 config_get_str_or_default (Config * conf, const char * key, char * def); 00121 00122 char *config_get_str_or_fail (Config *conf, const char *key); 00123 00124 /* These four functions fetch the entire array associated with a key into vals. 00125 * vals is an array passed in by the caller of length len elements. The 00126 * number of elements actually stored in vals is returned. Also, -1 is 00127 * returned on error. */ 00128 int 00129 config_get_int_array (Config * conf, const char * key, int * vals, int len); 00130 int 00131 config_get_boolean_array (Config * conf, const char * key, int * vals, int len); 00132 int 00133 config_get_double_array (Config * conf, const char * key, double * vals, int len); 00134 int 00135 config_get_str_array (Config * conf, const char * key, char ** vals, int len); 00136 00141 int config_get_array_len (Config *conf, const char * key); 00142 00143 /* Allocates and returns a NULL-terminated array of strings. */ 00144 char ** 00145 config_get_str_array_alloc (Config * conf, const char * key); 00146 00147 void config_str_array_free ( char **data); 00148 00149 /* Creates a new config struct */ 00150 Config * 00151 config_alloc( void ); 00152 00153 00154 /* These four functions search for a key, or create it if it does not exist. 00155 * They then store a single value associated with that key. 00156 * Return 0 on success, -1 on failure. */ 00157 int 00158 config_set_int (Config * conf, 00159 const char * key, 00160 int val); 00161 int 00162 config_set_boolean (Config * conf, 00163 const char * key, 00164 int val); 00165 int 00166 config_set_double (Config * conf, 00167 const char * key, 00168 double val); 00169 int 00170 config_set_str (Config * conf, 00171 const char * key, 00172 char * val); 00173 00174 /* These four functions search for a key, or create it if it does not exist. 00175 * They store the entire input array contained in vals to that key. 00176 * vals is an array passed in by the caller of length len elements. The 00177 * Return 0 on success, -1 on failure. */ 00178 int 00179 config_set_int_array (Config * conf, 00180 const char * key, 00181 int * vals, 00182 int len); 00183 int 00184 config_set_boolean_array (Config * conf, 00185 const char * key, 00186 int * vals, 00187 int len); 00188 int 00189 config_set_double_array (Config * conf, 00190 const char * key, 00191 double * vals, 00192 int len); 00193 int 00194 config_set_str_array (Config * conf, 00195 const char * key, 00196 char ** vals, 00197 int len); 00198 00199 00200 #ifdef __cplusplus 00201 } 00202 #endif 00203 00204 #endif 00205 00208 /*Previous 3 lines appended automatically on Sun Feb 5 19:40:16 WET 2012 */