Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / core / include / dpl / zip_input.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        zip_input.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the header file of zip input
21  */
22 #ifndef DPL_ZIP_INPUT_H
23 #define DPL_ZIP_INPUT_H
24
25 #include <dpl/exception.h>
26 #include <dpl/noncopyable.h>
27 #include <dpl/abstract_input.h>
28 #include <utility>
29 #include <vector>
30 #include <string>
31
32 namespace DPL {
33 class ZipInput :
34     private Noncopyable
35 {
36   public:
37     class Exception
38     {
39       public:
40         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
41         DECLARE_EXCEPTION_TYPE(Base, OpenFailed)
42         DECLARE_EXCEPTION_TYPE(Base, ReadGlobalInfoFailed)
43         DECLARE_EXCEPTION_TYPE(Base, ReadGlobalCommentFailed)
44         DECLARE_EXCEPTION_TYPE(Base, SeekFileFailed)
45         DECLARE_EXCEPTION_TYPE(Base, FileInfoFailed)
46         DECLARE_EXCEPTION_TYPE(Base, OpenFileFailed)
47         DECLARE_EXCEPTION_TYPE(Base, ReadFileFailed)
48     };
49
50     typedef std::pair<size_t, size_t> FileHandle;
51
52     struct FileInfo
53     {
54         // File handle
55         FileHandle handle;
56
57         // File name and comment
58         std::string name;
59         std::string comment;
60
61         // File information
62         off64_t compressedSize;               //< compressed size
63         off64_t uncompressedSize;             //< uncompressed size
64
65         FileInfo() :
66             handle(),
67             name(),
68             comment(),
69             compressedSize(0),
70             uncompressedSize(0)
71         {}
72
73         FileInfo(const FileHandle &handleArg,
74                  const std::string &nameArg,
75                  const std::string &commentArg,
76                  const off64_t &compressedSizeArg,
77                  const off64_t &uncompressedSizeArg) :
78             handle(handleArg),
79             name(nameArg),
80             comment(commentArg),
81             compressedSize(compressedSizeArg),
82             uncompressedSize(uncompressedSizeArg)
83         {}
84     };
85
86     class File :
87         public DPL::AbstractInput
88     {
89       private:
90         void *m_file;
91
92         friend class ZipInput;
93         File(class Device *device, FileHandle handle);
94
95       public:
96         ~File();
97
98         virtual DPL::BinaryQueueAutoPtr Read(size_t size);
99     };
100
101   private:
102     class Device * m_device;
103     void *m_masterFile;
104
105     size_t m_numberOfFiles;
106     size_t m_globalCommentSize;
107     std::string m_globalComment;
108     size_t m_totalUncompressedSize;
109
110     // At least cache handles
111     typedef std::vector<FileInfo> FileInfoList;
112     FileInfoList m_fileInfos;
113
114     void ReadGlobalInfo(void *masterFile);
115     void ReadGlobalComment(void *masterFile);
116     void ReadInfos(void *masterFile);
117
118   public:
119     typedef FileInfoList::const_iterator const_iterator;
120     typedef FileInfoList::const_reverse_iterator const_reverse_iterator;
121     typedef FileInfoList::size_type size_type;
122
123   public:
124     /**
125      * Open zip file from file
126      */
127     explicit ZipInput(const std::string &fileName);
128
129     /**
130      * Destructor
131      */
132     ~ZipInput();
133
134     // Iterators
135     const_iterator begin() const;
136     const_iterator end() const;
137
138     const_reverse_iterator rbegin() const;
139     const_reverse_iterator rend() const;
140
141     // Size, empty
142     size_type size() const;
143     bool empty() const;
144
145     /**
146      * Open a binary file for given file name
147      *
148      * @return file object
149      * @param[in] fileName Zip file name to open
150      * @exception std::bad_alloc Cannot allocate memory to hold additional data
151      * @exception SteamOpenFailed Cannot find file with given handle
152      * @see BinaryQueue::BufferDeleterFree
153      */
154     File *OpenFile(const std::string &fileName);
155
156     /**
157      * Get archive global comment
158      *
159      * @return Global archive comment
160      */
161     const std::string &GetGlobalComment() const;
162     size_t GetTotalUncompressedSize() const;
163 };
164 } // namespace DPL
165
166 #endif // DPL_ZIP_INPUT_H