• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
74     /*
75      * This holds information about files in the asset hierarchy.
76      */
77     class FileInfo {
78     public:
FileInfo(void)79         FileInfo(void) {}
FileInfo(const String8 & path)80         FileInfo(const String8& path)      // useful for e.g. svect.indexOf
81             : mFileName(path), mFileType(kFileTypeUnknown)
82             {}
~FileInfo(void)83         ~FileInfo(void) {}
FileInfo(const FileInfo & src)84         FileInfo(const FileInfo& src) {
85             copyMembers(src);
86         }
87         const FileInfo& operator= (const FileInfo& src) {
88             if (this != &src)
89                 copyMembers(src);
90             return *this;
91         }
92 
copyMembers(const FileInfo & src)93         void copyMembers(const FileInfo& src) {
94             mFileName = src.mFileName;
95             mFileType = src.mFileType;
96             mSourceName = src.mSourceName;
97         }
98 
99         /* need this for SortedVector; must compare only on file name */
100         bool operator< (const FileInfo& rhs) const {
101             return mFileName < rhs.mFileName;
102         }
103 
104         /* used by AssetManager */
105         bool operator== (const FileInfo& rhs) const {
106             return mFileName == rhs.mFileName;
107         }
108 
set(const String8 & path,FileType type)109         void set(const String8& path, FileType type) {
110             mFileName = path;
111             mFileType = type;
112         }
113 
getFileName(void)114         const String8& getFileName(void) const { return mFileName; }
setFileName(const String8 & path)115         void setFileName(const String8& path) { mFileName = path; }
116 
getFileType(void)117         FileType getFileType(void) const { return mFileType; }
setFileType(FileType type)118         void setFileType(FileType type) { mFileType = type; }
119 
getSourceName(void)120         const String8& getSourceName(void) const { return mSourceName; }
setSourceName(const String8 & path)121         void setSourceName(const String8& path) { mSourceName = path; }
122 
123         /*
124          * Handy utility for finding an entry in a sorted vector of FileInfo.
125          * Returns the index of the matching entry, or -1 if none found.
126          */
127         static int findEntry(const SortedVector<FileInfo>* pVector,
128             const String8& fileName);
129 
130     private:
131         String8    mFileName;      // filename only
132         FileType    mFileType;      // regular, directory, etc
133 
134         String8    mSourceName;    // currently debug-only
135     };
136 
137     /* AssetManager uses this to initialize us */
setFileList(SortedVector<FileInfo> * list)138     void setFileList(SortedVector<FileInfo>* list) { mFileInfo = list; }
139 
140     SortedVector<FileInfo>* mFileInfo;
141 };
142 
143 }; // namespace android
144 
145 #endif // __LIBS_ASSETDIR_H
146