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