usermode/library/common/config_generic.h

00001 /*
00002     Copyright (C) 2011 Computer Sciences Department, 
00003     University of Wisconsin -- Madison
00004 
00005     ----------------------------------------------------------------------
00006 
00007     This file is part of Mnemosyne: Lightweight Persistent Memory, 
00008     originally developed at the University of Wisconsin -- Madison.
00009 
00010     Mnemosyne was originally developed primarily by Haris Volos
00011     with contributions from Andres Jaan Tack.
00012 
00013     ----------------------------------------------------------------------
00014 
00015     Mnemosyne is free software; you can redistribute it and/or
00016     modify it under the terms of the GNU General Public License
00017     as published by the Free Software Foundation, version 2
00018     of the License.
00019  
00020     Mnemosyne is distributed in the hope that it will be useful,
00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023     GNU General Public License for more details.
00024 
00025     You should have received a copy of the GNU General Public License
00026     along with this program; if not, write to the Free Software
00027     Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00028     Boston, MA  02110-1301, USA.
00029 
00030 ### END HEADER ###
00031 */
00032 
00033 #ifndef _CONFIG_GENERIC_H
00034 #define _CONFIG_GENERIC_H
00035 
00036 #include <stdio.h>
00037 #include <libconfig.h>
00038 
00039 #define CONFIG_NO_CHECK    0
00040 #define CONFIG_RANGE_CHECK 1
00041 #define CONFIG_LIST_CHECK  2
00042 
00043 /* 
00044  * The lookup functions return the value of a configuration variable based on 
00045  * the following order: 
00046  *  1) value of environment variable
00047  *  2) value in configuration file
00048  *  3) default value
00049  */
00050 
00051 
00052 #define CONFIG_SETTING_LOOKUP(config, values, group, member, type_name, type,  \
00053                               default_val, validitycheck, args...)             \
00054   (values)->member = default_val;                                              \
00055   m_config_setting_lookup_##type_name(config, #group, #member,                 \
00056                                       &((values)->member),                     \
00057                                       validitycheck, args);
00058 
00059 #define CONFIG_SETTING_ENTRY(config, values, group, member, type_name, type,    \
00060                              default_val, validitycheck, args...)               \
00061   type member;
00062 
00063 #define CONFIG_GROUP_STRUCT(group)                                             \
00064   struct {                                                                     \
00065     FOREACH_RUNTIME_CONFIG_SETTING(CONFIG_SETTING_ENTRY, group, NULL, NULL)    \
00066   }
00067 
00068 static inline void
00069 config_setting_print_bool(FILE * stream, char *group, char *member, int val) {
00070     fprintf(stream, "%s.%s = %s\n", group, member, val==1 ? "true": "false");
00071 }
00072 
00073 static inline void
00074 config_setting_print_int(FILE * stream, char *group, char *member, int val) {
00075     fprintf(stream, "%s.%s = %d\n", group, member, val);
00076 }
00077 
00078 static inline void
00079 config_setting_print_string(FILE * stream, char *group, char *member, char *val) {
00080     fprintf(stream, "%s.%s = %s\n", group, member, val);
00081 }
00082 
00083 
00084 #define CONFIG_SETTING_PRINT(config, values, group, member, type_name, type,   \
00085                              default_val, validitycheck, args...)              \
00086  config_setting_print_##type_name(__stream, #group, #member, (values)->member);
00087 
00088 
00089 #define CONFIG_GROUP_PRINT(stream, group, values)                              \
00090 do {                                                                           \
00091   FILE *__stream = stream;                                                     \
00092   FOREACH_RUNTIME_CONFIG_SETTING(CONFIG_SETTING_PRINT, group, NULL, values)    \
00093 } while(0);
00094 
00095 int m_config_setting_lookup_string(config_t *cfg, char *group_name, char *member_name, char **value, int validity_check, ...);
00096 int m_config_setting_lookup_int(config_t *cfg, char *group_name, char *member_name, int *value, int validity_check, ...);
00097 int m_config_setting_lookup_bool(config_t *cfg, char *group_name, char *member_name, int *value, int validity_check, ...);
00098 
00099 #endif 

Generated on Sat Apr 23 11:43:34 2011 for Mnemosyne by  doxygen 1.4.7