blob: 76f87aec65569c43dc36b62993d806b02df9de49 [file] [log] [blame]
Adam Lesinskia6fe3452015-12-09 15:20:52 -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#include "Source.h"
18#include "io/FileSystem.h"
19#include "util/Files.h"
20#include "util/Maybe.h"
21#include "util/StringPiece.h"
22#include "util/Util.h"
23
24#include <utils/FileMap.h>
25
26namespace aapt {
27namespace io {
28
29RegularFile::RegularFile(const Source& source) : mSource(source) {
30}
31
32std::unique_ptr<IData> RegularFile::openAsData() {
33 android::FileMap map;
34 if (Maybe<android::FileMap> map = file::mmapPath(mSource.path, nullptr)) {
35 return util::make_unique<MmappedData>(std::move(map.value()));
36 }
37 return {};
38}
39
40const Source& RegularFile::getSource() const {
41 return mSource;
42}
43
44FileCollectionIterator::FileCollectionIterator(FileCollection* collection) :
45 mCurrent(collection->mFiles.begin()), mEnd(collection->mFiles.end()) {
46}
47
48bool FileCollectionIterator::hasNext() {
49 return mCurrent != mEnd;
50}
51
52IFile* FileCollectionIterator::next() {
53 IFile* result = mCurrent->second.get();
54 ++mCurrent;
55 return result;
56}
57
58IFile* FileCollection::insertFile(const StringPiece& path) {
59 return (mFiles[path.toString()] = util::make_unique<RegularFile>(Source(path))).get();
60}
61
62IFile* FileCollection::findFile(const StringPiece& path) {
63 auto iter = mFiles.find(path.toString());
64 if (iter != mFiles.end()) {
65 return iter->second.get();
66 }
67 return nullptr;
68}
69
70std::unique_ptr<IFileCollectionIterator> FileCollection::iterator() {
71 return util::make_unique<FileCollectionIterator>(this);
72}
73
74} // namespace io
75} // namespace aapt