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