1 /*
2  * Copyright (C) 2019 The Android Open Source Project
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 #pragma once
17 
18 #include <android-base/macros.h>
19 #include <android-base/parsedouble.h>
20 #include <android-base/properties.h>
21 #include <log/log.h>
22 
23 #include <fstream>
24 #include <map>
25 #include <sstream>
26 
27 namespace aidl {
28 namespace android {
29 namespace hardware {
30 namespace vibrator {
31 namespace utils {
32 
33 template <typename T>
34 class Is_Iterable {
35   private:
36     template <typename U>
37     static std::true_type test(typename U::iterator *u);
38 
39     template <typename U>
40     static std::false_type test(U *u);
41 
42   public:
43     static const bool value = decltype(test<T>(0))::value;
44 };
45 
46 template <typename T, bool B>
47 using Enable_If_Iterable = std::enable_if_t<Is_Iterable<T>::value == B>;
48 
49 template <typename T, typename U = void>
50 using Enable_If_Signed = std::enable_if_t<std::is_signed_v<T>, U>;
51 
52 template <typename T, typename U = void>
53 using Enable_If_Unsigned = std::enable_if_t<std::is_unsigned_v<T>, U>;
54 
55 // override for default behavior of printing as a character
56 inline std::ostream &operator<<(std::ostream &stream, const int8_t value) {
57     return stream << +value;
58 }
59 // override for default behavior of printing as a character
60 inline std::ostream &operator<<(std::ostream &stream, const uint8_t value) {
61     return stream << +value;
62 }
63 
64 template <typename T>
toUnderlying(const T value)65 inline auto toUnderlying(const T value) {
66     return static_cast<std::underlying_type_t<T>>(value);
67 }
68 
69 template <typename T>
unpack(std::istream & stream,T * value)70 inline Enable_If_Iterable<T, true> unpack(std::istream &stream, T *value) {
71     for (auto &entry : *value) {
72         stream >> entry;
73     }
74 }
75 
76 template <typename T>
unpack(std::istream & stream,T * value)77 inline Enable_If_Iterable<T, false> unpack(std::istream &stream, T *value) {
78     stream >> *value;
79 }
80 
81 template <>
82 inline void unpack<std::string>(std::istream &stream, std::string *value) {
83     *value = std::string(std::istreambuf_iterator(stream), {});
84     stream.setstate(std::istream::eofbit);
85 }
86 
87 template <typename T>
getProperty(const std::string & key,const T def)88 inline Enable_If_Signed<T, T> getProperty(const std::string &key, const T def) {
89     if (std::is_floating_point_v<T>) {
90         float result;
91         std::string value = ::android::base::GetProperty(key, "");
92         if (!value.empty() && ::android::base::ParseFloat(value, &result)) {
93             return result;
94         }
95         return def;
96     } else {
97         return ::android::base::GetIntProperty(key, def);
98     }
99 }
100 
101 template <typename T>
getProperty(const std::string & key,const T def)102 inline Enable_If_Unsigned<T, T> getProperty(const std::string &key, const T def) {
103     return ::android::base::GetUintProperty(key, def);
104 }
105 
106 template <>
107 inline bool getProperty<bool>(const std::string &key, const bool def) {
108     return ::android::base::GetBoolProperty(key, def);
109 }
110 
111 template <typename T>
openNoCreate(const std::string & file,T * outStream)112 static void openNoCreate(const std::string &file, T *outStream) {
113     auto mode = std::is_base_of_v<std::ostream, T> ? std::ios_base::out : std::ios_base::in;
114 
115     // Force 'in' mode to prevent file creation
116     outStream->open(file, mode | std::ios_base::in);
117     if (!*outStream) {
118         ALOGE("Failed to open %s (%d): %s", file.c_str(), errno, strerror(errno));
119     }
120 }
121 
122 template <typename T>
123 static void fileFromEnv(const char *env, T *outStream, std::string *outName = nullptr) {
124     auto file = std::getenv(env);
125 
126     if (file == nullptr) {
127         ALOGE("Failed get env %s", env);
128         return;
129     }
130 
131     if (outName != nullptr) {
132         *outName = std::string(file);
133     }
134 
135     openNoCreate(file, outStream);
136 }
137 
138 static ATTRIBUTE_UNUSED auto pathsFromEnv(const char *env, const std::string &prefix = "") {
139     std::map<std::string, std::ifstream> ret;
140     auto value = std::getenv(env);
141 
142     if (value == nullptr) {
143         return ret;
144     }
145 
146     std::istringstream paths{value};
147     std::string path;
148 
149     while (paths >> path) {
150         ret[path].open(prefix + path);
151     }
152 
153     return ret;
154 }
155 
156 static ATTRIBUTE_UNUSED std::string trim(const std::string &str,
157                                          const std::string &whitespace = " \t") {
158     const auto str_begin = str.find_first_not_of(whitespace);
159     if (str_begin == std::string::npos) {
160         return "";
161     }
162 
163     const auto str_end = str.find_last_not_of(whitespace);
164     const auto str_range = str_end - str_begin + 1;
165 
166     return str.substr(str_begin, str_range);
167 }
168 
169 }  // namespace utils
170 }  // namespace vibrator
171 }  // namespace hardware
172 }  // namespace android
173 }  // namespace aidl
174