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