xmlwriter.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * libxlsxwriter
  3. *
  4. * Copyright 2014-2018, John McNamara, [email protected]. See LICENSE.txt.
  5. *
  6. * xmlwriter - A libxlsxwriter library for creating Excel XLSX
  7. * XML files.
  8. *
  9. * The xmlwriter library is used to create the XML sub-components files
  10. * in the Excel XLSX file format.
  11. *
  12. * This library is used in preference to a more generic XML library to allow
  13. * for customization and optimization for the XLSX file format.
  14. *
  15. * The xmlwriter functions are only used internally and do not need to be
  16. * called directly by the end user.
  17. *
  18. */
  19. #ifndef __XMLWRITER_H__
  20. #define __XMLWRITER_H__
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include "utility.h"
  25. #define LXW_MAX_ATTRIBUTE_LENGTH 256
  26. #define LXW_ATTR_32 32
  27. #define LXW_ATTRIBUTE_COPY(dst, src) \
  28. do{ \
  29. strncpy(dst, src, LXW_MAX_ATTRIBUTE_LENGTH -1); \
  30. dst[LXW_MAX_ATTRIBUTE_LENGTH - 1] = '\0'; \
  31. } while (0)
  32. /* *INDENT-OFF* */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* *INDENT-ON* */
  37. /* Attribute used in XML elements. */
  38. struct xml_attribute {
  39. char key[LXW_MAX_ATTRIBUTE_LENGTH];
  40. char value[LXW_MAX_ATTRIBUTE_LENGTH];
  41. /* Make the struct a queue.h list element. */
  42. STAILQ_ENTRY (xml_attribute) list_entries;
  43. };
  44. /* Use queue.h macros to define the xml_attribute_list type. */
  45. STAILQ_HEAD(xml_attribute_list, xml_attribute);
  46. /* Create a new attribute struct to add to a xml_attribute_list. */
  47. struct xml_attribute *lxw_new_attribute_str(const char *key,
  48. const char *value);
  49. struct xml_attribute *lxw_new_attribute_int(const char *key, uint32_t value);
  50. struct xml_attribute *lxw_new_attribute_dbl(const char *key, double value);
  51. /* Macro to initialize the xml_attribute_list pointers. */
  52. #define LXW_INIT_ATTRIBUTES() \
  53. STAILQ_INIT(&attributes)
  54. /* Macro to add attribute string elements to xml_attribute_list. */
  55. #define LXW_PUSH_ATTRIBUTES_STR(key, value) \
  56. do { \
  57. attribute = lxw_new_attribute_str((key), (value)); \
  58. STAILQ_INSERT_TAIL(&attributes, attribute, list_entries); \
  59. } while (0)
  60. /* Macro to add attribute int values to xml_attribute_list. */
  61. #define LXW_PUSH_ATTRIBUTES_INT(key, value) \
  62. do { \
  63. attribute = lxw_new_attribute_int((key), (value)); \
  64. STAILQ_INSERT_TAIL(&attributes, attribute, list_entries); \
  65. } while (0)
  66. /* Macro to add attribute double values to xml_attribute_list. */
  67. #define LXW_PUSH_ATTRIBUTES_DBL(key, value) \
  68. do { \
  69. attribute = lxw_new_attribute_dbl((key), (value)); \
  70. STAILQ_INSERT_TAIL(&attributes, attribute, list_entries); \
  71. } while (0)
  72. /* Macro to free xml_attribute_list and attribute. */
  73. #define LXW_FREE_ATTRIBUTES() \
  74. while (!STAILQ_EMPTY(&attributes)) { \
  75. attribute = STAILQ_FIRST(&attributes); \
  76. STAILQ_REMOVE_HEAD(&attributes, list_entries); \
  77. free(attribute); \
  78. }
  79. /**
  80. * Create the XML declaration in an XML file.
  81. *
  82. * @param xmlfile A FILE pointer to the output XML file.
  83. */
  84. void lxw_xml_declaration(FILE * xmlfile);
  85. /**
  86. * Write an XML start tag with optional attributes.
  87. *
  88. * @param xmlfile A FILE pointer to the output XML file.
  89. * @param tag The XML tag to write.
  90. * @param attributes An optional list of attributes to add to the tag.
  91. */
  92. void lxw_xml_start_tag(FILE * xmlfile,
  93. const char *tag,
  94. struct xml_attribute_list *attributes);
  95. /**
  96. * Write an XML start tag with optional un-encoded attributes.
  97. * This is a minor optimization for attributes that don't need encoding.
  98. *
  99. * @param xmlfile A FILE pointer to the output XML file.
  100. * @param tag The XML tag to write.
  101. * @param attributes An optional list of attributes to add to the tag.
  102. */
  103. void lxw_xml_start_tag_unencoded(FILE * xmlfile,
  104. const char *tag,
  105. struct xml_attribute_list *attributes);
  106. /**
  107. * Write an XML end tag.
  108. *
  109. * @param xmlfile A FILE pointer to the output XML file.
  110. * @param tag The XML tag to write.
  111. */
  112. void lxw_xml_end_tag(FILE * xmlfile, const char *tag);
  113. /**
  114. * Write an XML empty tag with optional attributes.
  115. *
  116. * @param xmlfile A FILE pointer to the output XML file.
  117. * @param tag The XML tag to write.
  118. * @param attributes An optional list of attributes to add to the tag.
  119. */
  120. void lxw_xml_empty_tag(FILE * xmlfile,
  121. const char *tag,
  122. struct xml_attribute_list *attributes);
  123. /**
  124. * Write an XML empty tag with optional un-encoded attributes.
  125. * This is a minor optimization for attributes that don't need encoding.
  126. *
  127. * @param xmlfile A FILE pointer to the output XML file.
  128. * @param tag The XML tag to write.
  129. * @param attributes An optional list of attributes to add to the tag.
  130. */
  131. void lxw_xml_empty_tag_unencoded(FILE * xmlfile,
  132. const char *tag,
  133. struct xml_attribute_list *attributes);
  134. /**
  135. * Write an XML element containing data and optional attributes.
  136. *
  137. * @param xmlfile A FILE pointer to the output XML file.
  138. * @param tag The XML tag to write.
  139. * @param data The data section of the XML element.
  140. * @param attributes An optional list of attributes to add to the tag.
  141. */
  142. void lxw_xml_data_element(FILE * xmlfile,
  143. const char *tag,
  144. const char *data,
  145. struct xml_attribute_list *attributes);
  146. char *lxw_escape_control_characters(const char *string);
  147. char *lxw_escape_data(const char *data);
  148. /* *INDENT-OFF* */
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152. /* *INDENT-ON* */
  153. #endif /* __XMLWRITER_H__ */