1 /*
2  * Copyright 2018 Matthieu Gautier <mgautier@kymeria.fr>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU  General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301, USA.
18  */
19 
20 #ifndef KIWIX_ENTRY_H
21 #define KIWIX_ENTRY_H
22 
23 #include <stdio.h>
24 #include <zim/article.h>
25 #include <exception>
26 #include <string>
27 
28 #include "common.h"
29 
30 using namespace std;
31 
32 namespace kiwix
33 {
34 
35 
36 class NoEntry : public std::exception {};
37 
38 /**
39  * A entry represent an.. entry in a zim file.
40  */
41 class Entry
42 {
43   public:
44     /**
45      * Default constructor.
46      *
47      * Construct an invalid entry.
48      */
49     Entry() = default;
50 
51     /**
52      * Construct an entry making reference to an zim article.
53      *
54      * @param article a zim::Article object
55      */
56     Entry(zim::Article article);
57     virtual ~Entry() = default;
58 
59     /**
60      * Get the path of the entry.
61      *
62      * The path is the "key" of an entry.
63      *
64      * @return the path of the entry.
65      */
66     std::string getPath() const;
67 
68     /**
69      * Get the title of the entry.
70      *
71      * @return the title of the entry.
72      */
73     std::string getTitle() const;
74 
75     /**
76      * Get the content of the entry.
77      *
78      * The string is a copy of the content.
79      * If you don't want to do a copy, use get_blob.
80      *
81      * @return the content of the entry.
82      */
83     std::string getContent() const;
84 
85     /**
86      * Get the blob of the entry.
87      *
88      * A blob make reference to the content without copying it.
89      *
90      * @param offset The starting offset of the blob.
91      * @return the blob of the entry.
92      */
93     zim::Blob   getBlob(offset_type offset = 0) const;
94 
95     /**
96      * Get the blob of the entry.
97      *
98      * A blob make reference to the content without copying it.
99      *
100      * @param offset The starting offset of the blob.
101      * @param size The size of the blob.
102      * @return the blob of the entry.
103      */
104     zim::Blob   getBlob(offset_type offset, size_type size) const;
105 
106     /**
107      * Get the info for direct access to the content of the entry.
108      *
109      * Some entry (ie binary ones) have their content plain stored
110      * in the zim file. Knowing the offset where the content is stored
111      * an user can directly read the content in the zim file bypassing the
112      * kiwix-lib/libzim.
113      *
114      * @return A pair specifying where to read the content.
115      *         The string is the real file to read (may be different that .zim
116      *         file if zim is cut).
117      *         The offset is the offset to read in the file.
118      *         Return <"",0> if is not possible to read directly.
119      */
120     std::pair<std::string, offset_type> getDirectAccessInfo() const;
121 
122     /**
123      * Get the size of the entry.
124      *
125      * @return the size of the entry.
126      */
127     size_type   getSize() const;
128 
129     /**
130      * Get the mime_type of the entry.
131      *
132      * @return the mime_type of the entry.
133      */
134     std::string getMimetype() const;
135 
136 
137     /**
138      * Get if the entry is a redirect entry.
139      *
140      * @return True if the entry is a redirect.
141      */
142     bool isRedirect() const;
143 
144     /**
145      * Get if the entry is a link target entry.
146      *
147      * @return True if the entry is a link target.
148      */
149     bool isLinkTarget() const;
150 
151     /**
152      * Get if the entry is a deleted entry.
153      *
154      * @return True if the entry is a deleted entry.
155      */
156     bool isDeleted() const;
157 
158     /**
159      * Get the entry pointed by this entry.
160      *
161      * @return the entry pointed.
162      * @throw NoEntry if the entry is not a redirected entry.
163      */
164     Entry getRedirectEntry() const;
165 
166     /**
167      * Get the final entry pointed by this entry.
168      *
169      * Follow the redirection until a "not redirecting" entry is found.
170      * If the entry is not a redirected entry, return the entry itself.
171      *
172      * @return the final entry.
173      */
174     Entry getFinalEntry() const;
175 
176     /**
177      * Convert the entry to a boolean value.
178      *
179      * @return True if the entry is valid.
180      */
181     explicit operator bool() const { return good(); }
182 
183   private:
184     zim::Article article;
185     mutable zim::Article final_article;
186 
good()187     bool good() const { return article.good(); }
188 };
189 
190 }
191 
192 #endif // KIWIX_ENTRY_H
193