blob: b8c3815fe651bd62c7a8f94d915c05fd8ae20982 [file] [log] [blame]
Brad Bishopf3df6b42017-01-06 10:14:09 -05001#pragma once
2
Patrick Venture9331ab72018-01-29 09:48:47 -08003#include "sensorset.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -05004
Kun Yibd9bc002019-08-21 10:05:27 -07005#include <fstream>
Patrick Venture043d3232018-08-31 10:10:53 -07006#include <string>
7
8namespace env
9{
Patrick Venture7a5285d2018-04-17 19:15:05 -070010
Kun Yibd9bc002019-08-21 10:05:27 -070011/** @class Env
12 * @brief Overridable std::getenv interface
13 */
14struct 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 */
24struct EnvImpl : public Env
25{
26 const char* get(const char* key) const override;
27};
28
29/** @brief Default instantiation of Env */
30extern EnvImpl env_impl;
31
Matt Spinler049e0dd2017-10-12 16:33:22 -050032/** @brief Reads an environment variable
33 *
Patrick Venturea24c8802018-04-17 19:38:06 -070034 * Reads the environment for that key
35 *
36 * @param[in] key - the key
Kun Yibd9bc002019-08-21 10:05:27 -070037 * @param[in] env - env interface that defaults to calling std::getenv
Patrick Venturea24c8802018-04-17 19:38:06 -070038 *
39 * @return string - the env var value
40 */
Kun Yibd9bc002019-08-21 10:05:27 -070041inline 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 Venturea24c8802018-04-17 19:38:06 -070047
48/** @brief Reads an environment variable
49 *
Matt Spinler049e0dd2017-10-12 16:33:22 -050050 * Reads <prefix>_<sensor.first><sensor.second>
51 *
52 * @param[in] prefix - the variable prefix
53 * @param[in] sensor - Sensor details
Kun Yibd9bc002019-08-21 10:05:27 -070054 * @param[in] env - env interface that defaults to calling std::getenv
Matt Spinler049e0dd2017-10-12 16:33:22 -050055 *
56 * @return string - the env var value
57 */
Kun Yibd9bc002019-08-21 10:05:27 -070058inline 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 Joseph1f8a9582017-06-12 20:10:59 +053070
Matt Spinler049e0dd2017-10-12 16:33:22 -050071/** @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 Yibd9bc002019-08-21 10:05:27 -070076 * @param[in] env - env interface that defaults to calling std::getenv
Matt Spinler049e0dd2017-10-12 16:33:22 -050077 *
78 * @return string - the env var value
79 */
Kun Yibd9bc002019-08-21 10:05:27 -070080inline 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 Spinler049e0dd2017-10-12 16:33:22 -050086
Matt Spinler82d507d2017-10-12 16:36:57 -050087/** @brief Gets the ID for the sensor with a level of indirection
88 *
Matt Spinler7c424802018-05-04 10:52:40 -050089 * Read the ID from the <path>/<item><X>_<suffix> file.
Matt Spinler82d507d2017-10-12 16:36:57 -050090 * <item> & <X> are populated from the sensor key.
91 *
92 * @param[in] path - Directory path of the label file
Matt Spinler7c424802018-05-04 10:52:40 -050093 * @param[in] fileSuffix - The file suffix
Matt Spinler82d507d2017-10-12 16:36:57 -050094 * @param[in] sensor - Sensor details
95 */
Kun Yibd9bc002019-08-21 10:05:27 -070096inline 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 Venture7a5285d2018-04-17 19:15:05 -0700122
Patrick Venture043d3232018-08-31 10:10:53 -0700123} // namespace env