libmobi
C library for handling MOBI format ebook documents
Macros | Functions | Variables
opf.c File Reference

Functions for handling OPF structures. More...

#include <stdlib.h>
#include <string.h>
#include "opf.h"
#include "xmlwriter.h"
#include "index.h"
#include "util.h"
#include "parse_rawml.h"
#include "debug.h"

Macros

#define _GNU_SOURCE   1
 
#define __USE_BSD   /* for strdup on linux/glibc */
 
#define mobi_opf_copy_tagtype(mobidata, struct_type, struct_element, member_name)
 Copy text data from EXTH record to "member_name" member of a structure with given type. More...
 
#define mobi_opf_set_tagtype(struct_type, struct_element, member_name, string)
 Set "member_name" member of a structure with given type to string value. More...
 
#define mobi_free_opf_struct_2el(struct_array, struct_member1, struct_member2)
 Macro to free generic OPF structure with two members. More...
 
#define mobi_free_opf_struct_3el(struct_array, struct_member1, struct_member2, struct_member3)
 Macro to free generic OPF structure with three members. More...
 

Functions

bool mobi_is_guide_type (const char *type)
 Check if type is valid OPF guide element. More...
 
MOBI_RET mobi_build_opf_guide (OPF *opf, const MOBIRawml *rawml)
 Reconstruct guide part of the OPF file. More...
 
MOBI_RET mobi_write_ncx_level (xmlTextWriterPtr writer, const NCX *ncx, const size_t level, const size_t from, const size_t to, size_t *seq)
 Write <navPoint> entries for given ncx level. More...
 
MOBI_RET mobi_xml_write_meta (xmlTextWriterPtr writer, const char *name, const char *content)
 Write element <meta name="name" content="content"> to XML buffer. More...
 
MOBI_RET mobi_opf_add_to_rawml (const char *opf_xml, MOBIRawml *rawml)
 Add reconstruced opf part to rawml. More...
 
MOBI_RET mobi_ncx_add_to_rawml (const char *ncx_xml, MOBIRawml *rawml)
 Add reconstruced ncx part to rawml. More...
 
MOBI_RET mobi_write_ncx_header (xmlTextWriterPtr writer, const OPF *opf, uint32_t maxlevel)
 Write ncx header. More...
 
MOBI_RET mobi_write_ncx (MOBIRawml *rawml, const NCX *ncx, const OPF *opf, uint32_t maxlevel)
 Build ncx document using libxml2 and append it to rawml. More...
 
void mobi_free_ncx (NCX *ncx, size_t count)
 Free array of ncx entries. More...
 
MOBI_RET mobi_build_ncx (MOBIRawml *rawml, const OPF *opf)
 Parse ncx index, recreate ncx document and append it to rawml. More...
 
void mobi_opf_set_item (OPFmeta **meta, const char *name, const char *content)
 Set values for attributes of OPF manifest tag. More...
 
MOBI_RET mobi_get_opf_from_exth (OPFmetadata *metadata, const MOBIData *m)
 Copy text data from EXTH record to OPFmetadata tags structure. More...
 
MOBI_RET mobi_build_opf_metadata (OPF *opf, const MOBIData *m, const MOBIRawml *rawml)
 Recreate OPF structure. More...
 
MOBI_RET mobi_xml_write_element_ns (xmlTextWriterPtr writer, const char *name, const char **content, const char *ns)
 Write array of xml elements of given name to XML buffer. More...
 
MOBI_RET mobi_xml_write_dcmeta (xmlTextWriterPtr writer, const char *name, const char **content)
 Write array of Dublin Core elements of given name to XML buffer. More...
 
MOBI_RET mobi_xml_write_xmeta (xmlTextWriterPtr writer, const char *name, const char **content)
 Write array of custom MOBI elements of given name to XML buffer. More...
 
MOBI_RET mobi_xml_write_opfmeta (xmlTextWriterPtr writer, const OPFmeta **meta)
 Write array of <meta> elements to XML buffer. More...
 
MOBI_RET mobi_xml_write_reference (xmlTextWriterPtr writer, const OPFreference **reference)
 Write array of <referenece> elements to XML buffer. More...
 
MOBI_RET mobi_xml_write_item (xmlTextWriterPtr writer, const char *id, const char *href, const char *media_type)
 Write single element to XML buffer. More...
 
MOBI_RET mobi_xml_write_spine (xmlTextWriterPtr writer, const MOBIRawml *rawml)
 Write opf <spine> part to XML buffer. More...
 
MOBI_RET mobi_xml_write_manifest (xmlTextWriterPtr writer, const MOBIRawml *rawml)
 Write all manifest elements to XML buffer. More...
 
MOBI_RET mobi_xml_write_dcmeta_identifier (xmlTextWriterPtr writer, const OPFidentifier **identifier)
 Write array of Dublin Core identifier elements to XML buffer. More...
 
MOBI_RET mobi_xml_write_dcmeta_creator (xmlTextWriterPtr writer, const OPFcreator **creator, const char *name)
 Write array of Dublin Core creator/contributor elements to XML buffer. More...
 
MOBI_RET mobi_xml_write_dcmeta_subject (xmlTextWriterPtr writer, const OPFsubject **subject)
 Write array of Dublin Core subject elements to XML buffer. More...
 
MOBI_RET mobi_xml_write_dcmeta_date (xmlTextWriterPtr writer, const OPFdate **date)
 Write array of Dublin Core date elements to XML buffer. More...
 
MOBI_RET mobi_xml_write_xmeta_srp (xmlTextWriterPtr writer, const OPFsrp **srp)
 Write array of custom srp elements to XML buffer. More...
 
void mobi_free_opf_array (char **array)
 Free array of OPF sturcture members. More...
 
void mobi_free_opf_metadata (OPFmetadata *metadata)
 Free OPF metadata structure and data. More...
 
void mobi_free_opf_manifest (OPFmanifest *manifest)
 Free OPFmanifest structure and data. More...
 
void mobi_free_opf_spine (OPFspine *spine)
 Free OPFspine structure and data. More...
 
void mobi_free_opf_guide (OPFguide *guide)
 Free OPFguide structure and data. More...
 
void mobi_free_opf (OPF *opf)
 Free OPF structure and data. More...
 
MOBI_RET mobi_build_opf (MOBIRawml *rawml, const MOBIData *m)
 Recreate OPF structure. More...
 

Variables

const char * mobi_guide_types []
 Array of valid OPF guide types. More...
 

Detailed Description

Functions for handling OPF structures.

Copyright (c) 2020 Bartek Fabiszewski http://www.fabiszewski.net

This file is part of libmobi. Licensed under LGPL, either version 3, or any later. See http://www.gnu.org/licenses/

Macro Definition Documentation

◆ mobi_free_opf_struct_2el

#define mobi_free_opf_struct_2el (   struct_array,
  struct_member1,
  struct_member2 
)
Value:
{ \
if (struct_array) { \
size_t i = 0; \
while (i < OPF_META_MAX_TAGS) { \
if (struct_array[i] == NULL) { \
break; \
} \
free(struct_array[i]->struct_member1); \
free(struct_array[i]->struct_member2); \
free(struct_array[i]); \
i++; \
} \
free(struct_array); \
} \
}
#define OPF_META_MAX_TAGS
Maximum number of opf meta tags.
Definition: opf.h:18

Macro to free generic OPF structure with two members.

Parameters
[in]struct_arrayStructure name
[in]struct_member1Structure member 1
[in]struct_member2Structure member 2

◆ mobi_free_opf_struct_3el

#define mobi_free_opf_struct_3el (   struct_array,
  struct_member1,
  struct_member2,
  struct_member3 
)
Value:
{ \
if (struct_array) { \
size_t i = 0; \
while (i < OPF_META_MAX_TAGS) { \
if (struct_array[i] == NULL) { \
break; \
} \
free(struct_array[i]->struct_member1); \
free(struct_array[i]->struct_member2); \
free(struct_array[i]->struct_member3); \
free(struct_array[i]); \
i++; \
} \
free(struct_array); \
} \
}

Macro to free generic OPF structure with three members.

Parameters
[in]struct_arrayStructure name
[in]struct_member1Structure member 1
[in]struct_member2Structure member 2
[in]struct_member3Structure member 3

◆ mobi_opf_copy_tagtype

#define mobi_opf_copy_tagtype (   mobidata,
  struct_type,
  struct_element,
  member_name 
)

Copy text data from EXTH record to "member_name" member of a structure with given type.

Data will copied from curr->data. It will allocate memory for the array of structures if not already allocated. It will find first array index that is not already used

Parameters
[in]mobidataMobidata structure
[in]struct_typeStructure type defined with typedef
[in]struct_elementMember member_name of this structure will be set to EXTH data
[in]member_nameStructure member name that will be modified

◆ mobi_opf_set_tagtype

#define mobi_opf_set_tagtype (   struct_type,
  struct_element,
  member_name,
  string 
)

Set "member_name" member of a structure with given type to string value.

It will allocate memory for the array of structures if not already allocated. It will find first array index that is not already used

Parameters
[in]struct_typeStructure type defined with typedef
[in]struct_elementMember member_name of this structure will be set to EXTH data
[in]member_nameStructure member name that will be modified
[in]stringString value that will be assigned to the structure memeber

Function Documentation

◆ mobi_build_ncx()

MOBI_RET mobi_build_ncx ( MOBIRawml rawml,
const OPF opf 
)

Parse ncx index, recreate ncx document and append it to rawml.

Parameters
[in,out]rawmlMOBIRawml structure
[in]opfOPF structure to fetch some data
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_build_opf()

MOBI_RET mobi_build_opf ( MOBIRawml rawml,
const MOBIData m 
)

Recreate OPF structure.

This function will fill OPF structure with parsed index data and convert it to xml file. The file will be stored in MOBIRawml structure.

Parameters
[in,out]rawmlOPF xml file will be appended to rawml->markup linked list
[in]mMOBIData structure containing document metadata
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_build_opf_guide()

MOBI_RET mobi_build_opf_guide ( OPF opf,
const MOBIRawml rawml 
)

Reconstruct guide part of the OPF file.

Parameters
[in,out]opfStructure OPF->OPFguide will be filled with parsed data
[in]rawmlStructure MOBIRawml will be parsed
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_build_opf_metadata()

MOBI_RET mobi_build_opf_metadata ( OPF opf,
const MOBIData m,
const MOBIRawml rawml 
)

Recreate OPF structure.

Parameters
[in,out]opfStructure OPF->OPFmetadata will be filled with parsed data
[in]mMOBIData structure containing document metadata
[in]rawmlMOBIRawml structure containing parsed records
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_free_ncx()

void mobi_free_ncx ( NCX ncx,
size_t  count 
)

Free array of ncx entries.

Parameters
[in]ncxArray of NCX structures with ncx content
[in]countSize of the array

◆ mobi_free_opf()

void mobi_free_opf ( OPF opf)

Free OPF structure and data.

Parameters
[in]opfOPF structure

◆ mobi_free_opf_array()

void mobi_free_opf_array ( char **  array)

Free array of OPF sturcture members.

Parameters
[in]arrayArray

◆ mobi_free_opf_guide()

void mobi_free_opf_guide ( OPFguide guide)

Free OPFguide structure and data.

Parameters
[in]guideOPF opf->guide structure

◆ mobi_free_opf_manifest()

void mobi_free_opf_manifest ( OPFmanifest manifest)

Free OPFmanifest structure and data.

Parameters
[in]manifestOPF opf->manifest structure

◆ mobi_free_opf_metadata()

void mobi_free_opf_metadata ( OPFmetadata metadata)

Free OPF metadata structure and data.

Parameters
[in]metadataOPF opf->metadata structure

◆ mobi_free_opf_spine()

void mobi_free_opf_spine ( OPFspine spine)

Free OPFspine structure and data.

Parameters
[in]spineOPF opf->spine structure

◆ mobi_get_opf_from_exth()

MOBI_RET mobi_get_opf_from_exth ( OPFmetadata metadata,
const MOBIData m 
)

Copy text data from EXTH record to OPFmetadata tags structure.

Parameters
[in,out]metadataStructure OPFmetadata will be filled with parsed data
[in]mMOBIData structure with loaded data
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_is_guide_type()

bool mobi_is_guide_type ( const char *  type)

Check if type is valid OPF guide element.

Compares types with elements of mobi_guide_types[] array

Parameters
[in]typeOPF guide type
Returns
True if type is valid guide type, false otherwise

◆ mobi_ncx_add_to_rawml()

MOBI_RET mobi_ncx_add_to_rawml ( const char *  ncx_xml,
MOBIRawml rawml 
)

Add reconstruced ncx part to rawml.

Parameters
[in]ncx_xmlOPF xml string
[in,out]rawmlNew data will be added to MOBIRawml rawml->resources structure
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_opf_add_to_rawml()

MOBI_RET mobi_opf_add_to_rawml ( const char *  opf_xml,
MOBIRawml rawml 
)

Add reconstruced opf part to rawml.

Parameters
[in]opf_xmlOPF xml string
[in,out]rawmlNew data will be added to MOBIRawml rawml->resources structure
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_opf_set_item()

void mobi_opf_set_item ( OPFmeta **  meta,
const char *  name,
const char *  content 
)

Set values for attributes of OPF manifest tag.

It will allocate memory for the OPFitem members: id, href and media-type. It will find first array index that is not already used

Parameters
[in,out]metaArray of OPFmeta structures to be filled with data
[in]nameValue of the name attribute
[in]contentValue of the content attribute

◆ mobi_write_ncx()

MOBI_RET mobi_write_ncx ( MOBIRawml rawml,
const NCX ncx,
const OPF opf,
uint32_t  maxlevel 
)

Build ncx document using libxml2 and append it to rawml.

Parameters
[in,out]rawmlMOBIRawml structure
[in]ncxArray of NCX structures with ncx content
[in]opfOPF structure to fetch some data
[in]maxlevelValue of dtb:depth attribute
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_write_ncx_header()

MOBI_RET mobi_write_ncx_header ( xmlTextWriterPtr  writer,
const OPF opf,
uint32_t  maxlevel 
)

Write ncx header.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]opfOPF structure to fetch some data
[in]maxlevelValue of dtb:depth attribute
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_write_ncx_level()

MOBI_RET mobi_write_ncx_level ( xmlTextWriterPtr  writer,
const NCX ncx,
const size_t  level,
const size_t  from,
const size_t  to,
size_t *  seq 
)

Write <navPoint> entries for given ncx level.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]ncxArray of NCX structures with ncx content
[in]levelTOC level
[in]fromFirst entry in NCX array to copy from
[in]toLast entry in NCX array to copy from
[in]seqSequential number for playOrder attribute
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_dcmeta()

MOBI_RET mobi_xml_write_dcmeta ( xmlTextWriterPtr  writer,
const char *  name,
const char **  content 
)

Write array of Dublin Core elements of given name to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElementNS() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]nameXML element name
[in]contentArray of XML element contents
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_dcmeta_creator()

MOBI_RET mobi_xml_write_dcmeta_creator ( xmlTextWriterPtr  writer,
const OPFcreator **  creator,
const char *  name 
)

Write array of Dublin Core creator/contributor elements to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElementNS() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]creatorOPFcreator structure representing creator/contributor element
[in]nameOPF creator value
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_dcmeta_date()

MOBI_RET mobi_xml_write_dcmeta_date ( xmlTextWriterPtr  writer,
const OPFdate **  date 
)

Write array of Dublin Core date elements to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElementNS() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]dateOPFdate structure representing date element
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_dcmeta_identifier()

MOBI_RET mobi_xml_write_dcmeta_identifier ( xmlTextWriterPtr  writer,
const OPFidentifier **  identifier 
)

Write array of Dublin Core identifier elements to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElementNS() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]identifierOPFidentifier structure representing identifier element
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_dcmeta_subject()

MOBI_RET mobi_xml_write_dcmeta_subject ( xmlTextWriterPtr  writer,
const OPFsubject **  subject 
)

Write array of Dublin Core subject elements to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElementNS() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]subjectOPFsubject structure representing subject element
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_element_ns()

MOBI_RET mobi_xml_write_element_ns ( xmlTextWriterPtr  writer,
const char *  name,
const char **  content,
const char *  ns 
)

Write array of xml elements of given name to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElementNS() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]nameXML element name
[in]contentArray of XML element contents
[in]nsXML namespace string or NULL if empty
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_item()

MOBI_RET mobi_xml_write_item ( xmlTextWriterPtr  writer,
const char *  id,
const char *  href,
const char *  media_type 
)

Write single element to XML buffer.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]idAttribute "id"
[in]hrefAttribute "href"
[in]media_typeAttribute "media-type"
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_manifest()

MOBI_RET mobi_xml_write_manifest ( xmlTextWriterPtr  writer,
const MOBIRawml rawml 
)

Write all manifest elements to XML buffer.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]rawmlMOBIRawml structure containing parts metadata
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_meta()

MOBI_RET mobi_xml_write_meta ( xmlTextWriterPtr  writer,
const char *  name,
const char *  content 
)

Write element <meta name="name" content="content"> to XML buffer.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]nameAttribute name
[in]contentAttribute content
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_opfmeta()

MOBI_RET mobi_xml_write_opfmeta ( xmlTextWriterPtr  writer,
const OPFmeta **  meta 
)

Write array of <meta> elements to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElement() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]metaArray of OPFmeta structures
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_reference()

MOBI_RET mobi_xml_write_reference ( xmlTextWriterPtr  writer,
const OPFreference **  reference 
)

Write array of <referenece> elements to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElement() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]referenceArray of OPFreference structures
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_spine()

MOBI_RET mobi_xml_write_spine ( xmlTextWriterPtr  writer,
const MOBIRawml rawml 
)

Write opf <spine> part to XML buffer.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]rawmlMOBIRawml structure containing parts metadata
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_xmeta()

MOBI_RET mobi_xml_write_xmeta ( xmlTextWriterPtr  writer,
const char *  name,
const char **  content 
)

Write array of custom MOBI elements of given name to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElementNS() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]nameXML element name
[in]contentArray of XML element contents
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_xml_write_xmeta_srp()

MOBI_RET mobi_xml_write_xmeta_srp ( xmlTextWriterPtr  writer,
const OPFsrp **  srp 
)

Write array of custom srp elements to XML buffer.

Wrapper for libxml2 xmlTextWriterWriteElementNS() function. Writes xml element for each not-null entry in the input array.

Parameters
[in,out]writerxmlTextWriterPtr to write to
[in]srpOPFsrp structure representing srp element
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

Variable Documentation

◆ mobi_guide_types

const char* mobi_guide_types[]
Initial value:
= {
"cover",
"title-page",
"toc",
"index",
"glossary",
"acknowledgements",
"bibliography",
"colophon",
"copyright-page",
"dedication",
"epigraph",
"foreword",
"loi",
"lot",
"notes",
"preface",
"text",
NULL
}

Array of valid OPF guide types.

http://www.idpf.org/epub/20/spec/OPF_2.0.1_draft.htm#Section2.6