Go to the documentation of this file.00001
00002 #include "Calibration/Tools/bin/trivialParser.h"
00003 #include <iostream>
00004 #include <cstdlib>
00005
00006 trivialParser::trivialParser (std::string configFile)
00007 {
00008 parse (configFile) ;
00009 print ("[ctor] ") ;
00010 }
00011
00012
00013
00014
00015
00016 double
00017 trivialParser::getVal (std::string name)
00018 {
00019 if (m_config.count (name)) return m_config[name] ;
00020 std::cerr << "[trivialParser] no value for "
00021 << name
00022 << " found\n" ;
00023 return -999999. ;
00024 }
00025
00026
00027
00028
00029
00030 void
00031 trivialParser::parse (std::string configFile)
00032 {
00033 std::ifstream input (configFile.c_str ()) ;
00034 do {
00035 std::string linea = getNextLine (input) ;
00036 if (linea.empty ()) continue ;
00037 std::string name (linea,0,linea.find ('=',0)) ;
00038 eraseSpaces (name) ;
00039 std::string valuestring (linea,linea.find ('=', 0) + 1,
00040 linea.size () - linea.find ('=', 0) - 1) ;
00041 eraseSpaces (valuestring) ;
00042 double value = strtod ( valuestring.c_str (), NULL ) ;
00043 m_config [name] = value ;
00044 } while (!input.eof () ) ;
00045 }
00046
00047
00048
00049
00050
00051 std::string
00052 trivialParser::getNextLine (std::ifstream & input)
00053 {
00054
00055 std::string singleLine ;
00056 do {
00057 getline (input, singleLine,'\n') ;
00058
00059 } while ((
00060 singleLine.find ('#',0) != std::string::npos ||
00061 singleLine.find ('=',0) == std::string::npos ||
00062 singleLine.size () < 3
00063 ) &&
00064 !input.eof ()) ;
00065
00066 return singleLine ;
00067 }
00068
00069
00070
00071
00072
00073 void
00074 trivialParser::print (std::string prefix)
00075 {
00076 std::cerr << "read parameters: " << std::endl ;
00077 for (std::map<std::string, double>::const_iterator mapIT = m_config.begin () ;
00078 mapIT != m_config.end () ;
00079 ++mapIT)
00080 {
00081 std::cerr << prefix << mapIT->first << " = " << mapIT->second << "\n" ;
00082 }
00083 }
00084
00085
00086
00087
00088
00089 void
00090 trivialParser::eraseSpaces (std::string & word)
00091 {
00092 while (word.find (' ',0) != std::string::npos)
00093 {
00094 word.erase (word.find (' ',0), 1) ;
00095 }
00096 return ;
00097 }
00098