Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 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 | |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 17 | #include <cstdlib> |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 18 | #include <fstream> |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 19 | |
| 20 | #include "env.hpp" |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 21 | #include "hwmon.hpp" |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 22 | |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 23 | namespace env { |
| 24 | |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 25 | std::string getEnv(const char* key) |
| 26 | { |
| 27 | auto value = std::getenv(key); |
| 28 | return (value) ? std::string(value) : std::string(); |
| 29 | } |
| 30 | |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 31 | std::string getEnv( |
| 32 | const char* prefix, const SensorSet::key_type& sensor) |
| 33 | { |
| 34 | std::string key; |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 35 | |
| 36 | key.assign(prefix); |
| 37 | key.append(1, '_'); |
| 38 | key.append(sensor.first); |
| 39 | key.append(sensor.second); |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 40 | |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 41 | return getEnv(key.c_str()); |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Matt Spinler | 049e0dd | 2017-10-12 16:33:22 -0500 | [diff] [blame] | 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 | |
Matt Spinler | 82d507d | 2017-10-12 16:36:57 -0500 | [diff] [blame] | 53 | std::string getIndirectID( |
| 54 | std::string path, |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 55 | const std::string& fileSuffix, |
Matt Spinler | 82d507d | 2017-10-12 16:36:57 -0500 | [diff] [blame] | 56 | 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, '_'); |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 63 | path.append(fileSuffix); |
Matt Spinler | 82d507d | 2017-10-12 16:36:57 -0500 | [diff] [blame] | 64 | |
| 65 | std::ifstream handle(path.c_str()); |
| 66 | if (!handle.fail()) |
| 67 | { |
| 68 | content.assign( |
| 69 | (std::istreambuf_iterator<char>(handle)), |
| 70 | (std::istreambuf_iterator<char>())); |
| 71 | |
| 72 | if (!content.empty()) |
| 73 | { |
| 74 | //remove the newline |
| 75 | content.pop_back(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return content; |
| 80 | } |
| 81 | |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 82 | } // namespace env |
| 83 | |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 84 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |