Format
[apps/agl-service-can-low-level.git] / 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 ini_config::map ini_config::get_keys(const std::string& section, bool wo_prefix)
62 {
63         map ret;
64         std::string key;
65
66         std::string prefix = section + '/';
67         for(auto i = config_.begin();
68                 i != config_.end();
69                 ++i)
70         {
71                 if (starts_with(i->first, prefix))
72                 {
73                         if(wo_prefix)
74                                 key = i->first.substr(section.size()+1);
75                         else
76                                 key = i->first;
77                         ret[key] = i->second;
78                 }
79         }
80         return ret;
81 }
82
83 std::string ini_config::get_value(const std::string& section, const std::string& key)
84 {
85         return config_[section + '/' + key];
86 }
87
88 ini_config::line_type ini_config::qualify(std::string& line)
89 {
90         if (line.size())
91         {
92                 for (std::string::value_type c : line)
93                 {
94                         if (c == '#') return line_type::ignore;
95                         if (c == '[') return line_type::section;
96                         if (!std::isspace(c, std::locale("C"))) return line_type::key;
97                 }
98         }
99         return line_type::ignore;
100 }