NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
configuration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
28 #include <iostream>
29 #include <sstream>
30 #include <fstream>
31 #include "configuration.h"
32 
33 using namespace std;
34 
36 configuration::~configuration(){}
37 
38 int configuration::getintvalue( const std::string& key )
39 {
40  if (!iskey( key )){
41  std::cout<<"Cannot find the key in the config file, Key: "<<key<<endl;
42  throw 0;
43  }
44  std::istringstream ss( this->data.operator [] ( key ) );
45  int result;
46  ss >> result;
47  if (!ss.eof())
48  {
49  std::cout<<"Problematic key: "<<key<<endl;
50  std::cout<<"Error reading configuration file"<<endl;
51  throw 1;
52  }
53  return result;
54 }
55 
56 
57 double configuration::getDoubleValue(const std::string& key )
58 {
59  if (!iskey( key )) throw 0;
60  std::istringstream ss( this->data.operator [] ( key ) );
61  double result;
62  ss >> result;
63  if (!ss.eof()) throw 1;
64  return result;
65 }
66 
67 std::string configuration::getStringValue(const std::string& key )
68 {
69  if (!iskey( key )) throw 0;
70  std::istringstream ss( this->data.operator [] ( key ) );
71  string result;
72  ss >> result;
73  if (!ss.eof()) throw 1;
74  return result;
75 }
76 
77 void configuration::readFile(const std::string filename)
78 {
79  std::string s, key, value;
80  std::ifstream confFile(&filename[0]);
81  if(!confFile.is_open())
82  {
83  std::cout<<"Warning! Config.ini file not found!"<<std::endl;
84  return;
85  }
86 
87  // For each (key, value) pair in the file
88  while (std::getline( confFile, s ))
89  {
90  std::string::size_type begin = s.find_first_not_of( " \f\t\v" );
91 
92  // Skip blank lines
93  if (begin == std::string::npos) continue;
94 
95  // Skip commentary
96  if (std::string( "#;" ).find( s[ begin ] ) != std::string::npos) continue;
97 
98  // Extract the key value
99  std::string::size_type end = s.find( '=', begin );
100  key = s.substr( begin, end - begin );
101 
102  // (No leading or trailing whitespace allowed)
103  key.erase( key.find_last_not_of( " \f\t\v" ) + 1 );
104 
105  // No blank keys allowed
106  if (key.empty()) continue;
107 
108  // Extract the value (no leading or trailing whitespace allowed)
109  begin = s.find_first_not_of( " \f\n\r\t\v", end + 1 );
110  end = s.find_last_not_of( " \f\n\r\t\v" ) + 1;
111 
112  value = s.substr( begin, end - begin );
113 
114  // Insert the properly extracted (key, value) pair into the map
115  this->data[ key ] = value;
116  }
117  confFile.close();
118  return;
119 }
120 
121 
122 void configuration::writeToFile(const std::string filename)
123 {
124  ofstream confFile(&filename[0]);
125  for (std::map <std::string, std::string>::const_iterator iter = this->data.begin(); iter != this->data.end(); iter++)
126  confFile << iter->first << " = " << iter->second << endl;
127  confFile.close();
128  return;
129 }
A class to read a learning configuration from a .ini file.