blob: 5745e16b46494d7e68a8e3bb27ecf284087da5d3 [file] [log] [blame]
Brad Bishopf3df6b42017-01-06 10:14:09 -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 */
16
Brad Bishopf3df6b42017-01-06 10:14:09 -050017#include <cstdlib>
Tom Joseph1f8a9582017-06-12 20:10:59 +053018#include <fstream>
19#include <string>
20#include "hwmon.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -050021#include "sensorset.hpp"
22
23std::string getEnv(
24 const char* prefix, const SensorSet::key_type& sensor)
25{
26 std::string key;
27 std::string value;
28
29 key.assign(prefix);
30 key.append(1, '_');
31 key.append(sensor.first);
32 key.append(sensor.second);
33 auto env = getenv(key.c_str());
34 if (env)
35 {
36 value.assign(env);
37 }
38
39 return value;
40}
41
Tom Joseph1f8a9582017-06-12 20:10:59 +053042std::string getIndirectLabelEnv(
43 const char* prefix, std::string path, const SensorSet::key_type& sensor)
44{
45 std::string key;
46 std::string value;
47
48 path.append(sensor.first);
49 path.append(sensor.second);
50 path.append(1, '_');
51 path.append(hwmon::entry::label);
52
53 std::ifstream handle(path.c_str());
54 if (handle.fail())
55 {
56 return value;
57 }
58
59 std::string content(
60 (std::istreambuf_iterator<char>(handle)),
61 (std::istreambuf_iterator<char>()));
62
63 if (content.empty())
64 {
65 return value;
66 }
67
68 content.pop_back();
69
70 key.assign(prefix);
71 key.append(1, '_');
72 key.append(sensor.first);
73 key.append(content);
74 auto env = getenv(key.c_str());
75 if (env)
76 {
77 value.assign(env);
78 }
79
80 return value;
81}
82
Brad Bishopf3df6b42017-01-06 10:14:09 -050083// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4