blob: 6be8807735f164eca0fa0ede9ef120bc206c51d2 [file] [log] [blame]
Adam Lesinskia40e9722015-11-24 19:11:46 -08001/*
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
Adam Lesinskia6fe3452015-12-09 15:20:52 -080020#include <map>
Adam Lesinskia40e9722015-11-24 19:11:46 -080021
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "io/File.h"
23
Adam Lesinskia40e9722015-11-24 19:11:46 -080024namespace aapt {
25namespace io {
26
Adam Lesinski00451162017-10-03 07:44:08 -070027// A regular file from the file system. Uses mmap to open the data.
Adam Lesinskia40e9722015-11-24 19:11:46 -080028class RegularFile : public IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 public:
30 explicit RegularFile(const Source& source);
Adam Lesinskia40e9722015-11-24 19:11:46 -080031
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 std::unique_ptr<IData> OpenAsData() override;
Adam Lesinski00451162017-10-03 07:44:08 -070033 std::unique_ptr<io::InputStream> OpenInputStream() override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 const Source& GetSource() const override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080035
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 private:
Adam Lesinski00451162017-10-03 07:44:08 -070037 DISALLOW_COPY_AND_ASSIGN(RegularFile);
38
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 Source source_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080040};
41
Adam Lesinskia6fe3452015-12-09 15:20:52 -080042class FileCollection;
43
44class FileCollectionIterator : public IFileCollectionIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 public:
46 explicit FileCollectionIterator(FileCollection* collection);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 bool HasNext() override;
49 io::IFile* Next() override;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 private:
Adam Lesinski00451162017-10-03 07:44:08 -070052 DISALLOW_COPY_AND_ASSIGN(FileCollectionIterator);
53
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 std::map<std::string, std::unique_ptr<IFile>>::const_iterator current_, end_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080055};
56
Adam Lesinski00451162017-10-03 07:44:08 -070057// An IFileCollection representing the file system.
Adam Lesinskia40e9722015-11-24 19:11:46 -080058class FileCollection : public IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 public:
Adam Lesinski00451162017-10-03 07:44:08 -070060 FileCollection() = default;
61
62 // Adds a file located at path. Returns the IFile representation of that file.
Adam Lesinskid5083f62017-01-16 15:07:21 -080063 IFile* InsertFile(const android::StringPiece& path);
64 IFile* FindFile(const android::StringPiece& path) override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 std::unique_ptr<IFileCollectionIterator> Iterator() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080066
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 private:
Adam Lesinski00451162017-10-03 07:44:08 -070068 DISALLOW_COPY_AND_ASSIGN(FileCollection);
69
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 friend class FileCollectionIterator;
Adam Lesinski00451162017-10-03 07:44:08 -070071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 std::map<std::string, std::unique_ptr<IFile>> files_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080073};
74
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075} // namespace io
76} // namespace aapt
Adam Lesinskia40e9722015-11-24 19:11:46 -080077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078#endif // AAPT_IO_FILESYSTEM_H