Tom Cherry | 927c5d5 | 2017-12-11 01:40:07 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2017 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 | #include "property_type.h" |
| 18 | |
| 19 | #include <android-base/parsedouble.h> |
| 20 | #include <android-base/parseint.h> |
| 21 | #include <android-base/strings.h> |
| 22 | |
| 23 | using android::base::ParseDouble; |
| 24 | using android::base::ParseInt; |
| 25 | using android::base::ParseUint; |
| 26 | using android::base::Split; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace init { |
| 30 | |
| 31 | bool CheckType(const std::string& type_string, const std::string& value) { |
| 32 | auto type_strings = Split(type_string, " "); |
| 33 | if (type_strings.empty()) { |
| 34 | return false; |
| 35 | } |
| 36 | auto type = type_strings[0]; |
| 37 | |
| 38 | if (type == "string") { |
| 39 | return true; |
| 40 | } |
| 41 | if (type == "bool") { |
| 42 | return value == "true" || value == "false" || value == "1" || value == "0"; |
| 43 | } |
| 44 | if (type == "int") { |
| 45 | int64_t parsed; |
| 46 | return ParseInt(value, &parsed); |
| 47 | } |
| 48 | if (type == "uint") { |
| 49 | uint64_t parsed; |
| 50 | if (value.empty() || value.front() == '-') { |
| 51 | return false; |
| 52 | } |
| 53 | return ParseUint(value, &parsed); |
| 54 | } |
| 55 | if (type == "double") { |
| 56 | double parsed; |
| 57 | return ParseDouble(value.c_str(), &parsed); |
| 58 | } |
| 59 | if (type == "size") { |
| 60 | auto it = value.begin(); |
| 61 | while (it != value.end() && isdigit(*it)) { |
| 62 | it++; |
| 63 | } |
| 64 | if (it == value.begin() || it == value.end() || (*it != 'g' && *it != 'k' && *it != 'm')) { |
| 65 | return false; |
| 66 | } |
| 67 | it++; |
| 68 | return it == value.end(); |
| 69 | } |
| 70 | if (type == "enum") { |
| 71 | for (auto it = std::next(type_strings.begin()); it != type_strings.end(); ++it) { |
| 72 | if (*it == value) { |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | } // namespace init |
| 81 | } // namespace android |