Home | History | Annotate | Download | only in io
      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 <map>
     21 
     22 #include "io/File.h"
     23 
     24 namespace aapt {
     25 namespace io {
     26 
     27 // A regular file from the file system. Uses mmap to open the data.
     28 class RegularFile : public IFile {
     29  public:
     30   explicit RegularFile(const Source& source);
     31 
     32   std::unique_ptr<IData> OpenAsData() override;
     33   std::unique_ptr<io::InputStream> OpenInputStream() override;
     34   const Source& GetSource() const override;
     35 
     36  private:
     37   DISALLOW_COPY_AND_ASSIGN(RegularFile);
     38 
     39   Source source_;
     40 };
     41 
     42 class FileCollection;
     43 
     44 class FileCollectionIterator : public IFileCollectionIterator {
     45  public:
     46   explicit FileCollectionIterator(FileCollection* collection);
     47 
     48   bool HasNext() override;
     49   io::IFile* Next() override;
     50 
     51  private:
     52   DISALLOW_COPY_AND_ASSIGN(FileCollectionIterator);
     53 
     54   std::map<std::string, std::unique_ptr<IFile>>::const_iterator current_, end_;
     55 };
     56 
     57 // An IFileCollection representing the file system.
     58 class FileCollection : public IFileCollection {
     59  public:
     60   FileCollection() = default;
     61 
     62   // Adds a file located at path. Returns the IFile representation of that file.
     63   IFile* InsertFile(const android::StringPiece& path);
     64   IFile* FindFile(const android::StringPiece& path) override;
     65   std::unique_ptr<IFileCollectionIterator> Iterator() override;
     66 
     67  private:
     68   DISALLOW_COPY_AND_ASSIGN(FileCollection);
     69 
     70   friend class FileCollectionIterator;
     71 
     72   std::map<std::string, std::unique_ptr<IFile>> files_;
     73 };
     74 
     75 }  // namespace io
     76 }  // namespace aapt
     77 
     78 #endif  // AAPT_IO_FILESYSTEM_H
     79