1 // Copyright (C) 2019 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "utils/api_level.h" 16 17 #include <json/reader.h> 18 #include <llvm/Support/raw_ostream.h> 19 20 #include "utils/string_utils.h" 21 22 23 namespace header_checker { 24 namespace utils { 25 26 Load(std::istream & stream)27bool ApiLevelMap::Load(std::istream &stream) { 28 Json::CharReaderBuilder builder; 29 Json::Value json; 30 std::string error_message; 31 if (!Json::parseFromStream(builder, stream, &json, &error_message)) { 32 llvm::errs() << "Cannot load ApiLevelMap: " << error_message << "\n"; 33 return false; 34 } 35 36 const Json::Value null_value; 37 for (const Json::String &key : json.getMemberNames()) { 38 Json::Value value = json.get(key, null_value); 39 if (!value.isInt()) { 40 llvm::errs() << "Cannot load ApiLevelMap: " << key 41 << " is not mapped to an integer.\n"; 42 return false; 43 } 44 codename_to_api_level_[key] = value.asInt(); 45 } 46 return true; 47 } 48 Parse(const std::string & api) const49std::optional<ApiLevel> ApiLevelMap::Parse(const std::string &api) const { 50 auto it = codename_to_api_level_.find(api); 51 if (it != codename_to_api_level_.end()) { 52 return it->second; 53 } 54 return ParseInt(api); 55 } 56 57 58 } // namespace utils 59 } // namespace header_checker 60