Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 3 | #include "sensorset.hpp" |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 4 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 5 | #include <fstream> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 6 | #include <string> |
| 7 | |
| 8 | namespace env |
| 9 | { |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 10 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 11 | /** @class Env |
| 12 | * @brief Overridable std::getenv interface |
| 13 | */ |
| 14 | struct Env |
| 15 | { |
| 16 | virtual ~Env() = default; |
| 17 | |
| 18 | virtual const char* get(const char* key) const = 0; |
| 19 | }; |
| 20 | |
| 21 | /** @class EnvImpl |
| 22 | * @brief Concrete implementation that calls std::getenv |
| 23 | */ |
| 24 | struct EnvImpl : public Env |
| 25 | { |
| 26 | const char* get(const char* key) const override; |
| 27 | }; |
| 28 | |
| 29 | /** @brief Default instantiation of Env */ |
| 30 | extern EnvImpl env_impl; |
| 31 | |
Matt Spinler | 049e0dd | 2017-10-12 16:33:22 -0500 | [diff] [blame] | 32 | /** @brief Reads an environment variable |
| 33 | * |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 34 | * Reads the environment for that key |
| 35 | * |
| 36 | * @param[in] key - the key |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 37 | * @param[in] env - env interface that defaults to calling std::getenv |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 38 | * |
| 39 | * @return string - the env var value |
| 40 | */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 41 | inline std::string getEnv(const char* key, const Env* env = &env_impl) |
| 42 | { |
| 43 | // Be aware value could be nullptr |
| 44 | auto value = env->get(key); |
| 45 | return (value) ? std::string(value) : std::string(); |
| 46 | } |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 47 | |
| 48 | /** @brief Reads an environment variable |
| 49 | * |
Matt Spinler | 049e0dd | 2017-10-12 16:33:22 -0500 | [diff] [blame] | 50 | * Reads <prefix>_<sensor.first><sensor.second> |
| 51 | * |
| 52 | * @param[in] prefix - the variable prefix |
| 53 | * @param[in] sensor - Sensor details |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 54 | * @param[in] env - env interface that defaults to calling std::getenv |
Matt Spinler | 049e0dd | 2017-10-12 16:33:22 -0500 | [diff] [blame] | 55 | * |
| 56 | * @return string - the env var value |
| 57 | */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 58 | inline std::string getEnv(const char* prefix, const SensorSet::key_type& sensor, |
| 59 | const Env* env = &env_impl) |
| 60 | { |
| 61 | std::string key; |
| 62 | |
| 63 | key.assign(prefix); |
| 64 | key.append(1, '_'); |
| 65 | key.append(sensor.first); |
| 66 | key.append(sensor.second); |
| 67 | |
| 68 | return getEnv(key.c_str(), env); |
| 69 | } |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 70 | |
Matt Spinler | 049e0dd | 2017-10-12 16:33:22 -0500 | [diff] [blame] | 71 | /** @brief Reads an environment variable, and takes type and id separately |
| 72 | * |
| 73 | * @param[in] prefix - the variable prefix |
| 74 | * @param[in] type - sensor type, like 'temp' |
| 75 | * @param[in] id - sensor ID, like '5' |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 76 | * @param[in] env - env interface that defaults to calling std::getenv |
Matt Spinler | 049e0dd | 2017-10-12 16:33:22 -0500 | [diff] [blame] | 77 | * |
| 78 | * @return string - the env var value |
| 79 | */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 80 | inline std::string getEnv(const char* prefix, const std::string& type, |
| 81 | const std::string& id, const Env* env = &env_impl) |
| 82 | { |
| 83 | SensorSet::key_type sensor{type, id}; |
| 84 | return getEnv(prefix, sensor, env); |
| 85 | } |
Matt Spinler | 049e0dd | 2017-10-12 16:33:22 -0500 | [diff] [blame] | 86 | |
Matt Spinler | 82d507d | 2017-10-12 16:36:57 -0500 | [diff] [blame] | 87 | /** @brief Gets the ID for the sensor with a level of indirection |
| 88 | * |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 89 | * Read the ID from the <path>/<item><X>_<suffix> file. |
Matt Spinler | 82d507d | 2017-10-12 16:36:57 -0500 | [diff] [blame] | 90 | * <item> & <X> are populated from the sensor key. |
| 91 | * |
| 92 | * @param[in] path - Directory path of the label file |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 93 | * @param[in] fileSuffix - The file suffix |
Matt Spinler | 82d507d | 2017-10-12 16:36:57 -0500 | [diff] [blame] | 94 | * @param[in] sensor - Sensor details |
| 95 | */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 96 | inline std::string getIndirectID(std::string path, |
| 97 | const std::string& fileSuffix, |
| 98 | const SensorSet::key_type& sensor) |
| 99 | { |
| 100 | std::string content; |
| 101 | |
| 102 | path.append(sensor.first); |
| 103 | path.append(sensor.second); |
| 104 | path.append(1, '_'); |
| 105 | path.append(fileSuffix); |
| 106 | |
| 107 | std::ifstream handle(path.c_str()); |
| 108 | if (!handle.fail()) |
| 109 | { |
| 110 | content.assign((std::istreambuf_iterator<char>(handle)), |
| 111 | (std::istreambuf_iterator<char>())); |
| 112 | |
| 113 | if (!content.empty()) |
| 114 | { |
| 115 | // remove the newline |
| 116 | content.pop_back(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return content; |
| 121 | } |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 122 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 123 | } // namespace env |