Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef AAPT_IO_FILESYSTEM_H |
| 18 | #define AAPT_IO_FILESYSTEM_H |
| 19 | |
| 20 | #include "io/File.h" |
| 21 | #include "util/Files.h" |
| 22 | |
| 23 | namespace aapt { |
| 24 | namespace io { |
| 25 | |
| 26 | /** |
| 27 | * A regular file from the file system. Uses mmap to open the data. |
| 28 | */ |
| 29 | class RegularFile : public IFile { |
| 30 | public: |
| 31 | RegularFile(const Source& source) : mSource(source) { |
| 32 | } |
| 33 | |
| 34 | std::unique_ptr<IData> openAsData() override { |
| 35 | android::FileMap map; |
| 36 | if (Maybe<android::FileMap> map = file::mmapPath(mSource.path, nullptr)) { |
| 37 | return util::make_unique<MmappedData>(std::move(map.value())); |
| 38 | } |
| 39 | return {}; |
| 40 | } |
| 41 | |
| 42 | const Source& getSource() const override { |
| 43 | return mSource; |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | Source mSource; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * An IFileCollection representing the file system. |
| 52 | */ |
| 53 | class FileCollection : public IFileCollection { |
| 54 | public: |
| 55 | /** |
| 56 | * Adds a file located at path. Returns the IFile representation of that file. |
| 57 | */ |
| 58 | IFile* insertFile(const StringPiece& path) { |
| 59 | mFiles.push_back(util::make_unique<RegularFile>(Source(path))); |
| 60 | return mFiles.back().get(); |
| 61 | } |
| 62 | |
| 63 | const_iterator begin() const override { |
| 64 | return mFiles.begin(); |
| 65 | } |
| 66 | |
| 67 | const_iterator end() const override { |
| 68 | return mFiles.end(); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | std::vector<std::unique_ptr<IFile>> mFiles; |
| 73 | }; |
| 74 | |
| 75 | } // namespace io |
| 76 | } // namespace aapt |
| 77 | |
| 78 | #endif // AAPT_IO_FILESYSTEM_H |