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 template<typename T>
38 T aConfig(std::string section, std::string name, size_t pos = 0);
39 template<typename T>
40 std::vector<T> aConfigVec(std::string section, std::string name);
41
42
43 private:
44 static void handleMissingKey (std::string);
45 configList mConfigurations;
46
47};
48
49template <>
50bool ConfigParser::aConfig<bool>(std::string section, std::string name, size_t pos);
51
52template <typename T>
53T ConfigParser::aConfig(std::string section, std::string configName, size_t pos) {
54
55 T tmp;
56
57 const auto& mConfigRef = mConfigurations;
58 auto search = mConfigRef.find(section + " - " + configName);
59
60 if (search == mConfigRef.end()) {
61 handleMissingKey (std::string("Could not find required configuration section ") + section + std::string(" key ") + configName);
62 }
63
64 std::istringstream iss(search->second[0]);
65
66 if (search->second[0].find( "0x" ) != std::string::npos)
67 iss >> std::hex >> tmp;
68 else
69 iss >> std::dec >> tmp;
70
71 return tmp;
72}
73
74template <>
75std::vector<bool> ConfigParser::aConfigVec<bool>(std::string section, std::string name);
76
77template <typename T>
78std::vector<T> ConfigParser::aConfigVec(std::string section, std::string configName) {
79
80
81 const auto& mConfigRef = mConfigurations;
82 auto search = mConfigRef.find(section + " - " + configName);
83
84 if (search == mConfigRef.end()) {
85 handleMissingKey (std::string("Could not find required configuration section ") + section + std::string(" key ") + configName);
86 }
87
88
89 std::vector<T> tmp(search->second.size());
90 for (unsigned i = 0; i < search->second.size(); i++) {
91
92 std::istringstream iss(search->second[i]);
93
94 if (search->second[i].find( "0x" ) != std::string::npos)
95 iss >> std::hex >> tmp[i];
96 else
97 iss >> std::dec >> tmp[i];
98 }
99 return tmp;
100}
101#endif
Definition configparser.hpp:32