Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 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 | #include "LoadedApk.h" |
| 18 | |
| 19 | namespace aapt { |
| 20 | |
| 21 | std::unique_ptr<LoadedApk> LoadedApk::LoadApkFromPath( |
| 22 | IAaptContext* context, const StringPiece& path) { |
| 23 | Source source(path); |
| 24 | std::string error; |
| 25 | std::unique_ptr<io::ZipFileCollection> apk = |
| 26 | io::ZipFileCollection::Create(path, &error); |
| 27 | if (!apk) { |
| 28 | context->GetDiagnostics()->Error(DiagMessage(source) << error); |
| 29 | return {}; |
| 30 | } |
| 31 | |
| 32 | io::IFile* file = apk->FindFile("resources.arsc"); |
| 33 | if (!file) { |
| 34 | context->GetDiagnostics()->Error(DiagMessage(source) |
| 35 | << "no resources.arsc found"); |
| 36 | return {}; |
| 37 | } |
| 38 | |
| 39 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
| 40 | if (!data) { |
| 41 | context->GetDiagnostics()->Error(DiagMessage(source) |
| 42 | << "could not open resources.arsc"); |
| 43 | return {}; |
| 44 | } |
| 45 | |
| 46 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
| 47 | BinaryResourceParser parser(context, table.get(), source, data->data(), |
| 48 | data->size()); |
| 49 | if (!parser.Parse()) { |
| 50 | return {}; |
| 51 | } |
| 52 | |
| 53 | return util::make_unique<LoadedApk>(source, std::move(apk), std::move(table)); |
| 54 | } |
| 55 | |
| 56 | } // namespace aapt |