Avoid warning about unmanaged switch case in INI library
[apps/low-level-can-service.git] / CAN-binder / libs / ini-config / ini-config.cpp
1 /*
2  * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
3  * Author "Loïc Collignon" <loic.collignon@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "ini-config.hpp"
19
20 bool starts_with(const std::string& text, const std::string& token) 
21 {
22         if(text.length() < token.length()) return false;
23         return (text.compare(0, token.length(), token) == 0);
24 }
25
26 void ini_config::read_file(const std::string& filename)
27 {
28         std::ifstream f(filename);
29         if (f)
30         {
31                 std::regex r_section("^\\s*\\[([^\\]]+)\\]\\s*(#.*)?$");
32                 std::regex r_key("^\\s*([^\\s]+)\\s*=\\s*\"([^\"]+)\"\\s*(#.*)?$");
33                 std::string current_section;
34                 std::string line;
35                 while (std::getline(f, line))
36                 {
37                         std::smatch mr;
38
39                         switch (qualify(line))
40                         {
41                         case line_type::section:
42                                 if (std::regex_match(line, mr, r_section) && mr.size() >= 2 && mr[1].matched)
43                                 {
44                                         current_section = mr[1].str();
45                                 }
46                                 break;
47                         case line_type::key:
48                                 if(std::regex_match(line, mr, r_key) && mr.size() >= 2 && mr[1].matched)
49                                 {
50                                         std::string key = current_section + '/' + mr[1].str();
51                                         config_[key] = (mr.size() >= 3 && mr[2].matched) ? mr[2].str() : "";
52                                 }
53                                 break;
54                         case line_type::ignore:
55                                 break;
56                         }
57                 }
58         }
59 }
60
61 std::map<std::string, std::string> ini_config::get_keys(const std::string& section)
62 {
63         std::map<std::string, std::string> ret;
64         
65         std::string prefix = section + '/';
66         for(auto i = config_.begin();
67                 i != config_.end();
68                 ++i)
69         {
70                 if (starts_with(i->first, prefix))
71                 {
72                         ret[i->first] = i->second;
73                 }
74         }
75         return ret;
76 }
77
78 std::string ini_config::get_value(const std::string& section, const std::string& key)
79 {
80         return config_[section + '/' + key];
81 }
82
83 ini_config::line_type ini_config::qualify(std::string& line)
84 {
85         if (line.size())
86         {
87                 for (std::string::value_type c : line)
88                 {
89                         if (c == '#') return line_type::ignore;
90                         if (c == '[') return line_type::section;
91                         if (!std::isspace(c, std::locale("C"))) return line_type::key;
92                 }
93         }
94         return line_type::ignore;
95 }