blob: ab4b35c71da4c05bd36254e35ac3af83f9734e75 [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>
Patrick Venture9331ab72018-01-29 09:48:47 -080019
20#include "env.hpp"
Tom Joseph1f8a9582017-06-12 20:10:59 +053021#include "hwmon.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -050022
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
Matt Spinler82d507d2017-10-12 16:36:57 -050051std::string getIndirectID(
52 std::string path,
53 const SensorSet::key_type& sensor)
54{
55 std::string content;
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 content.assign(
66 (std::istreambuf_iterator<char>(handle)),
67 (std::istreambuf_iterator<char>()));
68
69 if (!content.empty())
70 {
71 //remove the newline
72 content.pop_back();
73 }
74 }
75
76 return content;
77}
78
Brad Bishopf3df6b42017-01-06 10:14:09 -050079// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4