GGS(GenericGEANT4Simulation)Software  2.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSInputParser.cpp
Go to the documentation of this file.
1 /*
2  * GGSInputParser.cpp
3  *
4  * Created on: 2010-10-06
5  * Author: Emiliano Mocchiutti
6  */
7 
10 #include "utils/GGSInputParser.h"
11 #include "utils/GGSSmartLog.h"
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <cstring>
16 #include <iostream>
17 #include <fstream>
18 #include <sstream>
19 #include <boost/algorithm/string.hpp>
20 
21 
22 
24  static GGSInputParser parserInstance;
25  return parserInstance;
26 }
27 
28 GGSInputParser::GGSInputParser() {
29 }
30 
31 GGSInputParser::~GGSInputParser() {
32 }
33 
34 void GGSInputParser::ReadInput(std::string fileName) {
35  int argc = 0;
36  std::string mych[1000];
37  ReadInput(fileName, &argc, (std::string *) mych);
38 }
39 
40 void GGSInputParser::ReadInput(std::string fileName, int* argc, std::string* argv) {
41  static const std::string routineName("GGSInputParser::ReadInput");
42 
43  _configFileName = fileName;
44 
45  std::ifstream inputFile(fileName.c_str());
46  if (!inputFile.is_open())
47  return;
48  argv[0] = "Exec";
49  int pargc = 1;
50 
51  if (inputFile.fail()) {
52  COUT(ERROR) << "Error in reading configuration file. No parameter has been loaded." << ENDL;
53  }
54  else {
55  std::string lineData, lineBuffer;
56  while (!(inputFile.eof())) {
57  getline(inputFile, lineBuffer);
58  std::stringstream streamBuffer(lineBuffer, std::ios_base::in);
59  getline(streamBuffer, lineData, '#');
60  if (lineData.length() > 0) {
61  // Search for parameter name
62  std::string::iterator startWord = lineData.begin(), endWord = lineData.begin();
63  while (*startWord == ' ' && startWord != lineData.end())
64  startWord++;
65  endWord = startWord;
66  while (*endWord != ' ' && endWord != lineData.end())
67  endWord++;
68 
69  // Found the parameter name.
70  std::string parName(startWord, endWord);
71  boost::trim(parName);
72 
73  // Search for parameter value
74  startWord = endWord;
75  while (*startWord == ' ' && startWord != lineData.end())
76  startWord++;
77  endWord = startWord;
78  while (*endWord != ' ' && endWord != lineData.end())
79  endWord++;
80 
81  // Found the parameter value.
82  std::string parValue(startWord, endWord);
83  boost::trim(parValue);
84 
85  // Put the name and the value in te parameters vector
86  if (strcmp(parName.c_str(), "")) {
87  argv[pargc] = "";
88  argv[pargc] = parName;
89  pargc++;
90  }
91  if (strcmp(parValue.c_str(), "")) {
92  argv[pargc] = "";
93  argv[pargc] = parValue;
94  pargc++;
95  }
96  _parameters.push_back(std::pair<std::string, std::string>(parName, parValue));
97 
98  }
99  }
100  }
101 
102  *argc = pargc;
103 
104 }
105 
106 const std::string GGSInputParser::_nullValue("");
107 
108 const std::string &GGSInputParser::GetParameter(std::string parameterName) {
109 
110  std::vector<std::pair<std::string, std::string> >::iterator iter = _parameters.begin();
111 
112  for (iter = _parameters.begin(); iter != _parameters.end(); iter++) {
113  if (iter->first == parameterName)
114  return iter->second;
115  }
116 
117  return _nullValue;
118 
119 }
120 
121 bool GGSInputParser::GetFlag(std::string flagName) {
122 
123  std::string auxFlag = GetParameter(flagName);
124  boost::to_lower(auxFlag);
125  if (auxFlag == std::string("true"))
126  return true;
127 
128  return false;
129 }
130 
131 void GGSInputParser::Report() {
132  static const std::string routineName("GGSInputParser::Report");
133  COUT(INFO) << "Parameters read from " << _configFileName << ENDL;
134 
135  std::vector<std::pair<std::string, std::string> >::iterator iter = _parameters.begin();
136  while (iter != _parameters.end()) {
137  CCOUT(INFO) << " - " << iter->first << ": " << iter->second << ENDL;
138  iter++;
139  }
140 
141 }
static GGSInputParser & GetInstance()
Get instance of the singleton.
#define ENDL
Definition: GGSSmartLog.h:93
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:66
#define CCOUT(level)
Smart log utility which prints no header at the beginning of the line.
Definition: GGSSmartLog.h:88
void ReadInput(std::string fileName, int *argc, std::string *argv)
Reads configuration file.
Class needed to parse configuration file.