00001 #include <Shared/Config.h>
00002 #include <Shared/Serializer.h>
00003 #include <stdio.h>
00004 #include <string>
00005 #include <ctype.h>
00006
00007 Config* config=NULL;
00008 using namespace SerializerNS;
00009
00010 void* Config::setValue(section_t section, const char *key, const char *value,
00011 bool ) {
00012 switch (section) {
00013 case sec_wireless:
00014 if (strncasecmp(key,"id",29)==0) {
00015 wireless.id=atoi(value);
00016 return &wireless.id;
00017 }
00018 break;
00019 case sec_vision:
00020 if (strncasecmp(key,"white_balance",29)==0) {
00021 if (strncasecmp(value,"indoor",49)==0) {
00022 vision.white_balance=1;
00023 } else if (strncasecmp(value,"flourescent",49)==0) {
00024 vision.white_balance=3;
00025 } else if (strncasecmp(value,"outdoor",49)==0) {
00026 vision.white_balance=2;
00027 }
00028 return &vision.white_balance;
00029 } else if (strncasecmp(key,"gain",29)==0) {
00030 if (strncasecmp(value,"low",49)==0) {
00031 vision.gain=1;
00032 } else if (strncasecmp(value,"mid",49)==0) {
00033 vision.gain=2;
00034 } else if (strncasecmp(value,"high",49)==0) {
00035 vision.gain=3;
00036 }
00037 return &vision.gain;
00038 } else if (strncasecmp(key,"shutter_speed",29)==0) {
00039 if (strncasecmp(value,"slow",49)==0) {
00040 vision.shutter_speed=1;
00041 } else if (strncasecmp(value,"mid",49)==0) {
00042 vision.shutter_speed=2;
00043 } else if (strncasecmp(value,"fast",49)==0) {
00044 vision.shutter_speed=3;
00045 }
00046 return &vision.shutter_speed;
00047 } else if (strncasecmp(key,"resolution",29)==0) {
00048 if (strncasecmp(value,"full",49)==0) {
00049 vision.resolution=1;
00050 } else if (strncasecmp(value,"half",49)==0) {
00051 vision.resolution=2;
00052 } else if (strncasecmp(value,"quarter",49)==0) {
00053 vision.resolution=3;
00054 }
00055 return &vision.resolution;
00056 } else if (strncasecmp(key,"thresh",29)==0) {
00057 strncpy(vision.thresh,value,49);
00058 return &vision.thresh;
00059 } else if (strncasecmp(key,"colors",29)==0) {
00060 strncpy(vision.colors,value,49);
00061 return &vision.colors;
00062 } else if (strncasecmp(key,"raw_port",29)==0) {
00063 vision.raw_port=atoi(value);
00064 return &vision.raw_port;
00065 } else if (strncasecmp(key,"rle_port",29)==0) {
00066 vision.rle_port=atoi(value);
00067 return &vision.rle_port;
00068 } else if (strncasecmp(key,"obj_port",29)==0) {
00069 vision.obj_port=atoi(value);
00070 return &vision.obj_port;
00071 } else if (strncasecmp(key,"raw_encoding",29)==0) {
00072 if (strncasecmp(value,"half",49)==0) {
00073 vision.raw_encoding=packet_visionraw_half;
00074 } else if (strncasecmp(value,"full",49)==0) {
00075 vision.raw_encoding=packet_visionraw_full;
00076 } else if (strncasecmp(value,"yfull_uvhalf",49)==0) {
00077 vision.raw_encoding=packet_visionraw_yfull_uvhalf;
00078 }
00079 return &vision.raw_encoding;
00080 }
00081 break;
00082 case sec_main:
00083 if (strncasecmp(key,"console_port",29)==0) {
00084 main.console_port=atoi(value);
00085 return &main.console_port;
00086 } else if (strncasecmp(key,"stderr_port",29)==0) {
00087 main.stderr_port=atoi(value);
00088 return &main.stderr_port;
00089 } else if (strncasecmp(key,"error_level",29)==0) {
00090 main.error_level=atoi(value);
00091 return &main.error_level;
00092 } else if (strncasecmp(key,"debug_level",29)==0) {
00093 main.debug_level=atoi(value);
00094 return &main.debug_level;
00095 } else if (strncasecmp(key,"verbose_level",29)==0) {
00096 main.verbose_level=atoi(value);
00097 return &main.verbose_level;
00098 } else if (strncasecmp(key,"wsjoints_port",29)==0) {
00099 main.wsjoints_port=atoi(value);
00100 return &main.wsjoints_port;
00101 } else if (strncasecmp(key,"wspids_port",29)==0) {
00102 main.wspids_port=atoi(value);
00103 return &main.wspids_port;
00104 } else if (strncasecmp(key,"headControl_port",29)==0) {
00105 main.headControl_port=atoi(value);
00106 return &main.headControl_port;
00107 } else if (strncasecmp(key,"walkControl_port",29)==0) {
00108 main.walkControl_port=atoi(value);
00109 return &main.walkControl_port;
00110 } else if (strncasecmp(key,"estopControl_port",29)==0) {
00111 main.estopControl_port=atoi(value);
00112 return &main.estopControl_port;
00113 } else if (strncasecmp(key,"aibo3d_port",29)==0) {
00114 main.aibo3d_port=atoi(value);
00115 return &main.aibo3d_port;
00116 } else if (strncasecmp(key,"wmmonitor_port",29)==0) {
00117 main.wmmonitor_port=atoi(value);
00118 return &main.wmmonitor_port;
00119 } else if (strncasecmp(key,"use_VT100",29)==0) {
00120 main.use_VT100=extractBool(value);
00121 return &main.use_VT100;
00122 }
00123 break;
00124 case sec_behaviors:
00125 break;
00126 case sec_controller:
00127 if (strncasecmp(key,"gui_port",29)==0) {
00128 controller.gui_port = atoi(value);
00129 return &controller.gui_port ;
00130 } else if (strncasecmp(key,"select_snd",29)==0) {
00131 strncpy(controller.select_snd,value,49);
00132 return &controller.select_snd;
00133 } else if (strncasecmp(key,"next_snd",29)==0) {
00134 strncpy(controller.next_snd,value,49);
00135 return &controller.next_snd;
00136 } else if (strncasecmp(key,"prev_snd",29)==0) {
00137 strncpy(controller.prev_snd,value,49);
00138 return &controller.prev_snd;
00139 } else if (strncasecmp(key,"read_snd",29)==0) {
00140 strncpy(controller.read_snd,value,49);
00141 return &controller.read_snd;
00142 } else if (strncasecmp(key,"cancel_snd",29)==0) {
00143 strncpy(controller.cancel_snd,value,49);
00144 return &controller.cancel_snd;
00145 }
00146 break;
00147 case sec_motion:
00148 if (strncasecmp(key,"root",29)==0) {
00149 motion.root=value;
00150 return &motion.root;
00151 } else if (strncasecmp(key,"estop_on_snd",29)==0) {
00152 strncpy(motion.estop_on_snd,value,49);
00153 return &motion.estop_on_snd;
00154 } else if (strncasecmp(key,"estop_off_snd",29)==0) {
00155 strncpy(motion.estop_off_snd,value,49);
00156 return &motion.estop_off_snd;
00157 } else if (strncasecmp(key,"max_head_tilt_speed",29)==0) {
00158 motion.max_head_tilt_speed=atof(value);
00159 } else if (strncasecmp(key,"max_head_pan_speed",29)==0) {
00160 motion.max_head_pan_speed=atof(value);
00161 } else if (strncasecmp(key,"max_head_roll_speed",29)==0) {
00162 motion.max_head_roll_speed=atof(value);
00163 }
00164 break;
00165 case sec_worldmodel2:
00166 if (strncasecmp(key,"dm_port",29)==0) {
00167 worldmodel2.dm_port = atoi(value);
00168 return &worldmodel2.dm_port ;
00169 } else if (strncasecmp(key,"hm_port",29)==0) {
00170 worldmodel2.hm_port = atoi(value);
00171 return &worldmodel2.hm_port ;
00172 } else if (strncasecmp(key,"gm_port",29)==0) {
00173 worldmodel2.gm_port = atoi(value);
00174 return &worldmodel2.gm_port ;
00175 } else if (strncasecmp(key,"fs_port",29)==0) {
00176 worldmodel2.fs_port = atoi(value);
00177 return &worldmodel2.fs_port ;
00178 }
00179 break;
00180 case sec_sound:
00181 if (strncasecmp(key,"root",29)==0) {
00182 sound.root=value;
00183 return &sound.root;
00184 } else if (strncasecmp(key,"sample_rate",29)==0) {
00185 sound.sample_rate = atoi(value);
00186 return &sound.sample_rate ;
00187 } else if (strncasecmp(key,"sample_bits",29)==0) {
00188 sound.sample_bits = atoi(value);
00189 return &sound.sample_bits ;
00190 } else if (strncasecmp(key,"preload",29)==0) {
00191 sound.preload.push_back(value);
00192 return &sound.preload ;
00193 }
00194 break;
00195 default:
00196 break;
00197 }
00198 return NULL;
00199 }
00200
00201 Config::section_t Config::parseSection(const char* key) {
00202 if (strncasecmp(key,"wireless",29)==0) {
00203 return sec_wireless;
00204 } else if (strncasecmp(key,"vision",29)==0) {
00205 return sec_vision;
00206 } else if (strncasecmp(key,"main",29)==0) {
00207 return sec_main;
00208 } else if (strncasecmp(key,"behaviors",29)==0) {
00209 return sec_behaviors;
00210 } else if (strncasecmp(key,"controller",29)==0) {
00211 return sec_controller;
00212 } else if (strncasecmp(key,"motion",29)==0) {
00213 return sec_motion;
00214 } else if (strncasecmp(key,"worldmodel2",29)==0) {
00215 return sec_worldmodel2;
00216 } else if (strncasecmp(key,"sound",29)==0) {
00217 return sec_sound;
00218 } else {
00219 return sec_invalid;
00220 }
00221 }
00222
00223 void Config::readConfig(const char* filename) {
00224 FILE* fp = fopen(filename, "r");
00225 char buf[80], key[30], value[50];
00226 section_t section=sec_invalid;
00227 if (fp==NULL) return;
00228
00229 while (fscanf(fp,"%79[^\n]\n", buf)!=EOF) {
00230 if (sscanf(buf,"[%29[^]]",key)>0) {
00231 section=parseSection(key);
00232 } else if (sscanf(buf,"%29[^=]=%49s",key,value)>1) {
00233 setValue(section, key, value);
00234 }
00235 }
00236 fclose(fp);
00237 }
00238
00239 bool Config::extractBool(const char * value) {
00240 int i=0;
00241 while(isspace(value[i])) i++;
00242 if(strncasecmp(&value[i],"t",29)==0)
00243 return true;
00244 else if(strncasecmp(&value[i],"f",29)==0)
00245 return false;
00246 else if(strncasecmp(&value[i],"true",29)==0)
00247 return true;
00248 else if(strncasecmp(&value[i],"false",29)==0)
00249 return false;
00250 else if(strncasecmp(&value[i],"y",29)==0)
00251 return true;
00252 else if(strncasecmp(&value[i],"n",29)==0)
00253 return false;
00254 else if(strncasecmp(&value[i],"yes",29)==0)
00255 return true;
00256 else if(strncasecmp(&value[i],"no",29)==0)
00257 return false;
00258 else
00259 return atoi(value);
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272