1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker *
4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker *
8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker *
10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker */
16*8f0ba417SAndroid Build Coastguard Worker
17*8f0ba417SAndroid Build Coastguard Worker #pragma once
18*8f0ba417SAndroid Build Coastguard Worker
19*8f0ba417SAndroid Build Coastguard Worker #include <errno.h>
20*8f0ba417SAndroid Build Coastguard Worker #include <stdlib.h>
21*8f0ba417SAndroid Build Coastguard Worker
22*8f0ba417SAndroid Build Coastguard Worker #include <limits>
23*8f0ba417SAndroid Build Coastguard Worker #include <string>
24*8f0ba417SAndroid Build Coastguard Worker
25*8f0ba417SAndroid Build Coastguard Worker namespace android {
26*8f0ba417SAndroid Build Coastguard Worker namespace base {
27*8f0ba417SAndroid Build Coastguard Worker
28*8f0ba417SAndroid Build Coastguard Worker // Parse floating value in the string 's' and sets 'out' to that value if it exists.
29*8f0ba417SAndroid Build Coastguard Worker // Optionally allows the caller to define a 'min' and 'max' beyond which
30*8f0ba417SAndroid Build Coastguard Worker // otherwise valid values will be rejected. Returns boolean success.
31*8f0ba417SAndroid Build Coastguard Worker template <typename T, T (*strtox)(const char* str, char** endptr)>
ParseFloatingPoint(const char * s,T * out,T min,T max)32*8f0ba417SAndroid Build Coastguard Worker static inline bool ParseFloatingPoint(const char* s, T* out, T min, T max) {
33*8f0ba417SAndroid Build Coastguard Worker errno = 0;
34*8f0ba417SAndroid Build Coastguard Worker char* end;
35*8f0ba417SAndroid Build Coastguard Worker T result = strtox(s, &end);
36*8f0ba417SAndroid Build Coastguard Worker if (errno != 0 || s == end || *end != '\0') {
37*8f0ba417SAndroid Build Coastguard Worker return false;
38*8f0ba417SAndroid Build Coastguard Worker }
39*8f0ba417SAndroid Build Coastguard Worker if (result < min || max < result) {
40*8f0ba417SAndroid Build Coastguard Worker return false;
41*8f0ba417SAndroid Build Coastguard Worker }
42*8f0ba417SAndroid Build Coastguard Worker if (out != nullptr) {
43*8f0ba417SAndroid Build Coastguard Worker *out = result;
44*8f0ba417SAndroid Build Coastguard Worker }
45*8f0ba417SAndroid Build Coastguard Worker return true;
46*8f0ba417SAndroid Build Coastguard Worker }
47*8f0ba417SAndroid Build Coastguard Worker
48*8f0ba417SAndroid Build Coastguard Worker // Parse double value in the string 's' and sets 'out' to that value if it exists.
49*8f0ba417SAndroid Build Coastguard Worker // Optionally allows the caller to define a 'min' and 'max' beyond which
50*8f0ba417SAndroid Build Coastguard Worker // otherwise valid values will be rejected. Returns boolean success.
51*8f0ba417SAndroid Build Coastguard Worker static inline bool ParseDouble(const char* s, double* out,
52*8f0ba417SAndroid Build Coastguard Worker double min = std::numeric_limits<double>::lowest(),
53*8f0ba417SAndroid Build Coastguard Worker double max = std::numeric_limits<double>::max()) {
54*8f0ba417SAndroid Build Coastguard Worker return ParseFloatingPoint<double, strtod>(s, out, min, max);
55*8f0ba417SAndroid Build Coastguard Worker }
56*8f0ba417SAndroid Build Coastguard Worker static inline bool ParseDouble(const std::string& s, double* out,
57*8f0ba417SAndroid Build Coastguard Worker double min = std::numeric_limits<double>::lowest(),
58*8f0ba417SAndroid Build Coastguard Worker double max = std::numeric_limits<double>::max()) {
59*8f0ba417SAndroid Build Coastguard Worker return ParseFloatingPoint<double, strtod>(s.c_str(), out, min, max);
60*8f0ba417SAndroid Build Coastguard Worker }
61*8f0ba417SAndroid Build Coastguard Worker
62*8f0ba417SAndroid Build Coastguard Worker // Parse float value in the string 's' and sets 'out' to that value if it exists.
63*8f0ba417SAndroid Build Coastguard Worker // Optionally allows the caller to define a 'min' and 'max' beyond which
64*8f0ba417SAndroid Build Coastguard Worker // otherwise valid values will be rejected. Returns boolean success.
65*8f0ba417SAndroid Build Coastguard Worker static inline bool ParseFloat(const char* s, float* out,
66*8f0ba417SAndroid Build Coastguard Worker float min = std::numeric_limits<float>::lowest(),
67*8f0ba417SAndroid Build Coastguard Worker float max = std::numeric_limits<float>::max()) {
68*8f0ba417SAndroid Build Coastguard Worker return ParseFloatingPoint<float, strtof>(s, out, min, max);
69*8f0ba417SAndroid Build Coastguard Worker }
70*8f0ba417SAndroid Build Coastguard Worker static inline bool ParseFloat(const std::string& s, float* out,
71*8f0ba417SAndroid Build Coastguard Worker float min = std::numeric_limits<float>::lowest(),
72*8f0ba417SAndroid Build Coastguard Worker float max = std::numeric_limits<float>::max()) {
73*8f0ba417SAndroid Build Coastguard Worker return ParseFloatingPoint<float, strtof>(s.c_str(), out, min, max);
74*8f0ba417SAndroid Build Coastguard Worker }
75*8f0ba417SAndroid Build Coastguard Worker
76*8f0ba417SAndroid Build Coastguard Worker } // namespace base
77*8f0ba417SAndroid Build Coastguard Worker } // namespace android
78