Swarm-NG  1.1
config.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2008-2010 by Mario Juric & Swarm-NG Development Team *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 3 of the License. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
18 
24 #include "../common.hpp"
25 #include "../peyton/util.hpp"
26 
27 #include "config.hpp"
28 
29 using peyton::util::trim;
30 
31 
32 namespace swarm {
33 
35 void config::initialize(const char* const key_value_pairs[][2],const int n){
36  $_(n);
37  for(int i =0 ; i < n ; i++ )
38  insert( std::pair<std::string,std::string>( key_value_pairs[i][0], key_value_pairs[i][1] ) ) ;
39 
40 }
41 
43 config config::load(const std::string &fn, config cfg)
44 {
45  std::ifstream in(fn.c_str());
46  if(!in) ERROR("Cannot open configuration file '" + fn + "'.");
47 
48  std::string line;
49  int iline = 0;
50  while(std::getline(in, line))
51  {
52  iline++;
53  line = trim(line);
54  if(line.empty()) { continue; }
55  if(line[0] == '#') { continue; }
56 
57  size_t eqpos = line.find('=');
58  if(eqpos == std::string::npos) ERROR("Error on line " + line + ": '=' sign expected.");
59 
60  std::string key = trim(line.substr(0, eqpos)), val = trim(line.substr(eqpos+1));
61 
62  cfg[key] = val;
63  }
64  return cfg;
65 }
66 
67 
68 }