Snoopy Version 6.0 (Albert)
libconfig.h
Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002    libconfig - A library for processing structured configuration files
00003    Copyright (C) 2005-2009  Mark A Lindner
00004 
00005    This file is part of libconfig.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public License
00009    as published by the Free Software Foundation; either version 2.1 of
00010    the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful, but
00013    WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, see
00019    <http://www.gnu.org/licenses/>.
00020    ----------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __libconfig_h
00024 #define __libconfig_h
00025 
00026 #ifdef __cplusplus
00027 extern "C" {
00028 #endif /* __cplusplus */
00029 
00030 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
00031 #if defined(LIBCONFIG_STATIC)
00032 #define LIBCONFIG_API
00033 #elif defined(LIBCONFIG_EXPORTS)
00034 #define LIBCONFIG_API __declspec(dllexport)
00035 #else /* ! LIBCONFIG_EXPORTS */
00036 #define LIBCONFIG_API __declspec(dllimport)
00037 #endif /* LIBCONFIG_STATIC */
00038 #else /* ! WIN32 */
00039 #define LIBCONFIG_API
00040 #endif /* WIN32 */
00041 
00042 #include <stdio.h>
00043 
00044 #define CONFIG_TYPE_NONE    0
00045 #define CONFIG_TYPE_GROUP   1
00046 #define CONFIG_TYPE_INT     2
00047 #define CONFIG_TYPE_INT64   3
00048 #define CONFIG_TYPE_FLOAT   4
00049 #define CONFIG_TYPE_STRING  5
00050 #define CONFIG_TYPE_BOOL    6
00051 #define CONFIG_TYPE_ARRAY   7
00052 #define CONFIG_TYPE_LIST    8
00053 
00054 #define CONFIG_FORMAT_DEFAULT  0
00055 #define CONFIG_FORMAT_HEX      1
00056 
00057 #define CONFIG_OPTION_AUTOCONVERT 0x01
00058 
00059 #define CONFIG_TRUE  (1)
00060 #define CONFIG_FALSE (0)
00061 
00062 typedef union config_value_t
00063 {
00064   long ival;
00065   long long llval;
00066   double fval;
00067   char *sval;
00068   struct config_list_t *list;
00069 } config_value_t;
00070 
00071 typedef struct config_setting_t
00072 {
00073   char *name;
00074   short type;
00075   short format;
00076   config_value_t value;
00077   struct config_setting_t *parent;
00078   struct config_t *config;
00079   void *hook;
00080   unsigned int line;
00081 } config_setting_t;
00082 
00083 typedef struct config_list_t
00084 {
00085   unsigned int length;
00086   unsigned int capacity;
00087   config_setting_t **elements;
00088 } config_list_t;
00089 
00090 typedef struct config_t
00091 {
00092   config_setting_t *root;
00093   void (*destructor)(void *);
00094   int flags;
00095   const char *error_text;
00096   int error_line;
00097 } config_t;
00098 
00099 extern LIBCONFIG_API int config_read(config_t *config, FILE *stream);
00100 extern LIBCONFIG_API void config_write(const config_t *config, FILE *stream);
00101 
00102 extern LIBCONFIG_API void config_set_auto_convert(config_t *config, int flag);
00103 extern LIBCONFIG_API int config_get_auto_convert(const config_t *config);
00104 
00105 extern LIBCONFIG_API int config_read_file(config_t *config,
00106                                           const char *filename);
00107 extern LIBCONFIG_API int config_write_file(config_t *config,
00108                                            const char *filename);
00109 
00110 extern LIBCONFIG_API void config_set_destructor(config_t *config,
00111                                                 void (*destructor)(void *));
00112 
00113 extern LIBCONFIG_API void config_init(config_t *config);
00114 extern LIBCONFIG_API void config_destroy(config_t *config);
00115 
00116 extern LIBCONFIG_API long config_setting_get_int(
00117   const config_setting_t *setting);
00118 extern LIBCONFIG_API long long config_setting_get_int64(
00119   const config_setting_t *setting);
00120 extern LIBCONFIG_API double config_setting_get_float(
00121   const config_setting_t *setting);
00122 extern LIBCONFIG_API int config_setting_get_bool(
00123   const config_setting_t *setting);
00124 extern LIBCONFIG_API const char *config_setting_get_string(
00125   const config_setting_t *setting);
00126 
00127 extern LIBCONFIG_API int config_setting_lookup_int(
00128   const config_setting_t *setting, const char *name, long *value);
00129 extern LIBCONFIG_API int config_setting_lookup_int64(
00130   const config_setting_t *setting, const char *name, long long *value);
00131 extern LIBCONFIG_API int config_setting_lookup_float(
00132   const config_setting_t *setting, const char *name, double *value);
00133 extern LIBCONFIG_API int config_setting_lookup_bool(
00134   const config_setting_t *setting, const char *name, int *value);
00135 extern LIBCONFIG_API int config_setting_lookup_string(
00136   const config_setting_t *setting, const char *name, const char **value);
00137 
00138 extern LIBCONFIG_API int config_setting_set_int(config_setting_t *setting,
00139                                                 long value);
00140 extern LIBCONFIG_API int config_setting_set_int64(config_setting_t *setting,
00141                                                   long long value);
00142 extern LIBCONFIG_API int config_setting_set_float(config_setting_t *setting,
00143                                                   double value);
00144 extern LIBCONFIG_API int config_setting_set_bool(config_setting_t *setting,
00145                                                  int value);
00146 extern LIBCONFIG_API int config_setting_set_string(config_setting_t *setting,
00147                                                    const char *value);
00148 
00149 extern LIBCONFIG_API int config_setting_set_format(config_setting_t *setting,
00150                                                    short format);
00151 extern LIBCONFIG_API short config_setting_get_format(config_setting_t *setting);
00152 
00153 extern LIBCONFIG_API long config_setting_get_int_elem(
00154   const config_setting_t *setting, int idx);
00155 extern LIBCONFIG_API long long config_setting_get_int64_elem(
00156   const config_setting_t *setting, int idx);
00157 extern LIBCONFIG_API double config_setting_get_float_elem(
00158   const config_setting_t *setting, int idx);
00159 extern LIBCONFIG_API int config_setting_get_bool_elem(
00160   const config_setting_t *setting, int idx);
00161 extern LIBCONFIG_API const char *config_setting_get_string_elem(
00162   const config_setting_t *setting, int idx);
00163 
00164 extern LIBCONFIG_API config_setting_t *config_setting_set_int_elem(
00165   config_setting_t *setting, int idx, long value);
00166 extern LIBCONFIG_API config_setting_t *config_setting_set_int64_elem(
00167   config_setting_t *setting, int idx, long long value);
00168 extern LIBCONFIG_API config_setting_t *config_setting_set_float_elem(
00169   config_setting_t *setting, int idx, double value);
00170 extern LIBCONFIG_API config_setting_t *config_setting_set_bool_elem(
00171   config_setting_t *setting, int idx, int value);
00172 extern LIBCONFIG_API config_setting_t *config_setting_set_string_elem(
00173   config_setting_t *setting, int idx, const char *value);
00174 
00175 #define /* int */ config_setting_type(/* const config_setting_t * */ S) \
00176   ((S)->type)
00177 
00178 #define /* int */ config_setting_is_group(/* const config_setting_t * */ S) \
00179   ((S)->type == CONFIG_TYPE_GROUP)
00180 #define /* int */ config_setting_is_array(/* const config_setting_t * */ S) \
00181   ((S)->type == CONFIG_TYPE_ARRAY)
00182 #define /* int */ config_setting_is_list(/* const config_setting_t * */ S) \
00183   ((S)->type == CONFIG_TYPE_LIST)
00184 
00185 #define /* int */ config_setting_is_aggregate( \
00186   /* const config_setting_t * */ S)                                     \
00187   (((S)->type == CONFIG_TYPE_GROUP) || ((S)->type == CONFIG_TYPE_LIST)  \
00188    || ((S)->type == CONFIG_TYPE_ARRAY))
00189   
00190 #define /* int */ config_setting_is_number(/* const config_setting_t * */ S) \
00191   (((S)->type == CONFIG_TYPE_INT)                                       \
00192    || ((S)->type == CONFIG_TYPE_INT64)                                  \
00193    || ((S)->type == CONFIG_TYPE_FLOAT))
00194   
00195 #define /* int */ config_setting_is_scalar(/* const config_setting_t * */ S) \
00196   (((S)->type == CONFIG_TYPE_BOOL) || ((S)->type == CONFIG_TYPE_STRING) \
00197    || config_setting_is_number(S))
00198 
00199 #define /* const char * */ config_setting_name( \
00200   /* const config_setting_t * */ S)             \
00201   ((S)->name)
00202 
00203 #define /* config_setting_t * */ config_setting_parent( \
00204   /* const config_setting_t * */ S)                     \
00205   ((S)->parent)
00206 
00207 #define /* int */ config_setting_is_root(       \
00208   /* const config_setting_t * */ S)             \
00209   ((S)->parent ? CONFIG_FALSE : CONFIG_TRUE)
00210 
00211 extern LIBCONFIG_API int config_setting_index(const config_setting_t *setting);
00212 
00213 extern LIBCONFIG_API int config_setting_length(
00214   const config_setting_t *setting);
00215 extern LIBCONFIG_API config_setting_t *config_setting_get_elem(
00216   const config_setting_t *setting, unsigned int idx);
00217 
00218 extern LIBCONFIG_API config_setting_t *config_setting_get_member(
00219   const config_setting_t *setting, const char *name);
00220 
00221 extern LIBCONFIG_API config_setting_t *config_setting_add(
00222   config_setting_t *parent, const char *name, int type);
00223 extern LIBCONFIG_API int config_setting_remove(config_setting_t *parent,
00224                                                const char *name);
00225 extern LIBCONFIG_API int config_setting_remove_elem(config_setting_t *parent,
00226                                                     unsigned int idx);
00227 extern LIBCONFIG_API void config_setting_set_hook(config_setting_t *setting,
00228                                                   void *hook);
00229 
00230 #define config_setting_get_hook(S) ((S)->hook)
00231 
00232 extern LIBCONFIG_API config_setting_t *config_lookup(const config_t *config,
00233                                                      const char *path);
00234 
00235 extern LIBCONFIG_API int config_lookup_int(const config_t *config,
00236                                            const char *path, long *value);
00237 extern LIBCONFIG_API int config_lookup_int64(const config_t *config,
00238                                              const char *path,
00239                                              long long *value);
00240 extern LIBCONFIG_API int config_lookup_float(const config_t *config,
00241                                              const char *path, double *value);
00242 extern LIBCONFIG_API int config_lookup_bool(const config_t *config,
00243                                             const char *path, int *value);
00244 extern LIBCONFIG_API int config_lookup_string(const config_t *config,
00245                                               const char *path,
00246                                               const char **value);
00247 
00248 #define /* config_setting_t * */ config_root_setting( \
00249   /* const config_t * */ C)                           \
00250   ((C)->root)
00251 
00252 #define /* unsigned short */ config_setting_source_line( \
00253   /* const config_t */ C)                                \
00254   ((C)->line)
00255 
00256 #define /* const char * */ config_error_text(/* const config_t * */ C)  \
00257   ((C)->error_text)
00258 
00259 #define /* int */ config_error_line(/* const config_t * */ C)   \
00260   ((C)->error_line)
00261 
00262 #ifdef __cplusplus
00263 }
00264 #endif /* __cplusplus */
00265 
00266 #endif /* __libconfig_h */