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 | */ |
Matthew Barth | 6292aee | 2016-10-06 10:15:48 -0500 | [diff] [blame] | 16 | #include "sensorset.hpp" |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 17 | |
Brad Bishop | 6e8f623 | 2017-01-04 15:39:03 -0500 | [diff] [blame] | 18 | #include "hwmon.hpp" |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 19 | |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame] | 20 | #include <filesystem> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <regex> |
| 23 | |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 24 | // TODO: Issue#2 - STL regex generates really bloated code. Use POSIX regex |
| 25 | // interfaces instead. |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 26 | static const std::regex sensors_regex = std::regex( |
| 27 | "^(fan|in|temp|power|energy|curr)([0-9]+)_([a-z]*)", std::regex::extended); |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 28 | static const auto sensor_regex_match_count = 4; |
| 29 | |
| 30 | SensorSet::SensorSet(const std::string& path) |
| 31 | { |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame] | 32 | namespace fs = std::filesystem; |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 33 | |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 34 | for (const auto& file : fs::directory_iterator(path)) |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 35 | { |
| 36 | std::smatch match; |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 37 | auto fileName = file.path().filename(); |
| 38 | std::regex_search(fileName.native(), match, sensors_regex); |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 39 | |
Brad Bishop | 6bb97a9 | 2016-12-19 13:06:40 -0500 | [diff] [blame] | 40 | if (match.size() != sensor_regex_match_count) |
| 41 | { |
| 42 | continue; |
| 43 | } |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 44 | |
Brad Bishop | 6e8f623 | 2017-01-04 15:39:03 -0500 | [diff] [blame] | 45 | if (match[3] == hwmon::entry::label) |
| 46 | { |
| 47 | continue; |
| 48 | } |
| 49 | |
Patrick Venture | d0f5097 | 2018-12-19 14:43:12 -0800 | [diff] [blame] | 50 | _container[make_pair(match[1], match[2])].emplace(match[3]); |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 51 | } |
| 52 | } |
Brad Bishop | 03476f1 | 2016-12-19 13:09:12 -0500 | [diff] [blame] | 53 | |
| 54 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |