cawlign 0.0.1
Codon-aware pairwise sequence alignment
Loading...
Searching...
No Matches
configparser.hpp
1// Copyright (c) 2018 Daniel Zilles
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21#ifndef CONFIGPARSER_HPP
22#define CONFIGPARSER_HPP
23
24#include <string>
25#include <vector>
26#include <map>
27#include <iostream>
28#include <sstream>
29
30typedef std::map<std::string, std::vector<std::string>> configList;
31
33
34 public:
35 ConfigParser(std::ifstream& configFile);
36
37 bool hasConfig(std::string section, std::string name) const {
38 return mConfigurations.find(section + " - " + name) != mConfigurations.end();
39 }
40 template<typename T>
41 T aConfig(std::string section, std::string name, size_t pos = 0);
42 template<typename T>
43 std::vector<T> aConfigVec(std::string section, std::string name);
44
45
46 private:
47 static void handleMissingKey (std::string);
48 configList mConfigurations;
49
50};
51
52template <>
53bool ConfigParser::aConfig<bool>(std::string section, std::string name, size_t pos);
54
55template <typename T>
56T ConfigParser::aConfig(std::string section, std::string configName, size_t pos) {
57
58 T tmp;
59
60 const auto& mConfigRef = mConfigurations;
61 auto search = mConfigRef.find(section + " - " + configName);
62
63 if (search == mConfigRef.end()) {
64 handleMissingKey (std::string("Could not find required configuration section ") + section + std::string(" key ") + configName);
65 }
66
67 std::istringstream iss(search->second[0]);
68
69 if (search->second[0].find( "0x" ) != std::string::npos)
70 iss >> std::hex >> tmp;
71 else
72 iss >> std::dec >> tmp;
73
74 return tmp;
75}
76
77template <>
78std::vector<bool> ConfigParser::aConfigVec<bool>(std::string section, std::string name);
79
80template <typename T>
81std::vector<T> ConfigParser::aConfigVec(std::string section, std::string configName) {
82
83
84 const auto& mConfigRef = mConfigurations;
85 auto search = mConfigRef.find(section + " - " + configName);
86
87 if (search == mConfigRef.end()) {
88 handleMissingKey (std::string("Could not find required configuration section ") + section + std::string(" key ") + configName);
89 }
90
91
92 std::vector<T> tmp(search->second.size());
93 for (unsigned i = 0; i < search->second.size(); i++) {
94
95 std::istringstream iss(search->second[i]);
96
97 if (search->second[i].find( "0x" ) != std::string::npos)
98 iss >> std::hex >> tmp[i];
99 else
100 iss >> std::dec >> tmp[i];
101 }
102 return tmp;
103}
104#endif
ConfigParser(std::ifstream &configFile)
Definition configparser.cpp:46