/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "Source.h" #include "io/FileSystem.h" #include "util/Files.h" #include "util/Maybe.h" #include "util/StringPiece.h" #include "util/Util.h" #include namespace aapt { namespace io { RegularFile::RegularFile(const Source& source) : mSource(source) { } std::unique_ptr RegularFile::openAsData() { android::FileMap map; if (Maybe map = file::mmapPath(mSource.path, nullptr)) { if (map.value().getDataPtr() && map.value().getDataLength() > 0) { return util::make_unique(std::move(map.value())); } return util::make_unique(); } return {}; } const Source& RegularFile::getSource() const { return mSource; } FileCollectionIterator::FileCollectionIterator(FileCollection* collection) : mCurrent(collection->mFiles.begin()), mEnd(collection->mFiles.end()) { } bool FileCollectionIterator::hasNext() { return mCurrent != mEnd; } IFile* FileCollectionIterator::next() { IFile* result = mCurrent->second.get(); ++mCurrent; return result; } IFile* FileCollection::insertFile(const StringPiece& path) { return (mFiles[path.toString()] = util::make_unique(Source(path))).get(); } IFile* FileCollection::findFile(const StringPiece& path) { auto iter = mFiles.find(path.toString()); if (iter != mFiles.end()) { return iter->second.get(); } return nullptr; } std::unique_ptr FileCollection::iterator() { return util::make_unique(this); } } // namespace io } // namespace aapt