1 /*
2  * Copyright (C) 2016 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 
17 #pragma once
18 
19 #include <errno.h>
20 #include <stdlib.h>
21 
22 #include <limits>
23 #include <string>
24 
25 namespace android {
26 namespace base {
27 
28 // Parse floating value in the string 's' and sets 'out' to that value if it exists.
29 // Optionally allows the caller to define a 'min' and 'max' beyond which
30 // otherwise valid values will be rejected. Returns boolean success.
31 template <typename T, T (*strtox)(const char* str, char** endptr)>
ParseFloatingPoint(const char * s,T * out,T min,T max)32 static inline bool ParseFloatingPoint(const char* s, T* out, T min, T max) {
33   errno = 0;
34   char* end;
35   T result = strtox(s, &end);
36   if (errno != 0 || s == end || *end != '\0') {
37     return false;
38   }
39   if (result < min || max < result) {
40     return false;
41   }
42   if (out != nullptr) {
43     *out = result;
44   }
45   return true;
46 }
47 
48 // Parse double value in the string 's' and sets 'out' to that value if it exists.
49 // Optionally allows the caller to define a 'min' and 'max' beyond which
50 // otherwise valid values will be rejected. Returns boolean success.
51 static inline bool ParseDouble(const char* s, double* out,
52                                double min = std::numeric_limits<double>::lowest(),
53                                double max = std::numeric_limits<double>::max()) {
54   return ParseFloatingPoint<double, strtod>(s, out, min, max);
55 }
56 static inline bool ParseDouble(const std::string& s, double* out,
57                                double min = std::numeric_limits<double>::lowest(),
58                                double max = std::numeric_limits<double>::max()) {
59   return ParseFloatingPoint<double, strtod>(s.c_str(), out, min, max);
60 }
61 
62 // Parse float value in the string 's' and sets 'out' to that value if it exists.
63 // Optionally allows the caller to define a 'min' and 'max' beyond which
64 // otherwise valid values will be rejected. Returns boolean success.
65 static inline bool ParseFloat(const char* s, float* out,
66                               float min = std::numeric_limits<float>::lowest(),
67                               float max = std::numeric_limits<float>::max()) {
68   return ParseFloatingPoint<float, strtof>(s, out, min, max);
69 }
70 static inline bool ParseFloat(const std::string& s, float* out,
71                               float min = std::numeric_limits<float>::lowest(),
72                               float max = std::numeric_limits<float>::max()) {
73   return ParseFloatingPoint<float, strtof>(s.c_str(), out, min, max);
74 }
75 
76 }  // namespace base
77 }  // namespace android
78