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
29namespace 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 */
40class AssetDir {
41public:
42    AssetDir(void)
43        : mFileInfo(NULL)
44        {}
45    virtual ~AssetDir(void) {
46        delete mFileInfo;
47    }
48
49    /*
50     * Vector-style access.
51     */
52    size_t getFileCount(void) { return mFileInfo->size(); }
53    const String8& getFileName(int idx) {
54        return mFileInfo->itemAt(idx).getFileName();
55    }
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     */
63    FileType getFileType(int idx) {
64        return mFileInfo->itemAt(idx).getFileType();
65    }
66
67private:
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:
80        FileInfo(void) {}
81        FileInfo(const String8& path)      // useful for e.g. svect.indexOf
82            : mFileName(path), mFileType(kFileTypeUnknown)
83            {}
84        ~FileInfo(void) {}
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
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
110        void set(const String8& path, FileType type) {
111            mFileName = path;
112            mFileType = type;
113        }
114
115        const String8& getFileName(void) const { return mFileName; }
116        void setFileName(const String8& path) { mFileName = path; }
117
118        FileType getFileType(void) const { return mFileType; }
119        void setFileType(FileType type) { mFileType = type; }
120
121        const String8& getSourceName(void) const { return mSourceName; }
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 */
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