blob: 8dbb679c20f989c10ad843f27e37682c01da28f0 [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
Patrick Venture043d3232018-08-31 10:10:53 -070017#include "env.hpp"
18
19#include "hwmon.hpp"
20
Brad Bishopf3df6b42017-01-06 10:14:09 -050021#include <cstdlib>
Tom Joseph1f8a9582017-06-12 20:10:59 +053022#include <fstream>
Patrick Venture9331ab72018-01-29 09:48:47 -080023
Patrick Venture043d3232018-08-31 10:10:53 -070024namespace env
25{
Patrick Venture7a5285d2018-04-17 19:15:05 -070026
Patrick Venturea24c8802018-04-17 19:38:06 -070027std::string getEnv(const char* key)
28{
29 auto value = std::getenv(key);
30 return (value) ? std::string(value) : std::string();
31}
32
Patrick Venture043d3232018-08-31 10:10:53 -070033std::string getEnv(const char* prefix, const SensorSet::key_type& sensor)
Brad Bishopf3df6b42017-01-06 10:14:09 -050034{
35 std::string key;
Brad Bishopf3df6b42017-01-06 10:14:09 -050036
37 key.assign(prefix);
38 key.append(1, '_');
39 key.append(sensor.first);
40 key.append(sensor.second);
Brad Bishopf3df6b42017-01-06 10:14:09 -050041
Patrick Venturea24c8802018-04-17 19:38:06 -070042 return getEnv(key.c_str());
Brad Bishopf3df6b42017-01-06 10:14:09 -050043}
44
Patrick Venture043d3232018-08-31 10:10:53 -070045std::string getEnv(const char* prefix, const std::string& type,
46 const std::string& id)
Matt Spinler049e0dd2017-10-12 16:33:22 -050047{
48 SensorSet::key_type sensor{type, id};
49 return getEnv(prefix, sensor);
50}
51
Patrick Venture043d3232018-08-31 10:10:53 -070052std::string getIndirectID(std::string path, const std::string& fileSuffix,
53 const SensorSet::key_type& sensor)
Matt Spinler82d507d2017-10-12 16:36:57 -050054{
55 std::string content;
56
57 path.append(sensor.first);
58 path.append(sensor.second);
59 path.append(1, '_');
Matt Spinler7c424802018-05-04 10:52:40 -050060 path.append(fileSuffix);
Matt Spinler82d507d2017-10-12 16:36:57 -050061
62 std::ifstream handle(path.c_str());
63 if (!handle.fail())
64 {
Patrick Venture043d3232018-08-31 10:10:53 -070065 content.assign((std::istreambuf_iterator<char>(handle)),
66 (std::istreambuf_iterator<char>()));
Matt Spinler82d507d2017-10-12 16:36:57 -050067
68 if (!content.empty())
69 {
Patrick Venture043d3232018-08-31 10:10:53 -070070 // remove the newline
Matt Spinler82d507d2017-10-12 16:36:57 -050071 content.pop_back();
72 }
73 }
74
75 return content;
76}
77
Patrick Venture043d3232018-08-31 10:10:53 -070078} // namespace env
Patrick Venture7a5285d2018-04-17 19:15:05 -070079
Brad Bishopf3df6b42017-01-06 10:14:09 -050080// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4