Brad Bishop | 29dbfa6 | 2016-12-19 13:39:57 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 16 | #include <regex> |
| 17 | #include <iostream> |
Matthew Barth | 6292aee | 2016-10-06 10:15:48 -0500 | [diff] [blame] | 18 | #include "sensorset.hpp" |
| 19 | #include "directory.hpp" |
Brad Bishop | 6e8f623 | 2017-01-04 15:39:03 -0500 | [diff] [blame] | 20 | #include "hwmon.hpp" |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 21 | |
| 22 | // TODO: Issue#2 - STL regex generates really bloated code. Use POSIX regex |
| 23 | // interfaces instead. |
| 24 | static const std::regex sensors_regex = |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame^] | 25 | std::regex("^(fan|in|temp|power|energy|curr)([0-9]+)_([a-z]*)", |
| 26 | std::regex::extended); |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 27 | static const auto sensor_regex_match_count = 4; |
| 28 | |
| 29 | SensorSet::SensorSet(const std::string& path) |
| 30 | { |
| 31 | Directory d(path); |
| 32 | std::string file; |
| 33 | |
Brad Bishop | 6bb97a9 | 2016-12-19 13:06:40 -0500 | [diff] [blame] | 34 | while (d.next(file)) |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 35 | { |
| 36 | std::smatch match; |
| 37 | std::regex_search(file, match, sensors_regex); |
| 38 | |
Brad Bishop | 6bb97a9 | 2016-12-19 13:06:40 -0500 | [diff] [blame] | 39 | if (match.size() != sensor_regex_match_count) |
| 40 | { |
| 41 | continue; |
| 42 | } |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 43 | |
Brad Bishop | 6e8f623 | 2017-01-04 15:39:03 -0500 | [diff] [blame] | 44 | if (match[3] == hwmon::entry::label) |
| 45 | { |
| 46 | continue; |
| 47 | } |
| 48 | |
Brad Bishop | 6bb97a9 | 2016-12-19 13:06:40 -0500 | [diff] [blame] | 49 | container[make_pair(match[1], match[2])].emplace(match[3]); |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 50 | } |
| 51 | } |
Brad Bishop | 03476f1 | 2016-12-19 13:09:12 -0500 | [diff] [blame] | 52 | |
| 53 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |