1 /*
2  * Copyright (C) 2006 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 //
18 // Access a chunk of the asset hierarchy as if it were a single directory.
19 //
20 #ifndef __LIBS_ASSETDIR_H
21 #define __LIBS_ASSETDIR_H
22 
23 #include <androidfw/misc.h>
24 #include <utils/String8.h>
25 #include <utils/Vector.h>
26 #include <utils/SortedVector.h>
27 #include <sys/types.h>
28 
29 namespace android {
30 
31 /*
32  * This provides vector-style access to a directory.  We do this rather
33  * than modeling opendir/readdir access because it's simpler and the
34  * nature of the operation requires us to have all data on hand anyway.
35  *
36  * The list of files will be sorted in ascending order by ASCII value.
37  *
38  * The contents are populated by our friend, the AssetManager.
39  */
40 class AssetDir {
41 public:
AssetDir(void)42     AssetDir(void)
43         : mFileInfo(NULL)
44         {}
~AssetDir(void)45     virtual ~AssetDir(void) {
46         delete mFileInfo;
47     }
48 
49     /*
50      * Vector-style access.
51      */
getFileCount(void)52     size_t getFileCount(void) { return mFileInfo->size(); }
getFileName(int idx)53     const String8& getFileName(int idx) {
54         return mFileInfo->itemAt(idx).getFileName();
55     }
getSourceName(int idx)56     const String8& getSourceName(int idx) {
57         return mFileInfo->itemAt(idx).getSourceName();
58     }
59 
60     /*
61      * Get the type of a file (usually regular or directory).
62      */
getFileType(int idx)63     FileType getFileType(int idx) {
64         return mFileInfo->itemAt(idx).getFileType();
65     }
66 
67 private:
68     /* these operations are not implemented */
69     AssetDir(const AssetDir& src);
70     const AssetDir& operator=(const AssetDir& src);
71 
72     friend class AssetManager;
73     friend class AssetManager2;
74 
75     /*
76      * This holds information about files in the asset hierarchy.
77      */
78     class FileInfo {
79     public:
FileInfo(void)80         FileInfo(void) {}
FileInfo(const String8 & path)81         explicit FileInfo(const String8& path)      // useful for e.g. svect.indexOf
82             : mFileName(path), mFileType(kFileTypeUnknown)
83             {}
~FileInfo(void)84         ~FileInfo(void) {}
FileInfo(const FileInfo & src)85         FileInfo(const FileInfo& src) {
86             copyMembers(src);
87         }
88         const FileInfo& operator= (const FileInfo& src) {
89             if (this != &src)
90                 copyMembers(src);
91             return *this;
92         }
93 
copyMembers(const FileInfo & src)94         void copyMembers(const FileInfo& src) {
95             mFileName = src.mFileName;
96             mFileType = src.mFileType;
97             mSourceName = src.mSourceName;
98         }
99 
100         /* need this for SortedVector; must compare only on file name */
101         bool operator< (const FileInfo& rhs) const {
102             return mFileName < rhs.mFileName;
103         }
104 
105         /* used by AssetManager */
106         bool operator== (const FileInfo& rhs) const {
107             return mFileName == rhs.mFileName;
108         }
109 
set(const String8 & path,FileType type)110         void set(const String8& path, FileType type) {
111             mFileName = path;
112             mFileType = type;
113         }
114 
getFileName(void)115         const String8& getFileName(void) const { return mFileName; }
setFileName(const String8 & path)116         void setFileName(const String8& path) { mFileName = path; }
117 
getFileType(void)118         FileType getFileType(void) const { return mFileType; }
setFileType(FileType type)119         void setFileType(FileType type) { mFileType = type; }
120 
getSourceName(void)121         const String8& getSourceName(void) const { return mSourceName; }
setSourceName(const String8 & path)122         void setSourceName(const String8& path) { mSourceName = path; }
123 
124         /*
125          * Handy utility for finding an entry in a sorted vector of FileInfo.
126          * Returns the index of the matching entry, or -1 if none found.
127          */
128         static int findEntry(const SortedVector<FileInfo>* pVector,
129             const String8& fileName);
130 
131     private:
132         String8    mFileName;      // filename only
133         FileType    mFileType;      // regular, directory, etc
134 
135         String8    mSourceName;    // currently debug-only
136     };
137 
138     /* AssetManager uses this to initialize us */
setFileList(SortedVector<FileInfo> * list)139     void setFileList(SortedVector<FileInfo>* list) { mFileInfo = list; }
140 
141     SortedVector<FileInfo>* mFileInfo;
142 };
143 
144 }; // namespace android
145 
146 #endif // __LIBS_ASSETDIR_H
147