blob: 2952775b4f93f387c151992fcf62c7e0eb21c9f9 [file] [log] [blame]
Brad Bishop29dbfa62016-12-19 13:39:57 -05001/**
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 */
Brad Bishop08379a32017-03-06 21:28:46 -050016#include <experimental/filesystem>
Patrick Williams3667cf32015-10-20 22:39:11 -050017#include <regex>
18#include <iostream>
Matthew Barth6292aee2016-10-06 10:15:48 -050019#include "sensorset.hpp"
Brad Bishop6e8f6232017-01-04 15:39:03 -050020#include "hwmon.hpp"
Patrick Williams3667cf32015-10-20 22:39:11 -050021
22// TODO: Issue#2 - STL regex generates really bloated code. Use POSIX regex
23// interfaces instead.
24static const std::regex sensors_regex =
Brad Bishop5afe21a2017-01-06 20:44:05 -050025 std::regex("^(fan|in|temp|power|energy|curr)([0-9]+)_([a-z]*)",
26 std::regex::extended);
Patrick Williams3667cf32015-10-20 22:39:11 -050027static const auto sensor_regex_match_count = 4;
28
29SensorSet::SensorSet(const std::string& path)
30{
Brad Bishop08379a32017-03-06 21:28:46 -050031 namespace fs = std::experimental::filesystem;
Patrick Williams3667cf32015-10-20 22:39:11 -050032
Brad Bishop08379a32017-03-06 21:28:46 -050033 for (const auto& file : fs::directory_iterator(path))
Patrick Williams3667cf32015-10-20 22:39:11 -050034 {
35 std::smatch match;
Brad Bishop08379a32017-03-06 21:28:46 -050036 auto fileName = file.path().filename();
37 std::regex_search(fileName.native(), match, sensors_regex);
Patrick Williams3667cf32015-10-20 22:39:11 -050038
Brad Bishop6bb97a92016-12-19 13:06:40 -050039 if (match.size() != sensor_regex_match_count)
40 {
41 continue;
42 }
Patrick Williams3667cf32015-10-20 22:39:11 -050043
Brad Bishop6e8f6232017-01-04 15:39:03 -050044 if (match[3] == hwmon::entry::label)
45 {
46 continue;
47 }
48
Brad Bishop6bb97a92016-12-19 13:06:40 -050049 container[make_pair(match[1], match[2])].emplace(match[3]);
Patrick Williams3667cf32015-10-20 22:39:11 -050050 }
51}
Brad Bishop03476f12016-12-19 13:09:12 -050052
53// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4