EBOOK-TOOLS
epublib.h
Go to the documentation of this file.
1 #ifndef EPUBLIB_H
2 #define EPUBLIB_H 1
3 // generally needed includes
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7 
8 // For opening the zip file
9 #include <zip.h>
10 #include <zlib.h>
11 
12 // For parsing xml
13 #include <libxml/xmlreader.h>
14 
15 // For list stuff
16 #include "linklist.h"
17 #include "epub_shared.h"
18 
19 // General definitions
20 #ifdef _WIN32
21 # define PATH_SEPARATOR '\\'
22 #else
23 # define PATH_SEPARATOR '/'
24 #endif
25 
26 #ifdef __GNUC__
27 # define PRINTF_FORMAT(si, ftc) __attribute__ ((format (printf, si, ftc)))
28 #else
29 # define PRINTF_FORMAT(si, ftc)
30 #endif
31 
32 // MSVC-specific definitions
33 #ifdef _MSC_VER
34 # define strdup _strdup
35 #endif
36 
37 ///////////////////////////////////////////////////////////
38 // OCF definions
39 ///////////////////////////////////////////////////////////
40 #define METAINFO_DIR "META-INF"
41 #define MIMETYPE_FILENAME "mimetype"
42 #define CONTAINER_FILENAME "container.xml"
43 #define MANIFEST_FILENAME "manifest.xml"
44 #define METADATA_FILENAME "metadata.xml"
45 #define SIGNATURES_FILENAME "signatures.xml"
46 #define ENCRYPTION_FILENAME "encryption.xml"
47 #define RIGHTS_FILENAME "rights.xml"
48 
49 // An OCF root
50 struct root {
51  xmlChar *mediatype; // media type (mime)
52  xmlChar *fullpath; // full path to the root
53 };
54 
55 
56 struct ocf {
57  char *datapath; // The path that the data files relative to
58  char *filename; // The ebook filename
59  struct zip *arch; // The epub zip
60  char *mimetype; // For debugging
61  listPtr roots; // list of OCF roots
62  struct epub *epub; // back pointer
63 };
64 
65 struct meta {
66  xmlChar *name;
67  xmlChar *content;
68 };
69 
70 struct id {
71  xmlChar *id;
72  xmlChar *scheme;
73  xmlChar *string;
74 };
75 
76 struct date {
77  xmlChar *date;
78  xmlChar *event;
79 };
80 
81 struct creator {
82  xmlChar *name;
83  xmlChar *fileAs;
84  xmlChar *role;
85 };
86 
87 struct metadata {
104 };
105 
106 struct manifest {
107  xmlChar *nspace;
108  xmlChar *modules;
109  xmlChar *id;
110  xmlChar *href;
111  xmlChar *type;
112  xmlChar *fallback;
113  xmlChar *fbStyle;
114 
115 };
116 
117 struct guide {
118  xmlChar *type;
119  xmlChar *title;
120  xmlChar *href;
121 };
122 
123 struct site {
124  xmlChar *title;
125  xmlChar *href;
126 };
127 
128 struct tour {
129  xmlChar *id;
130  xmlChar *title;
132 };
133 
134 // Struct for navLabel and navInfo
135 struct tocLabel {
136  xmlChar *lang;
137  xmlChar *dir;
138  xmlChar *text;
139 };
140 
141 // struct for navPoint, pageTarget, navTarget
142 struct tocItem {
143  xmlChar *id;
144  xmlChar *src;
145  xmlChar *class;
146  xmlChar *type; //pages
148  int depth;
150  int value;
151 };
152 
153 // struct for navMap, pageList, navList
154 struct tocCategory {
155  xmlChar *id;
156  xmlChar *class;
157  listPtr info; //tocLabel
158  listPtr label; //tocLabel
159  listPtr items; //tocItem
160 };
161 
162 // General toc struct
163 struct toc {
164  struct tocCategory *navMap;
168 };
169 
170 struct spine {
171  xmlChar *idref;
172  int linear; //bool
173 };
174 
175 struct opf {
176  char *name;
177  xmlChar *tocName;
178  struct epub *epub;
180  struct toc *toc; // must in opf 2.0
184 
185  // might be NULL
188 };
189 
190 struct epuberr {
191  char lastStr[1025];
192  const char *str;
193  int len;
194  int type; /* for str: 0 = lastStr, 1 = external */
195 };
196 extern const char _epub_error_oom[];
197 #define _epub_err_set_const_str(_err, _err_string) \
198  do { \
199  (_err)->str = _err_string; \
200  (_err)->type = 1; \
201  } while (0)
202 #define _epub_err_set_str(_err, _err_string, _err_string_len) \
203  do { \
204  strncpy((_err)->lastStr, _err_string, _err_string_len); \
205  (_err)->len = _err_string_len; \
206  (_err)->str = (_err)->lastStr; \
207  (_err)->type = 0; \
208  } while (0)
209 #define _epub_err_set_oom(_epub_err) _epub_err_set_const_str(_epub_err, _epub_error_oom)
210 
211 // general structs
212 struct epub {
213  struct ocf *ocf;
214  struct opf *opf;
215  struct epuberr error;
216  int debug;
217 
218 };
219 
220 enum {
226 };
227 
228 struct eiterator {
230  struct epub *epub;
231  int opt;
233  char *cache;
234 };
235 
236 struct tit_info {
237  char *label;
238  int depth;
239  char *link;
240 };
241 
242 struct titerator {
244  struct epub *epub;
245  int opt;
247  struct tit_info cache;
248  int valid;
249 };
250 
251 // Ocf functions
252 struct ocf *_ocf_parse(struct epub *epub, const char *filename);
253 void _ocf_dump(struct ocf *ocf);
254 void _ocf_close(struct ocf *ocf);
255 struct zip *_ocf_open(struct ocf *ocf, const char *fileName);
256 int _ocf_get_file(struct ocf *ocf, const char *filename, char **fileStr);
257 int _ocf_get_data_file(struct ocf *ocf, const char *filename, char **fileStr);
258 int _ocf_check_file(struct ocf *ocf, const char *filename);
259 char *_ocf_root_by_type(struct ocf *ocf, const char *type);
260 char *_ocf_root_fullpath_by_type(struct ocf *ocf, const char *type);
261 
262 // Parsing ocf
263 int _ocf_parse_container(struct ocf *ocf);
264 int _ocf_parse_mimetype(struct ocf *ocf);
265 
266 // parsing opf
267 struct opf *_opf_parse(struct epub *epub, char *opfStr);
268 void _opf_dump(struct opf *opf);
269 void _opf_close(struct opf *opf);
270 
271 void _opf_parse_metadata(struct opf *opf, xmlTextReaderPtr reader);
272 void _opf_parse_spine(struct opf *opf, xmlTextReaderPtr reader);
273 void _opf_parse_manifest(struct opf *opf, xmlTextReaderPtr reader);
274 void _opf_parse_guide(struct opf *opf, xmlTextReaderPtr reader);
275 void _opf_parse_tours(struct opf *opf, xmlTextReaderPtr reader);
276 
277 // parse toc
278 void _opf_parse_toc(struct opf *opf, char *tocStr, int size);
279 void _opf_parse_navlist(struct opf *opf, xmlTextReaderPtr reader);
280 void _opf_parse_navmap(struct opf *opf, xmlTextReaderPtr reader);
281 void _opf_parse_pagelist(struct opf *opf, xmlTextReaderPtr reader);
282 struct tocLabel *_opf_parse_navlabel(struct opf *opf, xmlTextReaderPtr reader);
283 void _opf_free_toc_category(struct tocCategory *tc);
284 void _opf_free_toc(struct toc *toc);
285 struct toc *_opf_init_toc();
287 
288 xmlChar *_opf_label_get_by_lang(struct opf *opf, listPtr label, char *lang);
289 xmlChar *_opf_label_get_by_doc_lang(struct opf *opf, listPtr label);
290 
291 struct manifest *_opf_manifest_get_by_id(struct opf *opf, xmlChar* id);
292 
293 // epub functions
294 struct epub *epub_open(const char *filename, int debug);
295 void _epub_print_debug(struct epub *epub, int debug, const char *format, ...) PRINTF_FORMAT(3, 4);
296 char *epub_last_errStr(struct epub *epub);
297 
298 // List operations
299 void _list_free_root(struct root *data);
300 
301 void _list_free_creator(struct creator *data);
302 void _list_free_date(struct date *date);
303 void _list_free_id(struct id *id);
304 void _list_free_meta(struct meta *meta);
305 
306 void _list_free_spine(struct spine *spine);
308 void _list_free_guide(struct guide *guide);
309 void _list_free_tours(struct tour *tour);
310 
311 void _list_free_toc_label(struct tocLabel *tl);
312 void _list_free_toc_item(struct tocItem *ti);
313 
314 int _list_cmp_root_by_mediatype(struct root *root1, struct root *root2);
315 int _list_cmp_manifest_by_id(struct manifest *m1, struct manifest *m2);
316 int _list_cmp_toc_by_playorder(struct tocItem *t1, struct tocItem *t2);
317 int _list_cmp_label_by_lang(struct tocLabel *t1, struct tocLabel *t2);
318 
319 void _list_dump_root(struct root *root);
320 
321 void _list_dump_string(char *string);
322 void _list_dump_creator(struct creator *data);
323 void _list_dump_date(struct date *date);
324 void _list_dump_id(struct id *id);
325 void _list_dump_meta(struct meta *meta);
326 
327 void _list_dump_spine(struct spine *spine);
328 void _list_dump_guide(struct guide *guide);
329 void _list_dump_tour(struct tour *tour);
330 
331 #endif /* epublib_h */