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