MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSGenLib/ProcessConfigReader.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford 
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk. 
00011 //      
00012 //   This file is part of a  MOOS Core Component. 
00013 //        
00014 //   This program is free software; you can redistribute it and/or 
00015 //   modify it under the terms of the GNU General Public License as 
00016 //   published by the Free Software Foundation; either version 2 of the 
00017 //   License, or (at your option) any later version. 
00018 //          
00019 //   This program is distributed in the hope that it will be useful, 
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00022 //   General Public License for more details. 
00023 //            
00024 //   You should have received a copy of the GNU General Public License 
00025 //   along with this program; if not, write to the Free Software 
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00027 //   02111-1307, USA. 
00028 //
00030 // ProcessConfigReader.cpp: implementation of the CProcessConfigReader class.
00031 //
00033 #ifdef _WIN32
00034 #pragma warning(disable : 4786)
00035 #endif
00036 #include "MOOSGenLibGlobalHelper.h"
00037 #include "ProcessConfigReader.h"
00038 #include <iostream>
00039 
00041 // Construction/Destruction
00043 
00044 CProcessConfigReader::CProcessConfigReader()
00045 {
00046     
00047 }
00048 
00049 CProcessConfigReader::~CProcessConfigReader()
00050 {
00051     
00052 }
00053 
00054 
00055 
00056 
00057 void CProcessConfigReader::SetAppName(std::string sAppName)
00058 {
00059     m_sAppName = sAppName;
00060 }
00061 
00062 std::string CProcessConfigReader::GetAppName()
00063 {
00064     return m_sAppName;
00065 }
00066 
00067 std::string CProcessConfigReader::GetFileName()
00068 {
00069     return m_sFileName;
00070 }
00071 
00072 
00073 //GET A STRING LIST OF SETTINGS
00074 
00075 bool CProcessConfigReader::GetConfiguration(std::string sAppName, STRING_LIST &Params)
00076 {
00077     
00078     int nBrackets = 0;
00079     Params.clear();
00080     
00081     Reset();
00082     
00083     std::string sKey = "PROCESSCONFIG="+sAppName;
00084     
00085     if(GoTo(sKey))
00086     {
00087         std::string sBracket = GetNextValidLine();
00088         if(sBracket.find("{")==0)
00089         {
00090             nBrackets++;
00091             while(!GetFile()->eof())
00092             {
00093                 std::string sLine = GetNextValidLine();
00094                 
00095                 MOOSRemoveChars(sLine," \t\r");
00096                 
00097                 if(sLine.find("}")!=0)
00098                 {
00099 #if(1)
00100                     // jckerken 8-12-2004
00101                     // ignore if param = <empty string>
00102                     std::string sTmp(sLine);
00103                     std::string sTok = MOOSChomp(sTmp, "=");
00104                     
00105                     if (sTok.size() > 0) 
00106                     {
00107                         MOOSTrimWhiteSpace(sTmp);
00108                         
00109                         if (!sTmp.empty()) 
00110                         {
00111                             Params.push_front(sLine);
00112                         }
00113                         else if(sLine.find("[")!=std::string::npos || sLine.find("]")!=std::string::npos) 
00114                         {
00115                             Params.push_front(sLine);
00116                         }
00117                     } 
00118                     else 
00119                     {
00120                         Params.push_front(sLine);
00121                     }
00122 #else            
00123                     Params.push_front(sLine);
00124 #endif
00125                 }
00126                 else
00127                 {
00128                     return true;
00129                 }
00130                 
00131                 //quick error check - we don't allow nested { on single lines
00132                 if(sLine.find("{")==0)
00133                 {
00134                     MOOSTrace("CProcessConfigReader::GetConfiguration() missing \"}\" syntax error in mission file\n");
00135                 }
00136                 
00137                 
00138             }
00139         }
00140     }
00141     
00142     
00143     return false;
00144     
00145     
00146 }
00147 
00149 
00150 bool CProcessConfigReader::GetConfigurationParam(std::string sParam, bool & bVal)
00151 {
00152     if(!m_sAppName.empty())
00153     {
00154         std::string sVal;
00155         if(GetConfigurationParam(m_sAppName,sParam, sVal))
00156         {
00157             bVal = (MOOSStrCmp(sVal, "TRUE") ||
00158                     (MOOSIsNumeric(sVal) && atof(sVal.c_str()) > 0));
00159             
00160             return true;
00161         }
00162     }
00163     else
00164     {
00165         MOOSTrace("App Name not set in CProcessConfigReader::GetConfigurationParam()\n");
00166     }
00167     return false;
00168 }
00169 
00170 
00171 bool CProcessConfigReader::GetConfigurationParam(std::string sAppName, std::string sParam, bool & bVal)
00172 {
00173     std::string sVal;
00174     
00175     if (GetConfigurationParam(sAppName, sParam, sVal)) 
00176     {
00177         // jckerken 10/24/04 : made bin 0,1 also work
00178         // bVal =  MOOSStrCmp(sVal,"TRUE");
00179         bVal = (MOOSStrCmp(sVal, "TRUE") ||  (MOOSIsNumeric(sVal) && atof(sVal.c_str()) > 0));
00180         return true;
00181     }
00182     
00183     return false;
00184 }
00185 
00186 
00188 
00189 
00190 bool CProcessConfigReader::GetConfigurationParam(std::string sParam, double & dfVal)
00191 {
00192     if(!m_sAppName.empty())
00193     {
00194         return GetConfigurationParam(m_sAppName,sParam, dfVal);
00195     }
00196     else
00197     {
00198         MOOSTrace("App Name not set in CProcessConfigReader::GetConfigurationParam()\n");
00199     }
00200     
00201     return false;
00202 }
00203 
00204 bool CProcessConfigReader::GetConfigurationParam(std::string sAppName,std::string sParam, double & dfVal)
00205 {
00206     std::string sTmp;
00207     if(GetConfigurationParam(sAppName,sParam,sTmp))
00208     {
00209         if(!sTmp.empty() && (isdigit(sTmp[0]) || sTmp[0]=='-' ))
00210         {
00211             dfVal = atof(sTmp.c_str());
00212             
00213             return true;
00214         }
00215     }
00216     return false;
00217     
00218 }
00219 
00221 
00222 
00223 bool CProcessConfigReader::GetConfigurationParam(std::string sParam, float & fVal)
00224 {
00225     double dfVal;
00226     bool bSuccess = GetConfigurationParam(sParam,dfVal);
00227     if(bSuccess)
00228         fVal = (float) dfVal;
00229     return bSuccess;
00230 }
00231 
00232 bool CProcessConfigReader::GetConfigurationParam(std::string sAppName,std::string sParam, float & fVal)
00233 {
00234     double dfVal;
00235     bool bSuccess = GetConfigurationParam(sAppName,sParam,dfVal);
00236     if(bSuccess)
00237         fVal = (float) dfVal;
00238     return bSuccess;
00239 }
00240 
00242 
00243 bool CProcessConfigReader::GetConfigurationParam(std::string sAppName,std::string sParam, std::string &sVal)
00244 {
00245     Reset();
00246     
00247     STRING_LIST sParams;
00248     
00249     if(GetConfiguration( sAppName, sParams))
00250     {
00251         STRING_LIST::iterator p;
00252         for(p = sParams.begin();p!=sParams.end();p++)
00253         {
00254             std::string sTmp = *p;
00255             std::string sTok = MOOSChomp(sTmp,"=");
00256             
00257             MOOSTrimWhiteSpace(sTmp);
00258             
00259             if (sTmp.empty())
00260                 return false;
00261             
00262             if(MOOSStrCmp(sTok,sParam))
00263             {
00264                 sVal=sTmp;
00265                 return true;
00266             }
00267         }
00268     }
00269     return false;
00270 }
00271 
00272 
00273 bool CProcessConfigReader::GetConfigurationParam(std::string sParam, std::string & sVal)
00274 {
00275     if(!m_sAppName.empty())
00276     {
00277         return GetConfigurationParam(m_sAppName,sParam, sVal);
00278     }
00279     else
00280     {
00281         MOOSTrace("App Name not set in CProcessConfigReader::GetConfigurationParam()\n");
00282     }
00283     
00284     return false;
00285 }
00286 
00287 
00289 
00290 bool CProcessConfigReader::GetConfigurationParam(std::string sParam, int & nVal)
00291 {
00292     if (!m_sAppName.empty()) 
00293     {
00294         return GetConfigurationParam(m_sAppName, sParam, nVal);
00295     }
00296     else 
00297     {
00298         MOOSTrace("App Name not set in CProcessConfigReader::GetConfigurationParam()\n");
00299     }
00300     
00301     return false;
00302 }
00303 
00304 bool CProcessConfigReader::GetConfigurationParam(std::string sAppName, std::string sParam, int &nVal)
00305 {
00306     std::string sTmp;
00307     
00308     if (GetConfigurationParam(sAppName, sParam, sTmp)) {
00309         nVal = atoi(sTmp.c_str());
00310         return true;
00311     }
00312     
00313     return false;
00314 }
00315 
00316 bool CProcessConfigReader::GetConfigurationParam(std::string sParam, unsigned int &nVal)
00317 {
00318     int nIntVal;
00319     bool bSuccess = GetConfigurationParam(sParam,nIntVal);
00320     if(bSuccess)
00321         nVal = (unsigned int) nIntVal;
00322     return bSuccess;
00323 }
00324 
00325 bool CProcessConfigReader::GetConfigurationParam(std::string sAppName, std::string sParam, unsigned int &nVal)
00326 {
00327     int nIntVal;
00328     bool bSuccess = GetConfigurationParam(sAppName,sParam,nIntVal);
00329     if(bSuccess)
00330         nVal = (unsigned int) nIntVal;
00331     return bSuccess;
00332 }
00333 
00335 
00336 bool CProcessConfigReader::GetConfigurationParam(std::string sParam, std::vector<double> & Vec, int & nRows, int & nCols)
00337 {
00338     std::string sVal;
00339     if(GetConfigurationParam(sParam,sVal))
00340     {
00341         return MOOSVectorFromString(sVal,Vec,nRows,nCols);
00342     }
00343     return false;
00344 }
00345 
00346 
00347 
00348 
00349 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines