config.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2014, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Luca Iocchi (2014-2016)
00036 *********************************************************************/
00037 
00038 #include "config.h"
00039 #include <cstring>
00040 #include <cstdlib>
00041 
00042 #define STRLEN 200
00043 
00044 
00045 ConfigFile::ConfigFile(const char *filename)
00046 {
00047         f.open(filename);
00048         if (!f.good()) {
00049                 printf("ERROR. Cannot open config file %s.\n",filename);
00050                 return;
00051         }
00052 
00053         printf("Opened config file %s\n",filename);
00054         char buf[STRLEN]; char *p; char par[STRLEN], val[STRLEN], profile[STRLEN];
00055         int state=0;
00056         while (f.good()) {
00057                 f.getline(buf,STRLEN);
00058                 switch (state) {
00059                         case 0: // find SECTION
00060                         p = strstr(buf,"[SECTION Main]");
00061                         if (p)
00062                                 state++;
00063                         break;
00064                         case 1: // find PROFILE
00065                         p = strstr(buf,"PROFILE");
00066                         if (p) {
00067                                 sscanf(buf,"%s %s",par,profile);
00068                                 printf("Config Profile %s\n",profile);
00069                                 state++;
00070                         }
00071                         break;
00072             
00073                         case 2: // find correct profile
00074                         p = strstr(buf,"PROFILE");
00075                         if (p) {
00076                                 sscanf(p,"%s %s",par,val); int n = strlen(val);
00077                                 int k=0;
00078                                 while (val[k]!=']' && k<n) k++;
00079                                 val[k]='\0';
00080                                 printf("Read %s\n",val);
00081                                 if (strcmp(profile,val)==0) {
00082                                         printf("Right profile\n");
00083                                         state++;
00084                                 }
00085                         }
00086                         break;
00087                         
00088             case 3: // reading params from profile
00089                         p = strstr(buf,"[END]");
00090                         if (p) {
00091                                 state++;
00092                         }
00093                         else {
00094                                 if (buf[0]!='#' && buf[0]!=';' && buf[0]!='/') {
00095                                         int r = sscanf(buf,"%s %s",par,val);
00096                                         if (r>0) {
00097                                                 char *p = strstr(buf,"\"");
00098                                                 if (p) {
00099                                                         char *r = strstr(p+1,"\"");
00100                                                         if (r) {
00101                                                                 *r='\0';
00102                                                                 params[string(par)] = string(p+1);
00103                                                                 printf("   %s = %s\n",par,p+1);
00104                                                         }
00105                                                 }
00106                                                 else {
00107                                                         params[string(par)] = string(val);
00108                                                         printf("   %s = %s\n",par,val);
00109                                                 }
00110                                         }
00111                                 }
00112                         }
00113                         break;
00114 
00115                         case 4: // end
00116                         break;
00117                 }
00118                 // cout << buf << endl;
00119         }
00120 
00121         f.close();
00122 }
00123 
00124 ConfigFile::~ConfigFile()
00125 {
00126 
00127 }
00128 
00129 string ConfigFile::getParam(string p) {
00130         map<string,string>::const_iterator it = params.find(p);
00131         if (it!=params.end())
00132                 return it->second;
00133         else
00134                 return string("");
00135 }
00136 
00137 string ConfigFile::getParam(const char *p) {
00138         return getParam(string(p));
00139 }
00140 
00141 
00142 int ConfigFile::getIParam(const char *p, int def)
00143 {
00144         map<string,string>::const_iterator it = params.find(string(p));
00145         if (it!=params.end())
00146                 return atoi(it->second.c_str());
00147         else
00148                 return def;
00149 }
00150 
00151 double ConfigFile::getDParam(const char *p, double def)
00152 {
00153         map<string,string>::const_iterator it = params.find(string(p));
00154         if (it!=params.end())
00155                 return atof(it->second.c_str());
00156         else
00157                 return def;
00158 }
00159 


patrolling_sim
Author(s):
autogenerated on Thu Jun 6 2019 20:27:09