usermode/library/common/config_generic.c

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 #include <libconfig.h>
00034 #include <string.h>
00035 #include <stdlib.h>
00036 #include <stdarg.h>
00037 #include <ctype.h>
00038 #include "config_generic.h"
00039 
00040 #define ENVVAR_MAX_LEN 128
00041 
00042 static inline int 
00043 env_setting_lookup(char *group, char *member, char **value_str)
00044 {
00045         char name[ENVVAR_MAX_LEN];
00046         char *val;
00047         int  i;
00048         int  len;
00049         int  group_len = strlen(group);
00050         int  member_len = strlen(member);
00051 
00052         if ((group_len + member_len) > ENVVAR_MAX_LEN-1) {
00053                 return CONFIG_FALSE;
00054         }
00055         for (len=0, i=0; i<group_len; len++, i++) {
00056                 name[i] = toupper(group[i]);
00057         }
00058         name[len++] = '_';
00059         for (i=0; i<member_len; len++, i++) {
00060                 name[len] = toupper(member[i]);
00061         }
00062         name[len++] = '\0';
00063         val = getenv(name);
00064         if (val) {
00065                 *value_str = val;
00066                 return CONFIG_TRUE;
00067         } else {        
00068                 return CONFIG_FALSE;
00069         }       
00070 }
00071 
00072 static inline int
00073 env_setting_lookup_int(char *group, char *member, int *value)
00074 {
00075         char *value_str;
00076 
00077         if (env_setting_lookup(group, member, &value_str) == CONFIG_FALSE) {
00078                 return CONFIG_FALSE;
00079         }
00080 
00081         if (value_str) {
00082                 *value = atoi(value_str);
00083                 return CONFIG_TRUE;
00084         } else {
00085                 return CONFIG_FALSE;
00086         }
00087 }
00088 
00089 static inline int
00090 env_setting_lookup_bool(char *group, char *member, int *value)
00091 {
00092         return env_setting_lookup_int(group, member, value);
00093 }
00094 
00095 
00096 static inline int 
00097 env_setting_lookup_string(char *group, char *member, char **value)
00098 {
00099         return env_setting_lookup(group, member, value);        
00100 }
00101 
00102 
00103 int
00104 m_config_setting_lookup_bool(config_t *cfg, 
00105                              char *group_name, 
00106                              char *member_name, 
00107                              int *value, 
00108                              int validity_check, ...)
00109 {
00110         config_setting_t *group = config_lookup(cfg, group_name);
00111         int              val;
00112         int              found_val  = 0;
00113 
00114         if (env_setting_lookup_bool(group_name, member_name, &val) == CONFIG_TRUE) {
00115                 found_val = 1;
00116         } else {        
00117                 group = config_lookup(cfg, group_name);
00118             if (group && config_setting_lookup_bool(group, member_name , &val) == CONFIG_TRUE) {
00119                         found_val = 1;
00120                 }
00121         }
00122 
00123         if (found_val)  {
00124                 *value = val;
00125                 return CONFIG_TRUE;
00126         }
00127         return CONFIG_FALSE;
00128 }
00129 
00130 
00131 int
00132 m_config_setting_lookup_int(config_t *cfg, 
00133                             char *group_name, 
00134                             char *member_name, 
00135                             int *value, 
00136                             int validity_check, ...)
00137 {
00138         config_setting_t *group;
00139         int              min;
00140         int              max;
00141         int              list_length;
00142         int              i;
00143         int              val;
00144         int              listval;
00145         va_list          ap;
00146         int              found_val  = 0;
00147 
00148         if (env_setting_lookup_int(group_name, member_name, &val) == CONFIG_TRUE) {
00149                 found_val = 1;
00150         } else {        
00151                 group = config_lookup(cfg, group_name);
00152             if (group && config_setting_lookup_int(group, member_name , (long int *) &val) == CONFIG_TRUE) {
00153                         found_val = 1;
00154                 }
00155         }
00156 
00157         if (found_val)  {
00158                 switch (validity_check) {
00159                         case CONFIG_NO_CHECK:
00160                                 *value = val;
00161                                 return CONFIG_TRUE;
00162                         case CONFIG_RANGE_CHECK:
00163                                 va_start(ap, validity_check);
00164                                 min = va_arg(ap, int);
00165                                 max = va_arg(ap, int);
00166                                 va_end(ap);
00167                                 if (*value >= min && *value <= max) {
00168                                         *value = val;
00169                                         return CONFIG_TRUE;
00170                                 }
00171                                 break;
00172                         case CONFIG_LIST_CHECK:
00173                                 va_start(ap, validity_check);
00174                                 list_length = va_arg(ap, int);
00175                                 for (i=0; i<list_length; i++) {
00176                                         listval = va_arg(ap, int);
00177                                         if (val == listval) {
00178                                                 *value = val;
00179                                                 return CONFIG_TRUE;
00180                                         }
00181                                 }
00182                                 va_end(ap);
00183                                 break;
00184                 }
00185         }
00186         return CONFIG_FALSE;
00187 }
00188 
00189 
00190 int
00191 m_config_setting_lookup_string(config_t *cfg, 
00192                                char *group_name, 
00193                                char *member_name, 
00194                                char **value, 
00195                                int validity_check, ...)
00196 {
00197         config_setting_t *group = config_lookup(cfg, group_name);
00198         int              list_length;
00199         int              i;
00200         char             *val;
00201         va_list          ap;
00202         int              found_val = 0;
00203 
00204         if (env_setting_lookup_string(group_name, member_name, &val) == CONFIG_TRUE) {
00205                 found_val = 1;
00206         } else {        
00207                 group = config_lookup(cfg, group_name);
00208             if (group && config_setting_lookup_string(group, member_name , (const char **) &val) == CONFIG_TRUE) {
00209                         found_val = 1;
00210                 }
00211         }
00212 
00213         if (found_val == 1)     {
00214                 switch (validity_check) {
00215                         case CONFIG_NO_CHECK:
00216                                 *value = val;
00217                                 return CONFIG_TRUE;
00218                         case CONFIG_RANGE_CHECK:
00219                                 break;
00220                         case CONFIG_LIST_CHECK:
00221                                 va_start(ap, validity_check);
00222                                 list_length = va_arg(ap, int);
00223                                 for (i=0; i<list_length; i++) {
00224                                         if (strcmp(val, va_arg(ap, char *))==0) {
00225                                                 *value = val;
00226                                                 return CONFIG_TRUE;
00227                                         }
00228                                 }
00229                                 va_end(ap);
00230                                 break;
00231                 }
00232         }
00233         return CONFIG_FALSE;
00234 }

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