xref: /openbmc/phosphor-hwmon/env.cpp (revision c635e8609f5246224dfba7db57991de0b1c5327f)
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  */
16 
17 #include <cstdlib>
18 #include <fstream>
19 
20 #include "env.hpp"
21 #include "hwmon.hpp"
22 
23 namespace env {
24 
25 std::string getEnv(const char* key)
26 {
27     auto value = std::getenv(key);
28     return (value) ? std::string(value) : std::string();
29 }
30 
31 std::string getEnv(
32     const char* prefix, const SensorSet::key_type& sensor)
33 {
34     std::string key;
35 
36     key.assign(prefix);
37     key.append(1, '_');
38     key.append(sensor.first);
39     key.append(sensor.second);
40 
41     return getEnv(key.c_str());
42 }
43 
44 std::string getEnv(
45     const char* prefix,
46     const std::string& type,
47     const std::string& id)
48 {
49     SensorSet::key_type sensor{type, id};
50     return getEnv(prefix, sensor);
51 }
52 
53 std::string getIndirectID(
54         std::string path,
55         const SensorSet::key_type& sensor)
56 {
57     std::string content;
58 
59     path.append(sensor.first);
60     path.append(sensor.second);
61     path.append(1, '_');
62     path.append(hwmon::entry::label);
63 
64     std::ifstream handle(path.c_str());
65     if (!handle.fail())
66     {
67         content.assign(
68                 (std::istreambuf_iterator<char>(handle)),
69                 (std::istreambuf_iterator<char>()));
70 
71         if (!content.empty())
72         {
73             //remove the newline
74             content.pop_back();
75         }
76     }
77 
78     return content;
79 }
80 
81 }  // namespace env
82 
83 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
84