blob: efd081bc4dde5fafcaae288b1e16f5f4fb3fa69c [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
Matt Spinler049e0dd2017-10-12 16:33:22 -050042std::string getEnv(
43 const char* prefix,
44 const std::string& type,
45 const std::string& id)
46{
47 SensorSet::key_type sensor{type, id};
48 return getEnv(prefix, sensor);
49}
50
Tom Joseph1f8a9582017-06-12 20:10:59 +053051std::string getIndirectLabelEnv(
52 const char* prefix, std::string path, const SensorSet::key_type& sensor)
53{
54 std::string key;
55 std::string value;
56
57 path.append(sensor.first);
58 path.append(sensor.second);
59 path.append(1, '_');
60 path.append(hwmon::entry::label);
61
62 std::ifstream handle(path.c_str());
63 if (handle.fail())
64 {
65 return value;
66 }
67
68 std::string content(
69 (std::istreambuf_iterator<char>(handle)),
70 (std::istreambuf_iterator<char>()));
71
72 if (content.empty())
73 {
74 return value;
75 }
76
77 content.pop_back();
78
79 key.assign(prefix);
80 key.append(1, '_');
81 key.append(sensor.first);
82 key.append(content);
83 auto env = getenv(key.c_str());
84 if (env)
85 {
86 value.assign(env);
87 }
88
89 return value;
90}
91
Brad Bishopf3df6b42017-01-06 10:14:09 -050092// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4